epd2in9.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # *****************************************************************************
  2. # * | File : epd2in9.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.0
  8. # * | Date : 2019-06-20
  9. # # | Info : python3 demo
  10. # -----------------------------------------------------------------------------
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and//or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. import epdconfig
  30. # Display resolution
  31. EPD_WIDTH = 128
  32. EPD_HEIGHT = 296
  33. class EPD:
  34. def __init__(self):
  35. self.reset_pin = epdconfig.RST_PIN
  36. self.dc_pin = epdconfig.DC_PIN
  37. self.busy_pin = epdconfig.BUSY_PIN
  38. self.cs_pin = epdconfig.CS_PIN
  39. self.width = EPD_WIDTH
  40. self.height = EPD_HEIGHT
  41. lut_full_update = [
  42. 0x50, 0xAA, 0x55, 0xAA, 0x11, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0xFF, 0xFF, 0x1F, 0x00,
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  47. ]
  48. lut_partial_update = [
  49. 0x10, 0x18, 0x18, 0x08, 0x18, 0x18,
  50. 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  54. ]
  55. # Hardware reset
  56. def reset(self):
  57. epdconfig.digital_write(self.reset_pin, 1)
  58. epdconfig.delay_ms(200)
  59. epdconfig.digital_write(self.reset_pin, 0)
  60. epdconfig.delay_ms(10)
  61. epdconfig.digital_write(self.reset_pin, 1)
  62. epdconfig.delay_ms(200)
  63. def send_command(self, command):
  64. epdconfig.digital_write(self.dc_pin, 0)
  65. epdconfig.digital_write(self.cs_pin, 0)
  66. epdconfig.spi_writebyte([command])
  67. epdconfig.digital_write(self.cs_pin, 1)
  68. def send_data(self, data):
  69. epdconfig.digital_write(self.dc_pin, 1)
  70. epdconfig.digital_write(self.cs_pin, 0)
  71. epdconfig.spi_writebyte([data])
  72. epdconfig.digital_write(self.cs_pin, 1)
  73. def ReadBusy(self):
  74. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  75. epdconfig.delay_ms(200)
  76. def TurnOnDisplay(self):
  77. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  78. self.send_data(0xC4)
  79. self.send_command(0x20) # MASTER_ACTIVATION
  80. self.send_command(0xFF) # TERMINATE_FRAME_READ_WRITE
  81. print("e-Paper busy")
  82. self.ReadBusy()
  83. print("e-Paper busy release")
  84. def SetWindow(self, x_start, y_start, x_end, y_end):
  85. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  86. # x point must be the multiple of 8 or the last 3 bits will be ignored
  87. self.send_data((x_start >> 3) & 0xFF)
  88. self.send_data((x_end >> 3) & 0xFF)
  89. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  90. self.send_data(y_start & 0xFF)
  91. self.send_data((y_start >> 8) & 0xFF)
  92. self.send_data(y_end & 0xFF)
  93. self.send_data((y_end >> 8) & 0xFF)
  94. def SetCursor(self, x, y):
  95. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  96. # x point must be the multiple of 8 or the last 3 bits will be ignored
  97. self.send_data((x >> 3) & 0xFF)
  98. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  99. self.send_data(y & 0xFF)
  100. self.send_data((y >> 8) & 0xFF)
  101. self.ReadBusy()
  102. def init(self, lut):
  103. if (epdconfig.module_init() != 0):
  104. return -1
  105. # EPD hardware init start
  106. self.reset()
  107. self.send_command(0x01) # DRIVER_OUTPUT_CONTROL
  108. self.send_data((EPD_HEIGHT - 1) & 0xFF)
  109. self.send_data(((EPD_HEIGHT - 1) >> 8) & 0xFF)
  110. self.send_data(0x00) # GD = 0 SM = 0 TB = 0
  111. self.send_command(0x0C) # BOOSTER_SOFT_START_CONTROL
  112. self.send_data(0xD7)
  113. self.send_data(0xD6)
  114. self.send_data(0x9D)
  115. self.send_command(0x2C) # WRITE_VCOM_REGISTER
  116. self.send_data(0xA8) # VCOM 7C
  117. self.send_command(0x3A) # SET_DUMMY_LINE_PERIOD
  118. self.send_data(0x1A) # 4 dummy lines per gate
  119. self.send_command(0x3B) # SET_GATE_TIME
  120. self.send_data(0x08) # 2us per line
  121. self.send_command(0x11) # DATA_ENTRY_MODE_SETTING
  122. self.send_data(0x03) # X increment Y increment
  123. self.send_command(0x32) # WRITE_LUT_REGISTER
  124. for i in range(0, len(lut)):
  125. self.send_data(lut[i])
  126. # EPD hardware init end
  127. return 0
  128. def getbuffer(self, image):
  129. # print "bufsiz = ",(self.width//8) * self.height
  130. buf = [0xFF] * ((self.width//8) * self.height)
  131. image_monocolor = image.convert('1')
  132. imwidth, imheight = image_monocolor.size
  133. pixels = image_monocolor.load()
  134. # print "imwidth = %d, imheight = %d",imwidth,imheight
  135. if(imwidth == self.width and imheight == self.height):
  136. print("Vertical")
  137. for y in range(imheight):
  138. for x in range(imwidth):
  139. # Set the bits for the column of pixels at the current position.
  140. if pixels[x, y] == 0:
  141. buf[(x + y * self.width) // 8] &= ~(0x80 >> (x % 8))
  142. elif(imwidth == self.height and imheight == self.width):
  143. print("Horizontal")
  144. for y in range(imheight):
  145. for x in range(imwidth):
  146. newx = y
  147. newy = self.height - x - 1
  148. if pixels[x, y] == 0:
  149. buf[(newx + newy*self.width) // 8] &= ~(0x80 >> (y % 8))
  150. return buf
  151. def display(self, image):
  152. if (image == None):
  153. return
  154. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  155. for j in range(0, self.height):
  156. self.SetCursor(0, j)
  157. self.send_command(0x24) # WRITE_RAM
  158. for i in range(0, self.width // 8):
  159. self.send_data(image[i + j * (self.width // 8)])
  160. self.TurnOnDisplay()
  161. def Clear(self, color):
  162. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  163. for j in range(0, self.height):
  164. self.SetCursor(0, j)
  165. self.send_command(0x24) # WRITE_RAM
  166. for i in range(0, self.width // 8):
  167. self.send_data(color)
  168. self.TurnOnDisplay()
  169. def sleep(self):
  170. self.send_command(0x10) # DEEP_SLEEP_MODE
  171. self.send_data(0x01)
  172. epdconfig.module_exit()
  173. ### END OF FILE ###