epd7in5_V2.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 : 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 = 800
  33. EPD_HEIGHT = 480
  34. GRAY1 = 0xff #white
  35. GRAY2 = 0xC0
  36. GRAY3 = 0x80 #gray
  37. GRAY4 = 0x00 #Blackest
  38. logger = logging.getLogger(__name__)
  39. class EPD:
  40. def __init__(self):
  41. self.reset_pin = epdconfig.RST_PIN
  42. self.dc_pin = epdconfig.DC_PIN
  43. self.busy_pin = epdconfig.BUSY_PIN
  44. self.cs_pin = epdconfig.CS_PIN
  45. self.width = EPD_WIDTH
  46. self.height = EPD_HEIGHT
  47. self.GRAY1 = GRAY1 #white
  48. self.GRAY2 = GRAY2
  49. self.GRAY3 = GRAY3 #gray
  50. self.GRAY4 = GRAY4 #Blackest
  51. # Hardware reset
  52. def reset(self):
  53. epdconfig.digital_write(self.reset_pin, 1)
  54. epdconfig.delay_ms(20)
  55. epdconfig.digital_write(self.reset_pin, 0)
  56. epdconfig.delay_ms(2)
  57. epdconfig.digital_write(self.reset_pin, 1)
  58. epdconfig.delay_ms(20)
  59. def send_command(self, command):
  60. epdconfig.digital_write(self.dc_pin, 0)
  61. epdconfig.digital_write(self.cs_pin, 0)
  62. epdconfig.spi_writebyte([command])
  63. epdconfig.digital_write(self.cs_pin, 1)
  64. def send_data(self, data):
  65. epdconfig.digital_write(self.dc_pin, 1)
  66. epdconfig.digital_write(self.cs_pin, 0)
  67. epdconfig.spi_writebyte([data])
  68. epdconfig.digital_write(self.cs_pin, 1)
  69. def send_data2(self, data):
  70. epdconfig.digital_write(self.dc_pin, 1)
  71. epdconfig.digital_write(self.cs_pin, 0)
  72. epdconfig.SPI.writebytes2(data)
  73. epdconfig.digital_write(self.cs_pin, 1)
  74. def ReadBusy(self):
  75. logger.debug("e-Paper busy")
  76. self.send_command(0x71)
  77. busy = epdconfig.digital_read(self.busy_pin)
  78. while(busy == 0):
  79. self.send_command(0x71)
  80. busy = epdconfig.digital_read(self.busy_pin)
  81. epdconfig.delay_ms(20)
  82. logger.debug("e-Paper busy release")
  83. def init(self):
  84. if (epdconfig.module_init() != 0):
  85. return -1
  86. # EPD hardware init start
  87. self.reset()
  88. self.send_command(0x06) # btst
  89. self.send_data(0x17)
  90. self.send_data(0x17)
  91. self.send_data(0x28) # If an exception is displayed, try using 0x38
  92. self.send_data(0x17)
  93. self.send_command(0x01) #POWER SETTING
  94. self.send_data(0x07)
  95. self.send_data(0x07) #VGH=20V,VGL=-20V
  96. self.send_data(0x28) #VDH=15V
  97. self.send_data(0x17) #VDL=-15V
  98. self.send_command(0x04) #POWER ON
  99. epdconfig.delay_ms(100)
  100. self.ReadBusy()
  101. self.send_command(0X00) #PANNEL SETTING
  102. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  103. self.send_command(0x61) #tres
  104. self.send_data(0x03) #source 800
  105. self.send_data(0x20)
  106. self.send_data(0x01) #gate 480
  107. self.send_data(0xE0)
  108. self.send_command(0X15)
  109. self.send_data(0x00)
  110. # If the screen appears gray, use the annotated initialization command
  111. self.send_command(0X50)
  112. self.send_data(0x10)
  113. self.send_data(0x07)
  114. # self.send_command(0X50)
  115. # self.send_data(0x10)
  116. # self.send_data(0x17)
  117. # self.send_command(0X52)
  118. # self.send_data(0x03)
  119. self.send_command(0X60) #TCON SETTING
  120. self.send_data(0x22)
  121. # EPD hardware init end
  122. return 0
  123. def init_fast(self):
  124. if (epdconfig.module_init() != 0):
  125. return -1
  126. # EPD hardware init start
  127. self.reset()
  128. self.send_command(0X00) #PANNEL SETTING
  129. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  130. # If the screen appears gray, use the annotated initialization command
  131. self.send_command(0X50)
  132. self.send_data(0x10)
  133. self.send_data(0x07)
  134. # self.send_command(0X50)
  135. # self.send_data(0x10)
  136. # self.send_data(0x17)
  137. # self.send_command(0X52)
  138. # self.send_data(0x03)
  139. self.send_command(0x04) #POWER ON
  140. epdconfig.delay_ms(100)
  141. self.ReadBusy() #waiting for the electronic paper IC to release the idle signal
  142. #Enhanced display drive(Add 0x06 command)
  143. self.send_command(0x06) #Booster Soft Start
  144. self.send_data (0x27)
  145. self.send_data (0x27)
  146. self.send_data (0x18)
  147. self.send_data (0x17)
  148. self.send_command(0xE0)
  149. self.send_data(0x02)
  150. self.send_command(0xE5)
  151. self.send_data(0x5A)
  152. # EPD hardware init end
  153. return 0
  154. def init_part(self):
  155. if (epdconfig.module_init() != 0):
  156. return -1
  157. # EPD hardware init start
  158. self.reset()
  159. self.send_command(0X00) #PANNEL SETTING
  160. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  161. self.send_command(0x04) #POWER ON
  162. epdconfig.delay_ms(100)
  163. self.ReadBusy() #waiting for the electronic paper IC to release the idle signal
  164. self.send_command(0xE0)
  165. self.send_data(0x02)
  166. self.send_command(0xE5)
  167. self.send_data(0x6E)
  168. # EPD hardware init end
  169. return 0
  170. # The feature will only be available on screens sold after 24/10/23
  171. def init_4Gray(self):
  172. if (epdconfig.module_init() != 0):
  173. return -1
  174. # EPD hardware init start
  175. self.reset()
  176. self.send_command(0X00) #PANNEL SETTING
  177. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  178. self.send_command(0X50)
  179. self.send_data(0x10)
  180. self.send_data(0x07)
  181. self.send_command(0x04) #POWER ON
  182. epdconfig.delay_ms(100)
  183. self.ReadBusy() #waiting for the electronic paper IC to release the idle signal
  184. #Enhanced display drive(Add 0x06 command)
  185. self.send_command(0x06) #Booster Soft Start
  186. self.send_data (0x27)
  187. self.send_data (0x27)
  188. self.send_data (0x18)
  189. self.send_data (0x17)
  190. self.send_command(0xE0)
  191. self.send_data(0x02)
  192. self.send_command(0xE5)
  193. self.send_data(0x5F)
  194. # EPD hardware init end
  195. return 0
  196. def getbuffer(self, image):
  197. img = image
  198. imwidth, imheight = img.size
  199. if(imwidth == self.width and imheight == self.height):
  200. img = img.convert('1')
  201. elif(imwidth == self.height and imheight == self.width):
  202. # image has correct dimensions, but needs to be rotated
  203. img = img.rotate(90, expand=True).convert('1')
  204. else:
  205. logger.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
  206. # return a blank buffer
  207. return [0x00] * (int(self.width/8) * self.height)
  208. buf = bytearray(img.tobytes('raw'))
  209. # The bytes need to be inverted, because in the PIL world 0=black and 1=white, but
  210. # in the e-paper world 0=white and 1=black.
  211. for i in range(len(buf)):
  212. buf[i] ^= 0xFF
  213. return buf
  214. def getbuffer_4Gray(self, image):
  215. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  216. buf = [0xFF] * (int(self.width / 4) * self.height)
  217. image_monocolor = image.convert('L')
  218. imwidth, imheight = image_monocolor.size
  219. pixels = image_monocolor.load()
  220. i=0
  221. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  222. if(imwidth == self.width and imheight == self.height):
  223. logger.debug("Vertical")
  224. for y in range(imheight):
  225. for x in range(imwidth):
  226. # Set the bits for the column of pixels at the current position.
  227. if(pixels[x, y] == 0xC0):
  228. pixels[x, y] = 0x80
  229. elif (pixels[x, y] == 0x80):
  230. pixels[x, y] = 0x40
  231. i= i+1
  232. if(i%4 == 0):
  233. buf[int((x + (y * self.width))/4)] = ((pixels[x-3, y]&0xc0) | (pixels[x-2, y]&0xc0)>>2 | (pixels[x-1, y]&0xc0)>>4 | (pixels[x, y]&0xc0)>>6)
  234. elif(imwidth == self.height and imheight == self.width):
  235. logger.debug("Horizontal")
  236. for x in range(imwidth):
  237. for y in range(imheight):
  238. newx = y
  239. newy = self.height - x - 1
  240. if(pixels[x, y] == 0xC0):
  241. pixels[x, y] = 0x80
  242. elif (pixels[x, y] == 0x80):
  243. pixels[x, y] = 0x40
  244. i= i+1
  245. if(i%4 == 0):
  246. buf[int((newx + (newy * self.width))/4)] = ((pixels[x, y-3]&0xc0) | (pixels[x, y-2]&0xc0)>>2 | (pixels[x, y-1]&0xc0)>>4 | (pixels[x, y]&0xc0)>>6)
  247. return buf
  248. def display(self, image):
  249. if(self.width % 8 == 0):
  250. Width = self.width // 8
  251. else:
  252. Width = self.width // 8 +1
  253. Height = self.height
  254. image1 = [0xFF] * int(self.width * self.height / 8)
  255. for j in range(Height):
  256. for i in range(Width):
  257. image1[i + j * Width] = ~image[i + j * Width]
  258. self.send_command(0x10)
  259. self.send_data2(image1)
  260. self.send_command(0x13)
  261. self.send_data2(image)
  262. self.send_command(0x12)
  263. epdconfig.delay_ms(100)
  264. self.ReadBusy()
  265. def Clear(self):
  266. self.send_command(0x10)
  267. self.send_data2([0xFF] * int(self.width * self.height / 8))
  268. self.send_command(0x13)
  269. self.send_data2([0x00] * int(self.width * self.height / 8))
  270. self.send_command(0x12)
  271. epdconfig.delay_ms(100)
  272. self.ReadBusy()
  273. def display_Partial(self, Image, Xstart, Ystart, Xend, Yend):
  274. if((Xstart % 8 + Xend % 8 == 8 & Xstart % 8 > Xend % 8) | Xstart % 8 + Xend % 8 == 0 | (Xend - Xstart)%8 == 0):
  275. Xstart = Xstart // 8 * 8
  276. Xend = Xend // 8 * 8
  277. else:
  278. Xstart = Xstart // 8 * 8
  279. if Xend % 8 == 0:
  280. Xend = Xend // 8 * 8
  281. else:
  282. Xend = Xend // 8 * 8 + 1
  283. Width = (Xend - Xstart) // 8
  284. Height = Yend - Ystart
  285. self.send_command(0x50)
  286. self.send_data(0xA9)
  287. self.send_data(0x07)
  288. self.send_command(0x91) #This command makes the display enter partial mode
  289. self.send_command(0x90) #resolution setting
  290. self.send_data (Xstart//256)
  291. self.send_data (Xstart%256) #x-start
  292. self.send_data ((Xend-1)//256)
  293. self.send_data ((Xend-1)%256) #x-end
  294. self.send_data (Ystart//256) #
  295. self.send_data (Ystart%256) #y-start
  296. self.send_data ((Yend-1)//256)
  297. self.send_data ((Yend-1)%256) #y-end
  298. self.send_data (0x01)
  299. image1 = [0xFF] * int(self.width * self.height / 8)
  300. for j in range(Height):
  301. for i in range(Width):
  302. image1[i + j * Width] = ~Image[i + j * Width]
  303. self.send_command(0x13) #Write Black and White image to RAM
  304. self.send_data2(image1)
  305. self.send_command(0x12)
  306. epdconfig.delay_ms(100)
  307. self.ReadBusy()
  308. def display_4Gray(self, image):
  309. self.send_command(0x10)
  310. for i in range(0, 48000):
  311. temp3=0
  312. for j in range(0, 2):
  313. temp1 = image[i*2+j]
  314. for k in range(0, 2):
  315. temp2 = temp1&0xC0
  316. if(temp2 == 0xC0):
  317. temp3 |= 0x00
  318. elif(temp2 == 0x00):
  319. temp3 |= 0x01
  320. elif(temp2 == 0x80):
  321. temp3 |= 0x01
  322. else: #0x40
  323. temp3 |= 0x00
  324. temp3 <<= 1
  325. temp1 <<= 2
  326. temp2 = temp1&0xC0
  327. if(temp2 == 0xC0):
  328. temp3 |= 0x00
  329. elif(temp2 == 0x00):
  330. temp3 |= 0x01
  331. elif(temp2 == 0x80):
  332. temp3 |= 0x01
  333. else : #0x40
  334. temp3 |= 0x00
  335. if(j!=1 or k!=1):
  336. temp3 <<= 1
  337. temp1 <<= 2
  338. self.send_data(temp3)
  339. self.send_command(0x13)
  340. for i in range(0, 48000):
  341. temp3=0
  342. for j in range(0, 2):
  343. temp1 = image[i*2+j]
  344. for k in range(0, 2):
  345. temp2 = temp1&0xC0
  346. if(temp2 == 0xC0):
  347. temp3 |= 0x00
  348. elif(temp2 == 0x00):
  349. temp3 |= 0x01
  350. elif(temp2 == 0x80):
  351. temp3 |= 0x00
  352. else: #0x40
  353. temp3 |= 0x01
  354. temp3 <<= 1
  355. temp1 <<= 2
  356. temp2 = temp1&0xC0
  357. if(temp2 == 0xC0):
  358. temp3 |= 0x00
  359. elif(temp2 == 0x00):
  360. temp3 |= 0x01
  361. elif(temp2 == 0x80):
  362. temp3 |= 0x00
  363. else: #0x40
  364. temp3 |= 0x01
  365. if(j!=1 or k!=1):
  366. temp3 <<= 1
  367. temp1 <<= 2
  368. self.send_data(temp3)
  369. self.send_command(0x12)
  370. epdconfig.delay_ms(100)
  371. self.ReadBusy()
  372. def sleep(self):
  373. self.send_command(0x50)
  374. self.send_data(0XF7)
  375. self.send_command(0x02) # POWER_OFF
  376. self.ReadBusy()
  377. self.send_command(0x07) # DEEP_SLEEP
  378. self.send_data(0XA5)
  379. epdconfig.delay_ms(2000)
  380. epdconfig.module_exit()
  381. ### END OF FILE ###