u8g_dev_ssd1309_128x64.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. u8g_dev_ssd1309_128x64.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2012, 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. */
  27. #include "u8g.h"
  28. #define WIDTH 128
  29. #define HEIGHT 64
  30. #define PAGE_HEIGHT 8
  31. /* ssd1309 ini sequence*/
  32. static const uint8_t u8g_dev_ssd1309_128x64_init_seq[] PROGMEM={
  33. U8G_ESC_CS(0), /* disable chip */
  34. U8G_ESC_ADR(0), /* instruction mode */
  35. U8G_ESC_RST(1), /* do reset low pulse with (1*16)+2 milliseconds */
  36. U8G_ESC_CS(1), /* enable chip */
  37. 0xfd,0x12, /*Command Lock */
  38. 0xae, /*Set Display Off */
  39. 0xd5,0xa0, /*set Display Clock Divide Ratio/Oscillator Frequency */
  40. 0xa8,0x3f, /*Set Multiplex Ratio */
  41. 0x3d,0x00, /*Set Display Offset*/
  42. 0x40, /*Set Display Start Line*/
  43. 0xa1, /*Set Segment Re-Map*/
  44. 0xc8, /*Set COM Output Scan Direction*/
  45. 0xda,0x12, /*Set COM Pins Hardware Configuration*/
  46. 0x81,0xdf, /*Set Current Control */
  47. 0xd9,0x82, /*Set Pre-Charge Period */
  48. 0xdb,0x34, /*Set VCOMH Deselect Level */
  49. 0xa4, /*Set Entire Display On/Off */
  50. 0xa6, /*Set Normal/Inverse Display*/
  51. U8G_ESC_VCC(1), /*Power up VCC & Stabilized */
  52. U8G_ESC_DLY(50),
  53. 0xaf, /*Set Display On */
  54. U8G_ESC_DLY(50),
  55. U8G_ESC_CS(0), /* disable chip */
  56. U8G_ESC_END /* end of sequence */
  57. };
  58. /* select one init sequence here */
  59. #define u8g_dev_ssd1309_128x64_init_seq u8g_dev_ssd1309_128x64_init_seq
  60. static const uint8_t u8g_dev_ssd1309_128x64_data_start[] PROGMEM = {
  61. U8G_ESC_ADR(0), /* instruction mode */
  62. U8G_ESC_CS(1), /* enable chip */
  63. 0x010, /* set upper 4 bit of the col adr to 0 */
  64. 0x000, /* set lower 4 bit of the col adr to 4 */
  65. U8G_ESC_END /* end of sequence */
  66. };
  67. static const uint8_t u8g_dev_ssd13xx_sleep_on[] PROGMEM = {
  68. U8G_ESC_ADR(0), /* instruction mode */
  69. U8G_ESC_CS(1), /* enable chip */
  70. 0x0ae, /* display off */
  71. U8G_ESC_CS(0), /* disable chip */
  72. U8G_ESC_END /* end of sequence */
  73. };
  74. static const uint8_t u8g_dev_ssd13xx_sleep_off[] PROGMEM = {
  75. U8G_ESC_ADR(0), /* instruction mode */
  76. U8G_ESC_CS(1), /* enable chip */
  77. 0x0af, /* display on */
  78. U8G_ESC_DLY(50), /* delay 50 ms */
  79. U8G_ESC_CS(0), /* disable chip */
  80. U8G_ESC_END /* end of sequence */
  81. };
  82. uint8_t u8g_dev_ssd1309_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  83. {
  84. switch(msg)
  85. {
  86. case U8G_DEV_MSG_INIT:
  87. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_300NS);
  88. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1309_128x64_init_seq);
  89. break;
  90. case U8G_DEV_MSG_STOP:
  91. break;
  92. case U8G_DEV_MSG_PAGE_NEXT:
  93. {
  94. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  95. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1309_128x64_data_start);
  96. u8g_WriteByte(u8g, dev, 0x0b0 | pb->p.page); /* select current page (SSD1306) */
  97. u8g_SetAddress(u8g, dev, 1); /* data mode */
  98. if ( u8g_pb_WriteBuffer(pb, u8g, dev) == 0 )
  99. return 0;
  100. u8g_SetChipSelect(u8g, dev, 0);
  101. }
  102. break;
  103. case U8G_DEV_MSG_CONTRAST:
  104. u8g_SetChipSelect(u8g, dev, 1);
  105. u8g_SetAddress(u8g, dev, 0); /* instruction mode */
  106. u8g_WriteByte(u8g, dev, 0x081);
  107. u8g_WriteByte(u8g, dev, (*(uint8_t *)arg) >> 2);
  108. u8g_SetChipSelect(u8g, dev, 0);
  109. return 1;
  110. case U8G_DEV_MSG_SLEEP_ON:
  111. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd13xx_sleep_on);
  112. return 1;
  113. case U8G_DEV_MSG_SLEEP_OFF:
  114. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd13xx_sleep_off);
  115. return 1;
  116. }
  117. return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  118. }
  119. U8G_PB_DEV(u8g_dev_ssd1309_128x64_hw_spi, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_ssd1309_128x64_fn, U8G_COM_HW_SPI);
  120. U8G_PB_DEV(u8g_dev_ssd1309_128x64_sw_spi, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_ssd1309_128x64_fn, U8G_COM_SW_SPI);
  121. U8G_PB_DEV(u8g_dev_ssd1309_128x64_i2c, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_ssd1309_128x64_fn, U8G_COM_SSD_I2C);