ssd1306.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # MicroPython SSD1306 OLED driver, I2C and SPI interfaces
  2. import time
  3. import framebuf
  4. from micropython import const
  5. # register definitions
  6. SET_CONTRAST = const(0x81)
  7. SET_ENTIRE_ON = const(0xa4)
  8. SET_NORM_INV = const(0xa6)
  9. SET_DISP = const(0xae)
  10. SET_MEM_ADDR = const(0x20)
  11. SET_COL_ADDR = const(0x21)
  12. SET_PAGE_ADDR = const(0x22)
  13. SET_DISP_START_LINE = const(0x40)
  14. SET_SEG_REMAP = const(0xa0)
  15. SET_MUX_RATIO = const(0xa8)
  16. SET_COM_OUT_DIR = const(0xc0)
  17. SET_DISP_OFFSET = const(0xd3)
  18. SET_COM_PIN_CFG = const(0xda)
  19. SET_DISP_CLK_DIV = const(0xd5)
  20. SET_PRECHARGE = const(0xd9)
  21. SET_VCOM_DESEL = const(0xdb)
  22. SET_CHARGE_PUMP = const(0x8d)
  23. class SSD1306:
  24. def __init__(self, width, height, external_vcc):
  25. self.width = width
  26. self.height = height
  27. self.external_vcc = external_vcc
  28. self.pages = self.height // 8
  29. # Note the subclass must initialize self.framebuf to a framebuffer.
  30. # This is necessary because the underlying data buffer is different
  31. # between I2C and SPI implementations (I2C needs an extra byte).
  32. self.poweron()
  33. self.init_display()
  34. def init_display(self):
  35. for cmd in (
  36. SET_DISP | 0x00, # off
  37. # address setting
  38. SET_MEM_ADDR, 0x00, # horizontal
  39. # resolution and layout
  40. SET_DISP_START_LINE | 0x00,
  41. SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
  42. SET_MUX_RATIO, self.height - 1,
  43. SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
  44. SET_DISP_OFFSET, 0x00,
  45. SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
  46. # timing and driving scheme
  47. SET_DISP_CLK_DIV, 0x80,
  48. SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
  49. SET_VCOM_DESEL, 0x30, # 0.83*Vcc
  50. # display
  51. SET_CONTRAST, 0xff, # maximum
  52. SET_ENTIRE_ON, # output follows RAM contents
  53. SET_NORM_INV, # not inverted
  54. # charge pump
  55. SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
  56. SET_DISP | 0x01): # on
  57. self.write_cmd(cmd)
  58. self.fill(0)
  59. self.show()
  60. def poweroff(self):
  61. self.write_cmd(SET_DISP | 0x00)
  62. def contrast(self, contrast):
  63. self.write_cmd(SET_CONTRAST)
  64. self.write_cmd(contrast)
  65. def invert(self, invert):
  66. self.write_cmd(SET_NORM_INV | (invert & 1))
  67. def show(self):
  68. x0 = 0
  69. x1 = self.width - 1
  70. if self.width == 64:
  71. # displays with width of 64 pixels are shifted by 32
  72. x0 += 32
  73. x1 += 32
  74. self.write_cmd(SET_COL_ADDR)
  75. self.write_cmd(x0)
  76. self.write_cmd(x1)
  77. self.write_cmd(SET_PAGE_ADDR)
  78. self.write_cmd(0)
  79. self.write_cmd(self.pages - 1)
  80. self.write_framebuf()
  81. def fill(self, col):
  82. self.framebuf.fill(col)
  83. def pixel(self, x, y, col):
  84. self.framebuf.pixel(x, y, col)
  85. def scroll(self, dx, dy):
  86. self.framebuf.scroll(dx, dy)
  87. def text(self, string, x, y, col=1):
  88. self.framebuf.text(string, x, y, col)
  89. class SSD1306_I2C(SSD1306):
  90. def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
  91. self.i2c = i2c
  92. self.addr = addr
  93. self.temp = bytearray(2)
  94. # Add an extra byte to the data buffer to hold an I2C data/command byte
  95. # to use hardware-compatible I2C transactions. A memoryview of the
  96. # buffer is used to mask this byte from the framebuffer operations
  97. # (without a major memory hit as memoryview doesn't copy to a separate
  98. # buffer).
  99. self.buffer = bytearray(((height // 8) * width) + 1)
  100. self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
  101. self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
  102. super().__init__(width, height, external_vcc)
  103. def write_cmd(self, cmd):
  104. self.temp[0] = 0x80 # Co=1, D/C#=0
  105. self.temp[1] = cmd
  106. self.i2c.writeto(self.addr, self.temp)
  107. def write_framebuf(self):
  108. # Blast out the frame buffer using a single I2C transaction to support
  109. # hardware I2C interfaces.
  110. self.i2c.writeto(self.addr, self.buffer)
  111. def poweron(self):
  112. pass
  113. class SSD1306_SPI(SSD1306):
  114. def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
  115. self.rate = 10 * 1024 * 1024
  116. dc.init(dc.OUT, value=0)
  117. res.init(res.OUT, value=0)
  118. cs.init(cs.OUT, value=1)
  119. self.spi = spi
  120. self.dc = dc
  121. self.res = res
  122. self.cs = cs
  123. self.buffer = bytearray((height // 8) * width)
  124. self.framebuf = framebuf.FrameBuffer1(self.buffer, width, height)
  125. super().__init__(width, height, external_vcc)
  126. def write_cmd(self, cmd):
  127. self.spi.init(baudrate=self.rate, polarity=0, phase=0)
  128. self.cs.high()
  129. self.dc.low()
  130. self.cs.low()
  131. self.spi.write(bytearray([cmd]))
  132. self.cs.high()
  133. def write_framebuf(self):
  134. self.spi.init(baudrate=self.rate, polarity=0, phase=0)
  135. self.cs.high()
  136. self.dc.high()
  137. self.cs.low()
  138. self.spi.write(self.buffer)
  139. self.cs.high()
  140. def poweron(self):
  141. self.res.high()
  142. time.sleep_ms(1)
  143. self.res.low()
  144. time.sleep_ms(10)
  145. self.res.high()