LibDriver ST7920
Loading...
Searching...
No Matches
driver_st7920.c
Go to the documentation of this file.
1
36
37#include "driver_st7920.h"
38
42#define CHIP_NAME "Sitronix ST7920"
43#define MANUFACTURER_NAME "Sitronix"
44#define SUPPLY_VOLTAGE_MIN 2.7f
45#define SUPPLY_VOLTAGE_MAX 4.5f
46#define MAX_CURRENT 0.45f
47#define TEMPERATURE_MIN -30.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define ST7920_CMD 0
55#define ST7920_DATA 1
56
60#define ST7920_WRITE 0
61#define ST7920_READ 1
62
66#define ST7920_CMD_BASIC_DISPLAY_CLEAR 0x01
67#define ST7920_CMD_BASIC_RETURN_HOME 0x02
68#define ST7920_CMD_BASIC_ENTRY_MODE_SET 0x04
69#define ST7920_CMD_BASIC_DISPLAY_CONTROL 0x08
70#define ST7920_CMD_BASIC_CURSOR_DISPLAY_CONTROL 0x10
71#define ST7920_CMD_BASIC_FUNCTION_SET 0x20
72#define ST7920_CMD_BASIC_SET_CGRAM 0x40
73#define ST7920_CMD_BASIC_SET_DDRAM 0x80
74#define ST7920_CMD_EXT_STANFBY 0x01
75#define ST7920_CMD_EXT_SCROLL_RAM_ADDRESS_SELECT 0x02
76#define ST7920_CMD_EXT_REVERSE 0x04
77#define ST7920_CMD_EXT_FUNCTION_SET 0x20
78#define ST7920_CMD_EXT_SET_SCROLL_ADDRESS 0x40
79#define ST7920_CMD_EXT_SET_GRAPHIC_DISPLAY_RAM_ADDRESS 0x80
80
90static uint8_t a_st7920_serial_write(st7920_handle_t *handle, uint8_t data)
91{
92 uint8_t res;
93 uint8_t i;
94
95 for (i = 0; i < 8; i++) /* 8 bits */
96 {
97 if ((data & 0x80) != 0) /* if high */
98 {
99 res = handle->sid_gpio_write(1); /* sid 1 */
100 if (res != 0) /* check the result */
101 {
102 return 1; /* return error */
103 }
104 }
105 else
106 {
107 res = handle->sid_gpio_write(0); /* sid 0 */
108 if (res != 0) /* check the result */
109 {
110 return 1; /* return error */
111 }
112 }
113
114 res = handle->sclk_gpio_write(1); /* sclk high */
115 if (res != 0) /* check the result */
116 {
117 return 1; /* return error */
118 }
119 handle->delay_us(ST7920_COMMAND_DATA_DELAY); /* delay */
120 res = handle->sclk_gpio_write(0); /* sclk low */
121 if (res != 0) /* check the result */
122 {
123 return 1; /* return error */
124 }
125 data <<= 1; /* << 1 bit */
126 }
127
128 return 0; /* success return 0 */
129}
130
143static uint8_t a_st7920_write_byte(st7920_handle_t *handle, uint8_t rw, uint8_t rs, uint8_t data, uint32_t us)
144{
145 uint8_t res;
146 uint8_t reg;
147
148 res = handle->cs_gpio_write(1); /* set high */
149 if (res != 0) /* check the result */
150 {
151 handle->debug_print("st7920: write failed.\n"); /* write failed */
152
153 return 1; /* return error */
154 }
155 handle->delay_us(ST7920_COMMAND_DATA_DELAY); /* delay */
156
157 reg = 0xF8; /* set synchronizing bit string */
158 reg |= rw << 2; /* set rw */
159 reg |= rs << 1; /* set rs */
160 res = a_st7920_serial_write(handle, reg); /* serial write */
161 if (res != 0) /* check the result */
162 {
163 handle->debug_print("st7920: write failed.\n"); /* write failed */
164 (void)handle->cs_gpio_write(0); /* set low */
165
166 return 1; /* return error */
167 }
168
169 reg = 0; /* set 0 */
170 reg |= ((data >> 4) & 0xF) << 4; /* set higher data */
171 res = a_st7920_serial_write(handle, reg); /* serial write */
172 if (res != 0) /* check the result */
173 {
174 handle->debug_print("st7920: write failed.\n"); /* write failed */
175 (void)handle->cs_gpio_write(0); /* set low */
176
177 return 1; /* return error */
178 }
179
180 reg = 0; /* set 0 */
181 reg |= ((data >> 0) & 0xF) << 4; /* set lower data */
182 res = a_st7920_serial_write(handle, reg); /* serial write */
183 if (res != 0) /* check the result */
184 {
185 handle->debug_print("st7920: write failed.\n"); /* write failed */
186 (void)handle->cs_gpio_write(0); /* set low */
187
188 return 1; /* return error */
189 }
190
191 res = handle->cs_gpio_write(0); /* set low */
192 if (res != 0) /* check the result */
193 {
194 handle->debug_print("st7920: write failed.\n"); /* write failed */
195
196 return 1; /* return error */
197 }
198
199 handle->delay_us(us); /* delay us */
200
201 return 0; /* success return 0 */
202}
203
215{
216 uint8_t i, j;
217
218 if (handle == NULL) /* check handle */
219 {
220 return 2; /* return error */
221 }
222 if (handle->debug_print == NULL) /* check debug_print */
223 {
224 return 3; /* return error */
225 }
226 if (handle->cs_gpio_init == NULL) /* check cs_gpio_init */
227 {
228 handle->debug_print("st7920: cs_gpio_init is null.\n"); /* cs_gpio_init is null */
229
230 return 3; /* return error */
231 }
232 if (handle->cs_gpio_deinit == NULL) /* check cs_gpio_deinit */
233 {
234 handle->debug_print("st7920: cs_gpio_deinit is null.\n"); /* cs_gpio_deinit is null */
235
236 return 3; /* return error */
237 }
238 if (handle->cs_gpio_write == NULL) /* check cs_gpio_write */
239 {
240 handle->debug_print("st7920: cs_gpio_write is null.\n"); /* cs_gpio_write is null */
241
242 return 3; /* return error */
243 }
244 if (handle->sclk_gpio_init == NULL) /* check sclk_gpio_init */
245 {
246 handle->debug_print("st7920: sclk_gpio_init is null.\n"); /* sclk_gpio_init is null */
247
248 return 3; /* return error */
249 }
250 if (handle->sclk_gpio_deinit == NULL) /* check sclk_gpio_deinit */
251 {
252 handle->debug_print("st7920: sclk_gpio_deinit is null.\n"); /* sclk_gpio_deinit is null */
253
254 return 3; /* return error */
255 }
256 if (handle->sclk_gpio_write == NULL) /* check sclk_gpio_write */
257 {
258 handle->debug_print("st7920: sclk_gpio_write is null.\n"); /* sclk_gpio_write is null */
259
260 return 3; /* return error */
261 }
262 if (handle->sid_gpio_init == NULL) /* check sid_gpio_init */
263 {
264 handle->debug_print("st7920: sid_gpio_init is null.\n"); /* sid_gpio_init is null */
265
266 return 3; /* return error */
267 }
268 if (handle->sid_gpio_deinit == NULL) /* check sid_gpio_deinit */
269 {
270 handle->debug_print("st7920: sid_gpio_deinit is null.\n"); /* sid_gpio_deinit is null */
271
272 return 3; /* return error */
273 }
274 if (handle->sid_gpio_write == NULL) /* check sid_gpio_write */
275 {
276 handle->debug_print("st7920: sid_gpio_write is null.\n"); /* sid_gpio_write is null */
277
278 return 3; /* return error */
279 }
280 if (handle->delay_ms == NULL) /* check delay_ms */
281 {
282 handle->debug_print("st7920: delay_ms is null.\n"); /* delay_ms is null */
283
284 return 3; /* return error */
285 }
286 if (handle->delay_us == NULL) /* check delay_us */
287 {
288 handle->debug_print("st7920: delay_us is null.\n"); /* delay_us is null */
289
290 return 3; /* return error */
291 }
292
293 if (handle->cs_gpio_init() != 0) /* cs gpio init */
294 {
295 handle->debug_print("st7920: cs gpio init failed.\n"); /* cs gpio init failed */
296
297 return 1; /* return error */
298 }
299 if (handle->sclk_gpio_init() != 0) /* sclk gpio init */
300 {
301 handle->debug_print("st7920: sclk gpio init failed.\n"); /* sclk gpio init failed */
302 (void)handle->cs_gpio_deinit(); /* cs gpio deinit */
303
304 return 1; /* return error */
305 }
306 if (handle->sid_gpio_init() != 0) /* sid gpio init */
307 {
308 handle->debug_print("st7920: sid gpio init failed.\n"); /* sid gpio init failed */
309 (void)handle->cs_gpio_deinit(); /* cs gpio deinit */
310 (void)handle->sclk_gpio_deinit(); /* sclk gpio deinit */
311
312 return 1; /* return error */
313 }
314 handle->delay_ms(50); /* delay 50ms */
315 for (i = 0; i < 8; i++) /* x */
316 {
317 for (j = 0; j < 64; j++) /* y */
318 {
319 handle->gram[i][j] = 0; /* set 0 */
320 }
321 }
322 handle->inited = 1; /* flag finish initialization */
323
324 return 0; /* success return 0 */
325}
326
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 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
350 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
351 {
352 handle->debug_print("st7920: set function failed.\n"); /* set function failed */
353
354 return 1; /* return error */
355 }
356 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
358 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
359 {
360 handle->debug_print("st7920: set display control failed.\n"); /* set display control failed */
361
362 return 1; /* return error */
363 }
364 if (handle->cs_gpio_deinit() != 0) /* cs gpio deinit */
365 {
366 handle->debug_print("st7920: cs gpio deinit failed.\n"); /* cs gpio deinit failed */
367
368 return 1; /* return error */
369 }
370 if (handle->sclk_gpio_deinit() != 0) /* sclk gpio deinit */
371 {
372 handle->debug_print("st7920: sclk gpio deinit failed.\n"); /* sclk gpio deinit failed */
373
374 return 1; /* return error */
375 }
376 if (handle->sid_gpio_deinit() != 0) /* sid gpio deinit */
377 {
378 handle->debug_print("st7920: sid gpio deinit failed.\n"); /* sid gpio deinit failed */
379
380 return 1; /* return error */
381 }
382 handle->inited = 0; /* flag closed */
383
384 return 0; /* success return 0 */
385}
386
404uint8_t st7920_write_point(st7920_handle_t *handle, uint8_t x, uint8_t y, uint8_t data)
405{
406 if (handle == NULL) /* check handle */
407 {
408 return 2; /* return error */
409 }
410 if (handle->inited != 1) /* check handle initialization */
411 {
412 return 3; /* return error */
413 }
414 if (handle->basic_extended != 1) /* check command type */
415 {
416 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
417
418 return 4; /* return error */
419 }
420 if (x > 127) /* check the x range */
421 {
422 handle->debug_print("st7920: x is over 127.\n"); /* x is over 127 */
423
424 return 5; /* return error */
425 }
426 if (y > 63) /* check the y range */
427 {
428 handle->debug_print("st7920: y is over 63.\n"); /* y is over 63 */
429
430 return 6; /* return error */
431 }
432
433 if (data != 0) /* if 1 */
434 {
435 handle->gram[x / 16][y] |= 1 << (15 - (x % 16)); /* set data */
436 }
437 else /* if 0 */
438 {
439 handle->gram[x / 16][y] &= ~(1 << (15 - (x % 16))); /* set data */
440 }
441
442 y += handle->scroll_address; /* y += scroll address */
443 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
445 ((y - handle->scroll_address) % 32 + handle->scroll_address) % 64),
446 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
447 {
448 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
449
450 return 1; /* return error */
451 }
452 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
454 8 * ((y - handle->scroll_address) / 32) + x / 16),
455 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
456 {
457 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
458
459 return 1; /* return error */
460 }
461 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
462 (uint8_t)((handle->gram[x / 16][y % 64] >> 8) & 0xFF),
463 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
464 {
465 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
466
467 return 1; /* return error */
468 }
469 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
470 (uint8_t)((handle->gram[x / 16][y % 64] >> 0) & 0xFF),
471 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
472 {
473 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
474
475 return 1; /* return error */
476 }
477
478 return 0; /* success return 0 */
479}
480
498uint8_t st7920_read_point(st7920_handle_t *handle, uint8_t x, uint8_t y, uint8_t *data)
499{
500 if (handle == NULL) /* check handle */
501 {
502 return 2; /* return error */
503 }
504 if (handle->inited != 1) /* check handle initialization */
505 {
506 return 3; /* return error */
507 }
508 if (handle->basic_extended != 1) /* check command type */
509 {
510 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
511
512 return 4; /* return error */
513 }
514 if (x > 127) /* check the x range */
515 {
516 handle->debug_print("st7920: x is over 127.\n"); /* x is over 127 */
517
518 return 5; /* return error */
519 }
520 if (y > 63) /* check the y range */
521 {
522 handle->debug_print("st7920: y is over 63.\n"); /* y is over 63 */
523
524 return 6; /* return error */
525 }
526 *data = (handle->gram[x / 16][y] >> (15 - (x % 16))) & 0x01; /* set data */
527
528 return 0; /* success return 0 */
529}
530
548uint8_t st7920_write_string(st7920_handle_t *handle, uint8_t x, uint8_t y, char *str)
549{
550 uint16_t i;
551 uint8_t pos;
552
553 if (handle == NULL) /* check handle */
554 {
555 return 2; /* return error */
556 }
557 if (handle->inited != 1) /* check handle initialization */
558 {
559 return 3; /* return error */
560 }
561 if (handle->basic_extended != 0) /* check command type */
562 {
563 handle->debug_print("st7920: this command must be run in basic command mode.\n"); /* this command must be run in basic command mode */
564
565 return 4; /* return error */
566 }
567 if (x > 3) /* check the x range */
568 {
569 handle->debug_print("st7920: x is over 3.\n"); /* x is over 3 */
570
571 return 5; /* return error */
572 }
573 if (y > 7) /* check the y range */
574 {
575 handle->debug_print("st7920: y is over 7.\n"); /* y is over 7 */
576
577 return 6; /* return error */
578 }
579
580 if (x == 0) /* if x == 0 */
581 {
582 pos = 0x80 + y; /* set pos */
583 }
584 else if (x == 1) /* if x == 1 */
585 {
586 pos = 0x90 + y; /* set pos */
587 }
588 else if (x == 2) /* if x == 2 */
589 {
590 pos = 0x88 + y; /* set pos */
591 }
592 else /* x == 3 */
593 {
594 pos = 0x98 + y; /* set pos */
595 }
596 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD, pos,
597 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
598 {
599 handle->debug_print("st7920: set ddram address failed.\n"); /* set ddram address failed */
600
601 return 1; /* return error */
602 }
603
604 for (i = 0; str[i] != 0; i++) /* write the string */
605 {
606 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA, (uint8_t)str[i],
607 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
608 {
609 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
610
611 return 1; /* return error */
612 }
613 }
614
615 return 0; /* success return 0 */
616}
617
626static void a_st7920_draw_point(st7920_handle_t *handle, uint8_t x, uint8_t y, uint8_t data)
627{
628 if (data != 0) /* if 1 */
629 {
630 handle->gram[x / 16][y] |= 1 << (15 - (x % 16)); /* set data */
631 }
632 else /* if 0 */
633 {
634 handle->gram[x / 16][y] &= ~(1 << (15 - (x % 16))); /* set data */
635 }
636}
637
657uint8_t st7920_fill_rect(st7920_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t color)
658{
659 uint8_t x, y;
660 uint8_t i, j;
661 uint8_t pos_start, pos_end;
662
663 if (handle == NULL) /* check handle */
664 {
665 return 2; /* return error */
666 }
667 if (handle->inited != 1) /* check handle initialization */
668 {
669 return 3; /* return error */
670 }
671 if (handle->basic_extended != 1) /* check command type */
672 {
673 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
674
675 return 4; /* return error */
676 }
677 if ((left > 127) || (top > 63)) /* check left top */
678 {
679 handle->debug_print("st7920: left or top is invalid.\n"); /* left or top is invalid */
680
681 return 5; /* return error */
682 }
683 if ((right > 127) || (bottom > 63)) /* check right bottom */
684 {
685 handle->debug_print("st7920: right or bottom is invalid.\n"); /* right or bottom is invalid */
686
687 return 6; /* return error */
688 }
689 if ((left > right) || (top > bottom)) /* check left right top bottom */
690 {
691 handle->debug_print("st7920: left > right or top > bottom.\n"); /* left > right or top > bottom */
692
693 return 7; /* return error */
694 }
695
696 for (x = left; x <= right; x++) /* write x */
697 {
698 for (y = top; y <= bottom; y++) /* write y */
699 {
700 a_st7920_draw_point(handle, x, y, color); /* draw point */
701 }
702 }
703
704 pos_start = handle->scroll_address; /* set start */
705 pos_end = 32 + handle->scroll_address; /* set end */
706 for (i = pos_start; i < pos_end; i++) /* 32 line */
707 {
708 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
710 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
711 {
712 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
713
714 return 1; /* return error */
715 }
716 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
718 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
719 {
720 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
721
722 return 1; /* return error */
723 }
724 for (j = 0; j < 8; j++) /* 8 times */
725 {
726 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
727 (uint8_t)((handle->gram[j][i % 64] >> 8) & 0xFF),
728 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
729 {
730 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
731
732 return 1; /* return error */
733 }
734 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
735 (uint8_t)((handle->gram[j][i % 64] >> 0) & 0xFF),
736 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
737 {
738 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
739
740 return 1; /* return error */
741 }
742 }
743 }
744 for (i = pos_start; i < pos_end; i++) /* 32 line */
745 {
746 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
748 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
749 {
750 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
751
752 return 1; /* return error */
753 }
754 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
756 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
757 {
758 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
759
760 return 1; /* return error */
761 }
762 for (j = 0; j < 8; j++) /* 8 times */
763 {
764 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
765 (uint8_t)((handle->gram[j][(i + 32) % 64] >> 8) & 0xFF),
766 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
767 {
768 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
769
770 return 1; /* return error */
771 }
772 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
773 (uint8_t)((handle->gram[j][(i + 32) % 64] >> 0) & 0xFF),
774 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
775 {
776 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
777
778 return 1; /* return error */
779 }
780 }
781 }
782
783 return 0; /* return error */
784}
785
805uint8_t st7920_draw_picture(st7920_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 uint8_t i, j;
809 uint8_t pos_start, pos_end;
810
811 if (handle == NULL) /* check handle */
812 {
813 return 2; /* return error */
814 }
815 if (handle->inited != 1) /* check handle initialization */
816 {
817 return 3; /* return error */
818 }
819 if (handle->basic_extended != 1) /* check command type */
820 {
821 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
822
823 return 4; /* return error */
824 }
825 if ((left > 127) || (top > 63)) /* check left top */
826 {
827 handle->debug_print("st7920: left or top is invalid.\n"); /* left or top is invalid */
828
829 return 5; /* return error */
830 }
831 if ((right > 127) || (bottom > 63)) /* check right bottom */
832 {
833 handle->debug_print("st7920: right or bottom is invalid.\n"); /* right or bottom is invalid */
834
835 return 6; /* return error */
836 }
837 if ((left > right) || (top > bottom)) /* check left right top bottom */
838 {
839 handle->debug_print("st7920: left > right or top > bottom.\n"); /* left > right or top > bottom */
840
841 return 7; /* return error */
842 }
843
844 for (x = left; x <= right; x++) /* write x */
845 {
846 for (y = top; y <= bottom; y++) /* write y */
847 {
848 a_st7920_draw_point(handle, x, y, *img); /* draw point */
849 img++; /* img++ */
850 }
851 }
852
853 pos_start = handle->scroll_address; /* set start */
854 pos_end = 32 + handle->scroll_address; /* set end */
855 for (i = pos_start; i < pos_end; i++) /* 32 line */
856 {
857 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
859 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
860 {
861 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
862
863 return 1; /* return error */
864 }
865 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
867 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
868 {
869 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
870
871 return 1; /* return error */
872 }
873 for (j = 0; j < 8; j++) /* 8 times */
874 {
875 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
876 (uint8_t)((handle->gram[j][i % 64] >> 8) & 0xFF),
877 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
878 {
879 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
880
881 return 1; /* return error */
882 }
883 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
884 (uint8_t)((handle->gram[j][i % 64] >> 0) & 0xFF),
885 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
886 {
887 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
888
889 return 1; /* return error */
890 }
891 }
892 }
893 for (i = pos_start; i < pos_end; i++) /* 32 line */
894 {
895 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
897 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
898 {
899 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
900
901 return 1; /* return error */
902 }
903 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
905 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
906 {
907 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
908
909 return 1; /* return error */
910 }
911 for (j = 0; j < 8; j++) /* 8 times */
912 {
913 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
914 (uint8_t)((handle->gram[j][(i + 32) % 64] >> 8) & 0xFF),
915 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
916 {
917 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
918
919 return 1; /* return error */
920 }
921 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
922 (uint8_t)((handle->gram[j][(i + 32) % 64] >> 0) & 0xFF),
923 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
924 {
925 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
926
927 return 1; /* return error */
928 }
929 }
930 }
931
932 return 0; /* return error */
933}
934
954uint8_t st7920_draw_compress_picture(st7920_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t *img)
955{
956 uint8_t x, y;
957 uint8_t i, j;
958 uint8_t pos_start, pos_end;
959
960 if (handle == NULL) /* check handle */
961 {
962 return 2; /* return error */
963 }
964 if (handle->inited != 1) /* check handle initialization */
965 {
966 return 3; /* return error */
967 }
968 if (handle->basic_extended != 1) /* check command type */
969 {
970 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
971
972 return 4; /* return error */
973 }
974 if ((left > 127) || (top > 63)) /* check left top */
975 {
976 handle->debug_print("st7920: left or top is invalid.\n"); /* left or top is invalid */
977
978 return 5; /* return error */
979 }
980 if ((right > 127) || (bottom > 63)) /* check right bottom */
981 {
982 handle->debug_print("st7920: right or bottom is invalid.\n"); /* right or bottom is invalid */
983
984 return 6; /* return error */
985 }
986 if ((left > right) || (top > bottom)) /* check left right top bottom */
987 {
988 handle->debug_print("st7920: left > right or top > bottom.\n"); /* left > right or top > bottom */
989
990 return 7; /* return error */
991 }
992
993 for (x = left; x <= right; x++) /* write x */
994 {
995 for (y = top; y <= bottom; y++) /* write y */
996 {
997 uint32_t l;
998 uint32_t m;
999 uint32_t n;
1000
1001 l = (x - left) * (bottom - top + 1) + (y - top); /* get pos */
1002 m = l / 8; /* get m */
1003 n = l % 8; /* get n */
1004 if (((img[m] >> (7 - n)) & 0x01) != 0)
1005 {
1006 a_st7920_draw_point(handle, x, y, 0xFF); /* draw point */
1007 }
1008 else
1009 {
1010 a_st7920_draw_point(handle, x, y, 0x00); /* draw point */
1011 }
1012 }
1013 }
1014
1015 pos_start = handle->scroll_address; /* set start */
1016 pos_end = 32 + handle->scroll_address; /* set end */
1017 for (i = pos_start; i < pos_end; i++) /* 32 line */
1018 {
1019 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1021 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
1022 {
1023 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1024
1025 return 1; /* return error */
1026 }
1027 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1029 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
1030 {
1031 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1032
1033 return 1; /* return error */
1034 }
1035 for (j = 0; j < 8; j++) /* 8 times */
1036 {
1037 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1038 (uint8_t)((handle->gram[j][i % 64] >> 8) & 0xFF),
1039 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1040 {
1041 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1042
1043 return 1; /* return error */
1044 }
1045 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1046 (uint8_t)((handle->gram[j][i % 64] >> 0) & 0xFF),
1047 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1048 {
1049 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1050
1051 return 1; /* return error */
1052 }
1053 }
1054 }
1055 for (i = pos_start; i < pos_end; i++) /* 32 line */
1056 {
1057 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1059 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
1060 {
1061 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1062
1063 return 1; /* return error */
1064 }
1065 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1067 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
1068 {
1069 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1070
1071 return 1; /* return error */
1072 }
1073 for (j = 0; j < 8; j++) /* 8 times */
1074 {
1075 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1076 (uint8_t)((handle->gram[j][(i + 32) % 64] >> 8) & 0xFF),
1077 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1078 {
1079 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1080
1081 return 1; /* return error */
1082 }
1083 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1084 (uint8_t)((handle->gram[j][(i + 32) % 64] >> 0) & 0xFF),
1085 ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1086 {
1087 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1088
1089 return 1; /* return error */
1090 }
1091 }
1092 }
1093
1094 return 0; /* return error */
1095}
1096
1108{
1109 uint8_t i, j;
1110
1111 if (handle == NULL) /* check handle */
1112 {
1113 return 2; /* return error */
1114 }
1115 if (handle->inited != 1) /* check handle initialization */
1116 {
1117 return 3; /* return error */
1118 }
1119
1120 for (i = 0; i < 8; i++) /* x */
1121 {
1122 for (j = 0; j < 64; j++) /* y */
1123 {
1124 handle->gram[i][j] = 0; /* set 0 */
1125 }
1126 }
1127
1128 if (handle->basic_extended == 0)
1129 {
1130 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1131 ST7920_CMD_BASIC_DISPLAY_CLEAR, 2000) != 0) /* write command */
1132 {
1133 handle->debug_print("st7920: display clear failed.\n"); /* display clear failed */
1134
1135 return 1; /* return error */
1136 }
1137 }
1138 else
1139 {
1140 for (i = 0; i < 64; i++)
1141 {
1142 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1144 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
1145 {
1146 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1147
1148 return 1; /* return error */
1149 }
1150 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1152 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
1153 {
1154 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1155
1156 return 1; /* return error */
1157 }
1158 for (j = 0; j < 8; j++)
1159 {
1160 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1161 0x00, ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1162 {
1163 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1164
1165 return 1; /* return error */
1166 }
1167 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1168 0x00, ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1169 {
1170 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1171
1172 return 1; /* return error */
1173 }
1174 }
1175 }
1176 for (i = 0; i < 64; i++)
1177 {
1178 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1180 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
1181 {
1182 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1183
1184 return 1; /* return error */
1185 }
1186 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1188 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
1189 {
1190 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1191
1192 return 1; /* return error */
1193 }
1194 for (j = 0; j < 8; j++)
1195 {
1196 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1197 0x00, ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1198 {
1199 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1200
1201 return 1; /* return error */
1202 }
1203 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1204 0x00, ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1205 {
1206 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1207
1208 return 1; /* return error */
1209 }
1210 }
1211 }
1212 }
1213
1214 return 0; /* success return 0 */
1215}
1216
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 (handle->basic_extended != 0) /* check command type */
1239 {
1240 handle->debug_print("st7920: this command must be run in basic command mode.\n"); /* this command must be run in basic command mode */
1241
1242 return 4; /* return error */
1243 }
1244
1245 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1246 ST7920_CMD_BASIC_RETURN_HOME, ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1247 {
1248 handle->debug_print("st7920: return home failed.\n"); /* return home failed */
1249
1250 return 1; /* return error */
1251 }
1252
1253 return 0; /* success return 0 */
1254}
1255
1270{
1271 if (handle == NULL) /* check handle */
1272 {
1273 return 2; /* return error */
1274 }
1275 if (handle->inited != 1) /* check handle initialization */
1276 {
1277 return 3; /* return error */
1278 }
1279 if (handle->basic_extended != 0) /* check command type */
1280 {
1281 handle->debug_print("st7920: this command must be run in basic command mode.\n"); /* this command must be run in basic command mode */
1282
1283 return 4; /* return error */
1284 }
1285
1286 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1288 (mode << 1) | (shift << 0)),
1289 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1290 {
1291 handle->debug_print("st7920: set entry mode failed.\n"); /* set entry mode failed */
1292
1293 return 1; /* return error */
1294 }
1295
1296 return 0; /* success return 0 */
1297}
1298
1313uint8_t st7920_set_display_control(st7920_handle_t *handle, st7920_bool_t display_on, st7920_bool_t cursor_on, st7920_bool_t character_blink_on)
1314{
1315 if (handle == NULL) /* check handle */
1316 {
1317 return 2; /* return error */
1318 }
1319 if (handle->inited != 1) /* check handle initialization */
1320 {
1321 return 3; /* return error */
1322 }
1323 if (handle->basic_extended != 0) /* check command type */
1324 {
1325 handle->debug_print("st7920: this command must be run in basic command mode.\n"); /* this command must be run in basic command mode */
1326
1327 return 4; /* return error */
1328 }
1329
1330 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1331 (uint8_t)(ST7920_CMD_BASIC_DISPLAY_CONTROL | (display_on << 2) |
1332 (cursor_on << 1) | (character_blink_on << 0)),
1333 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1334 {
1335 handle->debug_print("st7920: set display control failed.\n"); /* set display control failed */
1336
1337 return 1; /* return error */
1338 }
1339
1340 return 0; /* success return 0 */
1341}
1342
1356{
1357 if (handle == NULL) /* check handle */
1358 {
1359 return 2; /* return error */
1360 }
1361 if (handle->inited != 1) /* check handle initialization */
1362 {
1363 return 3; /* return error */
1364 }
1365 if (handle->basic_extended != 0) /* check command type */
1366 {
1367 handle->debug_print("st7920: this command must be run in basic command mode.\n"); /* this command must be run in basic command mode */
1368
1369 return 4; /* return error */
1370 }
1371
1372 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1374 ((mode & 0x3) << 2)),
1375 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1376 {
1377 handle->debug_print("st7920: set display shift mode failed.\n"); /* set display shift mode failed */
1378
1379 return 1; /* return error */
1380 }
1381
1382 return 0; /* success return 0 */
1383}
1384
1398{
1399 if (handle == NULL) /* check handle */
1400 {
1401 return 2; /* return error */
1402 }
1403 if (handle->inited != 1) /* check handle initialization */
1404 {
1405 return 3; /* return error */
1406 }
1407
1408 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1409 (uint8_t)(ST7920_CMD_BASIC_FUNCTION_SET | bus_bit << 4 | mode << 2),
1410 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1411 {
1412 handle->debug_print("st7920: set function failed.\n"); /* set function failed */
1413
1414 return 1; /* return error */
1415 }
1416 handle->basic_extended = mode; /* set mode */
1417
1418 return 0; /* success return 0 */
1419}
1420
1434uint8_t st7920_set_cgram_address(st7920_handle_t *handle, uint8_t addr)
1435{
1436 if (handle == NULL) /* check handle */
1437 {
1438 return 2; /* return error */
1439 }
1440 if (handle->inited != 1) /* check handle initialization */
1441 {
1442 return 3; /* return error */
1443 }
1444 if (handle->basic_extended != 0) /* check command type */
1445 {
1446 handle->debug_print("st7920: this command must be run in basic command mode.\n"); /* this command must be run in basic command mode */
1447
1448 return 4; /* return error */
1449 }
1450 if (addr > 0x3F) /* check addr */
1451 {
1452 handle->debug_print("st7920: addr is over 0x3F.\n"); /* addr is over 0x3F */
1453
1454 return 5; /* return error */
1455 }
1456
1457 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1458 (uint8_t)(ST7920_CMD_BASIC_SET_CGRAM | (addr & 0x3F)),
1459 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1460 {
1461 handle->debug_print("st7920: set cgram address failed.\n"); /* set cgram address failed */
1462
1463 return 1; /* return error */
1464 }
1465
1466 return 0; /* success return 0 */
1467}
1468
1482uint8_t st7920_set_ddram_address(st7920_handle_t *handle, uint8_t addr)
1483{
1484 if (handle == NULL) /* check handle */
1485 {
1486 return 2; /* return error */
1487 }
1488 if (handle->inited != 1) /* check handle initialization */
1489 {
1490 return 3; /* return error */
1491 }
1492 if (handle->basic_extended != 0) /* check command type */
1493 {
1494 handle->debug_print("st7920: this command must be run in basic command mode.\n"); /* this command must be run in basic command mode */
1495
1496 return 4; /* return error */
1497 }
1498 if (addr > 0x7F) /* check addr */
1499 {
1500 handle->debug_print("st7920: addr is over 0x7F.\n"); /* addr is over 0x7F */
1501
1502 return 5; /* return error */
1503 }
1504
1505 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1506 (uint8_t)(ST7920_CMD_BASIC_SET_DDRAM | (addr & 0x7F)),
1507 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1508 {
1509 handle->debug_print("st7920: set ddram address failed.\n"); /* set ddram address failed */
1510
1511 return 1; /* return error */
1512 }
1513
1514 return 0; /* success return 0 */
1515}
1516
1530uint8_t st7920_write_ram(st7920_handle_t *handle, uint8_t *data, uint8_t len)
1531{
1532 uint8_t i;
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 (handle->basic_extended != 0) /* check command type */
1543 {
1544 handle->debug_print("st7920: this command must be run in basic command mode.\n"); /* this command must be run in basic command mode */
1545
1546 return 4; /* return error */
1547 }
1548
1549 for (i = 0; i < len; i++) /* write length */
1550 {
1551 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1552 data[i], ST7920_COMMAND_DATA_DELAY) != 0) /* write data */
1553 {
1554 handle->debug_print("st7920: write ram failed.\n"); /* set ddram address failed */
1555
1556 return 1; /* write ram failed */
1557 }
1558 }
1559
1560 return 0; /* success return 0 */
1561}
1562
1575{
1576 if (handle == NULL) /* check handle */
1577 {
1578 return 2; /* return error */
1579 }
1580 if (handle->inited != 1) /* check handle initialization */
1581 {
1582 return 3; /* return error */
1583 }
1584 if (handle->basic_extended != 1) /* check command type */
1585 {
1586 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
1587
1588 return 4; /* return error */
1589 }
1590
1591 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1592 ST7920_CMD_EXT_STANFBY, ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1593 {
1594 handle->debug_print("st7920: set standby failed.\n"); /* set standby failed */
1595
1596 return 1; /* return error */
1597 }
1598
1599 return 0; /* success return 0 */
1600}
1601
1615{
1616 if (handle == NULL) /* check handle */
1617 {
1618 return 2; /* return error */
1619 }
1620 if (handle->inited != 1) /* check handle initialization */
1621 {
1622 return 3; /* return error */
1623 }
1624 if (handle->basic_extended != 1) /* check command type */
1625 {
1626 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
1627
1628 return 4; /* return error */
1629 }
1630
1631 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1633 (enable & 0x01) << 0),
1634 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1635 {
1636 handle->debug_print("st7920: set vertical scroll failed.\n"); /* set vertical scroll failed */
1637
1638 return 1; /* return error */
1639 }
1640
1641 return 0; /* success return 0 */
1642}
1643
1657{
1658 if (handle == NULL) /* check handle */
1659 {
1660 return 2; /* return error */
1661 }
1662 if (handle->inited != 1) /* check handle initialization */
1663 {
1664 return 3; /* return error */
1665 }
1666 if (handle->basic_extended != 1) /* check command type */
1667 {
1668 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
1669
1670 return 4; /* return error */
1671 }
1672
1673 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1674 (uint8_t)(ST7920_CMD_EXT_REVERSE | (l & 0x03) << 0),
1675 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1676 {
1677 handle->debug_print("st7920: set reverse line failed.\n"); /* set reverse line failed */
1678
1679 return 1; /* return error */
1680 }
1681
1682 return 0; /* success return 0 */
1683}
1684
1699 st7920_command_mode_t mode, st7920_bool_t graphic_display_enable)
1700{
1701 if (handle == NULL) /* check handle */
1702 {
1703 return 2; /* return error */
1704 }
1705 if (handle->inited != 1) /* check handle initialization */
1706 {
1707 return 3; /* return error */
1708 }
1709
1710 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1711 (uint8_t)(ST7920_CMD_EXT_FUNCTION_SET | bus_bit << 4 | mode << 2 |
1712 graphic_display_enable << 1),
1713 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1714 {
1715 handle->debug_print("st7920: set extended function failed.\n"); /* set extended function failed */
1716
1717 return 1; /* return error */
1718 }
1719 handle->basic_extended = mode; /* set mode */
1720
1721 return 0; /* success return 0 */
1722}
1723
1737uint8_t st7920_set_scroll_address(st7920_handle_t *handle, uint8_t addr)
1738{
1739 if (handle == NULL) /* check handle */
1740 {
1741 return 2; /* return error */
1742 }
1743 if (handle->inited != 1) /* check handle initialization */
1744 {
1745 return 3; /* return error */
1746 }
1747 if (handle->basic_extended != 1) /* check command type */
1748 {
1749 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
1750
1751 return 4; /* return error */
1752 }
1753 if (addr > 0x3F) /* check addr */
1754 {
1755 handle->debug_print("st7920: addr is over 0x3F.\n"); /* addr is over 0x3F */
1756
1757 return 5; /* return error */
1758 }
1759
1760 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1761 (uint8_t)(ST7920_CMD_EXT_SET_SCROLL_ADDRESS | (addr & 0x3F)),
1762 ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1763 {
1764 handle->debug_print("st7920: set scroll address failed.\n"); /* set scroll address failed */
1765
1766 return 1; /* return error */
1767 }
1768 handle->scroll_address = addr; /* set addr */
1769
1770 return 0; /* success return 0 */
1771}
1772
1789uint8_t st7920_set_graphic_address(st7920_handle_t *handle, uint8_t vertical_addr, uint8_t horizontal_addr)
1790{
1791 if (handle == NULL) /* check handle */
1792 {
1793 return 2; /* return error */
1794 }
1795 if (handle->inited != 1) /* check handle initialization */
1796 {
1797 return 3; /* return error */
1798 }
1799 if (handle->basic_extended != 1) /* check command type */
1800 {
1801 handle->debug_print("st7920: this command must be run in extended command mode.\n"); /* this command must be run in extended command mode */
1802
1803 return 4; /* return error */
1804 }
1805 if (vertical_addr > 0x3F) /* check vertical_addr */
1806 {
1807 handle->debug_print("st7920: vertical_addr is over 0x3F.\n"); /* vertical_addr is over 0x3F */
1808
1809 return 5; /* return error */
1810 }
1811 if (horizontal_addr > 0xF) /* check horizontal_addr */
1812 {
1813 handle->debug_print("st7920: horizontal_addr is over 0xF.\n"); /* horizontal_addr is over 0xF */
1814
1815 return 6; /* return error */
1816 }
1817
1818 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1820 vertical_addr),
1821 ST7920_COMMAND_CMD_DELAY) != 0) /* set vertical addr */
1822 {
1823 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1824
1825 return 1; /* return error */
1826 }
1827 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1829 horizontal_addr),
1830 ST7920_COMMAND_CMD_DELAY) != 0) /* set horizontal addr */
1831 {
1832 handle->debug_print("st7920: set graphic address failed.\n"); /* set graphic address failed */
1833
1834 return 1; /* return error */
1835 }
1836
1837 return 0; /* success return 0 */
1838}
1839
1851uint8_t st7920_write_cmd(st7920_handle_t *handle, uint8_t cmd)
1852{
1853 if (handle == NULL) /* check handle */
1854 {
1855 return 2; /* return error */
1856 }
1857 if (handle->inited != 1) /* check handle initialization */
1858 {
1859 return 3; /* return error */
1860 }
1861
1862 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_CMD,
1863 cmd, ST7920_COMMAND_CMD_DELAY) != 0) /* write command */
1864 {
1865 handle->debug_print("st7920: write cmd failed.\n"); /* write cmd failed */
1866
1867 return 1; /* return error */
1868 }
1869
1870 return 0; /* success return 0 */
1871}
1872
1884uint8_t st7920_write_data(st7920_handle_t *handle, uint8_t data)
1885{
1886 if (handle == NULL) /* check handle */
1887 {
1888 return 2; /* return error */
1889 }
1890 if (handle->inited != 1) /* check handle initialization */
1891 {
1892 return 3; /* return error */
1893 }
1894
1895 if (a_st7920_write_byte(handle, ST7920_WRITE, ST7920_DATA,
1896 data, ST7920_COMMAND_DATA_DELAY) != 0) /* write command */
1897 {
1898 handle->debug_print("st7920: write data failed.\n"); /* write data failed */
1899
1900 return 1; /* return error */
1901 }
1902
1903 return 0; /* success return 0 */
1904}
1905
1915{
1916 if (info == NULL) /* check handle */
1917 {
1918 return 2; /* return error */
1919 }
1920
1921 memset(info, 0, sizeof(st7920_info_t)); /* initialize st7920 info structure */
1922 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1923 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1924 strncpy(info->interface, "GPIO", 8); /* copy interface name */
1925 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1926 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1927 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1928 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1929 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1930 info->driver_version = DRIVER_VERSION; /* set driver version */
1931
1932 return 0; /* success return 0 */
1933}
#define MAX_CURRENT
#define ST7920_CMD_EXT_SET_SCROLL_ADDRESS
#define ST7920_CMD_BASIC_SET_DDRAM
#define SUPPLY_VOLTAGE_MAX
#define ST7920_DATA
#define ST7920_WRITE
command type definition
#define ST7920_CMD_BASIC_DISPLAY_CLEAR
command definition
#define ST7920_CMD_BASIC_SET_CGRAM
#define ST7920_CMD
data type definition
#define TEMPERATURE_MAX
#define ST7920_CMD_EXT_SCROLL_RAM_ADDRESS_SELECT
#define ST7920_CMD_EXT_SET_GRAPHIC_DISPLAY_RAM_ADDRESS
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define ST7920_CMD_BASIC_DISPLAY_CONTROL
#define ST7920_CMD_EXT_REVERSE
#define CHIP_NAME
chip information definition
#define ST7920_CMD_BASIC_FUNCTION_SET
#define DRIVER_VERSION
#define ST7920_CMD_EXT_FUNCTION_SET
#define ST7920_CMD_BASIC_CURSOR_DISPLAY_CONTROL
#define ST7920_CMD_EXT_STANFBY
#define ST7920_CMD_BASIC_ENTRY_MODE_SET
#define ST7920_CMD_BASIC_RETURN_HOME
driver st7920 header file
uint8_t st7920_set_cgram_address(st7920_handle_t *handle, uint8_t addr)
set the cgram address
uint8_t st7920_info(st7920_info_t *info)
get chip's information
struct st7920_info_s st7920_info_t
st7920 information structure definition
uint8_t st7920_set_function(st7920_handle_t *handle, st7920_interface_bus_bit_t bus_bit, st7920_command_mode_t mode)
set the function
st7920_bool_t
st7920 bool enumeration definition
uint8_t st7920_write_ram(st7920_handle_t *handle, uint8_t *data, uint8_t len)
write the ram
uint8_t st7920_display_clear(st7920_handle_t *handle)
clear the display
uint8_t st7920_set_entry_mode(st7920_handle_t *handle, st7920_display_shift_t shift, st7920_address_counter_mode_t mode)
set the entry mode
st7920_display_shift_t
st7920 display shift enumeration definition
#define ST7920_COMMAND_DATA_DELAY
st7920 command data delay definition
struct st7920_handle_s st7920_handle_t
st7920 handle structure definition
st7920_command_mode_t
st7920 command mode enumeration definition
uint8_t st7920_return_home(st7920_handle_t *handle)
return the home
st7920_address_counter_mode_t
st7920 address counter mode enumeration definition
st7920_display_shift_mode_t
st7920 display shift mode enumeration definition
uint8_t st7920_init(st7920_handle_t *handle)
initialize the chip
uint8_t st7920_deinit(st7920_handle_t *handle)
close the chip
uint8_t st7920_set_ddram_address(st7920_handle_t *handle, uint8_t addr)
set the ddram address
st7920_interface_bus_bit_t
st7920 interface bus bit enumeration definition
uint8_t st7920_set_display_control(st7920_handle_t *handle, st7920_bool_t display_on, st7920_bool_t cursor_on, st7920_bool_t character_blink_on)
set the display control
uint8_t st7920_write_string(st7920_handle_t *handle, uint8_t x, uint8_t y, char *str)
show a string
#define ST7920_COMMAND_CMD_DELAY
st7920 command cmd delay definition
uint8_t st7920_set_display_shift_mode(st7920_handle_t *handle, st7920_display_shift_mode_t mode)
set the display shift mode
uint8_t st7920_draw_compress_picture(st7920_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t *img)
draw a compressed picture
uint8_t st7920_set_standby(st7920_handle_t *handle)
terminate to the standby mode
uint8_t st7920_write_point(st7920_handle_t *handle, uint8_t x, uint8_t y, uint8_t data)
write a point
uint8_t st7920_set_reverse_line(st7920_handle_t *handle, st7920_reverse_line_t l)
set the reverse line
uint8_t st7920_set_vertical_scroll(st7920_handle_t *handle, st7920_bool_t enable)
set the vertical scroll
uint8_t st7920_set_extended_function(st7920_handle_t *handle, st7920_interface_bus_bit_t bus_bit, st7920_command_mode_t mode, st7920_bool_t graphic_display_enable)
set the extended function
st7920_reverse_line_t
st7920 reverse line enumeration definition
uint8_t st7920_fill_rect(st7920_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t color)
fill a rectangle
uint8_t st7920_draw_picture(st7920_handle_t *handle, uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t *img)
draw a picture
uint8_t st7920_set_graphic_address(st7920_handle_t *handle, uint8_t vertical_addr, uint8_t horizontal_addr)
set the graphic address
uint8_t st7920_read_point(st7920_handle_t *handle, uint8_t x, uint8_t y, uint8_t *data)
read a point
uint8_t st7920_set_scroll_address(st7920_handle_t *handle, uint8_t addr)
set the scroll address
uint8_t st7920_write_data(st7920_handle_t *handle, uint8_t data)
write data
uint8_t st7920_write_cmd(st7920_handle_t *handle, uint8_t cmd)
write command
uint8_t(* sid_gpio_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* sclk_gpio_write)(uint8_t value)
uint8_t(* sclk_gpio_deinit)(void)
uint8_t(* cs_gpio_deinit)(void)
uint16_t gram[8][64]
void(* debug_print)(const char *const fmt,...)
uint8_t(* sid_gpio_write)(uint8_t value)
void(* delay_us)(uint32_t us)
uint8_t(* cs_gpio_init)(void)
uint8_t(* sclk_gpio_init)(void)
uint8_t(* cs_gpio_write)(uint8_t value)
uint8_t(* sid_gpio_deinit)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]