epd5in65f.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. def send_data_bulk(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 ReadBusyHigh(self):
  79. logger.debug("e-Paper busy")
  80. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  81. epdconfig.delay_ms(100)
  82. logger.debug("e-Paper busy release")
  83. def ReadBusyLow(self):
  84. logger.debug("e-Paper busy")
  85. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  86. epdconfig.delay_ms(100)
  87. logger.debug("e-Paper busy release")
  88. def init(self):
  89. if (epdconfig.module_init() != 0):
  90. return -1
  91. # EPD hardware init start
  92. self.reset()
  93. self.ReadBusyHigh()
  94. self.send_command(0x00)
  95. self.send_data(0xEF)
  96. self.send_data(0x08)
  97. self.send_command(0x01)
  98. self.send_data(0x37)
  99. self.send_data(0x00)
  100. self.send_data(0x23)
  101. self.send_data(0x23)
  102. self.send_command(0x03)
  103. self.send_data(0x00)
  104. self.send_command(0x06)
  105. self.send_data(0xC7)
  106. self.send_data(0xC7)
  107. self.send_data(0x1D)
  108. self.send_command(0x30)
  109. self.send_data(0x3c)
  110. self.send_command(0x41)
  111. self.send_data(0x00)
  112. self.send_command(0x50)
  113. self.send_data(0x37)
  114. self.send_command(0x60)
  115. self.send_data(0x22)
  116. self.send_command(0x61)
  117. self.send_data(0x02)
  118. self.send_data(0x58)
  119. self.send_data(0x01)
  120. self.send_data(0xC0)
  121. self.send_command(0xE3)
  122. self.send_data(0xAA)
  123. epdconfig.delay_ms(100)
  124. self.send_command(0x50)
  125. self.send_data(0x37)
  126. # EPD hardware init end
  127. return 0
  128. def getbuffer(self, image):
  129. # Create a pallette with the 7 colors supported by the panel
  130. pal_image = Image.new("P", (1,1))
  131. 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)
  132. # Check if we need to rotate the image
  133. imwidth, imheight = image.size
  134. if(imwidth == self.width and imheight == self.height):
  135. image_temp = image
  136. elif(imwidth == self.height and imheight == self.width):
  137. image_temp = image.rotate(90, expand=True)
  138. else:
  139. logger.warning("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width, self.height))
  140. # Convert the soruce image to the 7 colors, dithering if needed
  141. image_7color = image_temp.convert("RGB").quantize(palette=pal_image)
  142. buf_7color = bytearray(image_7color.tobytes('raw'))
  143. # PIL does not support 4 bit color, so pack the 4 bits of color
  144. # into a single byte to transfer to the panel
  145. buf = [0x00] * int(self.width * self.height / 2)
  146. idx = 0
  147. for i in range(0, len(buf_7color), 2):
  148. buf[idx] = (buf_7color[i] << 4) + buf_7color[i+1]
  149. idx += 1
  150. return buf
  151. def display(self,image):
  152. self.send_command(0x61) #Set Resolution setting
  153. self.send_data(0x02)
  154. self.send_data(0x58)
  155. self.send_data(0x01)
  156. self.send_data(0xC0)
  157. self.send_command(0x10)
  158. self.send_data_bulk(image)
  159. self.send_command(0x04) #0x04
  160. self.ReadBusyHigh()
  161. self.send_command(0x12) #0x12
  162. self.ReadBusyHigh()
  163. self.send_command(0x02) #0x02
  164. self.ReadBusyLow()
  165. epdconfig.delay_ms(500)
  166. def Clear(self):
  167. self.send_command(0x61) #Set Resolution setting
  168. self.send_data(0x02)
  169. self.send_data(0x58)
  170. self.send_data(0x01)
  171. self.send_data(0xC0)
  172. self.send_command(0x10)
  173. # Set all pixels to white
  174. buf = [0x11] * int(self.width * self.height / 2)
  175. self.send_data_bulk(buf)
  176. self.send_command(0x04) #0x04
  177. self.ReadBusyHigh()
  178. self.send_command(0x12) #0x12
  179. self.ReadBusyHigh()
  180. self.send_command(0x02) #0x02
  181. self.ReadBusyLow()
  182. epdconfig.delay_ms(500)
  183. def sleep(self):
  184. epdconfig.delay_ms(500)
  185. self.send_command(0x07) # DEEP_SLEEP
  186. self.send_data(0XA5)
  187. epdconfig.digital_write(self.reset_pin, 0)
  188. epdconfig.delay_ms(2000)
  189. epdconfig.module_exit()