epd5in79b.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. # Display resolution
  32. EPD_WIDTH = 792
  33. EPD_HEIGHT = 272
  34. logger = logging.getLogger(__name__)
  35. class EPD:
  36. def __init__(self):
  37. self.reset_pin = epdconfig.RST_PIN
  38. self.dc_pin = epdconfig.DC_PIN
  39. self.busy_pin = epdconfig.BUSY_PIN
  40. self.cs_pin = epdconfig.CS_PIN
  41. self.width = EPD_WIDTH
  42. self.height = EPD_HEIGHT
  43. # Hardware reset
  44. def reset(self):
  45. epdconfig.digital_write(self.reset_pin, 1)
  46. epdconfig.delay_ms(200)
  47. epdconfig.digital_write(self.reset_pin, 0)
  48. epdconfig.delay_ms(1)
  49. epdconfig.digital_write(self.reset_pin, 1)
  50. epdconfig.delay_ms(200)
  51. def send_command(self, command):
  52. epdconfig.digital_write(self.dc_pin, 0)
  53. epdconfig.digital_write(self.cs_pin, 0)
  54. epdconfig.spi_writebyte([command])
  55. epdconfig.digital_write(self.cs_pin, 1)
  56. def send_data(self, data):
  57. epdconfig.digital_write(self.dc_pin, 1)
  58. epdconfig.digital_write(self.cs_pin, 0)
  59. epdconfig.spi_writebyte([data])
  60. epdconfig.digital_write(self.cs_pin, 1)
  61. # send a lot of data
  62. def send_data2(self, data):
  63. epdconfig.digital_write(self.dc_pin, 1)
  64. epdconfig.digital_write(self.cs_pin, 0)
  65. epdconfig.spi_writebyte2(data)
  66. epdconfig.digital_write(self.cs_pin, 1)
  67. def ReadBusy(self):
  68. logger.debug("e-Paper busy")
  69. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  70. epdconfig.delay_ms(200)
  71. logger.debug("e-Paper busy release")
  72. def TurnOnDisplay(self):
  73. self.send_command(0x22)
  74. self.send_data(0xF7) # 24s # 0xD7 16s Probability refresh bad, probability damage ink screen
  75. self.send_command(0x20) # DISPLAY REFRESH
  76. epdconfig.delay_ms(100) # The delay here is necessary, 200uS at least!!!
  77. self.ReadBusy() # waiting for the electronic paper IC to release the idle signal
  78. def init(self):
  79. if (epdconfig.module_init() != 0):
  80. return -1
  81. self.reset()
  82. self.ReadBusy() # waiting for the electronic paper IC to release the idle signal
  83. self.send_command(0x12) # POWER ON
  84. self.ReadBusy() # waiting for the electronic paper IC to release the idle signal
  85. self.send_command(0x11)
  86. self.send_data(0x01)
  87. self.send_command(0x44) # Set Ram X- address Start / End position
  88. self.send_data(0x00) # XStart, POR = 00h
  89. self.send_data(0x31) # 400/8-1
  90. self.send_command(0x45) # Set Ram Y- address Start / End position
  91. self.send_data(0x0f)
  92. self.send_data(0x01) # 300-1
  93. self.send_data(0x00) # YEnd L
  94. self.send_data(0x00) # YEnd H
  95. self.send_command(0x4e)
  96. self.send_data(0x00)
  97. self.send_command(0x4f)
  98. self.send_data(0x0f)
  99. self.send_data(0x01)
  100. self.send_command(0x91)
  101. self.send_data(0x00)
  102. self.send_command(0xC4) # Set Ram X- address Start / End position
  103. self.send_data(0x31) # XStart, POR = 00h
  104. self.send_data(0x00) # 400/8-1
  105. self.send_command(0xC5) # Set Ram Y- address Start / End position
  106. self.send_data(0x0f)
  107. self.send_data(0x01) # 300-1
  108. self.send_data(0x00) # YEnd L
  109. self.send_data(0x00) # YEnd H
  110. self.send_command(0xCe)
  111. self.send_data(0x31)
  112. self.send_command(0xCf)
  113. self.send_data(0x0f)
  114. self.send_data(0x01)
  115. return 0
  116. def getbuffer(self, image):
  117. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  118. buf = [0xFF] * (int(self.width/8) * self.height)
  119. image_monocolor = image.convert('1')
  120. imwidth, imheight = image_monocolor.size
  121. pixels = image_monocolor.load()
  122. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  123. if(imwidth == self.width and imheight == self.height):
  124. logger.debug("Vertical")
  125. for y in range(imheight):
  126. for x in range(imwidth):
  127. # Set the bits for the column of pixels at the current position.
  128. if pixels[x, y] == 0:
  129. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  130. elif(imwidth == self.height and imheight == self.width):
  131. logger.debug("Horizontal")
  132. for y in range(imheight):
  133. for x in range(imwidth):
  134. newx = y
  135. newy = self.height - x - 1
  136. if pixels[x, y] == 0:
  137. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  138. return buf
  139. def display(self, imageblack, imagered):
  140. buf = [0x00] * int(self.width * self.height / 8)
  141. for i in range(0, int(self.width * self.height / 8)):
  142. buf[i] = ~imagered[i]
  143. Width =int(self.width / 16)+1
  144. Width1 =int(self.width / 8)
  145. Height = self.height
  146. self.send_command(0x24)
  147. for i in range(Height):
  148. self.send_data2(imageblack[i * Width1 : i * Width1+Width])
  149. self.send_command(0X26)
  150. for i in range(self.height):
  151. self.send_data2(buf[i * Width1 : i * Width1+Width])
  152. self.send_command(0xA4)
  153. for i in range(self.height):
  154. self.send_data2(imageblack[i * Width1 + Width - 1 : i * Width1 + Width * 2 - 1])
  155. self.send_command(0xA6)
  156. for i in range(self.height):
  157. self.send_data2(buf[i * Width1 + Width - 1 : i * Width1 + Width * 2 - 1])
  158. self.TurnOnDisplay()
  159. def Clear(self):
  160. self.send_command(0x24)
  161. self.send_data2([0xFF] * 13600)
  162. self.send_command(0X26)
  163. self.send_data2([0x00] * 13600)
  164. self.send_command(0xA4)
  165. self.send_data2([0xFF] * 13600)
  166. self.send_command(0xA6)
  167. self.send_data2([0x00] * 13600)
  168. self.TurnOnDisplay()
  169. def sleep(self):
  170. self.send_command(0X10) # deep sleep
  171. self.send_data(0x03)
  172. epdconfig.delay_ms(2000)
  173. epdconfig.module_exit()
  174. ### END OF FILE ###