ultralcd_st7920_u8glib_rrd.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef ULCDST7920_H
  2. #define ULCDST7920_H
  3. #include "Marlin.h"
  4. #ifdef U8GLIB_ST7920
  5. //set optimization so ARDUINO optimizes this file
  6. #pragma GCC optimize (3)
  7. #define ST7920_CLK_PIN LCD_PINS_D4
  8. #define ST7920_DAT_PIN LCD_PINS_ENABLE
  9. #define ST7920_CS_PIN LCD_PINS_RS
  10. //#define PAGE_HEIGHT 8 //128 byte framebuffer
  11. //#define PAGE_HEIGHT 16 //256 byte framebuffer
  12. #define PAGE_HEIGHT 32 //512 byte framebuffer
  13. #define WIDTH 128
  14. #define HEIGHT 64
  15. #include <U8glib.h>
  16. static void ST7920_SWSPI_SND_8BIT(uint8_t val)
  17. {
  18. uint8_t i;
  19. for( i=0; i<8; i++ )
  20. {
  21. WRITE(ST7920_CLK_PIN,0);
  22. WRITE(ST7920_DAT_PIN,val&0x80);
  23. val<<=1;
  24. WRITE(ST7920_CLK_PIN,1);
  25. }
  26. }
  27. #define ST7920_CS() {WRITE(ST7920_CS_PIN,1);u8g_10MicroDelay();}
  28. #define ST7920_NCS() {WRITE(ST7920_CS_PIN,0);}
  29. #define ST7920_SET_CMD() {ST7920_SWSPI_SND_8BIT(0xf8);u8g_10MicroDelay();}
  30. #define ST7920_SET_DAT() {ST7920_SWSPI_SND_8BIT(0xfa);u8g_10MicroDelay();}
  31. #define ST7920_WRITE_BYTE(a) {ST7920_SWSPI_SND_8BIT((a)&0xf0);ST7920_SWSPI_SND_8BIT((a)<<4);u8g_10MicroDelay();}
  32. #define ST7920_WRITE_BYTES(p,l) {uint8_t i;for(i=0;i<l;i++){ST7920_SWSPI_SND_8BIT(*p&0xf0);ST7920_SWSPI_SND_8BIT(*p<<4);p++;}u8g_10MicroDelay();}
  33. uint8_t u8g_dev_rrd_st7920_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  34. {
  35. uint8_t i,y;
  36. switch(msg)
  37. {
  38. case U8G_DEV_MSG_INIT:
  39. {
  40. SET_OUTPUT(ST7920_CS_PIN);
  41. WRITE(ST7920_CS_PIN,0);
  42. SET_OUTPUT(ST7920_DAT_PIN);
  43. WRITE(ST7920_DAT_PIN,0);
  44. SET_OUTPUT(ST7920_CLK_PIN);
  45. WRITE(ST7920_CLK_PIN,1);
  46. ST7920_CS();
  47. u8g_Delay(90); //initial delay for boot up
  48. ST7920_SET_CMD();
  49. ST7920_WRITE_BYTE(0x08); //display off, cursor+blink off
  50. ST7920_WRITE_BYTE(0x01); //clear CGRAM ram
  51. u8g_Delay(10); //delay for CGRAM clear
  52. ST7920_WRITE_BYTE(0x3E); //extended mode + GDRAM active
  53. for(y=0;y<HEIGHT/2;y++) //clear GDRAM
  54. {
  55. ST7920_WRITE_BYTE(0x80|y); //set y
  56. ST7920_WRITE_BYTE(0x80); //set x = 0
  57. ST7920_SET_DAT();
  58. for(i=0;i<2*WIDTH/8;i++) //2x width clears both segments
  59. ST7920_WRITE_BYTE(0);
  60. ST7920_SET_CMD();
  61. }
  62. ST7920_WRITE_BYTE(0x0C); //display on, cursor+blink off
  63. ST7920_NCS();
  64. }
  65. break;
  66. case U8G_DEV_MSG_STOP:
  67. break;
  68. case U8G_DEV_MSG_PAGE_NEXT:
  69. {
  70. uint8_t *ptr;
  71. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  72. y = pb->p.page_y0;
  73. ptr = (uint8_t*)pb->buf;
  74. ST7920_CS();
  75. for( i = 0; i < PAGE_HEIGHT; i ++ )
  76. {
  77. ST7920_SET_CMD();
  78. if ( y < 32 )
  79. {
  80. ST7920_WRITE_BYTE(0x80 | y); //y
  81. ST7920_WRITE_BYTE(0x80); //x=0
  82. }
  83. else
  84. {
  85. ST7920_WRITE_BYTE(0x80 | (y-32)); //y
  86. ST7920_WRITE_BYTE(0x80 | 8); //x=64
  87. }
  88. ST7920_SET_DAT();
  89. ST7920_WRITE_BYTES(ptr,WIDTH/8); //ptr is incremented inside of macro
  90. y++;
  91. }
  92. ST7920_NCS();
  93. }
  94. break;
  95. }
  96. #if PAGE_HEIGHT == 8
  97. return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
  98. #elif PAGE_HEIGHT == 16
  99. return u8g_dev_pb16h1_base_fn(u8g, dev, msg, arg);
  100. #else
  101. return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
  102. #endif
  103. }
  104. uint8_t u8g_dev_st7920_128x64_rrd_buf[WIDTH*(PAGE_HEIGHT/8)] U8G_NOCOMMON;
  105. u8g_pb_t u8g_dev_st7920_128x64_rrd_pb = {{PAGE_HEIGHT,HEIGHT,0,0,0},WIDTH,u8g_dev_st7920_128x64_rrd_buf};
  106. u8g_dev_t u8g_dev_st7920_128x64_rrd_sw_spi = {u8g_dev_rrd_st7920_128x64_fn,&u8g_dev_st7920_128x64_rrd_pb,&u8g_com_null_fn};
  107. class U8GLIB_ST7920_128X64_RRD : public U8GLIB
  108. {
  109. public:
  110. U8GLIB_ST7920_128X64_RRD(uint8_t dummy) : U8GLIB(&u8g_dev_st7920_128x64_rrd_sw_spi) {}
  111. };
  112. #endif //U8GLIB_ST7920
  113. #endif //ULCDST7920_H