epd2in7_V2.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. # *****************************************************************************
  2. # * | File : epd2in7_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2022-09-17
  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 = 176
  33. EPD_HEIGHT = 264
  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. LUT_DATA_4Gray = [
  52. 0x40,0x48,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  53. 0x8,0x48,0x10,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  54. 0x2,0x48,0x4,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  55. 0x20,0x48,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  56. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  57. 0xA,0x19,0x0,0x3,0x8,0x0,0x0,
  58. 0x14,0x1,0x0,0x14,0x1,0x0,0x3,
  59. 0xA,0x3,0x0,0x8,0x19,0x0,0x0,
  60. 0x1,0x0,0x0,0x0,0x0,0x0,0x1,
  61. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  62. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  63. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  64. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  65. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  66. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  67. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  68. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  69. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  70. 0x22,0x17,0x41,0x0,0x32,0x1C,
  71. ]
  72. # Hardware reset
  73. def reset(self):
  74. epdconfig.digital_write(self.reset_pin, 1)
  75. epdconfig.delay_ms(200)
  76. epdconfig.digital_write(self.reset_pin, 0)
  77. epdconfig.delay_ms(2)
  78. epdconfig.digital_write(self.reset_pin, 1)
  79. epdconfig.delay_ms(200)
  80. def send_command(self, command):
  81. epdconfig.digital_write(self.dc_pin, 0)
  82. epdconfig.digital_write(self.cs_pin, 0)
  83. epdconfig.spi_writebyte([command])
  84. epdconfig.digital_write(self.cs_pin, 1)
  85. def send_data(self, data):
  86. epdconfig.digital_write(self.dc_pin, 1)
  87. epdconfig.digital_write(self.cs_pin, 0)
  88. epdconfig.spi_writebyte([data])
  89. epdconfig.digital_write(self.cs_pin, 1)
  90. def ReadBusy(self):
  91. logger.debug("e-Paper busy")
  92. while(epdconfig.digital_read(self.busy_pin) == 1): # 1: idle, 0: busy
  93. epdconfig.delay_ms(20)
  94. logger.debug("e-Paper busy release")
  95. def TurnOnDisplay(self):
  96. self.send_command(0x22) #Display Update Control
  97. self.send_data(0xF7)
  98. self.send_command(0x20) #Activate Display Update Sequence
  99. self.ReadBusy()
  100. def TurnOnDisplay_Fast(self):
  101. self.send_command(0x22) #Display Update Control
  102. self.send_data(0xC7)
  103. self.send_command(0x20) #Activate Display Update Sequence
  104. self.ReadBusy()
  105. def TurnOnDisplay_Partial(self):
  106. self.send_command(0x22) #Display Update Control
  107. self.send_data(0xFF)
  108. self.send_command(0x20) #Activate Display Update Sequence
  109. self.ReadBusy()
  110. def TurnOnDisplay_4GRAY(self):
  111. self.send_command(0x22) #Display Update Control
  112. self.send_data(0xC7)
  113. self.send_command(0x20) #Activate Display Update Sequence
  114. self.ReadBusy()
  115. def Lut(self):
  116. self.send_command(0x32)
  117. for i in range(159):
  118. self.send_data(self.LUT_DATA_4Gray[i])
  119. def init(self):
  120. if (epdconfig.module_init() != 0):
  121. return -1
  122. # EPD hardware init start
  123. self.reset()
  124. self.ReadBusy()
  125. self.send_command(0x12) #SWRESET
  126. self.ReadBusy()
  127. self.send_command(0x45) #set Ram-Y address start/end position
  128. self.send_data(0x00)
  129. self.send_data(0x00)
  130. self.send_data(0x07) #0x0107-->(263+1)=264
  131. self.send_data(0x01)
  132. self.send_command(0x4F) # set RAM y address count to 0;
  133. self.send_data(0x00)
  134. self.send_data(0x00)
  135. self.send_command(0x11) # data entry mode
  136. self.send_data(0x03)
  137. return 0
  138. def init_Fast(self):
  139. if (epdconfig.module_init() != 0):
  140. return -1
  141. # EPD hardware init start
  142. self.reset()
  143. self.ReadBusy()
  144. self.send_command(0x12) #SWRESET
  145. self.ReadBusy()
  146. self.send_command(0x12) #SWRESET
  147. self.ReadBusy()
  148. self.send_command(0x18) #Read built-in temperature sensor
  149. self.send_data(0x80)
  150. self.send_command(0x22) # Load temperature value
  151. self.send_data(0xB1)
  152. self.send_command(0x20)
  153. self.ReadBusy()
  154. self.send_command(0x1A) # Write to temperature register
  155. self.send_data(0x64)
  156. self.send_data(0x00)
  157. self.send_command(0x45) #set Ram-Y address start/end position
  158. self.send_data(0x00)
  159. self.send_data(0x00)
  160. self.send_data(0x07) #0x0107-->(263+1)=264
  161. self.send_data(0x01)
  162. self.send_command(0x4F) # set RAM y address count to 0;
  163. self.send_data(0x00)
  164. self.send_data(0x00)
  165. self.send_command(0x11) # data entry mode
  166. self.send_data(0x03)
  167. self.send_command(0x22) # Load temperature value
  168. self.send_data(0x91)
  169. self.send_command(0x20)
  170. self.ReadBusy()
  171. return 0
  172. def Init_4Gray(self):
  173. if (epdconfig.module_init() != 0):
  174. return -1
  175. self.reset()
  176. self.send_command(0x12) # soft reset
  177. self.ReadBusy();
  178. self.send_command(0x74) #set analog block control
  179. self.send_data(0x54)
  180. self.send_command(0x7E) #set digital block control
  181. self.send_data(0x3B)
  182. self.send_command(0x01) #Driver output control
  183. self.send_data(0x07)
  184. self.send_data(0x01)
  185. self.send_data(0x00)
  186. self.send_command(0x11) #data entry mode
  187. self.send_data(0x03)
  188. self.send_command(0x44) #set Ram-X address start/end position
  189. self.send_data(0x00)
  190. self.send_data(0x15) #0x15-->(21+1)*8=176
  191. self.send_command(0x45) #set Ram-Y address start/end position
  192. self.send_data(0x00)
  193. self.send_data(0x00)
  194. self.send_data(0x07) #0x0107-->(263+1)=264
  195. self.send_data(0x01)
  196. self.send_command(0x3C) #BorderWavefrom
  197. self.send_data(0x00)
  198. self.send_command(0x2C) #VCOM Voltage
  199. self.send_data(self.LUT_DATA_4Gray[158]) #0x1C
  200. self.send_command(0x3F) #EOPQ
  201. self.send_data(self.LUT_DATA_4Gray[153])
  202. self.send_command(0x03) #VGH
  203. self.send_data(self.LUT_DATA_4Gray[154])
  204. self.send_command(0x04) #
  205. self.send_data(self.LUT_DATA_4Gray[155]) #VSH1
  206. self.send_data(self.LUT_DATA_4Gray[156]) #VSH2
  207. self.send_data(self.LUT_DATA_4Gray[157]) #VSL
  208. self.Lut() #LUT
  209. self.send_command(0x4E) # set RAM x address count to 0;
  210. self.send_data(0x00)
  211. self.send_command(0x4F) # set RAM y address count to 0X199;
  212. self.send_data(0x00)
  213. self.send_data(0x00)
  214. self.ReadBusy()
  215. return 0
  216. def getbuffer(self, image):
  217. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  218. buf = [0xFF] * (int(self.width/8) * self.height)
  219. image_monocolor = image.convert('1')
  220. imwidth, imheight = image_monocolor.size
  221. pixels = image_monocolor.load()
  222. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  223. if(imwidth == self.width and imheight == self.height):
  224. logger.debug("Vertical")
  225. for y in range(imheight):
  226. for x in range(imwidth):
  227. # Set the bits for the column of pixels at the current position.
  228. if pixels[x, y] == 0:
  229. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  230. elif(imwidth == self.height and imheight == self.width):
  231. logger.debug("Horizontal")
  232. for y in range(imheight):
  233. for x in range(imwidth):
  234. newx = y
  235. newy = self.height - x - 1
  236. if pixels[x, y] == 0:
  237. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  238. return buf
  239. def getbuffer_4Gray(self, image):
  240. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  241. buf = [0xFF] * (int(self.width / 4) * self.height)
  242. image_monocolor = image.convert('L')
  243. imwidth, imheight = image_monocolor.size
  244. pixels = image_monocolor.load()
  245. i=0
  246. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  247. if(imwidth == self.width and imheight == self.height):
  248. logger.debug("Vertical")
  249. for y in range(imheight):
  250. for x in range(imwidth):
  251. # Set the bits for the column of pixels at the current position.
  252. if(pixels[x, y] == 0xC0):
  253. pixels[x, y] = 0x80
  254. elif (pixels[x, y] == 0x80):
  255. pixels[x, y] = 0x40
  256. i= i+1
  257. if(i%4 == 0):
  258. 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)
  259. elif(imwidth == self.height and imheight == self.width):
  260. logger.debug("Horizontal")
  261. for x in range(imwidth):
  262. for y in range(imheight):
  263. newx = y
  264. newy = self.height - x - 1
  265. if(pixels[x, y] == 0xC0):
  266. pixels[x, y] = 0x80
  267. elif (pixels[x, y] == 0x80):
  268. pixels[x, y] = 0x40
  269. i= i+1
  270. if(i%4 == 0):
  271. 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)
  272. return buf
  273. def Clear(self):
  274. if(self.width % 8 == 0):
  275. Width = self.width // 8
  276. else:
  277. Width = self.width // 8 +1
  278. Height = self.height
  279. self.send_command(0x24)
  280. for j in range(Height):
  281. for i in range(Width):
  282. self.send_data(0XFF)
  283. self.TurnOnDisplay()
  284. def display(self, image):
  285. if(self.width % 8 == 0):
  286. Width = self.width // 8
  287. else:
  288. Width = self.width // 8 +1
  289. Height = self.height
  290. self.send_command(0x24)
  291. for j in range(Height):
  292. for i in range(Width):
  293. self.send_data(image[i + j * Width])
  294. self.TurnOnDisplay()
  295. def display_Fast(self, image):
  296. if(self.width % 8 == 0):
  297. Width = self.width // 8
  298. else:
  299. Width = self.width // 8 +1
  300. Height = self.height
  301. self.send_command(0x24)
  302. for j in range(Height):
  303. for i in range(Width):
  304. self.send_data(image[i + j * Width])
  305. self.TurnOnDisplay_Fast()
  306. def display_Base(self, image):
  307. if(self.width % 8 == 0):
  308. Width = self.width // 8
  309. else:
  310. Width = self.width // 8 +1
  311. Height = self.height
  312. self.send_command(0x24) #Write Black and White image to RAM
  313. for j in range(Height):
  314. for i in range(Width):
  315. self.send_data(image[i + j * Width])
  316. self.send_command(0x26) #Write Black and White image to RAM
  317. for j in range(Height):
  318. for i in range(Width):
  319. self.send_data(image[i + j * Width])
  320. self.TurnOnDisplay()
  321. def display_Base_color(self, color):
  322. if(self.width % 8 == 0):
  323. Width = self.width // 8
  324. else:
  325. Width = self.width // 8 +1
  326. Height = self.height
  327. self.send_command(0x24) #Write Black and White image to RAM
  328. for j in range(Height):
  329. for i in range(Width):
  330. self.send_data(color)
  331. self.send_command(0x26) #Write Black and White image to RAM
  332. for j in range(Height):
  333. for i in range(Width):
  334. self.send_data(color)
  335. # self.TurnOnDisplay()
  336. def display_Partial(self, Image, Xstart, Ystart, Xend, Yend):
  337. if((Xstart % 8 + Xend % 8 == 8 & Xstart % 8 > Xend % 8) | Xstart % 8 + Xend % 8 == 0 | (Xend - Xstart)%8 == 0):
  338. Xstart = Xstart // 8
  339. Xend = Xend // 8
  340. else:
  341. Xstart = Xstart // 8
  342. if Xend % 8 == 0:
  343. Xend = Xend // 8
  344. else:
  345. Xend = Xend // 8 + 1
  346. if(self.width % 8 == 0):
  347. Width = self.width // 8
  348. else:
  349. Width = self.width // 8 +1
  350. Height = self.height
  351. Xend -= 1
  352. Yend -= 1
  353. # Reset
  354. self.reset()
  355. self.send_command(0x3C) #BorderWavefrom
  356. self.send_data(0x80)
  357. self.send_command(0x44) # set RAM x address start/end, in page 35
  358. self.send_data(Xstart & 0xff) # RAM x address start at 00h;
  359. self.send_data(Xend & 0xff) # RAM x address end at 0fh(15+1)*8->128
  360. self.send_command(0x45) # set RAM y address start/end, in page 35
  361. self.send_data(Ystart & 0xff) # RAM y address start at 0127h;
  362. self.send_data((Ystart>>8) & 0x01) # RAM y address start at 0127h;
  363. self.send_data(Yend & 0xff) # RAM y address end at 00h;
  364. self.send_data((Yend>>8) & 0x01)
  365. self.send_command(0x4E) # set RAM x address count to 0;
  366. self.send_data(Xstart & 0xff)
  367. self.send_command(0x4F) # set RAM y address count to 0X127;
  368. self.send_data(Ystart & 0xff)
  369. self.send_data((Ystart>>8) & 0x01)
  370. self.send_command(0x24) #Write Black and White image to RAM
  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_Partial()
  376. def display_4Gray(self, image):
  377. self.send_command(0x24)
  378. for i in range(0, 5808): #5808*4 46464
  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, 5808): #5808*4 46464
  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)
  440. self.send_data(0x01)
  441. epdconfig.delay_ms(2000)
  442. epdconfig.module_exit()
  443. ### END OF FILE ###