2
0

epd1in02.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. # *****************************************************************************
  2. # * | File : epd1in54.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2019-06-20
  9. # # | Info : python demo
  10. # -----------------------------------------------------------------------------
  11. # ******************************************************************************/
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. import logging
  31. from . import epdconfig
  32. # Display resolution
  33. EPD_WIDTH = 80
  34. EPD_HEIGHT = 128
  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. #full screen update LUT
  44. lut_w1 =[
  45. 0x60, 0x5A, 0x5A, 0x00, 0x00, 0x01,
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. ]
  53. lut_b1 =[
  54. 0x90, 0x5A, 0x5A, 0x00, 0x00, 0x01,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. ]
  62. # partial screen update LUT
  63. lut_w = [
  64. 0x60, 0x01, 0x01, 0x00, 0x00, 0x01,
  65. 0x80, 0x1f, 0x00, 0x00, 0x00, 0x01,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. ]
  72. lut_b = [
  73. 0x90, 0x01, 0x01, 0x00, 0x00, 0x01,
  74. 0x40, 0x1f, 0x00, 0x00, 0x00, 0x01,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. ]
  81. # Hardware reset
  82. def reset(self):
  83. epdconfig.digital_write(self.reset_pin, 1)
  84. epdconfig.delay_ms(200)
  85. epdconfig.digital_write(self.reset_pin, 0) # module reset
  86. epdconfig.delay_ms(2)
  87. epdconfig.digital_write(self.reset_pin, 1)
  88. epdconfig.delay_ms(200)
  89. def send_command(self, command):
  90. epdconfig.digital_write(self.dc_pin, 0)
  91. epdconfig.digital_write(self.cs_pin, 0)
  92. epdconfig.spi_writebyte([command])
  93. epdconfig.digital_write(self.cs_pin, 1)
  94. def send_data(self, data):
  95. epdconfig.digital_write(self.dc_pin, 1)
  96. epdconfig.digital_write(self.cs_pin, 0)
  97. epdconfig.spi_writebyte([data])
  98. epdconfig.digital_write(self.cs_pin, 1)
  99. def ReadBusy(self):
  100. logging.debug("e-Paper busy")
  101. self.send_command(0x71)
  102. busy = epdconfig.digital_read(self.busy_pin)
  103. busy =not(busy & 0x01)
  104. while(busy):
  105. self.send_command(0x71)
  106. busy = epdconfig.digital_read(self.busy_pin)
  107. busy =not(busy & 0x01)
  108. epdconfig.delay_ms(800)
  109. logging.debug("e-Paper busy release")
  110. def TurnOnDisplay(self):
  111. self.send_command(0x12)
  112. epdconfig.delay_ms(10)
  113. self.ReadBusy()
  114. def SetFulltReg(self):
  115. self.send_command(0x23)
  116. for count in range(0, 42):
  117. self.send_data(self.lut_w1[count])
  118. self.send_command(0x24)
  119. for count in range(0, 42):
  120. self.send_data(self.lut_b1[count])
  121. def SetPartReg(self):
  122. self.send_command(0x23)
  123. for count in range(0, 42):
  124. self.send_data(self.lut_w[count])
  125. self.send_command(0x24)
  126. for count in range(0, 42):
  127. self.send_data(self.lut_b[count])
  128. def Init(self):
  129. if (epdconfig.module_init() != 0):
  130. return -1
  131. # EPD hardware init start
  132. self.reset()
  133. self.send_command(0xD2)
  134. self.send_data(0x3F)
  135. self.send_command(0x00)
  136. self.send_data (0x6F) #from outside
  137. self.send_command(0x01) #power setting
  138. self.send_data (0x03)
  139. self.send_data (0x00)
  140. self.send_data (0x2b)
  141. self.send_data (0x2b)
  142. self.send_command(0x06) #Configuring the charge pump
  143. self.send_data(0x3f)
  144. self.send_command(0x2A) #Setting XON and the options of LUT
  145. self.send_data(0x00)
  146. self.send_data(0x00)
  147. self.send_command(0x30) #Set the clock frequency
  148. self.send_data(0x17) #50Hz
  149. self.send_command(0x50) #Set VCOM and data output interval
  150. self.send_data(0x57)
  151. self.send_command(0x60) #Set The non-overlapping period of Gate and Source.
  152. self.send_data(0x22)
  153. self.send_command(0x61) #resolution setting
  154. self.send_data (0x50) #source 128
  155. self.send_data (0x80)
  156. self.send_command(0x82) #sets VCOM_DC value
  157. self.send_data(0x12) #-1v
  158. self.send_command(0xe3)#Set POWER SAVING
  159. self.send_data(0x33)
  160. self.SetFulltReg()
  161. self.send_command(0x04) #power on
  162. self.ReadBusy()
  163. # EPD hardware init end
  164. return 0
  165. def Partial_Init(self):
  166. self.reset()
  167. self.send_command(0xD2)
  168. self.send_data(0x3F)
  169. self.send_command(0x00)
  170. self.send_data (0x6F) #from outside
  171. self.send_command(0x01) #power setting
  172. self.send_data (0x03)
  173. self.send_data (0x00)
  174. self.send_data (0x2b)
  175. self.send_data (0x2b)
  176. self.send_command(0x06) #Configuring the charge pump
  177. self.send_data(0x3f)
  178. self.send_command(0x2A) #Setting XON and the options of LUT
  179. self.send_data(0x00)
  180. self.send_data(0x00)
  181. self.send_command(0x30) #Set the clock frequency
  182. self.send_data(0x17)
  183. self.send_command(0x50) #Set VCOM and data output interval
  184. self.send_data(0xf2)
  185. self.send_command(0x60) #Set The non-overlapping period of Gate and Source.
  186. self.send_data(0x22)
  187. self.send_command(0x82) #Set VCOM_DC value
  188. self.send_data(0x12)#-1v
  189. self.send_command(0xe3)#Set POWER SAVING
  190. self.send_data(0x33)
  191. self.SetPartReg()
  192. self.send_command(0x04)#Set POWER SAVING
  193. self.ReadBusy()
  194. # EPD hardware init end
  195. return 0
  196. def getbuffer(self, image):
  197. buf = [0xFF] * (int(self.width / 8) * self.height)
  198. image_monocolor = image.convert('1')
  199. imwidth, imheight = image_monocolor.size
  200. pixels = image_monocolor.load()
  201. if(imwidth == self.width and imheight == self.height):
  202. logging.debug("Horizontal")
  203. for y in range(imheight):
  204. for x in range(imwidth):
  205. # Set the bits for the column of pixels at the current position.
  206. if pixels[x, y] == 0:
  207. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  208. elif(imwidth == self.height and imheight == self.width):
  209. logging.debug("Vertical")
  210. for y in range(imheight):
  211. for x in range(imwidth):
  212. newx = y
  213. newy = self.height - x - 1
  214. if pixels[x, y] == 0:
  215. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  216. return buf
  217. def Display(self, image):
  218. if (image == None):
  219. return
  220. # Width = (self.width % 8 == 0)? (self.width / 8 ): (self.width / 8 + 1)
  221. if(self.width % 8 == 0):
  222. Width = self.width / 8
  223. else:
  224. Width = self.width / 8 + 1
  225. self.send_command(0x10)
  226. for j in range(0, self.height):
  227. for i in range(0, int(Width)):
  228. self.send_data(0xff)
  229. self.send_command(0x13)
  230. for j in range(0, self.height):
  231. for i in range(0, int(Width)):
  232. self.send_data(image[i + j * int(Width)])
  233. self.TurnOnDisplay()
  234. def Clear(self):
  235. # Width = (self.width % 8 == 0)? (self.width / 8 ): (self.width / 8 + 1)
  236. if(self.width % 8 == 0):
  237. Width = self.width / 8
  238. else:
  239. Width = self.width / 8 + 1
  240. Height = self.height
  241. self.send_command(0x10)
  242. for j in range(0, Height):
  243. for i in range(0, int(Width)):
  244. self.send_data(0x00)
  245. self.send_command(0x13)
  246. for j in range(0, Height):
  247. for i in range(0, int(Width)):
  248. self.send_data(0xff)
  249. self.TurnOnDisplay()
  250. def DisplayPartial(self, old_Image, Image):
  251. # Set partial Windows */
  252. self.send_command(0x91) #This command makes the display enter partial mode
  253. self.send_command(0x90) #resolution setting
  254. self.send_data(0) #x-start
  255. self.send_data(79) #x-end
  256. self.send_data(0)
  257. self.send_data(127) #y-end
  258. self.send_data(0x00)
  259. # Width = (self.width % 8 == 0)? (self.width / 8 ): (self.width / 8 + 1)
  260. if(self.width % 8 == 0):
  261. Width = self.width / 8
  262. else:
  263. Width = self.width / 8 + 1
  264. Height = self.height
  265. # send data
  266. self.send_command(0x10)
  267. for j in range(0, Height):
  268. for i in range(0, int(Width)):
  269. self.send_data(old_Image[i + j * int(Width)])
  270. self.send_command(0x13)
  271. for j in range(0, Height):
  272. for i in range(0, int(Width)):
  273. self.send_data(Image[i + j * int(Width)])
  274. # Set partial refresh
  275. self.TurnOnDisplay()
  276. def Sleep(self):
  277. self.send_command(0x50)
  278. self.send_data(0xf7)
  279. self.send_command(0x02)
  280. self.ReadBusy()
  281. self.send_command(0x07)
  282. self.send_data(0xA5)
  283. epdconfig.delay_ms(200)
  284. epdconfig.module_exit()
  285. ### END OF FILE ###