epd5in83.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # *****************************************************************************
  2. # * | File : epd5in83.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.0
  8. # * | Date : 2019-06-20
  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 = 600
  33. EPD_HEIGHT = 448
  34. class EPD:
  35. def __init__(self):
  36. self.reset_pin = epdconfig.RST_PIN
  37. self.dc_pin = epdconfig.DC_PIN
  38. self.busy_pin = epdconfig.BUSY_PIN
  39. self.cs_pin = epdconfig.CS_PIN
  40. self.width = EPD_WIDTH
  41. self.height = EPD_HEIGHT
  42. # Hardware reset
  43. def reset(self):
  44. epdconfig.digital_write(self.reset_pin, 1)
  45. epdconfig.delay_ms(200)
  46. epdconfig.digital_write(self.reset_pin, 0)
  47. epdconfig.delay_ms(2)
  48. epdconfig.digital_write(self.reset_pin, 1)
  49. epdconfig.delay_ms(200)
  50. def send_command(self, command):
  51. epdconfig.digital_write(self.dc_pin, 0)
  52. epdconfig.digital_write(self.cs_pin, 0)
  53. epdconfig.spi_writebyte([command])
  54. epdconfig.digital_write(self.cs_pin, 1)
  55. def send_data(self, data):
  56. epdconfig.digital_write(self.dc_pin, 1)
  57. epdconfig.digital_write(self.cs_pin, 0)
  58. epdconfig.spi_writebyte([data])
  59. epdconfig.digital_write(self.cs_pin, 1)
  60. def ReadBusy(self):
  61. logging.debug("e-Paper busy")
  62. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  63. epdconfig.delay_ms(100)
  64. logging.debug("e-Paper busy release")
  65. def init(self):
  66. if (epdconfig.module_init() != 0):
  67. return -1
  68. # EPD hardware init start
  69. self.reset()
  70. self.send_command(0x01) # POWER_SETTING
  71. self.send_data(0x37)
  72. self.send_data(0x00)
  73. self.send_command(0x00) # PANEL_SETTING
  74. self.send_data(0xCF)
  75. self.send_data(0x08)
  76. self.send_command(0x06) # BOOSTER_SOFT_START
  77. self.send_data(0xc7)
  78. self.send_data(0xcc)
  79. self.send_data(0x28)
  80. self.send_command(0x04) # POWER_ON
  81. self.ReadBusy()
  82. self.send_command(0x30) # PLL_CONTROL
  83. self.send_data(0x3c)
  84. self.send_command(0x41) # TEMPERATURE_CALIBRATION
  85. self.send_data(0x00)
  86. self.send_command(0x50) # VCOM_AND_DATA_INTERVAL_SETTING
  87. self.send_data(0x77)
  88. self.send_command(0x60) # TCON_SETTING
  89. self.send_data(0x22)
  90. self.send_command(0x61) # TCON_RESOLUTION
  91. self.send_data(0x02) # source 600
  92. self.send_data(0x58)
  93. self.send_data(0x01) # gate 448
  94. self.send_data(0xC0)
  95. self.send_command(0x82) # VCM_DC_SETTING
  96. self.send_data(0x1E) # decide by LUT file
  97. self.send_command(0xe5) # FLASH MODE
  98. self.send_data(0x03)
  99. # EPD hardware init end
  100. return 0
  101. def getbuffer(self, image):
  102. buf = [0x00] * int(self.width * self.height / 4)
  103. image_monocolor = image.convert('1')
  104. imwidth, imheight = image_monocolor.size
  105. pixels = image_monocolor.load()
  106. logging.debug('imwidth = %d imheight = %d ',imwidth, imheight)
  107. if(imwidth == self.width and imheight == self.height):
  108. for y in range(imheight):
  109. for x in range(imwidth):
  110. # Set the bits for the column of pixels at the current position.
  111. if pixels[x, y] < 64: # black
  112. buf[int((x + y * self.width) / 4)] &= ~(0xC0 >> (x % 4 * 2))
  113. elif pixels[x, y] < 192: # convert gray to red
  114. buf[int((x + y * self.width) / 4)] &= ~(0xC0 >> (x % 4 * 2))
  115. buf[int((x + y * self.width) / 4)] |= 0x40 >> (x % 4 * 2)
  116. else: # white
  117. buf[int((x + y * self.width) / 4)] |= 0xC0 >> (x % 4 * 2)
  118. elif(imwidth == self.height and imheight == self.width):
  119. for y in range(imheight):
  120. for x in range(imwidth):
  121. newx = y
  122. newy = self.height - x - 1
  123. if pixels[x, y] < 64: # black
  124. buf[int((newx + newy*self.width) / 4)] &= ~(0xC0 >> (y % 4 * 2))
  125. elif pixels[x, y] < 192: # convert gray to red
  126. buf[int((newx + newy*self.width) / 4)] &= ~(0xC0 >> (y % 4 * 2))
  127. buf[int((newx + newy*self.width) / 4)] |= 0x40 >> (y % 4 * 2)
  128. else: # white
  129. buf[int((newx + newy*self.width) / 4)] |= 0xC0 >> (y % 4 * 2)
  130. return buf
  131. def display(self, image):
  132. self.send_command(0x10)
  133. for i in range(0, int(self.width / 4 * self.height)):
  134. temp1 = image[i]
  135. j = 0
  136. while (j < 4):
  137. if ((temp1 & 0xC0) == 0xC0):
  138. temp2 = 0x03
  139. elif ((temp1 & 0xC0) == 0x00):
  140. temp2 = 0x00
  141. else:
  142. temp2 = 0x04
  143. temp2 = (temp2 << 4) & 0xFF
  144. temp1 = (temp1 << 2) & 0xFF
  145. j += 1
  146. if((temp1 & 0xC0) == 0xC0):
  147. temp2 |= 0x03
  148. elif ((temp1 & 0xC0) == 0x00):
  149. temp2 |= 0x00
  150. else:
  151. temp2 |= 0x04
  152. temp1 = (temp1 << 2) & 0xFF
  153. self.send_data(temp2)
  154. j += 1
  155. self.send_command(0x12)
  156. epdconfig.delay_ms(100)
  157. self.ReadBusy()
  158. def Clear(self):
  159. self.send_command(0x10)
  160. for i in range(0, int(self.width / 4 * self.height)):
  161. for j in range(0, 4):
  162. self.send_data(0x33)
  163. self.send_command(0x12)
  164. self.ReadBusy()
  165. def sleep(self):
  166. self.send_command(0x02) # POWER_OFF
  167. self.ReadBusy()
  168. self.send_command(0x07) # DEEP_SLEEP
  169. self.send_data(0XA5)
  170. epdconfig.delay_ms(2000)
  171. epdconfig.module_exit()
  172. ### END OF FILE ###