ssd1306_mod.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # NOTE: Modified version to align with implemented I2C API in nrf port.
  2. #
  3. # Examples usage of SSD1306_SPI on pca10040
  4. #
  5. # from machine import Pin, SPI
  6. # from ssd1306 import SSD1306_SPI
  7. # spi = SPI(0, baudrate=40000000)
  8. # dc = Pin.board.PA11
  9. # res = Pin.board.PA12
  10. # cs = Pin.board.PA13
  11. # disp = SSD1306_SPI(128, 64, spi, dc, res, cs)
  12. #
  13. #
  14. # Example usage of SSD1306_I2C on pca10040
  15. #
  16. # from machine import Pin, I2C
  17. # from ssd1306_mod import SSD1306_I2C_Mod
  18. # i2c = I2C(0, Pin.board.PA3, Pin.board.PA4)
  19. # disp = SSD1306_I2C_Mod(128, 64, i2c)
  20. from ssd1306 import SSD1306_I2C
  21. SET_COL_ADDR = const(0x21)
  22. SET_PAGE_ADDR = const(0x22)
  23. class SSD1306_I2C_Mod(SSD1306_I2C):
  24. def show(self):
  25. x0 = 0
  26. x1 = self.width - 1
  27. if self.width == 64:
  28. # displays with width of 64 pixels are shifted by 32
  29. x0 += 32
  30. x1 += 32
  31. self.write_cmd(SET_COL_ADDR)
  32. self.write_cmd(x0)
  33. self.write_cmd(x1)
  34. self.write_cmd(SET_PAGE_ADDR)
  35. self.write_cmd(0)
  36. self.write_cmd(self.pages - 1)
  37. chunk_size = 254 # 255, excluding opcode.
  38. num_of_chunks = len(self.buffer) // chunk_size
  39. leftover = len(self.buffer) - (num_of_chunks * chunk_size)
  40. for i in range(0, num_of_chunks):
  41. self.write_data(self.buffer[chunk_size*i:chunk_size*(i+1)])
  42. if (leftover > 0):
  43. self.write_data(self.buffer[chunk_size * num_of_chunks:])
  44. def write_data(self, buf):
  45. buffer = bytearray([0x40]) + buf # Co=0, D/C#=1
  46. self.i2c.writeto(self.addr, buffer)