2
0

epd2in15g.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # *****************************************************************************
  2. # * | File : epd2in15g.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2024-08-07
  9. # # | Info : python demo
  10. # -----------------------------------------------------------------------------
  11. # ******************************************************************************/
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. import logging
  31. from . import epdconfig
  32. import PIL
  33. from PIL import Image
  34. import io
  35. # Display resolution
  36. EPD_WIDTH = 160
  37. EPD_HEIGHT = 296
  38. logger = logging.getLogger(__name__)
  39. class EPD:
  40. def __init__(self):
  41. self.reset_pin = epdconfig.RST_PIN
  42. self.dc_pin = epdconfig.DC_PIN
  43. self.busy_pin = epdconfig.BUSY_PIN
  44. self.cs_pin = epdconfig.CS_PIN
  45. self.width = EPD_WIDTH
  46. self.height = EPD_HEIGHT
  47. self.BLACK = 0x000000 # 00 BGR
  48. self.WHITE = 0xffffff # 01
  49. self.YELLOW = 0x00ffff # 10
  50. self.RED = 0x0000ff # 11
  51. # Hardware reset
  52. def reset(self):
  53. epdconfig.digital_write(self.reset_pin, 1)
  54. epdconfig.delay_ms(200)
  55. epdconfig.digital_write(self.reset_pin, 0) # module reset
  56. epdconfig.delay_ms(2)
  57. epdconfig.digital_write(self.reset_pin, 1)
  58. epdconfig.delay_ms(200)
  59. def send_command(self, command):
  60. epdconfig.digital_write(self.dc_pin, 0)
  61. epdconfig.digital_write(self.cs_pin, 0)
  62. epdconfig.spi_writebyte([command])
  63. epdconfig.digital_write(self.cs_pin, 1)
  64. def send_data(self, data):
  65. epdconfig.digital_write(self.dc_pin, 1)
  66. epdconfig.digital_write(self.cs_pin, 0)
  67. epdconfig.spi_writebyte([data])
  68. epdconfig.digital_write(self.cs_pin, 1)
  69. # send a lot of data
  70. def send_data2(self, data):
  71. epdconfig.digital_write(self.dc_pin, 1)
  72. epdconfig.digital_write(self.cs_pin, 0)
  73. epdconfig.spi_writebyte2(data)
  74. epdconfig.digital_write(self.cs_pin, 1)
  75. def ReadBusy(self):
  76. logger.debug("e-Paper busy H")
  77. epdconfig.delay_ms(100)
  78. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  79. epdconfig.delay_ms(5)
  80. logger.debug("e-Paper busy release")
  81. def TurnOnDisplay(self):
  82. self.send_command(0x12) # DISPLAY_REFRESH
  83. self.send_data(0X00)
  84. self.ReadBusy()
  85. def init(self):
  86. if (epdconfig.module_init() != 0):
  87. return -1
  88. # EPD hardware init start
  89. self.reset()
  90. self.ReadBusy()
  91. self.send_command(0x4D)
  92. self.send_data(0x78)
  93. self.send_command(0x00)
  94. self.send_data(0x0F)
  95. self.send_data(0x29)
  96. self.send_command(0x01)
  97. self.send_data(0x07)
  98. self.send_data(0x00)
  99. self.send_command(0x03)
  100. self.send_data(0x10)
  101. self.send_data(0x54)
  102. self.send_data(0x44)
  103. self.send_command(0x06)
  104. self.send_data(0x0F)
  105. self.send_data(0x0A)
  106. self.send_data(0x2F)
  107. self.send_data(0x25)
  108. self.send_data(0x22)
  109. self.send_data(0x2E)
  110. self.send_data(0x21)
  111. self.send_command(0x30)
  112. self.send_data(0x02)
  113. self.send_command(0x41)
  114. self.send_data(0x00)
  115. self.send_command(0x50)
  116. self.send_data(0x37)
  117. self.send_command(0x60)
  118. self.send_data(0x02)
  119. self.send_data(0x02)
  120. self.send_command(0x61)
  121. self.send_data(int(self.width/256))
  122. self.send_data(self.width%256)
  123. self.send_data(int(self.height/256))
  124. self.send_data(self.height%256)
  125. self.send_command(0x65)
  126. self.send_data(0x00)
  127. self.send_data(0x00)
  128. self.send_data(0x00)
  129. self.send_data(0x00)
  130. self.send_command(0XE7)
  131. self.send_data(0x1C)
  132. self.send_command(0xE3)
  133. self.send_data(0x22)
  134. self.send_command(0xE0)
  135. self.send_data(0x00)
  136. self.send_command(0xB4)
  137. self.send_data(0xD0)
  138. self.send_command(0xB5)
  139. self.send_data(0x03)
  140. self.send_command(0xE9)
  141. self.send_data(0x01)
  142. self.send_command(0x04)
  143. self.ReadBusy()
  144. return 0
  145. def getbuffer(self, image):
  146. # Create a pallette with the 4 colors supported by the panel
  147. pal_image = Image.new("P", (1,1))
  148. pal_image.putpalette( (0,0,0, 255,255,255, 255,255,0, 255,0,0) + (0,0,0)*252)
  149. # Check if we need to rotate the image
  150. imwidth, imheight = image.size
  151. if(imwidth == self.width and imheight == self.height):
  152. image_temp = image
  153. elif(imwidth == self.height and imheight == self.width):
  154. image_temp = image.rotate(90, expand=True)
  155. else:
  156. logger.warning("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width, self.height))
  157. # Convert the soruce image to the 4 colors, dithering if needed
  158. image_4color = image_temp.convert("RGB").quantize(palette=pal_image)
  159. buf_4color = bytearray(image_4color.tobytes('raw'))
  160. # into a single byte to transfer to the panel
  161. if self.width % 4 == 0 :
  162. Width = self.width // 4
  163. else :
  164. Width = self.width // 4 + 1
  165. Height = self.height
  166. buf = [0x00] * int(Width * Height)
  167. idx = 0
  168. for j in range(0, Height):
  169. for i in range(0, Width):
  170. buf[i + j * Width] = (buf_4color[idx] << 6) + (buf_4color[idx+1] << 4) + (buf_4color[idx+2] << 2) + buf_4color[idx+3]
  171. idx = idx + 4
  172. return buf
  173. def display(self, image):
  174. self.send_command(0x10)
  175. self.send_data2(image)
  176. self.TurnOnDisplay()
  177. def Clear(self, color=0x55):
  178. if self.width % 4 == 0 :
  179. Width = self.width // 4
  180. else :
  181. Width = self.width // 4 + 1
  182. Height = self.height
  183. self.send_command(0x10)
  184. for j in range(0, Height):
  185. for i in range(0, Width):
  186. self.send_data(color)
  187. self.TurnOnDisplay()
  188. def sleep(self):
  189. self.send_command(0x02) # POWER_OFF
  190. self.send_data(0X00)
  191. epdconfig.delay_ms(100)
  192. self.send_command(0x07) # DEEP_SLEEP
  193. self.send_data(0XA5)
  194. epdconfig.delay_ms(2000)
  195. epdconfig.module_exit()
  196. ### END OF FILE ###