epd2in36g.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # *****************************************************************************
  2. # * | File : epd2in36g.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2022-08-17
  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 = 168
  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. def ReadBusyH(self):
  70. logger.debug("e-Paper busy H")
  71. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  72. epdconfig.delay_ms(5)
  73. logger.debug("e-Paper busy H release")
  74. def ReadBusyL(self):
  75. logger.debug("e-Paper busy L")
  76. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: busy, 1: idle
  77. epdconfig.delay_ms(5)
  78. logger.debug("e-Paper busy L release")
  79. def TurnOnDisplay(self):
  80. self.send_command(0x12) # DISPLAY_REFRESH
  81. self.send_data(0x01)
  82. self.ReadBusyH()
  83. self.send_command(0x02) # POWER_OFF
  84. self.send_data(0X00)
  85. self.ReadBusyH()
  86. def init(self):
  87. if (epdconfig.module_init() != 0):
  88. return -1
  89. # EPD hardware init start
  90. self.reset()
  91. self.send_command(0x66)
  92. self.send_data(0x49)
  93. self.send_data(0x55)
  94. self.send_data(0x13)
  95. self.send_data(0x5D)
  96. self.send_command(0x66)
  97. self.send_data(0x49)
  98. self.send_data(0x55)
  99. self.send_command(0xB0)
  100. self.send_data(0x03)
  101. self.send_command(0x00)
  102. self.send_data(0x4F)
  103. self.send_data(0x69)
  104. self.send_command(0x03)
  105. self.send_data(0x00)
  106. self.send_command(0xF0)
  107. self.send_data(0xF6)
  108. self.send_data(0x0D)
  109. self.send_data(0x00)
  110. self.send_data(0x00)
  111. self.send_data(0x00)
  112. self.send_command(0x06)
  113. self.send_data(0xCF)
  114. self.send_data(0xDE)
  115. self.send_data(0x0F)
  116. self.send_command(0x41)
  117. self.send_data(0x00)
  118. self.send_command(0x50)
  119. self.send_data(0x30)
  120. self.send_command(0x60)
  121. self.send_data(0x0C)
  122. self.send_data(0x05)
  123. self.send_command(0x61)
  124. self.send_data(0xA8)
  125. self.send_data(0x01)
  126. self.send_data(0x28)
  127. self.send_command(0x84)
  128. self.send_data(0x01)
  129. return 0
  130. def getbuffer(self, image):
  131. # Create a pallette with the 4 colors supported by the panel
  132. pal_image = Image.new("P", (1,1))
  133. pal_image.putpalette( (0,0,0, 255,255,255, 255,255,0, 255,0,0) + (0,0,0)*252)
  134. # Check if we need to rotate the image
  135. imwidth, imheight = image.size
  136. if(imwidth == self.width and imheight == self.height):
  137. image_temp = image
  138. elif(imwidth == self.height and imheight == self.width):
  139. image_temp = image.rotate(90, expand=True)
  140. else:
  141. logger.warning("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width, self.height))
  142. # Convert the soruce image to the 4 colors, dithering if needed
  143. image_4color = image_temp.convert("RGB").quantize(palette=pal_image)
  144. buf_4color = bytearray(image_4color.tobytes('raw'))
  145. # into a single byte to transfer to the panel
  146. buf = [0x00] * int(self.width * self.height / 4)
  147. idx = 0
  148. for i in range(0, len(buf_4color), 4):
  149. buf[idx] = (buf_4color[i] << 6) + (buf_4color[i+1] << 4) + (buf_4color[i+2] << 2) + buf_4color[i+3]
  150. idx += 1
  151. return buf
  152. def display(self, image):
  153. if self.width % 4 == 0 :
  154. Width = self.width // 4
  155. else :
  156. Width = self.width // 4 + 1
  157. Height = self.height
  158. self.send_command(0x68)
  159. self.send_data(0x01)
  160. self.send_command(0x04)
  161. self.ReadBusyH()
  162. self.send_command(0x10)
  163. for j in range(0, Height):
  164. for i in range(0, Width):
  165. self.send_data(image[i + j * Width])
  166. self.send_command(0x68)
  167. self.send_data(0x00)
  168. self.TurnOnDisplay()
  169. def Clear(self, color=0x55):
  170. if self.width % 4 == 0 :
  171. Width = self.width // 4
  172. else :
  173. Width = self.width // 4 + 1
  174. Height = self.height
  175. self.send_command(0x68)
  176. self.send_data(0x01)
  177. self.send_command(0x04)
  178. self.ReadBusyH()
  179. self.send_command(0x10)
  180. for j in range(0, Height):
  181. for i in range(0, Width):
  182. self.send_data(color)
  183. self.send_command(0x68)
  184. self.send_data(0x00)
  185. self.TurnOnDisplay()
  186. def sleep(self):
  187. self.send_command(0x02) # POWER_OFF
  188. self.send_data(0x00)
  189. self.send_command(0x07) # DEEP_SLEEP
  190. self.send_data(0XA5)
  191. epdconfig.delay_ms(2000)
  192. epdconfig.module_exit()
  193. ### END OF FILE ###