epd2in9_V2.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # *****************************************************************************
  2. # * | File : epd2in9_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2020-10-20
  9. # # | Info : python 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 logging
  30. from . import epdconfig
  31. # Display resolution
  32. EPD_WIDTH = 128
  33. EPD_HEIGHT = 296
  34. class EPD:
  35. def __init__(self):
  36. self.reset_pin = epdconfig.RST_PIN
  37. self.dc_pin = epdconfig.DC_PIN
  38. self.busy_pin = epdconfig.BUSY_PIN
  39. self.cs_pin = epdconfig.CS_PIN
  40. self.width = EPD_WIDTH
  41. self.height = EPD_HEIGHT
  42. # Hardware reset
  43. def reset(self):
  44. epdconfig.digital_write(self.reset_pin, 1)
  45. epdconfig.delay_ms(200)
  46. epdconfig.digital_write(self.reset_pin, 0)
  47. epdconfig.delay_ms(5)
  48. epdconfig.digital_write(self.reset_pin, 1)
  49. epdconfig.delay_ms(200)
  50. def send_command(self, command):
  51. epdconfig.digital_write(self.dc_pin, 0)
  52. epdconfig.digital_write(self.cs_pin, 0)
  53. epdconfig.spi_writebyte([command])
  54. epdconfig.digital_write(self.cs_pin, 1)
  55. def send_data(self, data):
  56. epdconfig.digital_write(self.dc_pin, 1)
  57. epdconfig.digital_write(self.cs_pin, 0)
  58. epdconfig.spi_writebyte([data])
  59. epdconfig.digital_write(self.cs_pin, 1)
  60. def ReadBusy(self):
  61. logging.debug("e-Paper busy")
  62. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  63. epdconfig.delay_ms(200)
  64. logging.debug("e-Paper busy release")
  65. def TurnOnDisplay(self):
  66. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  67. self.send_data(0xF7)
  68. self.send_command(0x20) # MASTER_ACTIVATION
  69. self.ReadBusy()
  70. def TurnOnDisplay_Partial(self):
  71. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  72. self.send_data(0xFF)
  73. self.send_command(0x20) # MASTER_ACTIVATION
  74. self.ReadBusy()
  75. def SetWindow(self, x_start, y_start, x_end, y_end):
  76. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  77. # x point must be the multiple of 8 or the last 3 bits will be ignored
  78. self.send_data((x_start>>3) & 0xFF)
  79. self.send_data((x_end>>3) & 0xFF)
  80. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  81. self.send_data(y_start & 0xFF)
  82. self.send_data((y_start >> 8) & 0xFF)
  83. self.send_data(y_end & 0xFF)
  84. self.send_data((y_end >> 8) & 0xFF)
  85. def SetCursor(self, x, y):
  86. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  87. # x point must be the multiple of 8 or the last 3 bits will be ignored
  88. self.send_data(x & 0xFF)
  89. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  90. self.send_data(y & 0xFF)
  91. self.send_data((y >> 8) & 0xFF)
  92. self.ReadBusy()
  93. def init(self):
  94. if (epdconfig.module_init() != 0):
  95. return -1
  96. # EPD hardware init start
  97. self.reset()
  98. self.ReadBusy();
  99. self.send_command(0x12); #SWRESET
  100. self.ReadBusy();
  101. self.send_command(0x01); #Driver output control
  102. self.send_data(0x27);
  103. self.send_data(0x01);
  104. self.send_data(0x00);
  105. self.send_command(0x11); #data entry mode
  106. self.send_data(0x03);
  107. self.SetWindow(0, 0, self.width-1, self.height-1);
  108. self.send_command(0x3C); #BorderWavefrom
  109. self.send_data(0x05);
  110. self.send_command(0x21); # Display update control
  111. self.send_data(0x00);
  112. self.send_data(0x80);
  113. self.send_command(0x18); #Read built-in temperature sensor
  114. self.send_data(0x80);
  115. self.SetCursor(0, 0);
  116. self.ReadBusy();
  117. # EPD hardware init end
  118. return 0
  119. def getbuffer(self, image):
  120. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  121. buf = [0xFF] * (int(self.width/8) * self.height)
  122. image_monocolor = image.convert('1')
  123. imwidth, imheight = image_monocolor.size
  124. pixels = image_monocolor.load()
  125. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  126. if(imwidth == self.width and imheight == self.height):
  127. logging.debug("Vertical")
  128. for y in range(imheight):
  129. for x in range(imwidth):
  130. # Set the bits for the column of pixels at the current position.
  131. if pixels[x, y] == 0:
  132. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  133. elif(imwidth == self.height and imheight == self.width):
  134. logging.debug("Horizontal")
  135. for y in range(imheight):
  136. for x in range(imwidth):
  137. newx = y
  138. newy = self.height - x - 1
  139. if pixels[x, y] == 0:
  140. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  141. return buf
  142. def display(self, image):
  143. if (image == None):
  144. return
  145. self.send_command(0x24) # WRITE_RAM
  146. for j in range(0, self.height):
  147. for i in range(0, int(self.width / 8)):
  148. self.send_data(image[i + j * int(self.width / 8)])
  149. self.TurnOnDisplay()
  150. def display_Base(self, image):
  151. if (image == None):
  152. return
  153. self.send_command(0x24) # WRITE_RAM
  154. for j in range(0, self.height):
  155. for i in range(0, int(self.width / 8)):
  156. self.send_data(image[i + j * int(self.width / 8)])
  157. self.send_command(0x26) # WRITE_RAM
  158. for j in range(0, self.height):
  159. for i in range(0, int(self.width / 8)):
  160. self.send_data(image[i + j * int(self.width / 8)])
  161. self.TurnOnDisplay()
  162. def display_Partial(self, image):
  163. if (image == None):
  164. return
  165. epdconfig.digital_write(self.reset_pin, 0)
  166. epdconfig.delay_ms(5)
  167. epdconfig.digital_write(self.reset_pin, 1)
  168. epdconfig.delay_ms(10)
  169. self.send_command(0x3C); #BorderWavefrom
  170. self.send_data(0x80);
  171. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  172. self.SetCursor(0, 0)
  173. self.send_command(0x24) # WRITE_RAM
  174. for j in range(0, self.height):
  175. for i in range(0, int(self.width / 8)):
  176. self.send_data(image[i + j * int(self.width / 8)])
  177. self.TurnOnDisplay_Partial()
  178. def Clear(self, color):
  179. self.send_command(0x24) # WRITE_RAM
  180. for j in range(0, self.height):
  181. for i in range(0, int(self.width / 8)):
  182. self.send_data(color)
  183. self.TurnOnDisplay()
  184. def sleep(self):
  185. self.send_command(0x10) # DEEP_SLEEP_MODE
  186. self.send_data(0x01)
  187. def Dev_exit(self):
  188. epdconfig.module_exit()
  189. ### END OF FILE ###