LibDriver SSD1306
Loading...
Searching...
No Matches
driver_ssd1306.c
Go to the documentation of this file.
1
37
38#include "driver_ssd1306.h"
39#include "driver_ssd1306_font.h"
40
44#define CHIP_NAME "Solomon Systech SSD1306"
45#define MANUFACTURER_NAME "Solomon Systech"
46#define SUPPLY_VOLTAGE_MIN 1.65f
47#define SUPPLY_VOLTAGE_MAX 3.3f
48#define MAX_CURRENT 0.78f
49#define TEMPERATURE_MIN -40.0f
50#define TEMPERATURE_MAX 85.0f
51#define DRIVER_VERSION 2000
52
56#define SSD1306_CMD 0
57#define SSD1306_DATA 1
58
62#define SSD1306_CMD_LOWER_COLUMN_START_ADDRESS 0x00
63#define SSD1306_CMD_HIGHER_COLUMN_START_ADDRESS 0x10
64#define SSD1306_CMD_MEMORY_ADDRESSING_MODE 0x20
65#define SSD1306_CMD_SET_COLUMN_ADDRESS 0x21
66#define SSD1306_CMD_SET_PAGE_ADDRESS 0x22
67#define SSD1306_CMD_SET_FADE_OUT_AND_BLINKING 0x23
68#define SSD1306_CMD_RIGHT_HORIZONTAL_SCROLL 0x26
69#define SSD1306_CMD_LEFT_HORIZONTAL_SCROLL 0x27
70#define SSD1306_CMD_VERTICAL_RIGHT_HORIZONTAL_SCROLL 0x29
71#define SSD1306_CMD_VERTICAL_LEFT_HORIZONTAL_SCROLL 0x2A
72#define SSD1306_CMD_DEACTIVATE_SCROLL 0x2E
73#define SSD1306_CMD_ACTIVATE_SCROLL 0x2F
74#define SSD1306_CMD_DISPLAY_START_LINE 0x40
75#define SSD1306_CMD_CONTRAST_CONTROL 0x81
76#define SSD1306_CMD_CHARGE_PUMP_SETTING 0x8D
77#define SSD1306_CMD_COLUMN_0_MAPPED_TO_SEG0 0xA0
78#define SSD1306_CMD_COLUMN_127_MAPPED_TO_SEG0 0xA1
79#define SSD1306_CMD_VERTICAL_SCROLL_AREA 0xA3
80#define SSD1306_CMD_ENTIRE_DISPLAY_OFF 0xA4
81#define SSD1306_CMD_ENTIRE_DISPLAY_ON 0xA5
82#define SSD1306_CMD_NORMAL_DISPLAY 0xA6
83#define SSD1306_CMD_INVERSE_DISPLAY 0xA7
84#define SSD1306_CMD_MULTIPLEX_RATIO 0xA8
85#define SSD1306_CMD_DISPLAY_OFF 0xAE
86#define SSD1306_CMD_DISPLAY_ON 0xAF
87#define SSD1306_CMD_PAGE_ADDR 0xB0
88#define SSD1306_CMD_SCAN_DIRECTION_COM0_START 0xC0
89#define SSD1306_CMD_SCAN_DIRECTION_COMN_1_START 0xC8
90#define SSD1306_CMD_DISPLAY_OFFSET 0xD3
91#define SSD1306_CMD_DISPLAY_CLOCK_DIVIDE 0xD5
92#define SSD1306_CMD_SET_ZOOM_IN 0xD6
93#define SSD1306_CMD_PRE_CHARGE_PERIOD 0xD9
94#define SSD1306_CMD_COM_PINS_CONF 0xDA
95#define SSD1306_CMD_COMH_DESLECT_LEVEL 0xDB
96#define SSD1306_CMD_NOP 0xE3
97
108static uint8_t a_ssd1306_write_byte(ssd1306_handle_t *handle, uint8_t data, uint8_t cmd)
109{
110 uint8_t res;
111
112 if (handle->iic_spi == SSD1306_INTERFACE_IIC) /* if iic */
113 {
114 if (cmd != 0) /* if data */
115 {
116 if (handle->iic_write(handle->iic_addr, 0x40, &data, 1) != 0) /* write data */
117 {
118 return 1; /* return error */
119 }
120 else
121 {
122 return 0; /* success return 0 */
123 }
124 }
125 else
126 {
127 if (handle->iic_write(handle->iic_addr, 0x00, &data, 1) != 0) /* write command */
128 {
129 return 1; /* return error */
130 }
131 else
132 {
133 return 0; /* success return 0 */
134 }
135 }
136 }
137 else if (handle->iic_spi == SSD1306_INTERFACE_SPI) /* if spi */
138 {
139 res = handle->spi_cmd_data_gpio_write(cmd); /* write data command */
140 if (res != 0) /* check error */
141 {
142 return 1; /* return error */
143 }
144
145 if (handle->spi_write_cmd(&data, 1) != 0) /* write command */
146 {
147 return 1; /* return error */
148 }
149 else
150 {
151 return 0; /* success return 0 */
152 }
153 }
154 else
155 {
156 return 1; /* return error */
157 }
158}
159
171static uint8_t a_ssd1306_multiple_write_byte(ssd1306_handle_t *handle, uint8_t *data, uint8_t len, uint8_t cmd)
172{
173 uint8_t res;
174
175 if (handle->iic_spi == SSD1306_INTERFACE_IIC) /* if iic */
176 {
177 if (cmd != 0) /* if data */
178 {
179 if (handle->iic_write(handle->iic_addr, 0x40, data, len) != 0) /* write data */
180 {
181 return 1; /* return error */
182 }
183 else
184 {
185 return 0; /* success return 0 */
186 }
187 }
188 else
189 {
190 if (handle->iic_write(handle->iic_addr, 0x00, data, len) != 0) /* write command */
191 {
192 return 1; /* return error */
193 }
194 else
195 {
196 return 0; /* success return 0 */
197 }
198 }
199 }
200 else if (handle->iic_spi == SSD1306_INTERFACE_SPI) /* if spi */
201 {
202 res = handle->spi_cmd_data_gpio_write(cmd); /* write data command */
203 if (res != 0) /* check error */
204 {
205 return 1; /* return error */
206 }
207
208 if (handle->spi_write_cmd(data, len) != 0) /* write command */
209 {
210 return 1; /* return error */
211 }
212 else
213 {
214 return 0; /* success return 0 */
215 }
216 }
217 else
218 {
219 return 1; /* return error */
220 }
221}
222
234static uint8_t a_ssd1306_gram_draw_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t data)
235{
236 uint8_t pos;
237 uint8_t bx;
238 uint8_t temp = 0;
239
240 pos = y / 8; /* get y page */
241 bx = y % 8; /* get y point */
242 temp = 1 << bx; /* set data */
243 if (data != 0) /* if 1 */
244 {
245 handle->gram[x][pos] |= temp; /* set 1 */
246 }
247 else
248 {
249 handle->gram[x][pos] &= ~temp; /* set 0 */
250 }
251
252 return 0; /* success return 0 */
253}
254
268static uint8_t a_ssd1306_gram_show_char(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t chr, uint8_t size, uint8_t mode)
269{
270 uint8_t temp, t, t1;
271 uint8_t y0 = y;
272 uint8_t csize = (size / 8 + ((size % 8) ? 1 : 0)) * (size / 2); /* get size */
273
274 chr = chr - ' '; /* get index */
275 for (t = 0; t < csize; t++) /* write size */
276 {
277 if (size == 12) /* if size 12 */
278 {
279 temp = gsc_ssd1306_ascii_1206[chr][t]; /* get ascii 1206 */
280 }
281 else if (size == 16) /* if size 16 */
282 {
283 temp = gsc_ssd1306_ascii_1608[chr][t]; /* get ascii 1608 */
284 }
285 else if(size == 24) /* if size 24 */
286 {
287 temp = gsc_ssd1306_ascii_2412[chr][t]; /* get ascii 2412 */
288 }
289 else
290 {
291 return 1; /* return error */
292 }
293 for (t1 = 0; t1 < 8; t1++) /* write one line */
294 {
295 if ((temp & 0x80) != 0) /* if 1 */
296 {
297 if (a_ssd1306_gram_draw_point(handle, x, y, mode) != 0) /* draw point */
298 {
299 return 1; /* return error */
300 }
301 }
302 else
303 {
304 if (a_ssd1306_gram_draw_point(handle, x, y, !mode) != 0) /* draw point */
305 {
306 return 1; /* return error */
307 }
308 }
309 temp <<= 1; /* left shift 1 */
310 y++;
311 if ((y - y0) == size) /* reset size */
312 {
313 y = y0; /* set y */
314 x++; /* x++ */
315
316 break; /* break */
317 }
318 }
319 }
320
321 return 0; /* success return 0 */
322}
323
335{
336 uint8_t i;
337 uint8_t n;
338
339 if (handle == NULL) /* check handle */
340 {
341 return 2; /* return error */
342 }
343 if (handle->inited != 1) /* check handle initialization */
344 {
345 return 3; /* return error */
346 }
347
348 for (i = 0; i < 8; i++) /* write 8 page */
349 {
350 if (a_ssd1306_write_byte(handle, SSD1306_CMD_PAGE_ADDR+i, SSD1306_CMD) != 0) /* set page */
351 {
352 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
353
354 return 1; /* return error */
355 }
356 if (a_ssd1306_write_byte(handle, SSD1306_CMD_LOWER_COLUMN_START_ADDRESS, SSD1306_CMD) != 0) /* set lower column 0 */
357 {
358 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
359
360 return 1; /* return error */
361 }
362 if (a_ssd1306_write_byte(handle, SSD1306_CMD_HIGHER_COLUMN_START_ADDRESS, SSD1306_CMD) != 0) /* set higher column 0 */
363 {
364 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
365
366 return 1; /* return error */
367 }
368 for (n = 0; n < 128; n++) /* write 128 */
369 {
370 handle->gram[n][i] = 0x00; /* set black */
371 if (a_ssd1306_write_byte(handle, handle->gram[n][i], SSD1306_DATA) != 0) /* write data */
372 {
373 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
374
375 return 1; /* return error */
376 }
377 }
378 }
379
380 return 0; /* success return 0 */
381}
382
394{
395 uint8_t i;
396 uint8_t n;
397
398 if (handle == NULL) /* check handle */
399 {
400 return 2; /* return error */
401 }
402 if (handle->inited != 1) /* check handle initialization */
403 {
404 return 3; /* return error */
405 }
406
407 for (i = 0; i < 8; i++) /* write 8 page */
408 {
409 if (a_ssd1306_write_byte(handle, SSD1306_CMD_PAGE_ADDR+i, SSD1306_CMD) != 0) /* set page */
410 {
411 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
412
413 return 1; /* return error */
414 }
415 if (a_ssd1306_write_byte(handle, SSD1306_CMD_LOWER_COLUMN_START_ADDRESS, SSD1306_CMD) != 0) /* set lower column 0 */
416 {
417 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
418
419 return 1; /* return error */
420 }
421 if (a_ssd1306_write_byte(handle, SSD1306_CMD_HIGHER_COLUMN_START_ADDRESS, SSD1306_CMD) != 0) /* set higher column 0 */
422 {
423 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
424
425 return 1; /* return error */
426 }
427 for (n = 0; n < 128; n++) /* write 128 */
428 {
429 if (a_ssd1306_write_byte(handle, handle->gram[n][i], SSD1306_DATA) != 0) /* write data */
430 {
431 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
432
433 return 1; /* return error */
434 }
435 }
436 }
437
438 return 0; /* success return 0 */
439}
440
455uint8_t ssd1306_write_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t data)
456{
457 uint8_t pos;
458 uint8_t bx;
459 uint8_t temp = 0;
460
461 if (handle == NULL) /* check handle */
462 {
463 return 2; /* return error */
464 }
465 if (handle->inited != 1) /* check handle initialization */
466 {
467 return 3; /* return error */
468 }
469 if ((x > 127) || (y > 63)) /* check x, y */
470 {
471 handle->debug_print("ssd1306: x or y is invalid.\n"); /* x or y is invalid */
472
473 return 4; /* return error */
474 }
475
476 pos = y / 8; /* get y page */
477 bx = y % 8; /* get y point */
478 temp = 1 << bx; /* set data */
479 if (data != 0) /* check the data */
480 {
481 handle->gram[x][pos] |= temp; /* set 1 */
482 }
483 else
484 {
485 handle->gram[x][pos] &= ~temp; /* set 0 */
486 }
487 if (a_ssd1306_write_byte(handle, SSD1306_CMD_PAGE_ADDR + pos, SSD1306_CMD) != 0) /* write page addr */
488 {
489 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
490
491 return 1; /* return error */
492 }
493 if (a_ssd1306_write_byte(handle, SSD1306_CMD_LOWER_COLUMN_START_ADDRESS|(x&0x0F), SSD1306_CMD) != 0) /* write lower column */
494 {
495 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
496
497 return 1; /* return error */
498 }
499 if (a_ssd1306_write_byte(handle, SSD1306_CMD_HIGHER_COLUMN_START_ADDRESS|((x>4)&0x0F), SSD1306_CMD) != 0) /* write higher column */
500 {
501 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
502
503 return 1; /* return error */
504 }
505 if (a_ssd1306_write_byte(handle, handle->gram[x][pos], SSD1306_DATA) != 0) /* write data */
506 {
507 handle->debug_print("ssd1306: write byte failed.\n"); /* write byte failed */
508
509 return 1; /* return error */
510 }
511 else
512 {
513 return 0; /* success return 0 */
514 }
515}
516
531uint8_t ssd1306_read_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t *data)
532{
533 uint8_t pos;
534 uint8_t bx;
535 uint8_t temp = 0;
536
537 if (handle == NULL) /* check handle */
538 {
539 return 2; /* return error */
540 }
541 if (handle->inited != 1) /* check handle initialization */
542 {
543 return 3; /* return error */
544 }
545 if ((x > 127) || (y > 63)) /* check x, y */
546 {
547 handle->debug_print("ssd1306: x or y is invalid.\n"); /* x or y is invalid */
548
549 return 4; /* return error */
550 }
551
552 pos = y / 8; /* get y page */
553 bx = y % 8; /* get y point */
554 temp = 1 << bx; /* set data */
555 if ((handle->gram[x][pos] & temp) != 0) /* get data */
556 {
557 *data = 1; /* set 1 */
558 }
559 else
560 {
561 *data = 0; /* set 0 */
562 }
563
564 return 0; /* success return 0 */
565}
566
581uint8_t ssd1306_gram_write_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t data)
582{
583 uint8_t pos;
584 uint8_t bx;
585 uint8_t temp = 0;
586
587 if (handle == NULL) /* check handle */
588 {
589 return 2; /* return error */
590 }
591 if (handle->inited != 1) /* check handle initialization */
592 {
593 return 3; /* return error */
594 }
595 if ((x > 127) || (y > 63)) /* check x, y */
596 {
597 handle->debug_print("ssd1306: x or y is invalid.\n"); /* x or y is invalid */
598
599 return 4; /* return error */
600 }
601
602 pos = y / 8; /* get y page */
603 bx = y % 8; /* get y point */
604 temp = 1 << bx; /* set data */
605 if (data != 0) /* if 1 */
606 {
607 handle->gram[x][pos] |= temp; /* set 1 */
608 }
609 else
610 {
611 handle->gram[x][pos] &= ~temp; /* set 0 */
612 }
613
614 return 0; /* success return 0 */
615}
616
631uint8_t ssd1306_gram_read_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t *data)
632{
633 uint8_t pos;
634 uint8_t bx;
635 uint8_t temp = 0;
636
637 if (handle == NULL) /* check handle */
638 {
639 return 2; /* return error */
640 }
641 if (handle->inited != 1) /* check handle initialization */
642 {
643 return 3; /* return error */
644 }
645 if ((x > 127) || (y > 63)) /* check x, y */
646 {
647 handle->debug_print("ssd1306: x or y is invalid.\n"); /* x or y is invalid */
648
649 return 4; /* return error */
650 }
651
652 pos = y / 8; /* get y page */
653 bx = y % 8; /* get y point */
654 temp = 1 << bx; /* set data */
655 if ((handle->gram[x][pos] & temp) != 0) /* get data */
656 {
657 *data = 1; /* set 1 */
658 }
659 else
660 {
661 *data = 0; /* set 0 */
662 }
663
664 return 0; /* success return 0 */
665}
666
684uint8_t ssd1306_gram_write_string(ssd1306_handle_t *handle, uint8_t x, uint8_t y, char *str, uint16_t len, uint8_t color, ssd1306_font_t font)
685{
686 if (handle == NULL) /* check handle */
687 {
688 return 2; /* return error */
689 }
690 if (handle->inited != 1) /* check handle initialization */
691 {
692 return 3; /* return error */
693 }
694 if((x > 127) || (y > 63)) /* check x, y */
695 {
696 handle->debug_print("ssd1306: x or y is invalid.\n"); /* x or y is invalid */
697
698 return 4; /* return error */
699 }
700
701 while ((len != 0) && (*str <= '~') && (*str >= ' ')) /* write all string */
702 {
703 if (x > (127 - (font / 2))) /* check x point */
704 {
705 x = 0; /* set x */
706 y += (uint8_t)font; /* set next row */
707 }
708 if (y > (63 - font)) /* check y pont */
709 {
710 y = x = 0; /* reset to 0,0 */
711 }
712 if (a_ssd1306_gram_show_char(handle, x, y, *str, font, color) != 0) /* show a char */
713 {
714 return 1; /* return error */
715 }
716 x += (uint8_t)(font / 2); /* x + font/2 */
717 str++; /* str address++ */
718 len--; /* str length-- */
719 }
720
721 return 0; /* success return 0 */
722}
723
742uint8_t ssd1306_gram_fill_rect(ssd1306_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t color)
743{
744 uint8_t x, y;
745
746 if (handle == NULL) /* check handle */
747 {
748 return 2; /* return error */
749 }
750 if (handle->inited != 1) /* check handle initialization */
751 {
752 return 3; /* return error */
753 }
754 if ((left > 127) || (top > 63)) /* check left top */
755 {
756 handle->debug_print("ssd1306: left or top is invalid.\n"); /* left or top is invalid */
757
758 return 4; /* return error */
759 }
760 if ((right > 127) || (bottom > 63)) /* check right bottom */
761 {
762 handle->debug_print("ssd1306: right or bottom is invalid.\n"); /* right or bottom is invalid */
763
764 return 5; /* return error */
765 }
766 if ((left > right) || (top > bottom)) /* check left right top bottom */
767 {
768 handle->debug_print("ssd1306: left > right or top > bottom.\n"); /* left > right or top > bottom */
769
770 return 6; /* return error */
771 }
772
773 for (x = left; x <= right; x++) /* write x */
774 {
775 for (y = top; y <= bottom; y++) /* write y */
776 {
777 if (a_ssd1306_gram_draw_point(handle, x, y, color) != 0) /* draw point */
778 {
779 return 1; /* return error */
780 }
781 }
782 }
783
784 return 0; /* return error */
785}
786
805uint8_t ssd1306_gram_draw_picture(ssd1306_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t *img)
806{
807 uint8_t x, y;
808
809 if (handle == NULL) /* check handle */
810 {
811 return 2; /* return error */
812 }
813 if (handle->inited != 1) /* check handle initialization */
814 {
815 return 3; /* return error */
816 }
817 if ((left > 127) || (top > 63)) /* check left top */
818 {
819 handle->debug_print("ssd1306: left or top is invalid.\n"); /* left or top is invalid */
820
821 return 4; /* return error */
822 }
823 if ((right > 127) || (bottom > 63)) /* check right bottom */
824 {
825 handle->debug_print("ssd1306: right or bottom is invalid.\n"); /* right or bottom is invalid */
826
827 return 5; /* return error */
828 }
829 if ((left > right) || (top > bottom)) /* check left right top bottom */
830 {
831 handle->debug_print("ssd1306: left > right or top > bottom.\n"); /* left > right or top > bottom */
832
833 return 6; /* return error */
834 }
835
836 for (x = left; x <= right; x++) /* write x */
837 {
838 for (y = top; y <= bottom; y++) /* write y */
839 {
840 if (a_ssd1306_gram_draw_point(handle, x, y, *img) != 0) /* draw point */
841 {
842 return 1; /* return error */
843 }
844 img++; /* img++ */
845 }
846 }
847
848 return 0; /* succeed return 0 */
849}
850
865{
866 if (handle == NULL) /* check handle */
867 {
868 return 2; /* return error */
869 }
870 if (handle->debug_print == NULL) /* check debug_print */
871 {
872 return 3; /* return error */
873 }
874 if (handle->iic_init == NULL) /* check iic_init */
875 {
876 handle->debug_print("ssd1306: iic_init is null.\n"); /* iic_init is null */
877
878 return 3; /* return error */
879 }
880 if (handle->iic_deinit == NULL) /* check iic_deinit */
881 {
882 handle->debug_print("ssd1306: iic_deinit is null.\n"); /* iic_deinit is null */
883
884 return 3; /* return error */
885 }
886 if (handle->iic_write == NULL) /* check iic_write */
887 {
888 handle->debug_print("ssd1306: iic_write is null.\n"); /* iic_write is null */
889
890 return 3; /* return error */
891 }
892 if (handle->spi_init == NULL) /* check spi_init */
893 {
894 handle->debug_print("ssd1306: spi_init is null.\n"); /* spi_init is null */
895
896 return 3; /* return error */
897 }
898 if (handle->spi_deinit == NULL) /* check spi_deinit */
899 {
900 handle->debug_print("ssd1306: spi_deinit is null.\n"); /* spi_deinit is null */
901
902 return 3; /* return error */
903 }
904 if (handle->spi_write_cmd == NULL) /* check spi_write_cmd */
905 {
906 handle->debug_print("ssd1306: spi_write_cmd is null.\n"); /* spi_write_cmd is null */
907
908 return 3; /* return error */
909 }
910 if (handle->delay_ms == NULL) /* check delay_ms */
911 {
912 handle->debug_print("ssd1306: delay_ms is null.\n"); /* delay_ms is null */
913
914 return 3; /* return error */
915 }
916 if (handle->spi_cmd_data_gpio_init == NULL) /* check spi_cmd_data_gpio_init */
917 {
918 handle->debug_print("ssd1306: spi_cmd_data_gpio_init is null.\n"); /* spi_cmd_data_gpio_init is null */
919
920 return 3; /* return error */
921 }
922 if (handle->spi_cmd_data_gpio_deinit == NULL) /* check spi_cmd_data_gpio_deinit */
923 {
924 handle->debug_print("ssd1306: spi_cmd_data_gpio_deinit is null.\n"); /* spi_cmd_data_gpio_deinit is null */
925
926 return 3; /* return error */
927 }
928 if (handle->spi_cmd_data_gpio_write == NULL) /* check spi_cmd_data_gpio_write */
929 {
930 handle->debug_print("ssd1306: spi_cmd_data_gpio_write is null.\n"); /* spi_cmd_data_gpio_write is null */
931
932 return 3; /* return error */
933 }
934 if (handle->reset_gpio_init == NULL) /* check reset_gpio_init */
935 {
936 handle->debug_print("ssd1306: reset_gpio_init is null.\n"); /* reset_gpio_init is null */
937
938 return 3; /* return error */
939 }
940 if (handle->reset_gpio_deinit == NULL) /* check reset_gpio_deinit */
941 {
942 handle->debug_print("ssd1306: reset_gpio_deinit is null.\n"); /* reset_gpio_deinit is null */
943
944 return 3; /* return error */
945 }
946 if(handle->reset_gpio_write == NULL) /* check reset_gpio_write */
947 {
948 handle->debug_print("ssd1306: reset_gpio_write is null.\n"); /* reset_gpio_write is null */
949
950 return 3; /* return error */
951 }
952
953 if (handle->spi_cmd_data_gpio_init() != 0) /* check spi_cmd_data_gpio_init */
954 {
955 handle->debug_print("ssd1306: spi cmd data gpio init failed.\n"); /* spi cmd data gpio init failed */
956
957 return 5; /* return error */
958 }
959 if (handle->reset_gpio_init() != 0) /* reset gpio init */
960 {
961 handle->debug_print("ssd1306: reset gpio init failed.\n"); /* reset gpio init failed */
962 (void)handle->spi_cmd_data_gpio_deinit(); /* spi_cmd_data_gpio_deinit */
963
964 return 4; /* return error */
965 }
966 if (handle->reset_gpio_write(0) != 0) /* write 0 */
967 {
968 handle->debug_print("ssd1306: reset gpio write failed.\n"); /* reset gpio write failed */
969 (void)handle->spi_cmd_data_gpio_deinit(); /* spi_cmd_data_gpio_deinit */
970 (void)handle->reset_gpio_deinit(); /* reset_gpio_deinit */
971
972 return 4; /* return error */
973 }
974 handle->delay_ms(100); /* delay 100 ms */
975 if (handle->reset_gpio_write(1) != 0) /* write 1 */
976 {
977 handle->debug_print("ssd1306: reset gpio write failed.\n"); /* reset gpio write failed */
978 (void)handle->spi_cmd_data_gpio_deinit(); /* spi_cmd_data_gpio_deinit */
979 (void)handle->reset_gpio_deinit(); /* reset_gpio_deinit */
980
981 return 4; /* return error */
982 }
983 if (handle->iic_spi == SSD1306_INTERFACE_IIC) /* if iic interface */
984 {
985 if (handle->iic_init() != 0) /* iic init */
986 {
987 handle->debug_print("ssd1306: iic init failed.\n"); /* iic init failed */
988 (void)handle->spi_cmd_data_gpio_deinit(); /* spi_cmd_data_gpio_deinit */
989 (void)handle->reset_gpio_deinit(); /* reset_gpio_deinit */
990
991 return 1; /* return error */
992 }
993 }
994 else if (handle->iic_spi == SSD1306_INTERFACE_SPI) /* if spi interface */
995 {
996 if (handle->spi_init() != 0) /* spi init */
997 {
998 handle->debug_print("ssd1306: spi init failed.\n"); /* spi init failed */
999 (void)handle->spi_cmd_data_gpio_deinit(); /* spi_cmd_data_gpio_deinit */
1000 (void)handle->reset_gpio_deinit(); /* reset_gpio_deinit */
1001
1002 return 1; /* return error */
1003 }
1004 }
1005 else
1006 {
1007 handle->debug_print("ssd1306: interface is invalid.\n"); /* interface is invalid */
1008 (void)handle->spi_cmd_data_gpio_deinit(); /* spi_cmd_data_gpio_deinit */
1009 (void)handle->reset_gpio_deinit(); /* reset_gpio_deinit */
1010
1011 return 6; /* return error */
1012 }
1013 handle->inited = 1; /* flag inited */
1014
1015 return 0; /* success return 0 */
1016}
1017
1033{
1034 uint8_t buf[2];
1035
1036 if (handle == NULL) /* check handle */
1037 {
1038 return 2; /* return error */
1039 }
1040 if (handle->inited != 1) /* check handle initialization */
1041 {
1042 return 3; /* return error */
1043 }
1044
1045 buf[0] = SSD1306_CMD_CHARGE_PUMP_SETTING; /* charge pump off */
1046 buf[1] = 0x10 | (0 << 2); /* set charge pump */
1047 if (a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD) != 0) /* write command */
1048 {
1049 handle->debug_print("ssd1306: write command failed.\n"); /* write command failed */
1050
1051 return 4; /* return error */
1052 }
1053 if (a_ssd1306_write_byte(handle, SSD1306_CMD_DISPLAY_OFF, SSD1306_CMD) != 0) /* write display off */
1054 {
1055 handle->debug_print("ssd1306: write command failed.\n"); /* write command failed */
1056
1057 return 4; /* return error */
1058 }
1059 if (handle->reset_gpio_deinit() != 0) /* reset gpio deinit */
1060 {
1061 handle->debug_print("ssd1306: reset gpio deinit failed.\n"); /* reset gpio deinit failed */
1062
1063 return 5; /* return error */
1064 }
1065 if (handle->spi_cmd_data_gpio_deinit() != 0) /* spi cmd data gpio deinit */
1066 {
1067 handle->debug_print("ssd1306: spi cmd data gpio deinit failed.\n"); /* spi cmd data gpio deinit failed */
1068
1069 return 6; /* return error */
1070 }
1071 if (handle->iic_spi == SSD1306_INTERFACE_IIC) /* if iic interface */
1072 {
1073 if (handle->iic_deinit() != 0) /* iic deinit */
1074 {
1075 handle->debug_print("ssd1306: iic deinit failed.\n"); /* iic deinit failed */
1076
1077 return 1; /* return error */
1078 }
1079 }
1080 else if (handle->iic_spi == SSD1306_INTERFACE_SPI) /* if spi interface */
1081 {
1082 if (handle->spi_deinit() != 0) /* spi deinit */
1083 {
1084 handle->debug_print("ssd1306: spi deinit failed.\n"); /* spi deinit failed */
1085
1086 return 1; /* return error */
1087 }
1088 }
1089 else
1090 {
1091 handle->debug_print("ssd1306: interface is invalid.\n"); /* interface is invalid */
1092
1093 return 7; /* return error */
1094 }
1095 handle->inited = 0; /* flag close */
1096
1097 return 0; /* success return 0 */
1098}
1099
1110{
1111 if (handle == NULL) /* check handle */
1112 {
1113 return 2; /* return error */
1114 }
1115
1116 handle->iic_spi = (uint8_t)interface; /* set interface */
1117
1118 return 0; /* success return 0 */
1119}
1120
1131{
1132 if (handle == NULL) /* check handle */
1133 {
1134 return 2; /* return error */
1135 }
1136
1137 *interface = (ssd1306_interface_t)(handle->iic_spi); /* get interface */
1138
1139 return 0; /* success return 0 */
1140}
1141
1152{
1153 if (handle == NULL) /* check handle */
1154 {
1155 return 2; /* return error */
1156 }
1157
1158 handle->iic_addr = (uint8_t)addr_pin; /* set addr pin */
1159
1160 return 0; /* success return 0 */
1161}
1162
1173{
1174 if (handle == NULL) /* check handle */
1175 {
1176 return 2; /* return error */
1177 }
1178
1179 *addr_pin = (ssd1306_address_t)(handle->iic_addr); /* set address */
1180
1181 return 0; /* success return 0 */
1182}
1183
1197{
1198 if (handle == NULL) /* check handle */
1199 {
1200 return 2; /* return error */
1201 }
1202 if (handle->inited != 1) /* check handle initialization */
1203 {
1204 return 3; /* return error */
1205 }
1206 if (addr > 0x0F) /* check addr */
1207 {
1208 handle->debug_print("ssd1306: addr is invalid.\n"); /* addr is invalid */
1209
1210 return 4; /* return error */
1211 }
1212
1213 return a_ssd1306_write_byte(handle, SSD1306_CMD_LOWER_COLUMN_START_ADDRESS|(addr&0x0F), SSD1306_CMD); /* write command */
1214}
1215
1229{
1230 if (handle == NULL) /* check handle */
1231 {
1232 return 2; /* return error */
1233 }
1234 if (handle->inited != 1) /* check handle initialization */
1235 {
1236 return 3; /* return error */
1237 }
1238 if (addr > 0x0F) /* check addr */
1239 {
1240 handle->debug_print("ssd1306: addr is invalid.\n"); /* addr is invalid */
1241
1242 return 4; /* return error */
1243 }
1244
1245 return a_ssd1306_write_byte(handle, SSD1306_CMD_HIGHER_COLUMN_START_ADDRESS|(addr&0x0F), SSD1306_CMD); /* write command */
1246}
1247
1260{
1261 uint8_t buf[2];
1262
1263 if (handle == NULL) /* check handle */
1264 {
1265 return 2; /* return error */
1266 }
1267 if (handle->inited != 1) /* check handle initialization */
1268 {
1269 return 3; /* return error */
1270 }
1271
1272 buf[0] = SSD1306_CMD_MEMORY_ADDRESSING_MODE; /* set command mode */
1273 buf[1] = mode; /* set mode */
1274
1275 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
1276}
1277
1292uint8_t ssd1306_set_column_address_range(ssd1306_handle_t *handle, uint8_t start_addr, uint8_t end_addr)
1293{
1294 uint8_t buf[3];
1295
1296 if (handle == NULL) /* check handle */
1297 {
1298 return 2; /* return error */
1299 }
1300 if (handle->inited != 1) /* check handle initialization */
1301 {
1302 return 3; /* return error */
1303 }
1304 if (start_addr > 0x7F) /* check start addr */
1305 {
1306 handle->debug_print("ssd1306: start addr is invalid.\n"); /* start addr is invalid */
1307
1308 return 4; /* return error */
1309 }
1310 if (end_addr > 0x7F) /* check end addr */
1311 {
1312 handle->debug_print("ssd1306: end addr is invalid.\n"); /* end addr is invalid */
1313
1314 return 5; /* return error */
1315 }
1316
1317 buf[0] = SSD1306_CMD_SET_COLUMN_ADDRESS; /* set command */
1318 buf[1] = start_addr & 0x7F; /* set start address */
1319 buf[2] = end_addr & 0x7F; /* set end address */
1320
1321 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 3, SSD1306_CMD); /* write command */
1322}
1323
1338uint8_t ssd1306_set_page_address_range(ssd1306_handle_t *handle, uint8_t start_addr, uint8_t end_addr)
1339{
1340 uint8_t buf[3];
1341
1342 if (handle == NULL) /* check handle */
1343 {
1344 return 2; /* return error */
1345 }
1346 if (handle->inited != 1) /* check handle initialization */
1347 {
1348 return 3; /* return error */
1349 }
1350 if (start_addr > 0x07) /* check start addr */
1351 {
1352 handle->debug_print("ssd1306: start addr is invalid.\n"); /* start addr is invalid */
1353
1354 return 4; /* return error */
1355 }
1356 if (end_addr > 0x07) /* check end addr */
1357 {
1358 handle->debug_print("ssd1306: end addr is invalid.\n"); /* end_addr is invalid */
1359
1360 return 5; /* return error */
1361 }
1362
1363 buf[0] = SSD1306_CMD_SET_PAGE_ADDRESS; /* set command */
1364 buf[1] = start_addr & 0x07; /* set start address */
1365 buf[2] = end_addr & 0x07; /* set end address */
1366
1367 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 3, SSD1306_CMD); /* write command */
1368}
1369
1384{
1385 uint8_t buf[2];
1386
1387 if (handle == NULL) /* check handle */
1388 {
1389 return 2; /* return error */
1390 }
1391 if (handle->inited != 1) /* check handle initialization */
1392 {
1393 return 3; /* return error */
1394 }
1395 if (frames> 0x0F) /* check frames */
1396 {
1397 handle->debug_print("ssd1306: frames is invalid.\n"); /* frames is invalid */
1398
1399 return 4; /* return error */
1400 }
1401
1402 buf[0] = SSD1306_CMD_SET_FADE_OUT_AND_BLINKING; /* set command */
1403 buf[1] = (uint8_t)((mode << 4) | (frames & 0x0F)); /* set mode */
1404
1405 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
1406}
1407
1423uint8_t ssd1306_set_right_horizontal_scroll(ssd1306_handle_t *handle, uint8_t start_page_addr, uint8_t end_page_addr,
1425{
1426 uint8_t buf[7];
1427
1428 if (handle == NULL) /* check handle */
1429 {
1430 return 2; /* return error */
1431 }
1432 if (handle->inited != 1) /* check handle initialization */
1433 {
1434 return 3; /* return error */
1435 }
1436 if (start_page_addr > 0x07) /* check start_page_addr */
1437 {
1438 handle->debug_print("ssd1306: start page addr is invalid.\n"); /* start page addr is invalid */
1439
1440 return 4; /* return error */
1441 }
1442 if (end_page_addr > 0x07) /* check end_page_addr */
1443 {
1444 handle->debug_print("ssd1306: end page addr is invalid.\n"); /* end page addr is invalid */
1445
1446 return 5; /* return error */
1447 }
1448
1449 buf[0] = SSD1306_CMD_RIGHT_HORIZONTAL_SCROLL; /* set command */
1450 buf[1] = 0x00; /* set null */
1451 buf[2] = start_page_addr & 0x07; /* set start page address */
1452 buf[3] = frames & 0x07; /* set frames */
1453 buf[4] = end_page_addr & 0x07; /* set end page address */
1454 buf[5] = 0x00; /* set null */
1455 buf[6] = 0xFF; /* set frame end */
1456
1457 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 7, SSD1306_CMD); /* write command */
1458}
1459
1475uint8_t ssd1306_set_left_horizontal_scroll(ssd1306_handle_t *handle, uint8_t start_page_addr, uint8_t end_page_addr,
1477{
1478 uint8_t buf[7];
1479
1480 if (handle == NULL) /* check handle */
1481 {
1482 return 2; /* return error */
1483 }
1484 if (handle->inited != 1) /* check handle initialization */
1485 {
1486 return 3; /* return error */
1487 }
1488 if (start_page_addr > 0x07) /* check start_page_addr */
1489 {
1490 handle->debug_print("ssd1306: start_page_addr is invalid.\n"); /* start_page_addr is invalid */
1491
1492 return 4; /* return error */
1493 }
1494 if (end_page_addr > 0x07) /* check end_page_addr */
1495 {
1496 handle->debug_print("ssd1306: end_page_addr is invalid.\n"); /* end_page_addr is invalid */
1497
1498 return 5; /* return error */
1499 }
1500
1501 buf[0] = SSD1306_CMD_LEFT_HORIZONTAL_SCROLL; /* set command */
1502 buf[1] = 0x00; /* set null */
1503 buf[2] = start_page_addr & 0x07; /* set end page addr */
1504 buf[3] = frames & 0x07; /* set frames */
1505 buf[4] = end_page_addr & 0x07; /* set end page addr */
1506 buf[5] = 0x00; /* set null */
1507 buf[6] = 0xFF; /* set frame end */
1508
1509 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 7, SSD1306_CMD); /* write command */
1510}
1511
1529uint8_t ssd1306_set_vertical_right_horizontal_scroll(ssd1306_handle_t *handle, uint8_t start_page_addr, uint8_t end_page_addr,
1530 uint8_t rows, ssd1306_scroll_frame_t frames)
1531{
1532 uint8_t buf[6];
1533
1534 if (handle == NULL) /* check handle */
1535 {
1536 return 2; /* return error */
1537 }
1538 if (handle->inited != 1) /* check handle initialization */
1539 {
1540 return 3; /* return error */
1541 }
1542 if (start_page_addr > 0x07) /* check start_page_addr */
1543 {
1544 handle->debug_print("ssd1306: start_page_addr is invalid.\n"); /* start_page_addr is invalid */
1545
1546 return 4; /* return error */
1547 }
1548 if (end_page_addr > 0x07) /* check end page addr */
1549 {
1550 handle->debug_print("ssd1306: end_page_addr is invalid.\n"); /* end_page_addr is invalid */
1551
1552 return 5; /* return error */
1553 }
1554 if (rows > 0x3F) /* check rows */
1555 {
1556 handle->debug_print("ssd1306: rows is invalid.\n"); /* rows is invalid */
1557
1558 return 6; /* return error */
1559 }
1560
1561 buf[0] = SSD1306_CMD_VERTICAL_RIGHT_HORIZONTAL_SCROLL; /* set command */
1562 buf[1] = 0x00; /* set null */
1563 buf[2] = start_page_addr & 0x07; /* set start page addr */
1564 buf[3] = frames & 0x07; /* set frames */
1565 buf[4] = end_page_addr & 0x07; /* set end page addr */
1566 buf[5] = rows & 0x3F; /* set rows */
1567
1568 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 6, SSD1306_CMD); /* write command */
1569}
1570
1588uint8_t ssd1306_set_vertical_left_horizontal_scroll(ssd1306_handle_t *handle, uint8_t start_page_addr, uint8_t end_page_addr,
1589 uint8_t rows, ssd1306_scroll_frame_t frames)
1590{
1591 uint8_t buf[6];
1592
1593 if (handle == NULL) /* check handle */
1594 {
1595 return 2; /* return error */
1596 }
1597 if (handle->inited != 1) /* check handle initialization */
1598 {
1599 return 3; /* return error */
1600 }
1601 if (start_page_addr > 0x07) /* check start_page_addr */
1602 {
1603 handle->debug_print("ssd1306: start_page_addr is invalid.\n"); /* start_page_addr is invalid */
1604
1605 return 4; /* return error */
1606 }
1607 if (end_page_addr > 0x07) /* check end_page_addr */
1608 {
1609 handle->debug_print("ssd1306: end_page_addr is invalid.\n"); /* end_page_addr is invalid */
1610
1611 return 5; /* return error */
1612 }
1613 if (rows > 0x3F) /* check rows */
1614 {
1615 handle->debug_print("ssd1306: rows is invalid.\n"); /* rows is invalid */
1616
1617 return 6; /* return error */
1618 }
1619
1620 buf[0] = SSD1306_CMD_VERTICAL_LEFT_HORIZONTAL_SCROLL; /* set command */
1621 buf[1] = 0x00; /* set null */
1622 buf[2] = start_page_addr & 0x07; /* set start page addr */
1623 buf[3] = frames & 0x07; /* set frames */
1624 buf[4] = end_page_addr & 0x07; /* set end page addr */
1625 buf[5] = rows & 0x3F; /* set rows */
1626
1627 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 6, SSD1306_CMD); /* write command */
1628}
1629
1641{
1642 if (handle == NULL) /* check handle */
1643 {
1644 return 2; /* return error */
1645 }
1646 if (handle->inited != 1) /* check handle initialization */
1647 {
1648 return 3; /* return error */
1649 }
1650
1651 return a_ssd1306_write_byte(handle, SSD1306_CMD_DEACTIVATE_SCROLL, SSD1306_CMD); /* write command */
1652}
1653
1665{
1666 if (handle == NULL) /* check handle */
1667 {
1668 return 2; /* return error */
1669 }
1670 if (handle->inited != 1) /* check handle initialization */
1671 {
1672 return 3; /* return error */
1673 }
1674
1675 return a_ssd1306_write_byte(handle, SSD1306_CMD_ACTIVATE_SCROLL, SSD1306_CMD); /* write command */
1676}
1677
1691{
1692 if (handle == NULL) /* check handle */
1693 {
1694 return 2; /* return error */
1695 }
1696 if (handle->inited != 1) /* check handle initialization */
1697 {
1698 return 3; /* return error */
1699 }
1700 if (l > 0x3F) /* check line */
1701 {
1702 handle->debug_print("ssd1306: line is invalid.\n"); /* line is invalid */
1703
1704 return 4; /* return error */
1705 }
1706
1707 return a_ssd1306_write_byte(handle, SSD1306_CMD_DISPLAY_START_LINE|(l&0x3F), SSD1306_CMD); /* write command */
1708}
1709
1721uint8_t ssd1306_set_contrast(ssd1306_handle_t *handle, uint8_t contrast)
1722{
1723 uint8_t buf[2];
1724
1725 if (handle == NULL) /* check handle */
1726 {
1727 return 2; /* return error */
1728 }
1729 if (handle->inited != 1) /* check handle initialization */
1730 {
1731 return 3; /* return error */
1732 }
1733
1734 buf[0] = SSD1306_CMD_CONTRAST_CONTROL; /* set command */
1735 buf[1] = contrast; /* set contrast */
1736
1737 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
1738}
1739
1752{
1753 uint8_t buf[2];
1754
1755 if (handle == NULL) /* check handle */
1756 {
1757 return 2; /* return error */
1758 }
1759 if (handle->inited != 1) /* check handle initialization */
1760 {
1761 return 3; /* return error */
1762 }
1763
1764 buf[0] = SSD1306_CMD_CHARGE_PUMP_SETTING; /* set command */
1765 buf[1] = (uint8_t)(0x10 | (enable << 2)); /* set charge pump */
1766
1767 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
1768}
1769
1782{
1783 if (handle == NULL) /* check handle */
1784 {
1785 return 2; /* return error */
1786 }
1787 if (handle->inited != 1) /* check handle initialization */
1788 {
1789 return 3; /* return error */
1790 }
1791
1792 if (remap != 0) /* check remap */
1793 {
1794 return a_ssd1306_write_byte(handle, SSD1306_CMD_COLUMN_127_MAPPED_TO_SEG0, SSD1306_CMD); /* write remap */
1795 }
1796 else
1797 {
1798 return a_ssd1306_write_byte(handle, SSD1306_CMD_COLUMN_0_MAPPED_TO_SEG0, SSD1306_CMD); /* write remap */
1799 }
1800}
1801
1817uint8_t ssd1306_set_vertical_scroll_area(ssd1306_handle_t *handle, uint8_t start_row, uint8_t end_row)
1818{
1819 uint8_t buf[3];
1820
1821 if (handle == NULL) /* check handle */
1822 {
1823 return 2; /* return error */
1824 }
1825 if (handle->inited != 1) /* check handle initialization */
1826 {
1827 return 3; /* return error */
1828 }
1829 if (start_row > 0x3F) /* check start row */
1830 {
1831 handle->debug_print("ssd1306: start_row is invalid.\n"); /* start_row is invalid */
1832
1833 return 4; /* return error */
1834 }
1835 if (end_row > 0x7F) /* check end_row */
1836 {
1837 handle->debug_print("ssd1306: end_row is invalid.\n"); /* end_row is invalid */
1838
1839 return 5; /* return error */
1840 }
1841 if (end_row > start_row) /* check start_row and end_row */
1842 {
1843 handle->debug_print("ssd1306: end_row > start_row.\n"); /* end_row > start_row */
1844
1845 return 6; /* return error */
1846 }
1847
1848 buf[0] = SSD1306_CMD_VERTICAL_SCROLL_AREA; /* set command */
1849 buf[1] = start_row; /* set start row */
1850 buf[2] = end_row; /* set end row */
1851
1852 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 3, SSD1306_CMD); /* write command */
1853}
1854
1867{
1868 if (handle == NULL) /* check handle */
1869 {
1870 return 2; /* return error */
1871 }
1872 if (handle->inited != 1) /* check handle initialization */
1873 {
1874 return 3; /* return error */
1875 }
1876
1877 if (enable != 0) /* if enable */
1878 {
1879 return a_ssd1306_write_byte(handle, SSD1306_CMD_ENTIRE_DISPLAY_ON, SSD1306_CMD); /* write command */
1880 }
1881 else
1882 {
1883 return a_ssd1306_write_byte(handle, SSD1306_CMD_ENTIRE_DISPLAY_OFF, SSD1306_CMD); /* write command */
1884 }
1885}
1886
1899{
1900 if (handle == NULL) /* check handle */
1901 {
1902 return 2; /* return error */
1903 }
1904 if (handle->inited != 1) /* check handle initialization */
1905 {
1906 return 3; /* return error */
1907 }
1908
1909 if (mode != 0) /* check mode */
1910 {
1911 return a_ssd1306_write_byte(handle, SSD1306_CMD_INVERSE_DISPLAY, SSD1306_CMD); /* write command */
1912 }
1913 else
1914 {
1915 return a_ssd1306_write_byte(handle, SSD1306_CMD_NORMAL_DISPLAY, SSD1306_CMD); /* write command */
1916 }
1917}
1918
1932uint8_t ssd1306_set_multiplex_ratio(ssd1306_handle_t *handle, uint8_t multiplex)
1933{
1934 uint8_t buf[2];
1935
1936 if (handle == NULL) /* check handle */
1937 {
1938 return 2; /* return error */
1939 }
1940 if (handle->inited != 1) /* check handle initialization */
1941 {
1942 return 3; /* return error */
1943 }
1944 if (multiplex < 0x0F) /* check multiplex */
1945 {
1946 handle->debug_print("ssd1306: multiplex is too small.\n"); /* multiplex is too small */
1947
1948 return 4; /* return error */
1949 }
1950 if (multiplex > 0x3F) /* check multiplex */
1951 {
1952 handle->debug_print("ssd1306: multiplex is too large.\n"); /* multiplex is too large */
1953
1954 return 5; /* return error */
1955 }
1956
1957 buf[0] = SSD1306_CMD_MULTIPLEX_RATIO ; /* set command */
1958 buf[1] = multiplex; /* set multiplex */
1959
1960 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
1961}
1962
1975{
1976 if (handle == NULL) /* check handle */
1977 {
1978 return 2; /* return error */
1979 }
1980 if (handle->inited != 1) /* check handle initialization */
1981 {
1982 return 3; /* return error */
1983 }
1984
1985 if (on_off != 0) /* check on off */
1986 {
1987 return a_ssd1306_write_byte(handle, SSD1306_CMD_DISPLAY_ON, SSD1306_CMD); /* write command */
1988 }
1989 else
1990 {
1991 return a_ssd1306_write_byte(handle, SSD1306_CMD_DISPLAY_OFF, SSD1306_CMD); /* write command */
1992 }
1993}
1994
2007uint8_t ssd1306_set_page_address(ssd1306_handle_t *handle, uint8_t addr)
2008{
2009 if (handle == NULL) /* check handle */
2010 {
2011 return 2; /* return error */
2012 }
2013 if (handle->inited != 1) /* check handle initialization */
2014 {
2015 return 3; /* return error */
2016 }
2017 if (addr > 0x07) /* check addr */
2018 {
2019 handle->debug_print("ssd1306: addr is invalid.\n"); /* addr is invalid */
2020
2021 return 4; /* return error */
2022 }
2023
2024 return a_ssd1306_write_byte(handle, SSD1306_CMD_PAGE_ADDR|(addr&0x07), SSD1306_CMD); /* write command */
2025}
2026
2039{
2040 if (handle == NULL) /* check handle */
2041 {
2042 return 2; /* return error */
2043 }
2044 if (handle->inited != 1) /* check handle initialization */
2045 {
2046 return 3; /* return error */
2047 }
2048
2049 if (dir != 0) /* choose dir */
2050 {
2051 return a_ssd1306_write_byte(handle, SSD1306_CMD_SCAN_DIRECTION_COMN_1_START, SSD1306_CMD); /* write command */
2052 }
2053 else
2054 {
2055 return a_ssd1306_write_byte(handle, SSD1306_CMD_SCAN_DIRECTION_COM0_START, SSD1306_CMD); /* write command */
2056 }
2057}
2058
2071uint8_t ssd1306_set_display_offset(ssd1306_handle_t *handle, uint8_t offset)
2072{
2073 uint8_t buf[2];
2074
2075 if (handle == NULL) /* check handle */
2076 {
2077 return 2; /* return error */
2078 }
2079 if (handle->inited != 1) /* check handle initialization */
2080 {
2081 return 3; /* return error */
2082 }
2083 if (offset > 0x3F) /* check offset */
2084 {
2085 handle->debug_print("ssd1306: offset is invalid.\n"); /* offset is invalid */
2086
2087 return 4; /* return error */
2088 }
2089
2090 buf[0] = SSD1306_CMD_DISPLAY_OFFSET ; /* set command */
2091 buf[1] = offset; /* set offset */
2092
2093 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
2094}
2095
2110uint8_t ssd1306_set_display_clock(ssd1306_handle_t *handle, uint8_t oscillator_frequency, uint8_t clock_divide)
2111{
2112 uint8_t buf[2];
2113
2114 if (handle == NULL) /* check handle */
2115 {
2116 return 2; /* return error */
2117 }
2118 if (handle->inited != 1) /* check handle initialization */
2119 {
2120 return 3; /* return error */
2121 }
2122 if (oscillator_frequency> 0x0F) /* check oscillator_frequency */
2123 {
2124 handle->debug_print("ssd1306: oscillator frequency is invalid.\n"); /* oscillator frequency is invalid */
2125
2126 return 4; /* return error */
2127 }
2128 if (clock_divide> 0x0F) /* check clock_divide */
2129 {
2130 handle->debug_print("ssd1306: clock divide is invalid.\n"); /* clock divide is invalid */
2131
2132 return 5; /* return error */
2133 }
2134
2135 buf[0] = SSD1306_CMD_DISPLAY_CLOCK_DIVIDE ; /* set command */
2136 buf[1] = (oscillator_frequency<<4) | clock_divide; /* set oscillator frequency and clock divide */
2137
2138 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
2139}
2140
2153{
2154 uint8_t buf[2];
2155
2156 if (handle == NULL) /* check handle */
2157 {
2158 return 2; /* return error */
2159 }
2160 if (handle->inited != 1) /* check handle initialization */
2161 {
2162 return 3; /* return error */
2163 }
2164
2165 buf[0] = SSD1306_CMD_SET_ZOOM_IN ; /* set command */
2166 buf[1] = zoom; /* set zoom */
2167
2168 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
2169}
2170
2185uint8_t ssd1306_set_precharge_period(ssd1306_handle_t *handle, uint8_t phase1_period, uint8_t phase2_period)
2186{
2187 uint8_t buf[2];
2188
2189 if (handle == NULL) /* check handle */
2190 {
2191 return 2; /* return error */
2192 }
2193 if (handle->inited != 1) /* check handle initialization */
2194 {
2195 return 3; /* return error */
2196 }
2197 if (phase1_period> 0x0F) /* check phase1 period */
2198 {
2199 handle->debug_print("ssd1306: phase1 period is invalid.\n"); /* phase1 period is invalid */
2200
2201 return 4; /* return error */
2202 }
2203 if (phase2_period> 0x0F) /* check phase2 period */
2204 {
2205 handle->debug_print("ssd1306: phase2 period is invalid.\n"); /* phase2 period is invalid */
2206
2207 return 5; /* return error */
2208 }
2209
2210 buf[0] = SSD1306_CMD_PRE_CHARGE_PERIOD; /* set command */
2211 buf[1] = (phase2_period << 4) | phase1_period; /* set period */
2212
2213 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
2214}
2215
2229{
2230 uint8_t buf[2];
2231
2232 if (handle == NULL) /* check handle */
2233 {
2234 return 2; /* return error */
2235 }
2236 if (handle->inited != 1) /* check handle initialization */
2237 {
2238 return 3; /* return error */
2239 }
2240
2241 buf[0] = SSD1306_CMD_COM_PINS_CONF; /* set command */
2242 buf[1] = (uint8_t)((conf<<4) | (remap<<5) |0x02); /* set com pins */
2243
2244 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
2245}
2246
2259{
2260 uint8_t buf[2];
2261
2262 if (handle == NULL) /* check handle */
2263 {
2264 return 2; /* return error */
2265 }
2266 if (handle->inited != 1) /* check handle initialization */
2267 {
2268 return 3; /* return error */
2269 }
2270
2271 buf[0] = SSD1306_CMD_COMH_DESLECT_LEVEL; /* set command */
2272 buf[1] = (uint8_t)(level << 4); /* set level */
2273
2274 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, 2, SSD1306_CMD); /* write command */
2275}
2276
2289uint8_t ssd1306_write_cmd(ssd1306_handle_t *handle, uint8_t *buf, uint8_t len)
2290{
2291 if (handle == NULL) /* check handle */
2292 {
2293 return 2; /* return error */
2294 }
2295 if (handle->inited != 1) /* check handle initialization */
2296 {
2297 return 3; /* return error */
2298 }
2299
2300 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, len, SSD1306_CMD); /* write command */
2301}
2302
2315uint8_t ssd1306_write_data(ssd1306_handle_t *handle, uint8_t *buf, uint8_t len)
2316{
2317 if (handle == NULL) /* check handle */
2318 {
2319 return 2; /* return error */
2320 }
2321 if (handle->inited != 1) /* check handle initialization */
2322 {
2323 return 3; /* return error */
2324 }
2325
2326 return a_ssd1306_multiple_write_byte(handle, (uint8_t *)buf, len, SSD1306_DATA); /* write data */
2327}
2328
2338{
2339 if (info == NULL) /* check handle */
2340 {
2341 return 2; /* return error */
2342 }
2343
2344 memset(info, 0, sizeof(ssd1306_info_t)); /* initialize ssd1306 info structure */
2345 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2346 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2347 strncpy(info->interface, "IIC SPI", 8); /* copy interface name */
2348 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2349 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2350 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2351 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2352 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2353 info->driver_version = DRIVER_VERSION; /* set driver version */
2354
2355 return 0; /* success return 0 */
2356}
#define SSD1306_DATA
#define SSD1306_CMD_DEACTIVATE_SCROLL
#define SSD1306_CMD_SET_PAGE_ADDRESS
#define SSD1306_CMD_INVERSE_DISPLAY
#define SSD1306_CMD_DISPLAY_OFFSET
#define SSD1306_CMD_DISPLAY_START_LINE
#define SSD1306_CMD_SCAN_DIRECTION_COM0_START
#define MAX_CURRENT
#define SSD1306_CMD_COMH_DESLECT_LEVEL
#define SSD1306_CMD_DISPLAY_CLOCK_DIVIDE
#define SSD1306_CMD_PAGE_ADDR
#define SSD1306_CMD_SCAN_DIRECTION_COMN_1_START
#define SSD1306_CMD_DISPLAY_OFF
#define SUPPLY_VOLTAGE_MAX
#define SSD1306_CMD_DISPLAY_ON
#define SSD1306_CMD_RIGHT_HORIZONTAL_SCROLL
#define SSD1306_CMD_LOWER_COLUMN_START_ADDRESS
chip command definition
#define SSD1306_CMD_MEMORY_ADDRESSING_MODE
#define SSD1306_CMD_HIGHER_COLUMN_START_ADDRESS
#define SSD1306_CMD_LEFT_HORIZONTAL_SCROLL
#define SSD1306_CMD_ENTIRE_DISPLAY_OFF
#define SSD1306_CMD_SET_COLUMN_ADDRESS
#define SSD1306_CMD_NORMAL_DISPLAY
#define TEMPERATURE_MAX
#define SSD1306_CMD_COM_PINS_CONF
#define SSD1306_CMD_ENTIRE_DISPLAY_ON
#define SSD1306_CMD
chip command data definition
#define SSD1306_CMD_MULTIPLEX_RATIO
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define SSD1306_CMD_VERTICAL_LEFT_HORIZONTAL_SCROLL
#define SSD1306_CMD_SET_FADE_OUT_AND_BLINKING
#define SSD1306_CMD_CONTRAST_CONTROL
#define SSD1306_CMD_CHARGE_PUMP_SETTING
#define SSD1306_CMD_ACTIVATE_SCROLL
#define CHIP_NAME
chip information definition
#define SSD1306_CMD_COLUMN_0_MAPPED_TO_SEG0
#define SSD1306_CMD_VERTICAL_RIGHT_HORIZONTAL_SCROLL
#define SSD1306_CMD_SET_ZOOM_IN
#define DRIVER_VERSION
#define SSD1306_CMD_COLUMN_127_MAPPED_TO_SEG0
#define SSD1306_CMD_VERTICAL_SCROLL_AREA
#define SSD1306_CMD_PRE_CHARGE_PERIOD
driver ssd1306 header file
driver ssd1306 font header file
uint8_t ssd1306_write_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t data)
write a point
struct ssd1306_info_s ssd1306_info_t
ssd1306 information structure definition
ssd1306_left_right_remap_t
ssd1306 left right remap enumeration definition
uint8_t ssd1306_set_contrast(ssd1306_handle_t *handle, uint8_t contrast)
set the display contrast
uint8_t ssd1306_set_page_address_range(ssd1306_handle_t *handle, uint8_t start_addr, uint8_t end_addr)
set the page address range
uint8_t ssd1306_gram_read_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t *data)
read a point from the gram
uint8_t ssd1306_set_zoom_in(ssd1306_handle_t *handle, ssd1306_zoom_in_t zoom)
set the display zoom in
uint8_t ssd1306_set_scan_direction(ssd1306_handle_t *handle, ssd1306_scan_direction_t dir)
set the scan direction
ssd1306_scan_direction_t
ssd1306 scan direction enumeration definition
uint8_t ssd1306_set_precharge_period(ssd1306_handle_t *handle, uint8_t phase1_period, uint8_t phase2_period)
set the pre charge period
uint8_t ssd1306_gram_write_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t data)
write a point in the gram
uint8_t ssd1306_set_memory_addressing_mode(ssd1306_handle_t *handle, ssd1306_memory_addressing_mode_t mode)
set the memory addressing mode
ssd1306_interface_t
ssd1306 interface enumeration definition
struct ssd1306_handle_s ssd1306_handle_t
ssd1306 handle structure definition
ssd1306_display_t
ssd1306 display enumeration definition
uint8_t ssd1306_set_addr_pin(ssd1306_handle_t *handle, ssd1306_address_t addr_pin)
set the chip iic address
ssd1306_charge_pump_t
ssd1306 charge pump enumeration definition
ssd1306_memory_addressing_mode_t
ssd1306 memory addressing mode enumeration definition
uint8_t ssd1306_gram_draw_picture(ssd1306_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t *img)
draw a picture in the gram
ssd1306_entire_display_t
ssd1306 entire display enumeration definition
ssd1306_pin_conf_t
ssd1306 pin conf enumeration definition
uint8_t ssd1306_info(ssd1306_info_t *info)
get chip's information
ssd1306_font_t
ssd1306 font enumeration definition
ssd1306_segment_column_remap_t
ssd1306 segment column remap enumeration definition
uint8_t ssd1306_set_vertical_scroll_area(ssd1306_handle_t *handle, uint8_t start_row, uint8_t end_row)
set the vertical scroll area
uint8_t ssd1306_gram_fill_rect(ssd1306_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t color)
fill a rectangle in the gram
uint8_t ssd1306_set_charge_pump(ssd1306_handle_t *handle, ssd1306_charge_pump_t enable)
enable or disable the charge pump
uint8_t ssd1306_set_page_address(ssd1306_handle_t *handle, uint8_t addr)
set the page address
uint8_t ssd1306_get_interface(ssd1306_handle_t *handle, ssd1306_interface_t *interface)
get the chip interface
ssd1306_zoom_in_t
ssd1306 zoom in enumeration definition
uint8_t ssd1306_read_point(ssd1306_handle_t *handle, uint8_t x, uint8_t y, uint8_t *data)
read a point
uint8_t ssd1306_set_display_mode(ssd1306_handle_t *handle, ssd1306_display_mode_t mode)
set the display mode
uint8_t ssd1306_set_display_offset(ssd1306_handle_t *handle, uint8_t offset)
set the display offset
uint8_t ssd1306_set_display(ssd1306_handle_t *handle, ssd1306_display_t on_off)
enable or disable the display
uint8_t ssd1306_activate_scroll(ssd1306_handle_t *handle)
activate the scroll
uint8_t ssd1306_set_multiplex_ratio(ssd1306_handle_t *handle, uint8_t multiplex)
set the multiplex ratio
uint8_t ssd1306_set_display_start_line(ssd1306_handle_t *handle, uint8_t l)
set the display start line
uint8_t ssd1306_deactivate_scroll(ssd1306_handle_t *handle)
deactivate the scroll
ssd1306_address_t
ssd1306 address pin enumeration definition
uint8_t ssd1306_get_addr_pin(ssd1306_handle_t *handle, ssd1306_address_t *addr_pin)
get the chip iic address
uint8_t ssd1306_set_high_column_start_address(ssd1306_handle_t *handle, uint8_t addr)
set the high column start address
uint8_t ssd1306_set_display_clock(ssd1306_handle_t *handle, uint8_t oscillator_frequency, uint8_t clock_divide)
set the display clock
uint8_t ssd1306_deinit(ssd1306_handle_t *handle)
close the chip
ssd1306_display_mode_t
ssd1306 display mode enumeration definition
uint8_t ssd1306_set_segment_remap(ssd1306_handle_t *handle, ssd1306_segment_column_remap_t remap)
set the segment remap
ssd1306_scroll_frame_t
ssd1306 scroll frame enumeration definition
uint8_t ssd1306_set_right_horizontal_scroll(ssd1306_handle_t *handle, uint8_t start_page_addr, uint8_t end_page_addr, ssd1306_scroll_frame_t frames)
set the right horizontal scroll
uint8_t ssd1306_set_com_pins_hardware_conf(ssd1306_handle_t *handle, ssd1306_pin_conf_t conf, ssd1306_left_right_remap_t remap)
set the hardware com pins
uint8_t ssd1306_gram_write_string(ssd1306_handle_t *handle, uint8_t x, uint8_t y, char *str, uint16_t len, uint8_t color, ssd1306_font_t font)
draw a string in the gram
uint8_t ssd1306_set_left_horizontal_scroll(ssd1306_handle_t *handle, uint8_t start_page_addr, uint8_t end_page_addr, ssd1306_scroll_frame_t frames)
set the left horizontal scroll
uint8_t ssd1306_set_fade_blinking_mode(ssd1306_handle_t *handle, ssd1306_fade_blinking_mode_t mode, uint8_t frames)
set the fade blinking mode
uint8_t ssd1306_clear(ssd1306_handle_t *handle)
clear the screen
uint8_t ssd1306_set_deselect_level(ssd1306_handle_t *handle, ssd1306_deselect_level_t level)
set the deselect level
uint8_t ssd1306_set_column_address_range(ssd1306_handle_t *handle, uint8_t start_addr, uint8_t end_addr)
set the column address range
uint8_t ssd1306_set_vertical_left_horizontal_scroll(ssd1306_handle_t *handle, uint8_t start_page_addr, uint8_t end_page_addr, uint8_t rows, ssd1306_scroll_frame_t frames)
set the vertical left horizontal scroll
uint8_t ssd1306_gram_update(ssd1306_handle_t *handle)
update the gram data
uint8_t ssd1306_set_low_column_start_address(ssd1306_handle_t *handle, uint8_t addr)
set the low column start address
ssd1306_fade_blinking_mode_t
ssd1306 fade blinking mode enumeration definition
uint8_t ssd1306_set_entire_display(ssd1306_handle_t *handle, ssd1306_entire_display_t enable)
enable or disable the entire display
ssd1306_deselect_level_t
ssd1306 deselect level enumeration definition
uint8_t ssd1306_set_vertical_right_horizontal_scroll(ssd1306_handle_t *handle, uint8_t start_page_addr, uint8_t end_page_addr, uint8_t rows, ssd1306_scroll_frame_t frames)
set the vertical right horizontal scroll
uint8_t ssd1306_set_interface(ssd1306_handle_t *handle, ssd1306_interface_t interface)
set the chip interface
uint8_t ssd1306_init(ssd1306_handle_t *handle)
initialize the chip
@ SSD1306_INTERFACE_IIC
@ SSD1306_INTERFACE_SPI
uint8_t ssd1306_write_data(ssd1306_handle_t *handle, uint8_t *buf, uint8_t len)
write the register data
uint8_t ssd1306_write_cmd(ssd1306_handle_t *handle, uint8_t *buf, uint8_t len)
write the register command
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* spi_cmd_data_gpio_deinit)(void)
uint8_t(* reset_gpio_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* spi_deinit)(void)
uint8_t(* reset_gpio_init)(void)
uint8_t(* spi_cmd_data_gpio_init)(void)
uint8_t gram[128][8]
uint8_t(* spi_write_cmd)(uint8_t *buf, uint16_t len)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* spi_cmd_data_gpio_write)(uint8_t value)
uint8_t(* reset_gpio_write)(uint8_t value)
uint8_t(* iic_deinit)(void)
uint32_t driver_version
char manufacturer_name[32]