u8g_dev_ssd1306_128x32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. u8g_dev_ssd1306_128x32.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2011, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. 23 Feb 2013: Fixed, Issue 147
  27. */
  28. #include "u8g.h"
  29. #define WIDTH 128
  30. #define HEIGHT 32
  31. #define PAGE_HEIGHT 8
  32. /* init sequence adafruit 128x32 OLED (NOT TESTED) */
  33. static const uint8_t u8g_dev_ssd1306_128x32_adafruit1_init_seq[] PROGMEM = {
  34. U8G_ESC_CS(0), /* disable chip */
  35. U8G_ESC_ADR(0), /* instruction mode */
  36. U8G_ESC_RST(1), /* do reset low pulse with (1*16)+2 milliseconds */
  37. U8G_ESC_CS(1), /* enable chip */
  38. 0x0ae, /* display off, sleep mode */
  39. 0x0d5, 0x080, /* clock divide ratio (0x00=1) and oscillator frequency (0x8) */
  40. 0x0a8, 0x03f, /* */
  41. 0x0d3, 0x000, /* */
  42. 0x040, /* start line */
  43. 0x08d, 0x010, /* [1] charge pump setting (p62): 0x014 enable, 0x010 disable */
  44. 0x020, 0x000, /* */
  45. 0x0a1, /* segment remap a0/a1*/
  46. 0x0c8, /* c0: scan dir normal, c8: reverse */
  47. 0x0da, 0x012, /* com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5) */
  48. 0x081, 0x09f, /* [1] set contrast control */
  49. 0x0d9, 0x022, /* [1] pre-charge period 0x022/f1*/
  50. 0x0db, 0x040, /* vcomh deselect level */
  51. 0x02e, /* 2012-05-27: Deactivate scroll */
  52. 0x0a4, /* output ram to display */
  53. 0x0a6, /* none inverted normal display mode */
  54. 0x0af, /* display on */
  55. U8G_ESC_CS(0), /* disable chip */
  56. U8G_ESC_END /* end of sequence */
  57. };
  58. /* init sequence adafruit 128x32 OLED (NOT TESTED) */
  59. static const uint8_t u8g_dev_ssd1306_128x32_adafruit2_init_seq[] PROGMEM = {
  60. U8G_ESC_CS(0), /* disable chip */
  61. U8G_ESC_ADR(0), /* instruction mode */
  62. U8G_ESC_RST(1), /* do reset low pulse with (1*16)+2 milliseconds */
  63. U8G_ESC_CS(1), /* enable chip */
  64. 0x0ae, /* display off, sleep mode */
  65. 0x0d5, 0x080, /* clock divide ratio (0x00=1) and oscillator frequency (0x8) */
  66. 0x0a8, 0x03f, /* */
  67. 0x0d3, 0x000, /* */
  68. 0x040, /* start line */
  69. 0x08d, 0x014, /* [2] charge pump setting (p62): 0x014 enable, 0x010 disable */
  70. 0x020, 0x000, /* */
  71. 0x0a1, /* segment remap a0/a1*/
  72. 0x0c8, /* c0: scan dir normal, c8: reverse */
  73. 0x0da, 0x012, /* com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5) */
  74. 0x081, 0x0cf, /* [2] set contrast control */
  75. 0x0d9, 0x0f1, /* [2] pre-charge period 0x022/f1*/
  76. 0x0db, 0x040, /* vcomh deselect level */
  77. 0x02e, /* 2012-05-27: Deactivate scroll */
  78. 0x0a4, /* output ram to display */
  79. 0x0a6, /* none inverted normal display mode */
  80. 0x0af, /* display on */
  81. U8G_ESC_CS(0), /* disable chip */
  82. U8G_ESC_END /* end of sequence */
  83. };
  84. /* init sequence adafruit 128x32 OLED (TESTED - WORKING 23.02.13), like adafruit3, but with page addressing mode */
  85. static const uint8_t u8g_dev_ssd1306_128x32_adafruit3_init_seq[] PROGMEM = {
  86. U8G_ESC_CS(0), /* disable chip */
  87. U8G_ESC_ADR(0), /* instruction mode */
  88. U8G_ESC_RST(1), /* do reset low pulse with (1*16)+2 milliseconds */
  89. U8G_ESC_CS(1), /* enable chip */
  90. 0x0ae, /* display off, sleep mode */
  91. 0x0d5, 0x080, /* clock divide ratio (0x00=1) and oscillator frequency (0x8) */
  92. 0x0a8, 0x01f, /* Feb 23, 2013: 128x32 OLED: 0x01f, 128x32 OLED 0x03f */
  93. 0x0d3, 0x000, /* */
  94. 0x040, /* start line */
  95. 0x08d, 0x014, /* [2] charge pump setting (p62): 0x014 enable, 0x010 disable */
  96. 0x020, 0x002, /* com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5), Feb 23, 2013: 128x32 OLED: 0x002, 128x32 OLED 0x012 */
  97. 0x0a1, /* segment remap a0/a1*/
  98. 0x0c8, /* c0: scan dir normal, c8: reverse */
  99. 0x0da, 0x002, /* com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5) */
  100. 0x081, 0x0cf, /* [2] set contrast control */
  101. 0x0d9, 0x0f1, /* [2] pre-charge period 0x022/f1*/
  102. 0x0db, 0x040, /* vcomh deselect level */
  103. 0x02e, /* 2012-05-27: Deactivate scroll */
  104. 0x0a4, /* output ram to display */
  105. 0x0a6, /* none inverted normal display mode */
  106. 0x0af, /* display on */
  107. U8G_ESC_CS(0), /* disable chip */
  108. U8G_ESC_END /* end of sequence */
  109. };
  110. /* init sequence Univision datasheet (NOT TESTED) */
  111. static const uint8_t u8g_dev_ssd1306_128x32_univision_init_seq[] PROGMEM = {
  112. U8G_ESC_CS(0), /* disable chip */
  113. U8G_ESC_ADR(0), /* instruction mode */
  114. U8G_ESC_RST(1), /* do reset low pulse with (1*16)+2 milliseconds */
  115. U8G_ESC_CS(1), /* enable chip */
  116. 0x0ae, /* display off, sleep mode */
  117. 0x0d5, 0x080, /* clock divide ratio (0x00=1) and oscillator frequency (0x8) */
  118. 0x0a8, 0x03f, /* multiplex ratio */
  119. 0x0d3, 0x000, /* display offset */
  120. 0x040, /* start line */
  121. 0x08d, 0x010, /* charge pump setting (p62): 0x014 enable, 0x010 disable */
  122. 0x0a1, /* segment remap a0/a1*/
  123. 0x0c8, /* c0: scan dir normal, c8: reverse */
  124. 0x0da, 0x012, /* com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5) */
  125. 0x081, 0x09f, /* set contrast control */
  126. 0x0d9, 0x022, /* pre-charge period */
  127. 0x0db, 0x040, /* vcomh deselect level */
  128. 0x022, 0x000, /* page addressing mode WRONG: 3 byte cmd! */
  129. 0x0a4, /* output ram to display */
  130. 0x0a6, /* none inverted normal display mode */
  131. 0x0af, /* display on */
  132. U8G_ESC_CS(0), /* disable chip */
  133. U8G_ESC_END /* end of sequence */
  134. };
  135. /* select one init sequence here */
  136. //define u8g_dev_ssd1306_128x32_init_seq u8g_dev_ssd1306_128x32_univision_init_seq
  137. //define u8g_dev_ssd1306_128x32_init_seq u8g_dev_ssd1306_128x32_adafruit1_init_seq
  138. //define u8g_dev_ssd1306_128x32_init_seq u8g_dev_ssd1306_128x32_adafruit2_init_seq
  139. #define u8g_dev_ssd1306_128x32_init_seq u8g_dev_ssd1306_128x32_adafruit3_init_seq
  140. static const uint8_t u8g_dev_ssd1306_128x32_data_start[] PROGMEM = {
  141. U8G_ESC_ADR(0), /* instruction mode */
  142. U8G_ESC_CS(1), /* enable chip */
  143. 0x010, /* set upper 4 bit of the col adr. to 0 */
  144. 0x000, /* set lower 4 bit of the col adr. to 4 */
  145. U8G_ESC_END /* end of sequence */
  146. };
  147. static const uint8_t u8g_dev_ssd13xx_sleep_on[] PROGMEM = {
  148. U8G_ESC_ADR(0), /* instruction mode */
  149. U8G_ESC_CS(1), /* enable chip */
  150. 0x0ae, /* display off */
  151. U8G_ESC_CS(0), /* disable chip, bugfix 12 nov 2014 */
  152. U8G_ESC_END /* end of sequence */
  153. };
  154. static const uint8_t u8g_dev_ssd13xx_sleep_off[] PROGMEM = {
  155. U8G_ESC_ADR(0), /* instruction mode */
  156. U8G_ESC_CS(1), /* enable chip */
  157. 0x0af, /* display on */
  158. U8G_ESC_DLY(50), /* delay 50 ms */
  159. U8G_ESC_CS(0), /* disable chip, bugfix 12 nov 2014 */
  160. U8G_ESC_END /* end of sequence */
  161. };
  162. uint8_t u8g_dev_ssd1306_128x32_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  163. {
  164. switch(msg)
  165. {
  166. case U8G_DEV_MSG_INIT:
  167. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_300NS);
  168. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1306_128x32_init_seq);
  169. break;
  170. case U8G_DEV_MSG_STOP:
  171. break;
  172. case U8G_DEV_MSG_PAGE_NEXT:
  173. {
  174. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  175. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1306_128x32_data_start);
  176. u8g_WriteByte(u8g, dev, 0x0b0 | pb->p.page); /* select current page (SSD1306) */
  177. u8g_SetAddress(u8g, dev, 1); /* data mode */
  178. if ( u8g_pb_WriteBuffer(pb, u8g, dev) == 0 )
  179. return 0;
  180. u8g_SetChipSelect(u8g, dev, 0);
  181. }
  182. break;
  183. case U8G_DEV_MSG_SLEEP_ON:
  184. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd13xx_sleep_on);
  185. return 1;
  186. case U8G_DEV_MSG_SLEEP_OFF:
  187. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd13xx_sleep_off);
  188. return 1;
  189. }
  190. return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  191. }
  192. uint8_t u8g_dev_ssd1306_128x32_2x_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  193. {
  194. switch(msg)
  195. {
  196. case U8G_DEV_MSG_INIT:
  197. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_300NS);
  198. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1306_128x32_init_seq);
  199. break;
  200. case U8G_DEV_MSG_STOP:
  201. break;
  202. case U8G_DEV_MSG_PAGE_NEXT:
  203. {
  204. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  205. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1306_128x32_data_start);
  206. u8g_WriteByte(u8g, dev, 0x0b0 | (pb->p.page*2)); /* select current page (SSD1306) */
  207. u8g_SetAddress(u8g, dev, 1); /* data mode */
  208. u8g_WriteSequence(u8g, dev, pb->width, pb->buf);
  209. u8g_SetChipSelect(u8g, dev, 0);
  210. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1306_128x32_data_start);
  211. u8g_WriteByte(u8g, dev, 0x0b0 | (pb->p.page*2+1)); /* select current page (SSD1306) */
  212. u8g_SetAddress(u8g, dev, 1); /* data mode */
  213. u8g_WriteSequence(u8g, dev, pb->width, (uint8_t *)(pb->buf)+pb->width);
  214. u8g_SetChipSelect(u8g, dev, 0);
  215. }
  216. break;
  217. case U8G_DEV_MSG_SLEEP_ON:
  218. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd13xx_sleep_on);
  219. return 1;
  220. case U8G_DEV_MSG_SLEEP_OFF:
  221. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd13xx_sleep_off);
  222. return 1;
  223. }
  224. return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
  225. }
  226. U8G_PB_DEV(u8g_dev_ssd1306_128x32_sw_spi, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_ssd1306_128x32_fn, U8G_COM_SW_SPI);
  227. U8G_PB_DEV(u8g_dev_ssd1306_128x32_hw_spi, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_ssd1306_128x32_fn, U8G_COM_HW_SPI);
  228. U8G_PB_DEV(u8g_dev_ssd1306_128x32_i2c, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_ssd1306_128x32_fn, U8G_COM_SSD_I2C);
  229. uint8_t u8g_dev_ssd1306_128x32_2x_buf[WIDTH*2] U8G_NOCOMMON ;
  230. u8g_pb_t u8g_dev_ssd1306_128x32_2x_pb = { {16, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_ssd1306_128x32_2x_buf};
  231. u8g_dev_t u8g_dev_ssd1306_128x32_2x_sw_spi = { u8g_dev_ssd1306_128x32_2x_fn, &u8g_dev_ssd1306_128x32_2x_pb, U8G_COM_SW_SPI };
  232. u8g_dev_t u8g_dev_ssd1306_128x32_2x_hw_spi = { u8g_dev_ssd1306_128x32_2x_fn, &u8g_dev_ssd1306_128x32_2x_pb, U8G_COM_HW_SPI };
  233. u8g_dev_t u8g_dev_ssd1306_128x32_2x_i2c = { u8g_dev_ssd1306_128x32_2x_fn, &u8g_dev_ssd1306_128x32_2x_pb, U8G_COM_SSD_I2C };