epd5in65f.py 6.8 KB

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