@@ -49,9 +49,10 @@ type Config struct {
49
49
}
50
50
51
51
type Buser interface {
52
- configure (address uint16 , size int16 ) []byte // configure the bus with the given configuration and return the buffer to use
52
+ configure (address uint16 , size int16 ) []byte // configure the bus and return the image buffer to use
53
53
command (cmd uint8 ) error // send a command to the display
54
- flush () error // send the data in the buffer to the display
54
+ flush () error // send the image to the display, faster than "tx()" in i2c case since avoids slice copy
55
+ tx (data []byte , isCommand bool ) error // generic transmit function
55
56
}
56
57
57
58
type VccMode uint8
@@ -89,65 +90,76 @@ func (d *Device) Configure(cfg Config) {
89
90
d .buffer = d .bus .configure (cfg .Address , d .width * d .height / 8 )
90
91
91
92
time .Sleep (100 * time .Nanosecond )
92
- d .bus . command (DISPLAYOFF )
93
- d .bus . command (SETDISPLAYCLOCKDIV )
94
- d .bus . command (0x80 )
95
- d .bus . command (SETMULTIPLEX )
96
- d .bus . command (uint8 (d .height - 1 ))
97
- d .bus . command (SETDISPLAYOFFSET )
98
- d .bus . command (0x0 )
99
- d .bus . command (SETSTARTLINE | 0x0 )
100
- d .bus . command (CHARGEPUMP )
93
+ d .Command (DISPLAYOFF )
94
+ d .Command (SETDISPLAYCLOCKDIV )
95
+ d .Command (0x80 )
96
+ d .Command (SETMULTIPLEX )
97
+ d .Command (uint8 (d .height - 1 ))
98
+ d .Command (SETDISPLAYOFFSET )
99
+ d .Command (0x0 )
100
+ d .Command (SETSTARTLINE | 0x0 )
101
+ d .Command (CHARGEPUMP )
101
102
if d .vccState == EXTERNALVCC {
102
- d .bus . command (0x10 )
103
+ d .Command (0x10 )
103
104
} else {
104
- d .bus . command (0x14 )
105
+ d .Command (0x14 )
105
106
}
106
- d .bus . command (MEMORYMODE )
107
- d .bus . command (0x00 )
107
+ d .Command (MEMORYMODE )
108
+ d .Command (0x00 )
108
109
109
110
d .SetRotation (cfg .Rotation )
110
111
111
112
if (d .width == 128 && d .height == 64 ) || (d .width == 64 && d .height == 48 ) { // 128x64 or 64x48
112
- d .bus . command (SETCOMPINS )
113
- d .bus . command (0x12 )
114
- d .bus . command (SETCONTRAST )
113
+ d .Command (SETCOMPINS )
114
+ d .Command (0x12 )
115
+ d .Command (SETCONTRAST )
115
116
if d .vccState == EXTERNALVCC {
116
- d .bus . command (0x9F )
117
+ d .Command (0x9F )
117
118
} else {
118
- d .bus . command (0xCF )
119
+ d .Command (0xCF )
119
120
}
120
121
} else if d .width == 128 && d .height == 32 { // 128x32
121
- d .bus . command (SETCOMPINS )
122
- d .bus . command (0x02 )
123
- d .bus . command (SETCONTRAST )
124
- d .bus . command (0x8F )
122
+ d .Command (SETCOMPINS )
123
+ d .Command (0x02 )
124
+ d .Command (SETCONTRAST )
125
+ d .Command (0x8F )
125
126
} else if d .width == 96 && d .height == 16 { // 96x16
126
- d .bus . command (SETCOMPINS )
127
- d .bus . command (0x2 )
128
- d .bus . command (SETCONTRAST )
127
+ d .Command (SETCOMPINS )
128
+ d .Command (0x2 )
129
+ d .Command (SETCONTRAST )
129
130
if d .vccState == EXTERNALVCC {
130
- d .bus . command (0x10 )
131
+ d .Command (0x10 )
131
132
} else {
132
- d .bus . command (0xAF )
133
+ d .Command (0xAF )
133
134
}
134
135
} else {
135
136
// fail silently, it might work
136
137
println ("there's no configuration for this display's size" )
137
138
}
138
139
139
- d .bus . command (SETPRECHARGE )
140
+ d .Command (SETPRECHARGE )
140
141
if d .vccState == EXTERNALVCC {
141
- d .bus . command (0x22 )
142
+ d .Command (0x22 )
142
143
} else {
143
- d .bus . command (0xF1 )
144
+ d .Command (0xF1 )
144
145
}
145
- d .bus .command (SETVCOMDETECT )
146
- d .bus .command (0x40 )
147
- d .bus .command (DISPLAYALLON_RESUME )
148
- d .bus .command (NORMALDISPLAY )
149
- d .bus .command (DEACTIVATE_SCROLL )
150
- d .bus .command (DISPLAYON )
146
+ d .Command (SETVCOMDETECT )
147
+ d .Command (0x40 )
148
+ d .Command (DISPLAYALLON_RESUME )
149
+ d .Command (NORMALDISPLAY )
150
+ d .Command (DEACTIVATE_SCROLL )
151
+ d .Command (DISPLAYON )
152
+
153
+ }
154
+
155
+ // Command sends a command to the display
156
+ func (d * Device ) Command (command uint8 ) {
157
+ d .bus .command (command )
158
+ }
159
+
160
+ // Tx sends data to the display; if isCommand is false, this also updates the image buffer.
161
+ func (d * Device ) Tx (data []byte , isCommand bool ) error {
162
+ return d .bus .tx (data , isCommand )
151
163
}
152
164
153
165
// ClearBuffer clears the image buffer
@@ -170,12 +182,12 @@ func (d *Device) Display() error {
170
182
// In the 128x64 (SPI) screen resetting to 0x0 after 128 times corrupt the buffer
171
183
// Since we're printing the whole buffer, avoid resetting it in this case
172
184
if d .canReset {
173
- d .bus . command (COLUMNADDR )
174
- d .bus . command (d .resetCol [0 ])
175
- d .bus . command (d .resetCol [1 ])
176
- d .bus . command (PAGEADDR )
177
- d .bus . command (d .resetPage [0 ])
178
- d .bus . command (d .resetPage [1 ])
185
+ d .Command (COLUMNADDR )
186
+ d .Command (d .resetCol [0 ])
187
+ d .Command (d .resetCol [1 ])
188
+ d .Command (PAGEADDR )
189
+ d .Command (d .resetPage [0 ])
190
+ d .Command (d .resetPage [1 ])
179
191
}
180
192
181
193
return d .bus .flush ()
@@ -250,15 +262,15 @@ func (d *Device) SetRotation(rotation drivers.Rotation) error {
250
262
d .rotation = rotation
251
263
switch d .rotation {
252
264
case drivers .Rotation0 :
253
- d .bus . command (SEGREMAP | 0x1 ) // Reverse horizontal mapping
254
- d .bus . command (COMSCANDEC ) // Reverse vertical mapping
265
+ d .Command (SEGREMAP | 0x1 ) // Reverse horizontal mapping
266
+ d .Command (COMSCANDEC ) // Reverse vertical mapping
255
267
case drivers .Rotation180 :
256
- d .bus . command (SEGREMAP ) // Normal horizontal mapping
257
- d .bus . command (COMSCANINC ) // Normal vertical mapping
268
+ d .Command (SEGREMAP ) // Normal horizontal mapping
269
+ d .Command (COMSCANINC ) // Normal vertical mapping
258
270
// nothing to do
259
271
default :
260
- d .bus . command (SEGREMAP | 0x1 ) // Reverse horizontal mapping
261
- d .bus . command (COMSCANDEC ) // Reverse vertical mapping
272
+ d .Command (SEGREMAP | 0x1 ) // Reverse horizontal mapping
273
+ d .Command (COMSCANDEC ) // Reverse vertical mapping
262
274
}
263
275
return nil
264
276
}
@@ -268,9 +280,9 @@ func (d *Device) SetRotation(rotation drivers.Rotation) error {
268
280
// should be kept.
269
281
func (d * Device ) Sleep (sleepEnabled bool ) error {
270
282
if sleepEnabled {
271
- d .bus . command (DISPLAYOFF )
283
+ d .Command (DISPLAYOFF )
272
284
} else {
273
- d .bus . command (DISPLAYON )
285
+ d .Command (DISPLAYON )
274
286
}
275
287
return nil
276
288
}
0 commit comments