Pico-OLED-2.42.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. from machine import Pin,SPI,I2C
  2. import framebuf
  3. import time
  4. # Pin definition
  5. SCK = 10
  6. MOSI = 11
  7. RST = 12
  8. CS = 13
  9. DC = 14
  10. Device_SPI = 1
  11. Device_I2C = 0
  12. if(Device_SPI == 1):
  13. Device = Device_SPI
  14. else :
  15. Device = Device_I2C
  16. class OLED_2inch42(framebuf.FrameBuffer):
  17. def __init__(self):
  18. self.width = 128
  19. self.height = 64
  20. self.white = 0xffff
  21. self.balck = 0x0000
  22. self.cs = Pin(CS ,Pin.OUT)
  23. self.rst = Pin(RST,Pin.OUT)
  24. self.dc = Pin(DC ,Pin.OUT)
  25. if(Device == Device_SPI):
  26. self.cs(1)
  27. self.spi = SPI(1)
  28. self.spi = SPI(1,1000_000)
  29. self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
  30. self.dc(1)
  31. else :
  32. self.dc(0)
  33. self.cs(0)
  34. self.i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=1000000)
  35. self.temp = bytearray(2)
  36. self.buffer = bytearray(self.height * self.width // 8)
  37. super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
  38. self.init_display()
  39. def write_cmd(self, cmd):
  40. if(Device == Device_SPI):
  41. self.cs(1)
  42. self.dc(0)
  43. self.cs(0)
  44. self.spi.write(bytearray([cmd]))
  45. self.cs(1)
  46. else :
  47. self.temp[0] = 0x00 # Co=1, D/C#=0
  48. self.temp[1] = cmd
  49. self.i2c.writeto(0x3c, self.temp)
  50. def write_data(self, buf):
  51. if(Device == Device_SPI):
  52. self.cs(1)
  53. self.dc(1)
  54. self.cs(0)
  55. self.spi.write(bytearray([buf]))
  56. self.cs(1)
  57. else :
  58. self.temp[0] = 0x40 # Co=1, D/C#=0
  59. self.temp[1] = buf
  60. self.i2c.writeto(0x3c, self.temp)
  61. def init_display(self):
  62. """Initialize dispaly"""
  63. self.rst(1)
  64. time.sleep(0.001)
  65. self.rst(0)
  66. time.sleep(0.01)
  67. self.rst(1)
  68. self.write_cmd(0xAE)# Turn off the display
  69. self.write_cmd(0x00)# Set low column address
  70. self.write_cmd(0x10)# Set high column address
  71. self.write_cmd(0x20)# Set memory addressing mode
  72. self.write_cmd(0x00)# Horizontal addressing mode
  73. self.write_cmd(0xC8)# Set COM scan direction
  74. self.write_cmd(0xA6)# Set normal/inverse display
  75. self.write_cmd(0xA8)# Set multiplex ratio
  76. self.write_cmd(0x3F)# Set ratio to 63
  77. self.write_cmd(0xD3)# Set display offset
  78. self.write_cmd(0x00)# Offset value is 0
  79. self.write_cmd(0xD5)# Set display clock divide ratio/oscillator frequency
  80. self.write_cmd(0x80)# Default divide ratio
  81. self.write_cmd(0xD9)# Set pre-charge period
  82. self.write_cmd(0x22)# Default value
  83. self.write_cmd(0xDA)# Set COM pin configuration
  84. self.write_cmd(0x12)# Default configuration
  85. self.write_cmd(0xDB)# Set VCOMH
  86. self.write_cmd(0x40)# Default value
  87. self.write_cmd(0xA1)# Set segment remap
  88. self.write_cmd(0xAF)# Turn on the display
  89. def show(self):
  90. for page in range(0,8):
  91. self.write_cmd(0xb0 + page)
  92. self.write_cmd(0x04)
  93. self.write_cmd(0x00)
  94. if(Device == Device_SPI):
  95. self.dc(1)
  96. for num in range(0,128):
  97. self.write_data(self.buffer[page*128+num])
  98. if __name__=='__main__':
  99. OLED = OLED_2inch42()
  100. OLED.fill(0x0000)
  101. OLED.show()
  102. OLED.rect(0,0,127,63,OLED.white)
  103. OLED.rect(10,6,20,20,OLED.white)
  104. time.sleep(0.5)
  105. OLED.show()
  106. OLED.fill_rect(40,6,20,20,OLED.white)
  107. time.sleep(0.5)
  108. OLED.show()
  109. OLED.rect(70,6,20,20,OLED.white)
  110. time.sleep(0.5)
  111. OLED.show()
  112. OLED.fill_rect(100,6,20,20,OLED.white)
  113. time.sleep(0.5)
  114. OLED.show()
  115. time.sleep(1)
  116. OLED.fill(0x0000)
  117. OLED.line(0,0,5,63,OLED.white)
  118. OLED.show()
  119. time.sleep(0.01)
  120. OLED.line(0,0,20,63,OLED.white)
  121. OLED.show()
  122. time.sleep(0.01)
  123. OLED.line(0,0,35,63,OLED.white)
  124. OLED.show()
  125. time.sleep(0.01)
  126. OLED.line(0,0,65,63,OLED.white)
  127. OLED.show()
  128. time.sleep(0.01)
  129. OLED.line(0,0,95,63,OLED.white)
  130. OLED.show()
  131. time.sleep(0.01)
  132. OLED.line(0,0,125,63,OLED.white)
  133. OLED.show()
  134. time.sleep(0.01)
  135. OLED.line(0,0,125,63,OLED.white)
  136. OLED.show()
  137. time.sleep(0.1)
  138. OLED.line(0,0,125,63,OLED.white)
  139. OLED.show()
  140. time.sleep(0.01)
  141. OLED.line(0,0,125,63,OLED.white)
  142. OLED.show()
  143. time.sleep(0.01)
  144. OLED.line(127,1,125,63,OLED.white)
  145. OLED.show()
  146. time.sleep(0.01)
  147. OLED.line(127,1,110,63,OLED.white)
  148. OLED.show()
  149. time.sleep(0.01)
  150. OLED.line(127,1,95,63,OLED.white)
  151. OLED.show()
  152. time.sleep(0.01)
  153. OLED.line(127,1,65,63,OLED.white)
  154. OLED.show()
  155. time.sleep(0.01)
  156. OLED.line(127,1,35,63,OLED.white)
  157. OLED.show()
  158. time.sleep(0.01)
  159. OLED.line(127,1,1,63,OLED.white)
  160. OLED.show()
  161. time.sleep(0.01)
  162. OLED.line(127,1,1,63,OLED.white)
  163. OLED.show()
  164. time.sleep(0.01)
  165. OLED.line(127,1,1,63,OLED.white)
  166. OLED.show()
  167. time.sleep(0.01)
  168. OLED.line(127,1,1,1,OLED.white)
  169. OLED.show()
  170. time.sleep(1)
  171. OLED.fill(0x0000)
  172. OLED.text("128 x 64 Pixels",0,2,OLED.white)
  173. OLED.text("Pico-OLED-2.42",0,12,OLED.white)
  174. OLED.text("SSD1309",0,22,OLED.white)
  175. OLED.text("Waveshare",0,32,OLED.white)
  176. OLED.show()
  177. time.sleep(1)
  178. OLED.fill(0xFFFF)