epd7in5.py 7.3 KB

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