epd2in7.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. # *****************************************************************************
  2. # * | File : epd2in7.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 = 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_vcom_dc = [0x00, 0x00,
  52. 0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
  53. 0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
  54. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  55. 0x00, 0x12, 0x12, 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. ]
  60. lut_ww = [
  61. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  62. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  63. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  64. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. ]
  69. lut_bw = [
  70. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  71. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  72. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  73. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. ]
  78. lut_bb = [
  79. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  80. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  81. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  82. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. ]
  87. lut_wb = [
  88. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  89. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  90. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  91. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  92. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. ]
  96. ###################full screen update LUT######################
  97. #0~3 gray
  98. gray_lut_vcom = [
  99. 0x00, 0x00,
  100. 0x00, 0x0A, 0x00, 0x00, 0x00, 0x01,
  101. 0x60, 0x14, 0x14, 0x00, 0x00, 0x01,
  102. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  103. 0x00, 0x13, 0x0A, 0x01, 0x00, 0x01,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. ]
  108. #R21
  109. gray_lut_ww =[
  110. 0x40, 0x0A, 0x00, 0x00, 0x00, 0x01,
  111. 0x90, 0x14, 0x14, 0x00, 0x00, 0x01,
  112. 0x10, 0x14, 0x0A, 0x00, 0x00, 0x01,
  113. 0xA0, 0x13, 0x01, 0x00, 0x00, 0x01,
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. ]
  118. #R22H r
  119. gray_lut_bw =[
  120. 0x40, 0x0A, 0x00, 0x00, 0x00, 0x01,
  121. 0x90, 0x14, 0x14, 0x00, 0x00, 0x01,
  122. 0x00, 0x14, 0x0A, 0x00, 0x00, 0x01,
  123. 0x99, 0x0C, 0x01, 0x03, 0x04, 0x01,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. ]
  128. #R23H w
  129. gray_lut_wb =[
  130. 0x40, 0x0A, 0x00, 0x00, 0x00, 0x01,
  131. 0x90, 0x14, 0x14, 0x00, 0x00, 0x01,
  132. 0x00, 0x14, 0x0A, 0x00, 0x00, 0x01,
  133. 0x99, 0x0B, 0x04, 0x04, 0x01, 0x01,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  137. ]
  138. #R24H b
  139. gray_lut_bb =[
  140. 0x80, 0x0A, 0x00, 0x00, 0x00, 0x01,
  141. 0x90, 0x14, 0x14, 0x00, 0x00, 0x01,
  142. 0x20, 0x14, 0x0A, 0x00, 0x00, 0x01,
  143. 0x50, 0x13, 0x01, 0x00, 0x00, 0x01,
  144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  147. ]
  148. # Hardware reset
  149. def reset(self):
  150. epdconfig.digital_write(self.reset_pin, 1)
  151. epdconfig.delay_ms(200)
  152. epdconfig.digital_write(self.reset_pin, 0)
  153. epdconfig.delay_ms(5)
  154. epdconfig.digital_write(self.reset_pin, 1)
  155. epdconfig.delay_ms(200)
  156. def send_command(self, command):
  157. epdconfig.digital_write(self.dc_pin, 0)
  158. epdconfig.digital_write(self.cs_pin, 0)
  159. epdconfig.spi_writebyte([command])
  160. epdconfig.digital_write(self.cs_pin, 1)
  161. def send_data(self, data):
  162. epdconfig.digital_write(self.dc_pin, 1)
  163. epdconfig.digital_write(self.cs_pin, 0)
  164. epdconfig.spi_writebyte([data])
  165. epdconfig.digital_write(self.cs_pin, 1)
  166. def ReadBusy(self):
  167. logger.debug("e-Paper busy")
  168. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  169. epdconfig.delay_ms(200)
  170. logger.debug("e-Paper busy release")
  171. def set_lut(self):
  172. self.send_command(0x20) # vcom
  173. for count in range(0, 44):
  174. self.send_data(self.lut_vcom_dc[count])
  175. self.send_command(0x21) # ww --
  176. for count in range(0, 42):
  177. self.send_data(self.lut_ww[count])
  178. self.send_command(0x22) # bw r
  179. for count in range(0, 42):
  180. self.send_data(self.lut_bw[count])
  181. self.send_command(0x23) # wb w
  182. for count in range(0, 42):
  183. self.send_data(self.lut_bb[count])
  184. self.send_command(0x24) # bb b
  185. for count in range(0, 42):
  186. self.send_data(self.lut_wb[count])
  187. def gray_SetLut(self):
  188. self.send_command(0x20)
  189. for count in range(0, 44): #vcom
  190. self.send_data(self.gray_lut_vcom[count])
  191. self.send_command(0x21) #red not use
  192. for count in range(0, 42):
  193. self.send_data(self.gray_lut_ww[count])
  194. self.send_command(0x22) #bw r
  195. for count in range(0, 42):
  196. self.send_data(self.gray_lut_bw[count])
  197. self.send_command(0x23) #wb w
  198. for count in range(0, 42):
  199. self.send_data(self.gray_lut_wb[count])
  200. self.send_command(0x24) #bb b
  201. for count in range(0, 42):
  202. self.send_data(self.gray_lut_bb[count])
  203. self.send_command(0x25) #vcom
  204. for count in range(0, 42):
  205. self.send_data(self.gray_lut_ww[count])
  206. def init(self):
  207. if (epdconfig.module_init() != 0):
  208. return -1
  209. # EPD hardware init start
  210. self.reset()
  211. self.send_command(0x01) # POWER_SETTING
  212. self.send_data(0x03) # VDS_EN, VDG_EN
  213. self.send_data(0x00) # VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  214. self.send_data(0x2b) # VDH
  215. self.send_data(0x2b) # VDL
  216. self.send_data(0x09) # VDHR
  217. self.send_command(0x06) # BOOSTER_SOFT_START
  218. self.send_data(0x07)
  219. self.send_data(0x07)
  220. self.send_data(0x17)
  221. # Power optimization
  222. self.send_command(0xF8)
  223. self.send_data(0x60)
  224. self.send_data(0xA5)
  225. # Power optimization
  226. self.send_command(0xF8)
  227. self.send_data(0x89)
  228. self.send_data(0xA5)
  229. # Power optimization
  230. self.send_command(0xF8)
  231. self.send_data(0x90)
  232. self.send_data(0x00)
  233. # Power optimization
  234. self.send_command(0xF8)
  235. self.send_data(0x93)
  236. self.send_data(0x2A)
  237. # Power optimization
  238. self.send_command(0xF8)
  239. self.send_data(0xA0)
  240. self.send_data(0xA5)
  241. # Power optimization
  242. self.send_command(0xF8)
  243. self.send_data(0xA1)
  244. self.send_data(0x00)
  245. # Power optimization
  246. self.send_command(0xF8)
  247. self.send_data(0x73)
  248. self.send_data(0x41)
  249. self.send_command(0x16) # PARTIAL_DISPLAY_REFRESH
  250. self.send_data(0x00)
  251. self.send_command(0x04) # POWER_ON
  252. self.ReadBusy()
  253. self.send_command(0x00) # PANEL_SETTING
  254. self.send_data(0xAF) # KW-BF KWR-AF BWROTP 0f
  255. self.send_command(0x30) # PLL_CONTROL
  256. self.send_data(0x3A) # 3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  257. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  258. self.send_data(0x57)
  259. self.send_command(0x82) # VCM_DC_SETTING_REGISTER
  260. self.send_data(0x12)
  261. self.set_lut()
  262. return 0
  263. def Init_4Gray(self):
  264. if (epdconfig.module_init() != 0):
  265. return -1
  266. self.reset()
  267. self.send_command(0x01) #POWER SETTING
  268. self.send_data (0x03)
  269. self.send_data (0x00)
  270. self.send_data (0x2b)
  271. self.send_data (0x2b)
  272. self.send_command(0x06) #booster soft start
  273. self.send_data (0x07) #A
  274. self.send_data (0x07) #B
  275. self.send_data (0x17) #C
  276. self.send_command(0xF8) #boost??
  277. self.send_data (0x60)
  278. self.send_data (0xA5)
  279. self.send_command(0xF8) #boost??
  280. self.send_data (0x89)
  281. self.send_data (0xA5)
  282. self.send_command(0xF8) #boost??
  283. self.send_data (0x90)
  284. self.send_data (0x00)
  285. self.send_command(0xF8) #boost??
  286. self.send_data (0x93)
  287. self.send_data (0x2A)
  288. self.send_command(0xF8) #boost??
  289. self.send_data (0xa0)
  290. self.send_data (0xa5)
  291. self.send_command(0xF8) #boost??
  292. self.send_data (0xa1)
  293. self.send_data (0x00)
  294. self.send_command(0xF8) #boost??
  295. self.send_data (0x73)
  296. self.send_data (0x41)
  297. self.send_command(0x16)
  298. self.send_data(0x00)
  299. self.send_command(0x04)
  300. self.ReadBusy()
  301. self.send_command(0x00) #panel setting
  302. self.send_data(0xbf) #KW-BF KWR-AF BWROTP 0f
  303. self.send_command(0x30) #PLL setting
  304. self.send_data (0x90) #100hz
  305. self.send_command(0x61) #resolution setting
  306. self.send_data (0x00) #176
  307. self.send_data (0xb0)
  308. self.send_data (0x01) #264
  309. self.send_data (0x08)
  310. self.send_command(0x82) #vcom_DC setting
  311. self.send_data (0x12)
  312. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  313. self.send_data(0x57)
  314. def getbuffer(self, image):
  315. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  316. buf = [0xFF] * (int(self.width/8) * self.height)
  317. image_monocolor = image.convert('1')
  318. imwidth, imheight = image_monocolor.size
  319. pixels = image_monocolor.load()
  320. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  321. if(imwidth == self.width and imheight == self.height):
  322. logger.debug("Vertical")
  323. for y in range(imheight):
  324. for x in range(imwidth):
  325. # Set the bits for the column of pixels at the current position.
  326. if pixels[x, y] == 0:
  327. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  328. elif(imwidth == self.height and imheight == self.width):
  329. logger.debug("Horizontal")
  330. for y in range(imheight):
  331. for x in range(imwidth):
  332. newx = y
  333. newy = self.height - x - 1
  334. if pixels[x, y] == 0:
  335. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  336. return buf
  337. def getbuffer_4Gray(self, image):
  338. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  339. buf = [0xFF] * (int(self.width / 4) * self.height)
  340. image_monocolor = image.convert('L')
  341. imwidth, imheight = image_monocolor.size
  342. pixels = image_monocolor.load()
  343. i=0
  344. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  345. if(imwidth == self.width and imheight == self.height):
  346. logger.debug("Vertical")
  347. for y in range(imheight):
  348. for x in range(imwidth):
  349. # Set the bits for the column of pixels at the current position.
  350. if(pixels[x, y] == 0xC0):
  351. pixels[x, y] = 0x80
  352. elif (pixels[x, y] == 0x80):
  353. pixels[x, y] = 0x40
  354. i= i+1
  355. if(i%4 == 0):
  356. 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)
  357. elif(imwidth == self.height and imheight == self.width):
  358. logger.debug("Horizontal")
  359. for x in range(imwidth):
  360. for y in range(imheight):
  361. newx = y
  362. newy = self.height - x - 1
  363. if(pixels[x, y] == 0xC0):
  364. pixels[x, y] = 0x80
  365. elif (pixels[x, y] == 0x80):
  366. pixels[x, y] = 0x40
  367. i= i+1
  368. if(i%4 == 0):
  369. 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)
  370. return buf
  371. def display(self, image):
  372. self.send_command(0x10)
  373. for i in range(0, int(self.width * self.height / 8)):
  374. self.send_data(0xFF)
  375. self.send_command(0x13)
  376. for i in range(0, int(self.width * self.height / 8)):
  377. self.send_data(image[i])
  378. self.send_command(0x12)
  379. self.ReadBusy()
  380. def display_4Gray(self, image):
  381. self.send_command(0x10)
  382. for i in range(0, 5808): #5808*4 46464
  383. temp3=0
  384. for j in range(0, 2):
  385. temp1 = image[i*2+j]
  386. for k in range(0, 2):
  387. temp2 = temp1&0xC0
  388. if(temp2 == 0xC0):
  389. temp3 |= 0x01#white
  390. elif(temp2 == 0x00):
  391. temp3 |= 0x00 #black
  392. elif(temp2 == 0x80):
  393. temp3 |= 0x01 #gray1
  394. else: #0x40
  395. temp3 |= 0x00 #gray2
  396. temp3 <<= 1
  397. temp1 <<= 2
  398. temp2 = temp1&0xC0
  399. if(temp2 == 0xC0): #white
  400. temp3 |= 0x01
  401. elif(temp2 == 0x00): #black
  402. temp3 |= 0x00
  403. elif(temp2 == 0x80):
  404. temp3 |= 0x01 #gray1
  405. else : #0x40
  406. temp3 |= 0x00 #gray2
  407. if(j!=1 or k!=1):
  408. temp3 <<= 1
  409. temp1 <<= 2
  410. self.send_data(temp3)
  411. self.send_command(0x13)
  412. for i in range(0, 5808): #5808*4 46464
  413. temp3=0
  414. for j in range(0, 2):
  415. temp1 = image[i*2+j]
  416. for k in range(0, 2):
  417. temp2 = temp1&0xC0
  418. if(temp2 == 0xC0):
  419. temp3 |= 0x01#white
  420. elif(temp2 == 0x00):
  421. temp3 |= 0x00 #black
  422. elif(temp2 == 0x80):
  423. temp3 |= 0x00 #gray1
  424. else: #0x40
  425. temp3 |= 0x01 #gray2
  426. temp3 <<= 1
  427. temp1 <<= 2
  428. temp2 = temp1&0xC0
  429. if(temp2 == 0xC0): #white
  430. temp3 |= 0x01
  431. elif(temp2 == 0x00): #black
  432. temp3 |= 0x00
  433. elif(temp2 == 0x80):
  434. temp3 |= 0x00 #gray1
  435. else: #0x40
  436. temp3 |= 0x01 #gray2
  437. if(j!=1 or k!=1):
  438. temp3 <<= 1
  439. temp1 <<= 2
  440. self.send_data(temp3)
  441. self.gray_SetLut()
  442. self.send_command(0x12)
  443. epdconfig.delay_ms(200)
  444. self.ReadBusy()
  445. # pass
  446. def Clear(self, color=0xFF):
  447. self.send_command(0x10)
  448. for i in range(0, int(self.width * self.height / 8)):
  449. self.send_data(color)
  450. self.send_command(0x13)
  451. for i in range(0, int(self.width * self.height / 8)):
  452. self.send_data(color)
  453. self.send_command(0x12)
  454. self.ReadBusy()
  455. def sleep(self):
  456. self.send_command(0X50)
  457. self.send_data(0xf7)
  458. self.send_command(0X02)
  459. self.send_command(0X07)
  460. self.send_data(0xA5)
  461. epdconfig.delay_ms(2000)
  462. epdconfig.module_exit()
  463. ### END OF FILE ###