epd2in9_V2.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. # *****************************************************************************
  2. # * | File : epd2in9_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.1
  8. # * | Date : 2022-08-9
  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 = 128
  33. EPD_HEIGHT = 296
  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. WF_PARTIAL_2IN9 = [
  52. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  53. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  54. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  55. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  56. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  57. 0x0A,0x0,0x0,0x0,0x0,0x0,0x1,
  58. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  59. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  60. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  61. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  62. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  63. 0x0,0x0,0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  89. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  90. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x0, 0x0, 0x0,
  91. 0x22, 0x17, 0x41, 0x0, 0x32, 0x36
  92. ]
  93. Gray4 = [
  94. 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x20, 0x60, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x28, 0x60, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. 0x2A, 0x60, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x02, 0x00, 0x05, 0x14, 0x00, 0x00,
  100. 0x1E, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x01,
  101. 0x00, 0x02, 0x00, 0x05, 0x14, 0x00, 0x00,
  102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x24, 0x22, 0x22, 0x22, 0x23, 0x32, 0x00, 0x00, 0x00,
  112. 0x22, 0x17, 0x41, 0xAE, 0x32, 0x28
  113. ]
  114. # Hardware reset
  115. def reset(self):
  116. epdconfig.digital_write(self.reset_pin, 1)
  117. epdconfig.delay_ms(50)
  118. epdconfig.digital_write(self.reset_pin, 0)
  119. epdconfig.delay_ms(2)
  120. epdconfig.digital_write(self.reset_pin, 1)
  121. epdconfig.delay_ms(50)
  122. def send_command(self, command):
  123. epdconfig.digital_write(self.dc_pin, 0)
  124. epdconfig.digital_write(self.cs_pin, 0)
  125. epdconfig.spi_writebyte([command])
  126. epdconfig.digital_write(self.cs_pin, 1)
  127. def send_data(self, data):
  128. epdconfig.digital_write(self.dc_pin, 1)
  129. epdconfig.digital_write(self.cs_pin, 0)
  130. epdconfig.spi_writebyte([data])
  131. epdconfig.digital_write(self.cs_pin, 1)
  132. # send a lot of data
  133. def send_data2(self, data):
  134. epdconfig.digital_write(self.dc_pin, 1)
  135. epdconfig.digital_write(self.cs_pin, 0)
  136. epdconfig.spi_writebyte2(data)
  137. epdconfig.digital_write(self.cs_pin, 1)
  138. def ReadBusy(self):
  139. logger.debug("e-Paper busy")
  140. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  141. epdconfig.delay_ms(10)
  142. logger.debug("e-Paper busy release")
  143. def TurnOnDisplay(self):
  144. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  145. self.send_data(0xc7)
  146. self.send_command(0x20) # MASTER_ACTIVATION
  147. self.ReadBusy()
  148. def TurnOnDisplay_Partial(self):
  149. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  150. self.send_data(0x0F)
  151. self.send_command(0x20) # MASTER_ACTIVATION
  152. self.ReadBusy()
  153. def lut(self, lut):
  154. self.send_command(0x32)
  155. for i in range(0, 153):
  156. self.send_data(lut[i])
  157. self.ReadBusy()
  158. def SetLut(self, lut):
  159. self.lut(lut)
  160. self.send_command(0x3f)
  161. self.send_data(lut[153])
  162. self.send_command(0x03); # gate voltage
  163. self.send_data(lut[154])
  164. self.send_command(0x04); # source voltage
  165. self.send_data(lut[155]) # VSH
  166. self.send_data(lut[156]) # VSH2
  167. self.send_data(lut[157]) # VSL
  168. self.send_command(0x2c); # VCOM
  169. self.send_data(lut[158])
  170. def SetWindow(self, x_start, y_start, x_end, y_end):
  171. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  172. # x point must be the multiple of 8 or the last 3 bits will be ignored
  173. self.send_data((x_start>>3) & 0xFF)
  174. self.send_data((x_end>>3) & 0xFF)
  175. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  176. self.send_data(y_start & 0xFF)
  177. self.send_data((y_start >> 8) & 0xFF)
  178. self.send_data(y_end & 0xFF)
  179. self.send_data((y_end >> 8) & 0xFF)
  180. def SetCursor(self, x, y):
  181. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  182. # x point must be the multiple of 8 or the last 3 bits will be ignored
  183. self.send_data(x & 0xFF)
  184. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  185. self.send_data(y & 0xFF)
  186. self.send_data((y >> 8) & 0xFF)
  187. def init(self):
  188. if (epdconfig.module_init() != 0):
  189. return -1
  190. # EPD hardware init start
  191. self.reset()
  192. self.ReadBusy()
  193. self.send_command(0x12) #SWRESET
  194. self.ReadBusy()
  195. self.send_command(0x01) #Driver output control
  196. self.send_data(0x27)
  197. self.send_data(0x01)
  198. self.send_data(0x00)
  199. self.send_command(0x11) #data entry mode
  200. self.send_data(0x03)
  201. self.SetWindow(0, 0, self.width-1, self.height-1)
  202. self.send_command(0x21) # Display update control
  203. self.send_data(0x00)
  204. self.send_data(0x80)
  205. self.SetCursor(0, 0)
  206. self.ReadBusy()
  207. self.SetLut(self.WS_20_30)
  208. # EPD hardware init end
  209. return 0
  210. def Init_4Gray(self):
  211. if (epdconfig.module_init() != 0):
  212. return -1
  213. self.reset()
  214. epdconfig.delay_ms(100)
  215. self.ReadBusy()
  216. self.send_command(0x12) #SWRESET
  217. self.ReadBusy()
  218. self.send_command(0x01) #Driver output control
  219. self.send_data(0x27)
  220. self.send_data(0x01)
  221. self.send_data(0x00)
  222. self.send_command(0x11) #data entry mode
  223. self.send_data(0x03)
  224. self.SetWindow(8, 0, self.width, self.height-1)
  225. self.send_command(0x3C)
  226. self.send_data(0x04)
  227. self.SetCursor(1, 0)
  228. self.ReadBusy()
  229. self.SetLut(self.Gray4)
  230. # EPD hardware init end
  231. return 0
  232. def getbuffer(self, image):
  233. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  234. buf = [0xFF] * (int(self.width/8) * self.height)
  235. image_monocolor = image.convert('1')
  236. imwidth, imheight = image_monocolor.size
  237. pixels = image_monocolor.load()
  238. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  239. if(imwidth == self.width and imheight == self.height):
  240. logger.debug("Vertical")
  241. for y in range(imheight):
  242. for x in range(imwidth):
  243. # Set the bits for the column of pixels at the current position.
  244. if pixels[x, y] == 0:
  245. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  246. elif(imwidth == self.height and imheight == self.width):
  247. logger.debug("Horizontal")
  248. for y in range(imheight):
  249. for x in range(imwidth):
  250. newx = y
  251. newy = self.height - x - 1
  252. if pixels[x, y] == 0:
  253. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  254. return buf
  255. def getbuffer_4Gray(self, image):
  256. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  257. buf = [0xFF] * (int(self.width / 4) * self.height)
  258. image_monocolor = image.convert('L')
  259. imwidth, imheight = image_monocolor.size
  260. pixels = image_monocolor.load()
  261. i=0
  262. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  263. if(imwidth == self.width and imheight == self.height):
  264. logger.debug("Vertical")
  265. for y in range(imheight):
  266. for x in range(imwidth):
  267. # Set the bits for the column of pixels at the current position.
  268. if(pixels[x, y] == 0xC0):
  269. pixels[x, y] = 0x80
  270. elif (pixels[x, y] == 0x80):
  271. pixels[x, y] = 0x40
  272. i= i+1
  273. if(i%4 == 0):
  274. 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)
  275. elif(imwidth == self.height and imheight == self.width):
  276. logger.debug("Horizontal")
  277. for x in range(imwidth):
  278. for y in range(imheight):
  279. newx = y
  280. newy = self.height - x - 1
  281. if(pixels[x, y] == 0xC0):
  282. pixels[x, y] = 0x80
  283. elif (pixels[x, y] == 0x80):
  284. pixels[x, y] = 0x40
  285. i= i+1
  286. if(i%4 == 0):
  287. 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)
  288. return buf
  289. def display(self, image):
  290. if (image == None):
  291. return
  292. self.send_command(0x24) # WRITE_RAM
  293. self.send_data2(image)
  294. self.TurnOnDisplay()
  295. def display_Base(self, image):
  296. if (image == None):
  297. return
  298. self.send_command(0x24) # WRITE_RAM
  299. self.send_data2(image)
  300. self.send_command(0x26) # WRITE_RAM
  301. self.send_data2(image)
  302. self.TurnOnDisplay()
  303. def display_4Gray(self, image):
  304. self.send_command(0x24)
  305. for i in range(0, 4736):
  306. temp3=0
  307. for j in range(0, 2):
  308. temp1 = image[i*2+j]
  309. for k in range(0, 2):
  310. temp2 = temp1&0xC0
  311. if(temp2 == 0xC0):
  312. temp3 |= 0x00
  313. elif(temp2 == 0x00):
  314. temp3 |= 0x01
  315. elif(temp2 == 0x80):
  316. temp3 |= 0x01
  317. else: #0x40
  318. temp3 |= 0x00
  319. temp3 <<= 1
  320. temp1 <<= 2
  321. temp2 = temp1&0xC0
  322. if(temp2 == 0xC0):
  323. temp3 |= 0x00
  324. elif(temp2 == 0x00):
  325. temp3 |= 0x01
  326. elif(temp2 == 0x80):
  327. temp3 |= 0x01
  328. else : #0x40
  329. temp3 |= 0x00
  330. if(j!=1 or k!=1):
  331. temp3 <<= 1
  332. temp1 <<= 2
  333. self.send_data(temp3)
  334. self.send_command(0x26)
  335. for i in range(0, 4736):
  336. temp3=0
  337. for j in range(0, 2):
  338. temp1 = image[i*2+j]
  339. for k in range(0, 2):
  340. temp2 = temp1&0xC0
  341. if(temp2 == 0xC0):
  342. temp3 |= 0x00
  343. elif(temp2 == 0x00):
  344. temp3 |= 0x01
  345. elif(temp2 == 0x80):
  346. temp3 |= 0x00
  347. else: #0x40
  348. temp3 |= 0x01
  349. temp3 <<= 1
  350. temp1 <<= 2
  351. temp2 = temp1&0xC0
  352. if(temp2 == 0xC0):
  353. temp3 |= 0x00
  354. elif(temp2 == 0x00):
  355. temp3 |= 0x01
  356. elif(temp2 == 0x80):
  357. temp3 |= 0x00
  358. else: #0x40
  359. temp3 |= 0x01
  360. if(j!=1 or k!=1):
  361. temp3 <<= 1
  362. temp1 <<= 2
  363. self.send_data(temp3)
  364. self.TurnOnDisplay()
  365. def display_Partial(self, image):
  366. if (image == None):
  367. return
  368. epdconfig.digital_write(self.reset_pin, 0)
  369. epdconfig.delay_ms(2)
  370. epdconfig.digital_write(self.reset_pin, 1)
  371. epdconfig.delay_ms(2)
  372. self.SetLut(self.WF_PARTIAL_2IN9)
  373. self.send_command(0x37)
  374. self.send_data(0x00)
  375. self.send_data(0x00)
  376. self.send_data(0x00)
  377. self.send_data(0x00)
  378. self.send_data(0x00)
  379. self.send_data(0x40)
  380. self.send_data(0x00)
  381. self.send_data(0x00)
  382. self.send_data(0x00)
  383. self.send_data(0x00)
  384. self.send_command(0x3C) #BorderWavefrom
  385. self.send_data(0x80)
  386. self.send_command(0x22)
  387. self.send_data(0xC0)
  388. self.send_command(0x20)
  389. self.ReadBusy()
  390. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  391. self.SetCursor(0, 0)
  392. self.send_command(0x24) # WRITE_RAM
  393. self.send_data2(image)
  394. self.TurnOnDisplay_Partial()
  395. def Clear(self, color=0xFF):
  396. if self.width%8 == 0:
  397. linewidth = int(self.width/8)
  398. else:
  399. linewidth = int(self.width/8) + 1
  400. self.send_command(0x24) # WRITE_RAM
  401. self.send_data2([color] * int(self.height * linewidth))
  402. self.TurnOnDisplay()
  403. self.send_command(0x26) # WRITE_RAM
  404. self.send_data2([color] * int(self.height * linewidth))
  405. self.TurnOnDisplay()
  406. def sleep(self):
  407. self.send_command(0x10) # DEEP_SLEEP_MODE
  408. self.send_data(0x01)
  409. epdconfig.delay_ms(2000)
  410. epdconfig.module_exit()
  411. ### END OF FILE ###