epd13in3k.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. # *****************************************************************************
  2. # * | File : epd13in3k.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2023-09-08
  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 = 960
  33. EPD_HEIGHT = 680
  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. self.Lut_Partial=[
  52. 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x2A, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x15, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x01, 0x01, 0x01, 0x00,
  58. 0x0A, 0x00, 0x05, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x01, 0x01,
  67. 0x22, 0x22, 0x22, 0x22, 0x22,
  68. 0x17, 0x41, 0xA8, 0x32, 0x18,
  69. 0x00, 0x00,]
  70. self.LUT_DATA_4Gray =[
  71. 0x80, 0x48, 0x4A, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x0A, 0x48, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x88, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0xA8, 0x48, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x07, 0x23, 0x17, 0x02, 0x00,
  77. 0x05, 0x01, 0x05, 0x01, 0x02,
  78. 0x08, 0x02, 0x01, 0x04, 0x04,
  79. 0x00, 0x02, 0x00, 0x02, 0x01,
  80. 0x00, 0x00, 0x00, 0x00, 0x00,
  81. 0x00, 0x00, 0x00, 0x00, 0x00,
  82. 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x01,
  86. 0x22, 0x22, 0x22, 0x22, 0x22,
  87. 0x17, 0x41, 0xA8, 0x32, 0x30,
  88. 0x00, 0x00,]
  89. if (epdconfig.module_init() != 0):
  90. return -1
  91. # Hardware reset
  92. def reset(self):
  93. epdconfig.digital_write(self.reset_pin, 1)
  94. epdconfig.delay_ms(20)
  95. epdconfig.digital_write(self.reset_pin, 0)
  96. epdconfig.delay_ms(2)
  97. epdconfig.digital_write(self.reset_pin, 1)
  98. epdconfig.delay_ms(20)
  99. def send_command(self, command):
  100. epdconfig.digital_write(self.dc_pin, 0)
  101. epdconfig.digital_write(self.cs_pin, 0)
  102. epdconfig.spi_writebyte([command])
  103. epdconfig.digital_write(self.cs_pin, 1)
  104. def send_data(self, data):
  105. epdconfig.digital_write(self.dc_pin, 1)
  106. epdconfig.digital_write(self.cs_pin, 0)
  107. epdconfig.spi_writebyte([data])
  108. epdconfig.digital_write(self.cs_pin, 1)
  109. def send_data2(self, data):
  110. epdconfig.digital_write(self.dc_pin, 1)
  111. epdconfig.digital_write(self.cs_pin, 0)
  112. epdconfig.SPI.writebytes2(data)
  113. epdconfig.digital_write(self.cs_pin, 1)
  114. def ReadBusy(self):
  115. logger.debug("e-Paper busy")
  116. busy = epdconfig.digital_read(self.busy_pin)
  117. while(busy == 1):
  118. busy = epdconfig.digital_read(self.busy_pin)
  119. epdconfig.delay_ms(20)
  120. epdconfig.delay_ms(20)
  121. logger.debug("e-Paper busy release")
  122. def TurnOnDisplay(self):
  123. self.send_command(0x22) #Display Update Control
  124. self.send_data(0xF7)
  125. self.send_command(0x20) #Activate Display Update Sequence
  126. self.ReadBusy()
  127. def TurnOnDisplay_Part(self):
  128. self.send_command(0x22) #Display Update Control
  129. self.send_data(0xCF)
  130. self.send_command(0x20) #Activate Display Update Sequence
  131. self.ReadBusy()
  132. def TurnOnDisplay_4GRAY(self):
  133. self.send_command(0x22) #Display Update Control
  134. self.send_data(0xC7)
  135. self.send_command(0x20) #Activate Display Update Sequence
  136. self.ReadBusy()
  137. def Lut(self, LUT):
  138. self.send_command(0x32)
  139. for i in range(105):
  140. self.send_data(LUT[i])
  141. self.send_command(0x03)
  142. self.send_data(LUT[105])
  143. self.send_command(0x04)
  144. self.send_data(LUT[106])
  145. self.send_data(LUT[107])
  146. self.send_data(LUT[108])
  147. self.send_command(0x2C)
  148. self.send_data(LUT[109])
  149. def init(self):
  150. # EPD hardware init start
  151. self.reset()
  152. self.ReadBusy()
  153. self.send_command(0x12) #SWRESET
  154. self.ReadBusy()
  155. self.send_command(0x0C)
  156. self.send_data(0xAE)
  157. self.send_data(0xC7)
  158. self.send_data(0xC3)
  159. self.send_data(0xC0)
  160. self.send_data(0x80)
  161. self.send_command(0x01)
  162. self.send_data(0xA7)
  163. self.send_data(0x02)
  164. self.send_data(0x00)
  165. self.send_command(0x11)
  166. self.send_data(0x03)
  167. self.send_command(0x44)
  168. self.send_data(0x00)
  169. self.send_data(0x00)
  170. self.send_data(0xBF)
  171. self.send_data(0x03)
  172. self.send_command(0x45)
  173. self.send_data(0x00)
  174. self.send_data(0x00)
  175. self.send_data(0xA7)
  176. self.send_data(0x02)
  177. self.send_command(0x3C)
  178. self.send_data(0x05)
  179. self.send_command(0x18)
  180. self.send_data(0x80)
  181. self.send_command(0x4E)
  182. self.send_data(0x00)
  183. self.send_data(0x00)
  184. self.send_command(0x4F)
  185. self.send_data(0x00)
  186. self.send_data(0x00)
  187. # EPD hardware init end
  188. return 0
  189. def init_Part(self):
  190. self.reset()
  191. self.send_command(0x3C)
  192. self.send_data(0x80)
  193. self.Lut(self.Lut_Partial)
  194. self.send_command(0x37)
  195. self.send_data(0x00)
  196. self.send_data(0x00)
  197. self.send_data(0x00)
  198. self.send_data(0x00)
  199. self.send_data(0x00)
  200. self.send_data(0x40)
  201. self.send_data(0x00)
  202. self.send_data(0x00)
  203. self.send_data(0x00)
  204. self.send_data(0x00)
  205. self.send_command(0x3C)
  206. self.send_data(0x80)
  207. self.send_command(0x22)
  208. self.send_data(0xC0)
  209. self.send_command(0x20)
  210. self.ReadBusy()
  211. def init_4GRAY(self):
  212. self.reset()
  213. self.ReadBusy()
  214. self.send_command(0x12)
  215. self.ReadBusy()
  216. self.send_command(0x0C)
  217. self.send_data(0xAE)
  218. self.send_data(0xC7)
  219. self.send_data(0xC3)
  220. self.send_data(0xC0)
  221. self.send_data(0x80)
  222. self.send_command(0x01)
  223. self.send_data(0xA7)
  224. self.send_data(0x02)
  225. self.send_data(0x00)
  226. self.send_command(0x11)
  227. self.send_data(0x03)
  228. self.send_command(0x44)
  229. self.send_data(0x00)
  230. self.send_data(0x00)
  231. self.send_data(0xBF)
  232. self.send_data(0x03)
  233. self.send_command(0x45)
  234. self.send_data(0x00)
  235. self.send_data(0x00)
  236. self.send_data(0xA7)
  237. self.send_data(0x02)
  238. self.send_command(0x3C)
  239. self.send_data(0x00)
  240. self.send_command(0x18)
  241. self.send_data(0x80)
  242. self.send_command(0x4E)
  243. self.send_data(0x00)
  244. self.send_data(0x00)
  245. self.send_command(0x4F)
  246. self.send_data(0x00)
  247. self.send_data(0x00)
  248. self.Lut(self.LUT_DATA_4Gray)
  249. self.ReadBusy()
  250. def getbuffer(self, image):
  251. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  252. buf = [0xFF] * (int(self.width / 8) * self.height)
  253. image_monocolor = image.convert('1')
  254. imwidth, imheight = image_monocolor.size
  255. pixels = image_monocolor.load()
  256. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  257. if imwidth == self.width and imheight == self.height:
  258. logger.debug("Horizontal")
  259. for y in range(imheight):
  260. for x in range(imwidth):
  261. # Set the bits for the column of pixels at the current position.
  262. if pixels[x, y] == 0:
  263. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  264. elif imwidth == self.height and imheight == self.width:
  265. logger.debug("Vertical")
  266. for y in range(imheight):
  267. for x in range(imwidth):
  268. newx = y
  269. newy = self.height - x - 1
  270. if pixels[x, y] == 0:
  271. buf[int((newx + newy * self.width) / 8)] &= ~(0x80 >> (y % 8))
  272. return buf
  273. def getbuffer_4Gray(self, image):
  274. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  275. buf = [0xFF] * (int(self.width / 4) * self.height)
  276. image_monocolor = image.convert('L')
  277. imwidth, imheight = image_monocolor.size
  278. pixels = image_monocolor.load()
  279. i=0
  280. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  281. if(imwidth == self.width and imheight == self.height):
  282. logger.debug("Vertical")
  283. for y in range(imheight):
  284. for x in range(imwidth):
  285. # Set the bits for the column of pixels at the current position.
  286. if(pixels[x, y] == 0xC0):
  287. pixels[x, y] = 0x80
  288. elif (pixels[x, y] == 0x80):
  289. pixels[x, y] = 0x40
  290. i= i+1
  291. if(i%4 == 0):
  292. 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)
  293. elif(imwidth == self.height and imheight == self.width):
  294. logger.debug("Horizontal")
  295. for x in range(imwidth):
  296. for y in range(imheight):
  297. newx = y
  298. newy = self.height - x - 1
  299. if(pixels[x, y] == 0xC0):
  300. pixels[x, y] = 0x80
  301. elif (pixels[x, y] == 0x80):
  302. pixels[x, y] = 0x40
  303. i= i+1
  304. if(i%4 == 0):
  305. 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)
  306. return buf
  307. def Clear(self):
  308. buf = [0xFF] * (int(self.width/8) * self.height)
  309. self.send_command(0x24)
  310. self.send_data2(buf)
  311. self.TurnOnDisplay()
  312. def display(self, image):
  313. self.send_command(0x24)
  314. self.send_data2(image)
  315. self.TurnOnDisplay()
  316. def display_Base(self, image):
  317. self.send_command(0x24)
  318. self.send_data2(image)
  319. self.send_command(0x26)
  320. self.send_data2(image)
  321. self.TurnOnDisplay()
  322. def display_Base_color(self, color):
  323. if(self.width % 8 == 0):
  324. Width = self.width // 8
  325. else:
  326. Width = self.width // 8 +1
  327. Height = self.height
  328. self.send_command(0x24) #Write Black and White image to RAM
  329. for j in range(Height):
  330. for i in range(Width):
  331. self.send_data(color)
  332. self.send_command(0x26) #Write Black and White image to RAM
  333. for j in range(Height):
  334. for i in range(Width):
  335. self.send_data(color)
  336. # self.TurnOnDisplay()
  337. def display_Partial(self, Image, Xstart, Ystart, Xend, Yend):
  338. if((Xstart % 8 + Xend % 8 == 8 & Xstart % 8 > Xend % 8) | Xstart % 8 + Xend % 8 == 0 | (Xend - Xstart)%8 == 0):
  339. Xstart = Xstart // 8
  340. Xend = Xend // 8
  341. else:
  342. Xstart = Xstart // 8
  343. if Xend % 8 == 0:
  344. Xend = Xend // 8
  345. else:
  346. Xend = Xend // 8 + 1
  347. if(self.width % 8 == 0):
  348. Width = self.width // 8
  349. else:
  350. Width = self.width // 8 +1
  351. Height = self.height
  352. Xend -= 1
  353. Yend -= 1
  354. self.send_command(0x44)
  355. self.send_data((Xstart*8) & 0xff)
  356. self.send_data((Xstart>>5) & 0x01)
  357. self.send_data((Xend*8) & 0xff)
  358. self.send_data((Xend>>5) & 0x01)
  359. self.send_command(0x45)
  360. self.send_data(Ystart & 0xff)
  361. self.send_data((Ystart>>8) & 0x01)
  362. self.send_data(Yend & 0xff)
  363. self.send_data((Yend>>8) & 0x01)
  364. self.send_command(0x4E)
  365. self.send_data((Xstart*8) & 0xff)
  366. self.send_data((Xstart>>5) & 0x01)
  367. self.send_command(0x4F)
  368. self.send_data(Ystart & 0xff)
  369. self.send_data((Ystart>>8) & 0x01)
  370. self.send_command(0x24)
  371. for j in range(Height):
  372. for i in range(Width):
  373. if((j > Ystart-1) & (j < (Yend + 1)) & (i > Xstart-1) & (i < (Xend + 1))):
  374. self.send_data(Image[i + j * Width])
  375. self.TurnOnDisplay_Part()
  376. def display_4Gray(self, image):
  377. self.send_command(0x24)
  378. for i in range(0, 81600):
  379. temp3=0
  380. for j in range(0, 2):
  381. temp1 = image[i*2+j]
  382. for k in range(0, 2):
  383. temp2 = temp1&0xC0
  384. if(temp2 == 0xC0):
  385. temp3 |= 0x00
  386. elif(temp2 == 0x00):
  387. temp3 |= 0x01
  388. elif(temp2 == 0x80):
  389. temp3 |= 0x01
  390. else: #0x40
  391. temp3 |= 0x00
  392. temp3 <<= 1
  393. temp1 <<= 2
  394. temp2 = temp1&0xC0
  395. if(temp2 == 0xC0):
  396. temp3 |= 0x00
  397. elif(temp2 == 0x00):
  398. temp3 |= 0x01
  399. elif(temp2 == 0x80):
  400. temp3 |= 0x01
  401. else : #0x40
  402. temp3 |= 0x00
  403. if(j!=1 or k!=1):
  404. temp3 <<= 1
  405. temp1 <<= 2
  406. self.send_data(temp3)
  407. self.send_command(0x26)
  408. for i in range(0, 81600):
  409. temp3=0
  410. for j in range(0, 2):
  411. temp1 = image[i*2+j]
  412. for k in range(0, 2):
  413. temp2 = temp1&0xC0
  414. if(temp2 == 0xC0):
  415. temp3 |= 0x00
  416. elif(temp2 == 0x00):
  417. temp3 |= 0x01
  418. elif(temp2 == 0x80):
  419. temp3 |= 0x00
  420. else: #0x40
  421. temp3 |= 0x01
  422. temp3 <<= 1
  423. temp1 <<= 2
  424. temp2 = temp1&0xC0
  425. if(temp2 == 0xC0):
  426. temp3 |= 0x00
  427. elif(temp2 == 0x00):
  428. temp3 |= 0x01
  429. elif(temp2 == 0x80):
  430. temp3 |= 0x00
  431. else: #0x40
  432. temp3 |= 0x01
  433. if(j!=1 or k!=1):
  434. temp3 <<= 1
  435. temp1 <<= 2
  436. self.send_data(temp3)
  437. self.TurnOnDisplay_4GRAY()
  438. def sleep(self):
  439. self.send_command(0x10) # DEEP_SLEEP
  440. self.send_data(0x03)
  441. epdconfig.delay_ms(2000)
  442. epdconfig.module_exit()
  443. ### END OF FILE ###