ssd1306.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # MicroPython SSD1306 OLED driver, I2C and SPI interfaces
  2. from micropython import const
  3. import framebuf
  4. # register definitions
  5. SET_CONTRAST = const(0x81)
  6. SET_ENTIRE_ON = const(0xA4)
  7. SET_NORM_INV = const(0xA6)
  8. SET_DISP = const(0xAE)
  9. SET_MEM_ADDR = const(0x20)
  10. SET_COL_ADDR = const(0x21)
  11. SET_PAGE_ADDR = const(0x22)
  12. SET_DISP_START_LINE = const(0x40)
  13. SET_SEG_REMAP = const(0xA0)
  14. SET_MUX_RATIO = const(0xA8)
  15. SET_COM_OUT_DIR = const(0xC0)
  16. SET_DISP_OFFSET = const(0xD3)
  17. SET_COM_PIN_CFG = const(0xDA)
  18. SET_DISP_CLK_DIV = const(0xD5)
  19. SET_PRECHARGE = const(0xD9)
  20. SET_VCOM_DESEL = const(0xDB)
  21. SET_CHARGE_PUMP = const(0x8D)
  22. # Subclassing FrameBuffer provides support for graphics primitives
  23. # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
  24. class SSD1306(framebuf.FrameBuffer):
  25. def __init__(self, width, height, external_vcc):
  26. self.width = width
  27. self.height = height
  28. self.external_vcc = external_vcc
  29. self.pages = self.height // 8
  30. self.buffer = bytearray(self.pages * self.width)
  31. super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
  32. self.init_display()
  33. def init_display(self):
  34. for cmd in (
  35. SET_DISP | 0x00, # off
  36. # address setting
  37. SET_MEM_ADDR,
  38. 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,
  43. self.height - 1,
  44. SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
  45. SET_DISP_OFFSET,
  46. 0x00,
  47. SET_COM_PIN_CFG,
  48. 0x02 if self.width > 2 * self.height else 0x12,
  49. # timing and driving scheme
  50. SET_DISP_CLK_DIV,
  51. 0x80,
  52. SET_PRECHARGE,
  53. 0x22 if self.external_vcc else 0xF1,
  54. SET_VCOM_DESEL,
  55. 0x30, # 0.83*Vcc
  56. # display
  57. SET_CONTRAST,
  58. 0xFF, # maximum
  59. SET_ENTIRE_ON, # output follows RAM contents
  60. SET_NORM_INV, # not inverted
  61. # charge pump
  62. SET_CHARGE_PUMP,
  63. 0x10 if self.external_vcc else 0x14,
  64. SET_DISP | 0x01,
  65. ): # on
  66. self.write_cmd(cmd)
  67. self.fill(0)
  68. self.show()
  69. def poweroff(self):
  70. self.write_cmd(SET_DISP | 0x00)
  71. def poweron(self):
  72. self.write_cmd(SET_DISP | 0x01)
  73. def contrast(self, contrast):
  74. self.write_cmd(SET_CONTRAST)
  75. self.write_cmd(contrast)
  76. def invert(self, invert):
  77. self.write_cmd(SET_NORM_INV | (invert & 1))
  78. def show(self):
  79. x0 = 0
  80. x1 = self.width - 1
  81. if self.width == 64:
  82. # displays with width of 64 pixels are shifted by 32
  83. x0 += 32
  84. x1 += 32
  85. self.write_cmd(SET_COL_ADDR)
  86. self.write_cmd(x0)
  87. self.write_cmd(x1)
  88. self.write_cmd(SET_PAGE_ADDR)
  89. self.write_cmd(0)
  90. self.write_cmd(self.pages - 1)
  91. self.write_data(self.buffer)
  92. class SSD1306_I2C(SSD1306):
  93. def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
  94. self.i2c = i2c
  95. self.addr = addr
  96. self.temp = bytearray(2)
  97. self.write_list = [b"\x40", None] # Co=0, D/C#=1
  98. super().__init__(width, height, external_vcc)
  99. def write_cmd(self, cmd):
  100. self.temp[0] = 0x80 # Co=1, D/C#=0
  101. self.temp[1] = cmd
  102. self.i2c.writeto(self.addr, self.temp)
  103. def write_data(self, buf):
  104. self.write_list[1] = buf
  105. self.i2c.writevto(self.addr, self.write_list)
  106. class SSD1306_SPI(SSD1306):
  107. def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
  108. self.rate = 10 * 1024 * 1024
  109. dc.init(dc.OUT, value=0)
  110. res.init(res.OUT, value=0)
  111. cs.init(cs.OUT, value=1)
  112. self.spi = spi
  113. self.dc = dc
  114. self.res = res
  115. self.cs = cs
  116. import time
  117. self.res(1)
  118. time.sleep_ms(1)
  119. self.res(0)
  120. time.sleep_ms(10)
  121. self.res(1)
  122. super().__init__(width, height, external_vcc)
  123. def write_cmd(self, cmd):
  124. self.spi.init(baudrate=self.rate, polarity=0, phase=0)
  125. self.cs(1)
  126. self.dc(0)
  127. self.cs(0)
  128. self.spi.write(bytearray([cmd]))
  129. self.cs(1)
  130. def write_data(self, buf):
  131. self.spi.init(baudrate=self.rate, polarity=0, phase=0)
  132. self.cs(1)
  133. self.dc(1)
  134. self.cs(0)
  135. self.spi.write(buf)
  136. self.cs(1)