-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpilcdi2cssd1306.pas
279 lines (221 loc) · 8.42 KB
/
rpilcdi2cssd1306.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
{ --------------------------------------------------------------------------
Raspberry Pi OLED driver for:
- SSD1306 I2C OLED 128x64 displays
Requires the the rpiio library - https://github.com/zipplet/rpiio
Copyright (c) Michael Nixon 2016.
Distributed under the MIT license, please see the LICENSE file.
-------------------------------------------------------------------------- }
unit rpilcdi2cssd1306;
interface
uses baseunix, sysutils, classes, rpii2c;
const
{ SSD1306 module addresses }
LCD_SSD1306_ADDR_DEFAULT = $3c;
{ Control bytes }
LCD_SSD1306_CONTROL_COMMAND = $80; { Next byte is a command }
LCD_SSD1306_CONTROL_DATA1BYTE = $40; { Next byte is data }
LCD_SSD1306_CONTROL_ALLDATA = $00; { Data stream follows }
{ Initialisation commands }
LCD_SSD1306_CMD_CHARGEPUMP_SET = $8d;
LCD_SSD1306_CMD_CHARGEPUMP_ON = $14;
{ Must be followed by DISPLAYON }
LCD_SSD1306_CMD_SETDISPLAYCLOCKDIV = $d5;
LCD_SSD1306_CMD_SETMULTIPLEX = $a8;
{ Normal commands }
LCD_SSD1306_CMD_SETCONTRAST = $81;
LCD_SSD1306_CMD_DISPLAYON = $af;
LCD_SSD1306_CMD_DISPLAYOFF = $ae;
LCD_SSD1306_CMD_ALLPIXELSON = $a5;
LCD_SSD1306_CMD_ALLPIXELSOFF = $a4;
LCD_SSD1306_CMD_INVERSEON = $a7;
LCD_SSD1306_CMD_INVERSEOFF = $a6;
LCD_SSD1306_PIXEL_WIDTH = 128; { Width in pixels }
LCD_SSD1306_PIXEL_HEIGHT = 64; { Height in pixels }
LCD_SSD1306_PAGE_SIZE = 32; { Number of bytes per page }
LCD_SSD1306_GDDRAM_SIZE = 256; { Size of GDDRAM in bytes }
type
tSSD1306OLEDI2C = class(tobject)
private
i2cDevice: trpiI2CDevice;
displayBuffer: array[0..LCD_SSD1306_GDDRAM_SIZE - 1] of byte;
procedure writeDataByte(value: byte);
procedure writeCommandByte(value: byte);
procedure writeDataBytes(bytes: pointer; length: longint);
protected
public
constructor Create(i2cDeviceObject: trpiI2CDevice);
destructor Destroy; override;
procedure initialiseDisplay;
procedure setDisplay(onOrOff: boolean);
procedure setContrast(level: byte);
procedure setAllPixelsOn(enable: boolean);
procedure setInverseMode(enable: boolean);
end;
implementation
const
LCD_EXCEPTION_PREFIX = 'rpilcdi2cssd1306 driver: ';
LCD_EXCEPTION_NOHANDLE = LCD_EXCEPTION_PREFIX + 'I2C device object not set';
LCD_EXCEPTION_ACCESS = LCD_EXCEPTION_PREFIX + 'Unable to access I2C device';
{ --------------------------------------------------------------------------
-------------------------------------------------------------------------- }
{ --------------------------------------------------------------------------
Invert all pixels on the display
If <enable> is TRUE, GDDRAM bits will be interpreted as follows:
- 1 = off
- 0 = on
Throws an exception on failure.
-------------------------------------------------------------------------- }
procedure tSSD1306OLEDI2C.setInverseMode(enable: boolean);
begin
if not assigned(self.i2cDevice) then begin
raise exception.create(LCD_EXCEPTION_NOHANDLE);
end;
if enable then begin
self.writeCommandByte(LCD_SSD1306_CMD_INVERSEON);
end else begin
self.writeCommandByte(LCD_SSD1306_CMD_INVERSEOFF);
end;
end;
{ --------------------------------------------------------------------------
Light all pixels on the display at once.
If <enable> is TRUE, all pixels will be illuminated, otherwise GDDRAM
contents will be displayed.
Throws an exception on failure.
-------------------------------------------------------------------------- }
procedure tSSD1306OLEDI2C.setAllPixelsOn(enable: boolean);
begin
if not assigned(self.i2cDevice) then begin
raise exception.create(LCD_EXCEPTION_NOHANDLE);
end;
if enable then begin
self.writeCommandByte(LCD_SSD1306_CMD_ALLPIXELSON);
end else begin
self.writeCommandByte(LCD_SSD1306_CMD_ALLPIXELSOFF);
end;
end;
{ --------------------------------------------------------------------------
Set the display contrast.
<level> is the contrast level, from $00 to $ff.
Throws an exception on failure.
-------------------------------------------------------------------------- }
procedure tSSD1306OLEDI2C.setContrast(level: byte);
begin
if not assigned(self.i2cDevice) then begin
raise exception.create(LCD_EXCEPTION_NOHANDLE);
end;
self.writeCommandByte(LCD_SSD1306_CMD_SETCONTRAST);
self.writeCommandByte(level);
end;
{ --------------------------------------------------------------------------
Turn the display on or off.
Throws an exception on failure.
-------------------------------------------------------------------------- }
procedure tSSD1306OLEDI2C.setDisplay(onOrOff: boolean);
begin
if not assigned(self.i2cDevice) then begin
raise exception.create(LCD_EXCEPTION_NOHANDLE);
end;
if onOrOff then begin
self.writeCommandByte(LCD_SSD1306_CMD_DISPLAYON);
end else begin
self.writeCommandByte(LCD_SSD1306_CMD_DISPLAYOFF);
end;
end;
{ --------------------------------------------------------------------------
Write 1 data byte to the display.
Throws an exception on failure.
-------------------------------------------------------------------------- }
procedure tSSD1306OLEDI2C.writeDataByte(value: byte);
var
buffer: array[0..1] of byte;
begin
if not assigned(self.i2cDevice) then begin
raise exception.create(LCD_EXCEPTION_NOHANDLE);
end;
buffer[0] := LCD_SSD1306_CONTROL_DATA1BYTE;
buffer[1] := value;
self.i2cDevice.writeBytes(@buffer[0], 2);
end;
{ --------------------------------------------------------------------------
Write a command byte to the display.
Throws an exception on failure.
-------------------------------------------------------------------------- }
procedure tSSD1306OLEDI2C.writeCommandByte(value: byte);
var
buffer: array[0..1] of byte;
begin
if not assigned(self.i2cDevice) then begin
raise exception.create(LCD_EXCEPTION_NOHANDLE);
end;
buffer[0] := LCD_SSD1306_CONTROL_COMMAND;
buffer[1] := value;
self.i2cDevice.writeBytes(@buffer[0], 2);
end;
{ --------------------------------------------------------------------------
Write multiple data bytes to the display.
Throws an exception on failure.
-------------------------------------------------------------------------- }
procedure tSSD1306OLEDI2C.writeDataBytes(bytes: pointer; length: longint);
begin
if not assigned(self.i2cDevice) then begin
raise exception.create(LCD_EXCEPTION_NOHANDLE);
end;
self.i2cDevice.writeByte(LCD_SSD1306_CONTROL_ALLDATA);
self.i2cDevice.writeBytes(bytes, length);
end;
{ --------------------------------------------------------------------------
Initialise the display, with the backlight set to a default state.
Throws an exception on failure.
-------------------------------------------------------------------------- }
procedure tSSD1306OLEDI2C.InitialiseDisplay;
begin
if not assigned(self.i2cDevice) then begin
raise exception.create(LCD_EXCEPTION_NOHANDLE);
end;
{ Start with the display off }
self.setDisplay(false);
{ Set the display clock divider to the suggested ratio, $80 }
self.writeCommandByte(LCD_SSD1306_CMD_SETDISPLAYCLOCKDIV);
self.writeCommandByte($80);
{ Setup multiplexer }
self.writeCommandByte(LCD_SSD1306_CMD_SETMULTIPLEX);
self.writeCommandByte(LCD_SSD1306_PIXEL_HEIGHT - 1);
self.writeCommandByte(LCD_SSD1306_CMD_CHARGEPUMP_SET);
self.writeCommandByte(LCD_SSD1306_CMD_CHARGEPUMP_ON);
{ Set Display Offset }
self.writeCommandByte($d3);
self.writeCommandByte($00);
{ Set Display Start Line }
self.writeCommandByte($40);
{ Set Segment remap }
self.writeCommandByte($a0 or $1);
{ Set COM output direction }
self.writeCommandByte($c0);
{ Set COM pins hardware configuration }
self.writeCommandByte($da);
self.writeCommandByte($12);
self.setContrast($cf);
{ SETPRECHARGE }
self.writeCommandByte($d9);
self.writeCommandByte($f1);
self.setAllPixelsOn(false);
self.setInverseMode(false);
self.setDisplay(true);
end;
{ --------------------------------------------------------------------------
Class constructor
-------------------------------------------------------------------------- }
constructor tSSD1306OLEDI2C.Create(i2cDeviceObject: trpiI2CDevice);
begin
inherited Create;
self.i2cDevice := i2cDeviceObject;
end;
{ --------------------------------------------------------------------------
Class destructor
-------------------------------------------------------------------------- }
destructor tSSD1306OLEDI2C.Destroy;
begin
{}
inherited Destroy;
end;
end.