epd1in54_V2.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # *****************************************************************************
  2. # * | File : epd1in54_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1
  8. # * | Date : 2019-06-20
  9. # # | Info : python3 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 = 200
  32. EPD_HEIGHT = 200
  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) == 1):
  62. epdconfig.delay_ms(100)
  63. print("e-Paper busy release")
  64. def TurnOnDisplay(self):
  65. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  66. self.send_data(0xF7)
  67. self.send_command(0x20) # MASTER_ACTIVATION
  68. self.ReadBusy()
  69. def TurnOnDisplayPart(self):
  70. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  71. self.send_data(0xFF)
  72. self.send_command(0x20) # MASTER_ACTIVATION
  73. self.ReadBusy()
  74. def init(self):
  75. if (epdconfig.module_init() != 0):
  76. return -1
  77. # EPD hardware init start
  78. self.reset()
  79. self.ReadBusy()
  80. self.send_command(0x12) # SWRESET
  81. self.ReadBusy()
  82. self.send_command(0x01) # DRIVER_OUTPUT_CONTROL
  83. self.send_data(0xC7) # (EPD_HEIGHT - 1) & 0xFF
  84. self.send_data(0x00) # ((EPD_HEIGHT - 1) >> 8) & 0xFF
  85. self.send_data(0x01) # GD = 0 SM = 0 TB = 0
  86. self.send_command(0x11) # data entry mode
  87. self.send_data(0x01)
  88. self.send_command(0x44) # set Ram-X address start//end position
  89. self.send_data(0x00)
  90. self.send_data(0x18) # 0x0C-->(18+1)*8=200
  91. self.send_command(0x45) # set Ram-Y address start//end position
  92. self.send_data(0xC7) # 0xC7-->(199+1)=200
  93. self.send_data(0x00)
  94. self.send_data(0x00)
  95. self.send_data(0x00)
  96. self.send_command(0x3C) # BorderWavefrom
  97. self.send_data(0x01)
  98. self.send_command(0x18)
  99. self.send_data(0x80)
  100. self.send_command(0x22) # #Load Temperature and waveform setting.
  101. self.send_data(0XB1)
  102. self.send_command(0x20)
  103. self.send_command(0x4E) # set RAM x address count to 0;
  104. self.send_data(0x00)
  105. self.send_command(0x4F) # set RAM y address count to 0X199;
  106. self.send_data(0xC7)
  107. self.send_data(0x00)
  108. self.ReadBusy()
  109. def Clear(self, color):
  110. self.send_command(0x24)
  111. for j in range(0, self.height):
  112. for i in range(0, self.width // 8):
  113. self.send_data(color)
  114. self.TurnOnDisplay()
  115. def getbuffer(self, image):
  116. buf = [0xFF] * ((self.width//8) * self.height)
  117. image_monocolor = image.convert('1')
  118. imwidth, imheight = image_monocolor.size
  119. pixels = image_monocolor.load()
  120. if(imwidth == self.width and imheight == self.height):
  121. print("Horizontal")
  122. for y in range(imheight):
  123. for x in range(imwidth):
  124. # Set the bits for the column of pixels at the current position.
  125. if pixels[x, y] == 0:
  126. buf[(x + y * self.width) // 8] &= ~(0x80 >> (x % 8))
  127. elif(imwidth == self.height and imheight == self.width):
  128. print("Vertical")
  129. for y in range(imheight):
  130. for x in range(imwidth):
  131. newx = y
  132. newy = self.height - x - 1
  133. if pixels[x, y] == 0:
  134. buf[(newx + newy*self.width) // 8] &= ~(0x80 >> (y % 8))
  135. return buf
  136. def display(self, image):
  137. if (image == None):
  138. return
  139. self.send_command(0x24)
  140. for j in range(0, self.height):
  141. for i in range(0, self.width // 8):
  142. self.send_data(image[i + j * (self.width // 8)])
  143. self.TurnOnDisplay()
  144. def displayPartBaseImage(self, image):
  145. if (image == None):
  146. return
  147. self.send_command(0x24)
  148. for j in range(0, self.height):
  149. for i in range(0, self.width // 8):
  150. self.send_data(image[i + j * (self.width // 8)])
  151. self.send_command(0x26)
  152. for j in range(0, self.height):
  153. for i in range(0, self.width // 8):
  154. self.send_data(image[i + j * (self.width // 8)])
  155. self.TurnOnDisplayPart()
  156. def displayPart(self, image):
  157. if (image == None):
  158. return
  159. self.send_command(0x24)
  160. for j in range(0, self.height):
  161. for i in range(0, self.width // 8):
  162. self.send_data(image[i + j * (self.width // 8)])
  163. self.TurnOnDisplayPart()
  164. def sleep(self):
  165. self.send_command(0x10) # DEEP_SLEEP_MODE
  166. self.send_data(0x01)
  167. epdconfig.module_exit()
  168. ### END OF FILE ###