epd5in65f.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. # *****************************************************************************
  4. # * | File : epd5in65f.py
  5. # * | Author : Waveshare team
  6. # * | Function : Electronic paper driver
  7. # * | Info :
  8. # *----------------
  9. # * | This version: V1.0
  10. # * | Date : 2020-03-02
  11. # # | Info : python demo
  12. # -----------------------------------------------------------------------------
  13. # Permission is hereby granted, free of charge, to any person obtaining a copy
  14. # of this software and associated documnetation files (the "Software"), to deal
  15. # in the Software without restriction, including without limitation the rights
  16. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. # copies of the Software, and to permit persons to whom the Software is
  18. # furished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included in
  21. # all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. # THE SOFTWARE.
  30. #
  31. import logging
  32. from . import epdconfig
  33. import PIL
  34. from PIL import Image
  35. import io
  36. # Display resolution
  37. EPD_WIDTH = 600
  38. EPD_HEIGHT = 448
  39. logger = logging.getLogger(__name__)
  40. class EPD:
  41. def __init__(self):
  42. self.reset_pin = epdconfig.RST_PIN
  43. self.dc_pin = epdconfig.DC_PIN
  44. self.busy_pin = epdconfig.BUSY_PIN
  45. self.cs_pin = epdconfig.CS_PIN
  46. self.width = EPD_WIDTH
  47. self.height = EPD_HEIGHT
  48. self.BLACK = 0x000000 # 0000 BGR
  49. self.WHITE = 0xffffff # 0001
  50. self.GREEN = 0x00ff00 # 0010
  51. self.BLUE = 0xff0000 # 0011
  52. self.RED = 0x0000ff # 0100
  53. self.YELLOW = 0x00ffff # 0101
  54. self.ORANGE = 0x0080ff # 0110
  55. # Hardware reset
  56. def reset(self):
  57. epdconfig.digital_write(self.reset_pin, 1)
  58. epdconfig.delay_ms(600)
  59. epdconfig.digital_write(self.reset_pin, 0)
  60. epdconfig.delay_ms(2)
  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. # send a lot of data
  74. def send_data2(self, data):
  75. epdconfig.digital_write(self.dc_pin, 1)
  76. epdconfig.digital_write(self.cs_pin, 0)
  77. epdconfig.spi_writebyte2(data)
  78. epdconfig.digital_write(self.cs_pin, 1)
  79. def ReadBusyHigh(self):
  80. logger.debug("e-Paper busy")
  81. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  82. epdconfig.delay_ms(100)
  83. logger.debug("e-Paper busy release")
  84. def ReadBusyLow(self):
  85. logger.debug("e-Paper busy")
  86. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  87. epdconfig.delay_ms(100)
  88. logger.debug("e-Paper busy release")
  89. def init(self):
  90. if (epdconfig.module_init() != 0):
  91. return -1
  92. # EPD hardware init start
  93. self.reset()
  94. self.ReadBusyHigh()
  95. self.send_command(0x00)
  96. self.send_data(0xEF)
  97. self.send_data(0x08)
  98. self.send_command(0x01)
  99. self.send_data(0x37)
  100. self.send_data(0x00)
  101. self.send_data(0x23)
  102. self.send_data(0x23)
  103. self.send_command(0x03)
  104. self.send_data(0x00)
  105. self.send_command(0x06)
  106. self.send_data(0xC7)
  107. self.send_data(0xC7)
  108. self.send_data(0x1D)
  109. self.send_command(0x30)
  110. self.send_data(0x3c)
  111. self.send_command(0x41)
  112. self.send_data(0x00)
  113. self.send_command(0x50)
  114. self.send_data(0x37)
  115. self.send_command(0x60)
  116. self.send_data(0x22)
  117. self.send_command(0x61)
  118. self.send_data(0x02)
  119. self.send_data(0x58)
  120. self.send_data(0x01)
  121. self.send_data(0xC0)
  122. self.send_command(0xE3)
  123. self.send_data(0xAA)
  124. epdconfig.delay_ms(100)
  125. self.send_command(0x50)
  126. self.send_data(0x37)
  127. # EPD hardware init end
  128. return 0
  129. def getbuffer(self, image):
  130. # Create a pallette with the 7 colors supported by the panel
  131. pal_image = Image.new("P", (1,1))
  132. 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)
  133. # Check if we need to rotate the image
  134. imwidth, imheight = image.size
  135. if(imwidth == self.width and imheight == self.height):
  136. image_temp = image
  137. elif(imwidth == self.height and imheight == self.width):
  138. image_temp = image.rotate(90, expand=True)
  139. else:
  140. logger.warning("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width, self.height))
  141. # Convert the soruce image to the 7 colors, dithering if needed
  142. image_7color = image_temp.convert("RGB").quantize(palette=pal_image)
  143. buf_7color = bytearray(image_7color.tobytes('raw'))
  144. # PIL does not support 4 bit color, so pack the 4 bits of color
  145. # into a single byte to transfer to the panel
  146. buf = [0x00] * int(self.width * self.height / 2)
  147. idx = 0
  148. for i in range(0, len(buf_7color), 2):
  149. buf[idx] = (buf_7color[i] << 4) + buf_7color[i+1]
  150. idx += 1
  151. return buf
  152. def display(self,image):
  153. self.send_command(0x61) #Set Resolution setting
  154. self.send_data(0x02)
  155. self.send_data(0x58)
  156. self.send_data(0x01)
  157. self.send_data(0xC0)
  158. self.send_command(0x10)
  159. self.send_data2(image)
  160. self.send_command(0x04) #0x04
  161. self.ReadBusyHigh()
  162. self.send_command(0x12) #0x12
  163. self.ReadBusyHigh()
  164. self.send_command(0x02) #0x02
  165. self.ReadBusyLow()
  166. epdconfig.delay_ms(500)
  167. def Clear(self):
  168. self.send_command(0x61) #Set Resolution setting
  169. self.send_data(0x02)
  170. self.send_data(0x58)
  171. self.send_data(0x01)
  172. self.send_data(0xC0)
  173. self.send_command(0x10)
  174. # Set all pixels to white
  175. buf = [0x11] * int(self.width * self.height / 2)
  176. self.send_data2(buf)
  177. self.send_command(0x04) #0x04
  178. self.ReadBusyHigh()
  179. self.send_command(0x12) #0x12
  180. self.ReadBusyHigh()
  181. self.send_command(0x02) #0x02
  182. self.ReadBusyLow()
  183. epdconfig.delay_ms(500)
  184. def sleep(self):
  185. epdconfig.delay_ms(500)
  186. self.send_command(0x07) # DEEP_SLEEP
  187. self.send_data(0XA5)
  188. epdconfig.digital_write(self.reset_pin, 0)
  189. epdconfig.delay_ms(2000)
  190. epdconfig.module_exit()