epd5in83.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 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(2)
  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. def ReadBusy(self):
  62. logger.debug("e-Paper busy")
  63. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  64. epdconfig.delay_ms(100)
  65. logger.debug("e-Paper busy release")
  66. def init(self):
  67. if (epdconfig.module_init() != 0):
  68. return -1
  69. # EPD hardware init start
  70. self.reset()
  71. self.send_command(0x01) # POWER_SETTING
  72. self.send_data(0x37)
  73. self.send_data(0x00)
  74. self.send_command(0x00) # PANEL_SETTING
  75. self.send_data(0xCF)
  76. self.send_data(0x08)
  77. self.send_command(0x06) # BOOSTER_SOFT_START
  78. self.send_data(0xc7)
  79. self.send_data(0xcc)
  80. self.send_data(0x28)
  81. self.send_command(0x04) # POWER_ON
  82. self.ReadBusy()
  83. self.send_command(0x30) # PLL_CONTROL
  84. self.send_data(0x3c)
  85. self.send_command(0x41) # TEMPERATURE_CALIBRATION
  86. self.send_data(0x00)
  87. self.send_command(0x50) # VCOM_AND_DATA_INTERVAL_SETTING
  88. self.send_data(0x77)
  89. self.send_command(0x60) # TCON_SETTING
  90. self.send_data(0x22)
  91. self.send_command(0x61) # TCON_RESOLUTION
  92. self.send_data(0x02) # source 600
  93. self.send_data(0x58)
  94. self.send_data(0x01) # gate 448
  95. self.send_data(0xC0)
  96. self.send_command(0x82) # VCM_DC_SETTING
  97. self.send_data(0x1E) # decide by LUT file
  98. self.send_command(0xe5) # FLASH MODE
  99. self.send_data(0x03)
  100. # EPD hardware init end
  101. return 0
  102. def getbuffer(self, image):
  103. buf = [0x00] * int(self.width * self.height / 4)
  104. image_monocolor = image.convert('1')
  105. imwidth, imheight = image_monocolor.size
  106. pixels = image_monocolor.load()
  107. logger.debug('imwidth = %d imheight = %d ',imwidth, imheight)
  108. if(imwidth == self.width and imheight == self.height):
  109. for y in range(imheight):
  110. for x in range(imwidth):
  111. # Set the bits for the column of pixels at the current position.
  112. if pixels[x, y] < 64: # black
  113. buf[int((x + y * self.width) / 4)] &= ~(0xC0 >> (x % 4 * 2))
  114. elif pixels[x, y] < 192: # convert gray to red
  115. buf[int((x + y * self.width) / 4)] &= ~(0xC0 >> (x % 4 * 2))
  116. buf[int((x + y * self.width) / 4)] |= 0x40 >> (x % 4 * 2)
  117. else: # white
  118. buf[int((x + y * self.width) / 4)] |= 0xC0 >> (x % 4 * 2)
  119. elif(imwidth == self.height and imheight == self.width):
  120. for y in range(imheight):
  121. for x in range(imwidth):
  122. newx = y
  123. newy = self.height - x - 1
  124. if pixels[x, y] < 64: # black
  125. buf[int((newx + newy*self.width) / 4)] &= ~(0xC0 >> (y % 4 * 2))
  126. elif pixels[x, y] < 192: # convert gray to red
  127. buf[int((newx + newy*self.width) / 4)] &= ~(0xC0 >> (y % 4 * 2))
  128. buf[int((newx + newy*self.width) / 4)] |= 0x40 >> (y % 4 * 2)
  129. else: # white
  130. buf[int((newx + newy*self.width) / 4)] |= 0xC0 >> (y % 4 * 2)
  131. return buf
  132. def display(self, image):
  133. self.send_command(0x10)
  134. for i in range(0, int(self.width / 4 * self.height)):
  135. temp1 = image[i]
  136. j = 0
  137. while (j < 4):
  138. if ((temp1 & 0xC0) == 0xC0):
  139. temp2 = 0x03
  140. elif ((temp1 & 0xC0) == 0x00):
  141. temp2 = 0x00
  142. else:
  143. temp2 = 0x04
  144. temp2 = (temp2 << 4) & 0xFF
  145. temp1 = (temp1 << 2) & 0xFF
  146. j += 1
  147. if((temp1 & 0xC0) == 0xC0):
  148. temp2 |= 0x03
  149. elif ((temp1 & 0xC0) == 0x00):
  150. temp2 |= 0x00
  151. else:
  152. temp2 |= 0x04
  153. temp1 = (temp1 << 2) & 0xFF
  154. self.send_data(temp2)
  155. j += 1
  156. self.send_command(0x12)
  157. epdconfig.delay_ms(100)
  158. self.ReadBusy()
  159. def Clear(self):
  160. self.send_command(0x10)
  161. for i in range(0, int(self.width / 4 * self.height)):
  162. for j in range(0, 4):
  163. self.send_data(0x33)
  164. self.send_command(0x12)
  165. self.ReadBusy()
  166. def sleep(self):
  167. self.send_command(0x02) # POWER_OFF
  168. self.ReadBusy()
  169. self.send_command(0x07) # DEEP_SLEEP
  170. self.send_data(0XA5)
  171. epdconfig.delay_ms(2000)
  172. epdconfig.module_exit()
  173. ### END OF FILE ###