epd7in3f.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # *****************************************************************************
  2. # * | File : epd7in3f.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2022-10-20
  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 = 800
  37. EPD_HEIGHT = 480
  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 # 0000 BGR
  48. self.WHITE = 0xffffff # 0001
  49. self.GREEN = 0x00ff00 # 0010
  50. self.BLUE = 0xff0000 # 0011
  51. self.RED = 0x0000ff # 0100
  52. self.YELLOW = 0x00ffff # 0101
  53. self.ORANGE = 0x0080ff # 0110
  54. # Hardware reset
  55. def reset(self):
  56. epdconfig.digital_write(self.reset_pin, 1)
  57. epdconfig.delay_ms(20)
  58. epdconfig.digital_write(self.reset_pin, 0) # module reset
  59. epdconfig.delay_ms(2)
  60. epdconfig.digital_write(self.reset_pin, 1)
  61. epdconfig.delay_ms(20)
  62. def send_command(self, command):
  63. epdconfig.digital_write(self.dc_pin, 0)
  64. epdconfig.digital_write(self.cs_pin, 0)
  65. epdconfig.spi_writebyte([command])
  66. epdconfig.digital_write(self.cs_pin, 1)
  67. def send_data(self, data):
  68. epdconfig.digital_write(self.dc_pin, 1)
  69. epdconfig.digital_write(self.cs_pin, 0)
  70. epdconfig.spi_writebyte([data])
  71. epdconfig.digital_write(self.cs_pin, 1)
  72. # send a lot of data
  73. def send_data2(self, data):
  74. epdconfig.digital_write(self.dc_pin, 1)
  75. epdconfig.digital_write(self.cs_pin, 0)
  76. epdconfig.spi_writebyte2(data)
  77. epdconfig.digital_write(self.cs_pin, 1)
  78. def ReadBusyH(self):
  79. logger.debug("e-Paper busy H")
  80. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: busy, 1: idle
  81. epdconfig.delay_ms(5)
  82. logger.debug("e-Paper busy H release")
  83. def TurnOnDisplay(self):
  84. self.send_command(0x04) # POWER_ON
  85. self.ReadBusyH()
  86. self.send_command(0x12) # DISPLAY_REFRESH
  87. self.send_data(0X00)
  88. self.ReadBusyH()
  89. self.send_command(0x02) # POWER_OFF
  90. self.send_data(0X00)
  91. self.ReadBusyH()
  92. def init(self):
  93. if (epdconfig.module_init() != 0):
  94. return -1
  95. # EPD hardware init start
  96. self.reset()
  97. self.ReadBusyH()
  98. epdconfig.delay_ms(30)
  99. self.send_command(0xAA) # CMDH
  100. self.send_data(0x49)
  101. self.send_data(0x55)
  102. self.send_data(0x20)
  103. self.send_data(0x08)
  104. self.send_data(0x09)
  105. self.send_data(0x18)
  106. self.send_command(0x01)
  107. self.send_data(0x3F)
  108. self.send_data(0x00)
  109. self.send_data(0x32)
  110. self.send_data(0x2A)
  111. self.send_data(0x0E)
  112. self.send_data(0x2A)
  113. self.send_command(0x00)
  114. self.send_data(0x5F)
  115. self.send_data(0x69)
  116. self.send_command(0x03)
  117. self.send_data(0x00)
  118. self.send_data(0x54)
  119. self.send_data(0x00)
  120. self.send_data(0x44)
  121. self.send_command(0x05)
  122. self.send_data(0x40)
  123. self.send_data(0x1F)
  124. self.send_data(0x1F)
  125. self.send_data(0x2C)
  126. self.send_command(0x06)
  127. self.send_data(0x6F)
  128. self.send_data(0x1F)
  129. self.send_data(0x1F)
  130. self.send_data(0x22)
  131. self.send_command(0x08)
  132. self.send_data(0x6F)
  133. self.send_data(0x1F)
  134. self.send_data(0x1F)
  135. self.send_data(0x22)
  136. self.send_command(0x13) # IPC
  137. self.send_data(0x00)
  138. self.send_data(0x04)
  139. self.send_command(0x30)
  140. self.send_data(0x3C)
  141. self.send_command(0x41) # TSE
  142. self.send_data(0x00)
  143. self.send_command(0x50)
  144. self.send_data(0x3F)
  145. self.send_command(0x60)
  146. self.send_data(0x02)
  147. self.send_data(0x00)
  148. self.send_command(0x61)
  149. self.send_data(0x03)
  150. self.send_data(0x20)
  151. self.send_data(0x01)
  152. self.send_data(0xE0)
  153. self.send_command(0x82)
  154. self.send_data(0x1E)
  155. self.send_command(0x84)
  156. self.send_data(0x00)
  157. self.send_command(0x86) # AGID
  158. self.send_data(0x00)
  159. self.send_command(0xE3)
  160. self.send_data(0x2F)
  161. self.send_command(0xE0) # CCSET
  162. self.send_data(0x00)
  163. self.send_command(0xE6) # TSSET
  164. self.send_data(0x00)
  165. return 0
  166. def getbuffer(self, image):
  167. # Create a pallette with the 7 colors supported by the panel
  168. pal_image = Image.new("P", (1,1))
  169. pal_image.putpalette( (0,0,0, 255,255,255, 0,255,0, 0,0,255, 255,0,0, 255,255,0, 255,128,0) + (0,0,0)*249)
  170. # Check if we need to rotate the image
  171. imwidth, imheight = image.size
  172. if(imwidth == self.width and imheight == self.height):
  173. image_temp = image
  174. elif(imwidth == self.height and imheight == self.width):
  175. image_temp = image.rotate(90, expand=True)
  176. else:
  177. logger.warning("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width, self.height))
  178. # Convert the soruce image to the 7 colors, dithering if needed
  179. image_7color = image_temp.convert("RGB").quantize(palette=pal_image)
  180. buf_7color = bytearray(image_7color.tobytes('raw'))
  181. # PIL does not support 4 bit color, so pack the 4 bits of color
  182. # into a single byte to transfer to the panel
  183. buf = [0x00] * int(self.width * self.height / 2)
  184. idx = 0
  185. for i in range(0, len(buf_7color), 2):
  186. buf[idx] = (buf_7color[i] << 4) + buf_7color[i+1]
  187. idx += 1
  188. return buf
  189. def display(self, image):
  190. self.send_command(0x10)
  191. self.send_data2(image)
  192. self.TurnOnDisplay()
  193. def Clear(self, color=0x11):
  194. self.send_command(0x10)
  195. self.send_data2([color] * int(self.height) * int(self.width/2))
  196. self.TurnOnDisplay()
  197. def sleep(self):
  198. self.send_command(0x07) # DEEP_SLEEP
  199. self.send_data(0XA5)
  200. epdconfig.delay_ms(2000)
  201. epdconfig.module_exit()
  202. ### END OF FILE ###