forked from nopnop2002/esp-idf-ssd1306
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssd1306.c
455 lines (436 loc) · 12.1 KB
/
ssd1306.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* MIT License
*
* Copyright (c) 2020 nopnop2002
* Copyright (c) 2021 wolffshots
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* @file ssd1306.c
* @brief implementations of functions for setting up, interacting with and sending commands to an ssd1306 driven screen via spi or i2c
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "ssd1306.h"
#include "font8x8_basic.h"
#include "font8x8_readable.h"
#include "font8x8_readable_thin.h"
#include "font8x8_space.h"
#include <string.h>
#define tag CONFIG_SSD1306_TAG ///< tag for logging library
int center, top, bottom; ///< positions for oled
/**
* initialise the screen device
*/
void ssd1306_init(SSD1306_t *dev)
{
#if CONFIG_I2C_INTERFACE
i2c_master_init(dev, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_I2C_INTERFACE
#if CONFIG_SPI_INTERFACE
spi_master_init(dev, CONFIG_MOSI_GPIO, CONFIG_SCLK_GPIO, CONFIG_CS_GPIO, CONFIG_DC_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_SPI_INTERFACE
#if CONFIG_FLIP
dev._flip = true;
#endif
int width = 128;
int height;
#if CONFIG_SSD1306_128x64
height = 64;
#endif // CONFIG_SSD1306_128x64
#if CONFIG_SSD1306_128x32
height = 32;
#endif // CONFIG_SSD1306_128x32
if (dev->_address == SPIAddress)
{
spi_init(dev, width, height);
}
else
{
i2c_init(dev, width, height);
}
ssd1306_clear_screen(dev, false);
ssd1306_contrast(dev, 0xff);
#if CONFIG_SSD1306_128x64
top = 2;
center = 3;
bottom = 8;
#endif // CONFIG_SSD1306_128x64
#if CONFIG_SSD1306_128x32
top = 1;
center = 1;
bottom = 4;
#endif // CONFIG_SSD1306_128x32
ssd1306_clear_screen(dev, false);
ssd1306_contrast(dev, 0xff);
}
/**
* de-initialise the screen device and free resources
* @todo make sure to free more resources and de-init the actual spi/i2c session
*/
void ssd1306_deinit(SSD1306_t *dev)
{
/// @todo
}
/**
* display some text on the screen
* @param dev the screen device to interact with
* @param page the line to display the text on
* @param text the text data
* @param text_len the text length
* @param invert whether or not to invert the text and background colours
*/
void ssd1306_display_text(SSD1306_t *dev, int page, char *text, int text_len, bool invert)
{
if (page >= dev->_pages)
return;
int _text_len = text_len;
if (_text_len > 16)
_text_len = 16;
uint8_t seg = 0;
uint8_t image[8];
for (uint8_t i = 0; i < _text_len; i++)
{
memcpy(image, font8x8[(uint8_t)text[i]], 8);
if (invert)
ssd1306_invert(image, 8);
if (dev->_flip)
ssd1306_flip(image, 8);
if (dev->_address == SPIAddress)
{
spi_display_image(dev, page, seg, image, 8);
}
else
{
i2c_display_image(dev, page, seg, image, 8);
}
seg = seg + 8;
}
}
/**
* display some text on the screen
* @param dev the screen device to interact with
* @param line the line to display the text on
* @param text the text data
*/
void ssd1306_wrapped_display_text(SSD1306_t *dev, int line, char *text)
{
ssd1306_clear_line(dev, line, false);
ssd1306_display_text(dev, line, text, strlen(text), false);
}
/**
* display an image on the screen
* @param dev the screen device to interact with
* @param page the line to display the image on
* @param seg the segment of the image
* @param images the image data to display
* @param width the width of the image
*/
void ssd1306_display_image(SSD1306_t *dev, int page, int seg, uint8_t *images, int width)
{
if (dev->_address == SPIAddress)
{
spi_display_image(dev, page, seg, images, width);
}
else
{
i2c_display_image(dev, page, seg, images, width);
}
}
/**
* clear the screen (fills the screen with spaces)
* @param dev the screen device to interact with
* @param invert whether or not the cleared screen should be inverted
*/
void ssd1306_clear_screen(SSD1306_t *dev, bool invert)
{
char space[16];
memset(space, 0x20, sizeof(space));
for (int page = 0; page < dev->_pages; page++)
{
ssd1306_display_text(dev, page, space, sizeof(space), invert);
}
}
/**
* clear a specific line (fills the line with spaces)
* @param dev the screen device to interact with
* @param page the line to clear
* @param invert whether or not the cleared line should be inverted
*/
void ssd1306_clear_line(SSD1306_t *dev, int page, bool invert)
{
char space[16];
memset(space, 0x20, sizeof(space));
ssd1306_display_text(dev, page, space, sizeof(space), invert);
}
/**
* set the contrast of the screen
* @param dev the screen device to interact with
* @param contrast the level of contrast
*/
void ssd1306_contrast(SSD1306_t *dev, int contrast)
{
if (dev->_address == SPIAddress)
{
spi_contrast(dev, contrast);
}
else
{
i2c_contrast(dev, contrast);
}
}
/**
* start scrolling the screen via software
* @param dev the screen device to interact with
* @param start the start of the scroll
* @param end the end of the scroll
*/
void ssd1306_software_scroll(SSD1306_t *dev, int start, int end)
{
ESP_LOGI(tag, "software_scroll start=%d end=%d _pages=%d", start, end, dev->_pages);
if (start < 0 || end < 0)
{
dev->_scEnable = false;
}
else if (start >= dev->_pages || end >= dev->_pages)
{
dev->_scEnable = false;
}
else
{
dev->_scEnable = true;
dev->_scStart = start;
dev->_scEnd = end;
dev->_scDirection = 1;
if (start > end)
dev->_scDirection = -1;
for (int i = 0; i < dev->_pages; i++)
{
dev->_page[i]._valid = false;
dev->_page[i]._segLen = 0;
}
}
}
/**
* display some scrolling text on the screen
* @param dev the screen device to interact with
* @param text the text data
* @param text_len the text length
* @param invert whether or not the scrolling text should be inverted
*/
void ssd1306_scroll_text(SSD1306_t *dev, char *text, int text_len, bool invert)
{
ESP_LOGI(tag, "dev->_scEnable=%d", dev->_scEnable);
if (dev->_scEnable == false)
return;
void (*func)(SSD1306_t * dev, int page, int seg, uint8_t *images, int width);
if (dev->_address == SPIAddress)
{
func = spi_display_image;
}
else
{
func = i2c_display_image;
}
int srcIndex = dev->_scEnd - dev->_scDirection;
while (1)
{
int dstIndex = srcIndex + dev->_scDirection;
ESP_LOGD(tag, "srcIndex=%d dstIndex=%d", srcIndex, dstIndex);
dev->_page[dstIndex]._valid = dev->_page[srcIndex]._valid;
dev->_page[dstIndex]._segLen = dev->_page[srcIndex]._segLen;
for (int seg = 0; seg < dev->_width; seg++)
{
dev->_page[dstIndex]._segs[seg] = dev->_page[srcIndex]._segs[seg];
}
ESP_LOGD(tag, "_valid=%d", dev->_page[dstIndex]._valid);
if (dev->_page[dstIndex]._valid)
(*func)(dev, dstIndex, 0, dev->_page[dstIndex]._segs, dev->_page[srcIndex]._segLen);
if (srcIndex == dev->_scStart)
break;
srcIndex = srcIndex - dev->_scDirection;
}
int _text_len = text_len;
if (_text_len > 16)
_text_len = 16;
uint8_t seg = 0;
uint8_t image[8];
for (uint8_t i = 0; i < _text_len; i++)
{
memcpy(image, font8x8[(uint8_t)text[i]], 8);
if (invert)
ssd1306_invert(image, 8);
if (dev->_flip)
ssd1306_flip(image, 8);
(*func)(dev, srcIndex, seg, image, 8);
for (int j = 0; j < 8; j++)
dev->_page[srcIndex]._segs[seg + j] = image[j];
seg = seg + 8;
}
dev->_page[srcIndex]._valid = true;
dev->_page[srcIndex]._segLen = seg;
}
/**
* stop scrolling the screen via software
* @param dev the screen device to interact with
*/
void ssd1306_scroll_clear(SSD1306_t *dev)
{
ESP_LOGD(tag, "dev->_scEnable=%d", dev->_scEnable);
if (dev->_scEnable == false)
return;
int srcIndex = dev->_scEnd - dev->_scDirection;
while (1)
{
int dstIndex = srcIndex + dev->_scDirection;
ESP_LOGD(tag, "srcIndex=%d dstIndex=%d", srcIndex, dstIndex);
ssd1306_clear_line(dev, dstIndex, false);
dev->_page[dstIndex]._valid = false;
if (dstIndex == dev->_scStart)
break;
srcIndex = srcIndex - dev->_scDirection;
}
}
/**
* start scrolling the screen via hardware
* @param dev the screen device to interact with
* @param scroll the direction of the scroll
*/
void ssd1306_hardware_scroll(SSD1306_t *dev, ssd1306_scroll_type_t scroll)
{
if (dev->_address == SPIAddress)
{
spi_hardware_scroll(dev, scroll);
}
else
{
i2c_hardware_scroll(dev, scroll);
}
}
/**
* start scrolling the screen via hardware
* @param dev the screen device to interact with
* @param page the specific page to scroll
* @param scroll the direction of the scroll
*/
void ssd1306_hardware_scroll_line(SSD1306_t *dev, int page, ssd1306_scroll_type_t scroll)
{
if (dev->_address == SPIAddress)
{
spi_hardware_scroll_line(dev, page, scroll);
}
else
{
i2c_hardware_scroll_line(dev, page, scroll);
}
}
/**
* invert a buffer of data
* @param buf buffer to invert
* @param blen the length of the buffer
*/
void ssd1306_invert(uint8_t *buf, size_t blen)
{
uint8_t wk;
for (int i = 0; i < blen; i++)
{
wk = buf[i];
buf[i] = ~wk;
}
}
// Flip upside down
/**
* flip a buffer of data
* @param buf buffer to flip
* @param blen the length of the buffer
*/
void ssd1306_flip(uint8_t *buf, size_t blen)
{
for (int i = 0; i < blen; i++)
{
buf[i] = ssd1306_rotate(buf[i]);
}
}
// Rotate 8-bit data
// 0x12-->0x48
/**
* rotate a buffer of data
* @param buf buffer to rotate
* @param blen the length of the buffer
*/
uint8_t ssd1306_rotate(uint8_t ch1)
{
uint8_t ch2 = 0;
for (int j = 0; j < 8; j++)
{
ch2 = (ch2 << 1) + (ch1 & 0x01);
ch1 = ch1 >> 1;
}
return ch2;
}
/**
* fadeout the screen
* @param dev the screen device to interact with
*/
void ssd1306_fadeout(SSD1306_t *dev)
{
void (*func)(SSD1306_t * dev, int page, int seg, uint8_t *images, int width);
if (dev->_address == SPIAddress)
{
func = spi_display_image;
}
else
{
func = i2c_display_image;
}
uint8_t image[1];
for (int page = 0; page < dev->_pages; page++)
{
image[0] = 0xFF;
for (int line = 0; line < 8; line++)
{
if (dev->_flip)
{
image[0] = image[0] >> 1;
}
else
{
image[0] = image[0] << 1;
}
for (int seg = 0; seg < 128; seg++)
{
(*func)(dev, page, seg, image, 1);
}
}
}
}
/**
* dump the screen data
* @param dev the screen device to interact with
*/
void ssd1306_dump(SSD1306_t dev)
{
printf("_address=%x\n", dev._address);
printf("_width=%x\n", dev._width);
printf("_height=%x\n", dev._height);
printf("_pages=%x\n", dev._pages);
}