2
0

epd5in79g.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # *****************************************************************************
  2. # * | File : epd5in79b.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2024-03-05
  9. # # | Info : python demo
  10. # -----------------------------------------------------------------------------
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. import logging
  30. from . import epdconfig
  31. import PIL
  32. from PIL import Image
  33. import io
  34. # Display resolution
  35. EPD_WIDTH = 792
  36. EPD_HEIGHT = 272
  37. logger = logging.getLogger(__name__)
  38. class EPD:
  39. def __init__(self):
  40. self.reset_pin = epdconfig.RST_PIN
  41. self.dc_pin = epdconfig.DC_PIN
  42. self.busy_pin = epdconfig.BUSY_PIN
  43. self.cs_pin = epdconfig.CS_PIN
  44. self.width = EPD_WIDTH
  45. self.height = EPD_HEIGHT
  46. self.BLACK = 0x000000 # 00 BGR
  47. self.WHITE = 0xffffff # 01
  48. self.YELLOW = 0x00ffff # 10
  49. self.RED = 0x0000ff # 11
  50. # Hardware reset
  51. def reset(self):
  52. epdconfig.digital_write(self.reset_pin, 1)
  53. epdconfig.delay_ms(200)
  54. epdconfig.digital_write(self.reset_pin, 0)
  55. epdconfig.delay_ms(1)
  56. epdconfig.digital_write(self.reset_pin, 1)
  57. epdconfig.delay_ms(200)
  58. def send_command(self, command):
  59. epdconfig.digital_write(self.dc_pin, 0)
  60. epdconfig.digital_write(self.cs_pin, 0)
  61. epdconfig.spi_writebyte([command])
  62. epdconfig.digital_write(self.cs_pin, 1)
  63. def send_data(self, data):
  64. epdconfig.digital_write(self.dc_pin, 1)
  65. epdconfig.digital_write(self.cs_pin, 0)
  66. epdconfig.spi_writebyte([data])
  67. epdconfig.digital_write(self.cs_pin, 1)
  68. # send a lot of data
  69. def send_data2(self, data):
  70. epdconfig.digital_write(self.dc_pin, 1)
  71. epdconfig.digital_write(self.cs_pin, 0)
  72. epdconfig.spi_writebyte2(data)
  73. epdconfig.digital_write(self.cs_pin, 1)
  74. def ReadBusyH(self):
  75. logger.debug("e-Paper busy H")
  76. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  77. epdconfig.delay_ms(5)
  78. epdconfig.delay_ms(200)
  79. logger.debug("e-Paper busy H release")
  80. def ReadBusyL(self):
  81. logger.debug("e-Paper busy L")
  82. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: busy, 1: idle
  83. epdconfig.delay_ms(5)
  84. logger.debug("e-Paper busy L release")
  85. def TurnOnDisplay(self):
  86. self.send_command(0xA2)
  87. self.send_data(0x00)
  88. self.send_command(0x12)
  89. self.send_data(0x00)
  90. epdconfig.delay_ms(100)
  91. self.ReadBusyH()
  92. def init(self):
  93. if (epdconfig.module_init() != 0):
  94. return -1
  95. self.reset()
  96. self.ReadBusyH()
  97. self.send_command(0xA2)
  98. self.send_data(0x01)
  99. self.send_command(0x00)
  100. self.send_data(0x03)
  101. self.send_data(0x29)
  102. self.send_command(0xA2)
  103. self.send_data(0x02)
  104. self.send_command(0x00)
  105. self.send_data(0x07)
  106. self.send_data(0x29)
  107. self.send_command(0xA2)
  108. self.send_data(0x00)
  109. self.send_command(0x50)
  110. self.send_data(0x97)
  111. self.send_command(0x61)
  112. self.send_data(0x01)
  113. self.send_data(0x8c)
  114. self.send_data(0x01)
  115. self.send_data(0x10)
  116. self.send_command(0x06)
  117. self.send_data(0x38)
  118. self.send_data(0x38)
  119. self.send_data(0x38)
  120. self.send_data(0x00)
  121. self.send_command(0xE9)
  122. self.send_data(0x01)
  123. self.send_command(0xE0)
  124. self.send_data(0x01)
  125. self.send_command(0x04)
  126. self.ReadBusyH()
  127. return 0
  128. def getbuffer(self, image):
  129. # Create a pallette with the 4 colors supported by the panel
  130. pal_image = Image.new("P", (1,1))
  131. pal_image.putpalette( (0,0,0, 255,255,255, 255,255,0, 255,0,0) + (0,0,0)*252)
  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 4 colors, dithering if needed
  141. image_4color = image_temp.convert("RGB").quantize(palette=pal_image)
  142. buf_4color = bytearray(image_4color.tobytes('raw'))
  143. # into a single byte to transfer to the panel
  144. buf = [0x00] * int(self.width * self.height / 4)
  145. idx = 0
  146. for i in range(0, len(buf_4color), 4):
  147. buf[idx] = (buf_4color[i] << 6) + (buf_4color[i+1] << 4) + (buf_4color[i+2] << 2) + buf_4color[i+3]
  148. idx += 1
  149. return buf
  150. def display(self, image):
  151. Width =int(self.width / 8)
  152. Width1 =int(self.width / 4)
  153. Height = self.height
  154. self.send_command(0xA2)
  155. self.send_data(0x02)
  156. self.send_command(0x10)
  157. for i in range(Height//2):
  158. self.send_data2(image[i * Width1 : i * Width1+Width])
  159. self.send_data2(image[(Height-i-1) * Width1 : (Height-i-1) * Width1+Width])
  160. self.send_command(0xA2)
  161. self.send_data(0x01)
  162. self.send_command(0x10)
  163. for i in range(Height//2):
  164. self.send_data2(image[i * Width1+Width : (i+1) * Width1])
  165. self.send_data2(image[(Height-i-1) * Width1+Width : (Height-i) * Width1])
  166. self.TurnOnDisplay()
  167. def Clear(self, color=0x55):
  168. self.send_command(0xA2)
  169. self.send_data(0x02)
  170. self.send_command(0x10)
  171. self.send_data2([color] * int(self.height) * int(self.width/8))
  172. self.send_command(0xA2)
  173. self.send_data(0x01)
  174. self.send_command(0x10)
  175. self.send_data2([color] * int(self.height) * int(self.width/8))
  176. self.TurnOnDisplay()
  177. def sleep(self):
  178. self.send_command(0X10) # deep sleep
  179. self.send_data(0x03)
  180. epdconfig.delay_ms(2000)
  181. epdconfig.module_exit()
  182. ### END OF FILE ###