epd1in02.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. logger = logging.getLogger(__name__)
  36. class EPD:
  37. def __init__(self):
  38. self.reset_pin = epdconfig.RST_PIN
  39. self.dc_pin = epdconfig.DC_PIN
  40. self.busy_pin = epdconfig.BUSY_PIN
  41. self.cs_pin = epdconfig.CS_PIN
  42. self.width = EPD_WIDTH
  43. self.height = EPD_HEIGHT
  44. #full screen update LUT
  45. lut_w1 =[
  46. 0x60, 0x5A, 0x5A, 0x00, 0x00, 0x01,
  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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. ]
  54. lut_b1 =[
  55. 0x90, 0x5A, 0x5A, 0x00, 0x00, 0x01,
  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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. ]
  63. # partial screen update LUT
  64. lut_w = [
  65. 0x60, 0x01, 0x01, 0x00, 0x00, 0x01,
  66. 0x80, 0x1f, 0x00, 0x00, 0x00, 0x01,
  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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. ]
  73. lut_b = [
  74. 0x90, 0x01, 0x01, 0x00, 0x00, 0x01,
  75. 0x40, 0x1f, 0x00, 0x00, 0x00, 0x01,
  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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. ]
  82. # Hardware reset
  83. def reset(self):
  84. epdconfig.digital_write(self.reset_pin, 1)
  85. epdconfig.delay_ms(200)
  86. epdconfig.digital_write(self.reset_pin, 0) # module reset
  87. epdconfig.delay_ms(2)
  88. epdconfig.digital_write(self.reset_pin, 1)
  89. epdconfig.delay_ms(200)
  90. def send_command(self, command):
  91. epdconfig.digital_write(self.dc_pin, 0)
  92. epdconfig.digital_write(self.cs_pin, 0)
  93. epdconfig.spi_writebyte([command])
  94. epdconfig.digital_write(self.cs_pin, 1)
  95. def send_data(self, data):
  96. epdconfig.digital_write(self.dc_pin, 1)
  97. epdconfig.digital_write(self.cs_pin, 0)
  98. epdconfig.spi_writebyte([data])
  99. epdconfig.digital_write(self.cs_pin, 1)
  100. def ReadBusy(self):
  101. logger.debug("e-Paper busy")
  102. self.send_command(0x71)
  103. busy = epdconfig.digital_read(self.busy_pin)
  104. busy =not(busy & 0x01)
  105. while(busy):
  106. self.send_command(0x71)
  107. busy = epdconfig.digital_read(self.busy_pin)
  108. busy =not(busy & 0x01)
  109. epdconfig.delay_ms(800)
  110. logger.debug("e-Paper busy release")
  111. def TurnOnDisplay(self):
  112. self.send_command(0x12)
  113. epdconfig.delay_ms(10)
  114. self.ReadBusy()
  115. def SetFulltReg(self):
  116. self.send_command(0x23)
  117. for count in range(0, 42):
  118. self.send_data(self.lut_w1[count])
  119. self.send_command(0x24)
  120. for count in range(0, 42):
  121. self.send_data(self.lut_b1[count])
  122. def SetPartReg(self):
  123. self.send_command(0x23)
  124. for count in range(0, 42):
  125. self.send_data(self.lut_w[count])
  126. self.send_command(0x24)
  127. for count in range(0, 42):
  128. self.send_data(self.lut_b[count])
  129. def Init(self):
  130. if (epdconfig.module_init() != 0):
  131. return -1
  132. # EPD hardware init start
  133. self.reset()
  134. self.send_command(0xD2)
  135. self.send_data(0x3F)
  136. self.send_command(0x00)
  137. self.send_data (0x6F) #from outside
  138. self.send_command(0x01) #power setting
  139. self.send_data (0x03)
  140. self.send_data (0x00)
  141. self.send_data (0x2b)
  142. self.send_data (0x2b)
  143. self.send_command(0x06) #Configuring the charge pump
  144. self.send_data(0x3f)
  145. self.send_command(0x2A) #Setting XON and the options of LUT
  146. self.send_data(0x00)
  147. self.send_data(0x00)
  148. self.send_command(0x30) #Set the clock frequency
  149. self.send_data(0x17) #50Hz
  150. self.send_command(0x50) #Set VCOM and data output interval
  151. self.send_data(0x57)
  152. self.send_command(0x60) #Set The non-overlapping period of Gate and Source.
  153. self.send_data(0x22)
  154. self.send_command(0x61) #resolution setting
  155. self.send_data (0x50) #source 128
  156. self.send_data (0x80)
  157. self.send_command(0x82) #sets VCOM_DC value
  158. self.send_data(0x12) #-1v
  159. self.send_command(0xe3)#Set POWER SAVING
  160. self.send_data(0x33)
  161. self.SetFulltReg()
  162. self.send_command(0x04) #power on
  163. self.ReadBusy()
  164. # EPD hardware init end
  165. return 0
  166. def Partial_Init(self):
  167. self.reset()
  168. self.send_command(0xD2)
  169. self.send_data(0x3F)
  170. self.send_command(0x00)
  171. self.send_data (0x6F) #from outside
  172. self.send_command(0x01) #power setting
  173. self.send_data (0x03)
  174. self.send_data (0x00)
  175. self.send_data (0x2b)
  176. self.send_data (0x2b)
  177. self.send_command(0x06) #Configuring the charge pump
  178. self.send_data(0x3f)
  179. self.send_command(0x2A) #Setting XON and the options of LUT
  180. self.send_data(0x00)
  181. self.send_data(0x00)
  182. self.send_command(0x30) #Set the clock frequency
  183. self.send_data(0x17)
  184. self.send_command(0x50) #Set VCOM and data output interval
  185. self.send_data(0xf2)
  186. self.send_command(0x60) #Set The non-overlapping period of Gate and Source.
  187. self.send_data(0x22)
  188. self.send_command(0x82) #Set VCOM_DC value
  189. self.send_data(0x12)#-1v
  190. self.send_command(0xe3)#Set POWER SAVING
  191. self.send_data(0x33)
  192. self.SetPartReg()
  193. self.send_command(0x04)#Set POWER SAVING
  194. self.ReadBusy()
  195. # EPD hardware init end
  196. return 0
  197. def getbuffer(self, image):
  198. buf = [0xFF] * (int(self.width / 8) * self.height)
  199. image_monocolor = image.convert('1')
  200. imwidth, imheight = image_monocolor.size
  201. pixels = image_monocolor.load()
  202. if(imwidth == self.width and imheight == self.height):
  203. logger.debug("Horizontal")
  204. for y in range(imheight):
  205. for x in range(imwidth):
  206. # Set the bits for the column of pixels at the current position.
  207. if pixels[x, y] == 0:
  208. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  209. elif(imwidth == self.height and imheight == self.width):
  210. logger.debug("Vertical")
  211. for y in range(imheight):
  212. for x in range(imwidth):
  213. newx = y
  214. newy = self.height - x - 1
  215. if pixels[x, y] == 0:
  216. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  217. return buf
  218. def Display(self, image):
  219. if (image == None):
  220. return
  221. # Width = (self.width % 8 == 0)? (self.width / 8 ): (self.width / 8 + 1)
  222. if(self.width % 8 == 0):
  223. Width = self.width / 8
  224. else:
  225. Width = self.width / 8 + 1
  226. self.send_command(0x10)
  227. for j in range(0, self.height):
  228. for i in range(0, int(Width)):
  229. self.send_data(0xff)
  230. self.send_command(0x13)
  231. for j in range(0, self.height):
  232. for i in range(0, int(Width)):
  233. self.send_data(image[i + j * int(Width)])
  234. self.TurnOnDisplay()
  235. def Clear(self):
  236. # Width = (self.width % 8 == 0)? (self.width / 8 ): (self.width / 8 + 1)
  237. if(self.width % 8 == 0):
  238. Width = self.width / 8
  239. else:
  240. Width = self.width / 8 + 1
  241. Height = self.height
  242. self.send_command(0x10)
  243. for j in range(0, Height):
  244. for i in range(0, int(Width)):
  245. self.send_data(0x00)
  246. self.send_command(0x13)
  247. for j in range(0, Height):
  248. for i in range(0, int(Width)):
  249. self.send_data(0xff)
  250. self.TurnOnDisplay()
  251. def DisplayPartial(self, old_Image, Image):
  252. # Set partial Windows */
  253. self.send_command(0x91) #This command makes the display enter partial mode
  254. self.send_command(0x90) #resolution setting
  255. self.send_data(0) #x-start
  256. self.send_data(79) #x-end
  257. self.send_data(0)
  258. self.send_data(127) #y-end
  259. self.send_data(0x00)
  260. # Width = (self.width % 8 == 0)? (self.width / 8 ): (self.width / 8 + 1)
  261. if(self.width % 8 == 0):
  262. Width = self.width / 8
  263. else:
  264. Width = self.width / 8 + 1
  265. Height = self.height
  266. # send data
  267. self.send_command(0x10)
  268. for j in range(0, Height):
  269. for i in range(0, int(Width)):
  270. self.send_data(old_Image[i + j * int(Width)])
  271. self.send_command(0x13)
  272. for j in range(0, Height):
  273. for i in range(0, int(Width)):
  274. self.send_data(Image[i + j * int(Width)])
  275. # Set partial refresh
  276. self.TurnOnDisplay()
  277. def Sleep(self):
  278. self.send_command(0x50)
  279. self.send_data(0xf7)
  280. self.send_command(0x02)
  281. self.ReadBusy()
  282. self.send_command(0x07)
  283. self.send_data(0xA5)
  284. epdconfig.delay_ms(200)
  285. epdconfig.delay_ms(2000)
  286. epdconfig.module_exit()
  287. ### END OF FILE ###