epd2in9b_V4.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. # *****************************************************************************
  2. # * | File : epd2in9b_V4.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2023-12-18
  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. logger = logging.getLogger(__name__)
  35. class EPD:
  36. def __init__(self):
  37. self.reset_pin = epdconfig.RST_PIN
  38. self.dc_pin = epdconfig.DC_PIN
  39. self.busy_pin = epdconfig.BUSY_PIN
  40. self.cs_pin = epdconfig.CS_PIN
  41. self.width = EPD_WIDTH
  42. self.height = EPD_HEIGHT
  43. # Hardware reset
  44. def reset(self):
  45. epdconfig.digital_write(self.reset_pin, 1)
  46. epdconfig.delay_ms(200)
  47. epdconfig.digital_write(self.reset_pin, 0)
  48. epdconfig.delay_ms(2)
  49. epdconfig.digital_write(self.reset_pin, 1)
  50. epdconfig.delay_ms(200)
  51. def send_command(self, command):
  52. epdconfig.digital_write(self.dc_pin, 0)
  53. epdconfig.digital_write(self.cs_pin, 0)
  54. epdconfig.spi_writebyte([command])
  55. epdconfig.digital_write(self.cs_pin, 1)
  56. def send_data(self, data):
  57. epdconfig.digital_write(self.dc_pin, 1)
  58. epdconfig.digital_write(self.cs_pin, 0)
  59. epdconfig.spi_writebyte([data])
  60. epdconfig.digital_write(self.cs_pin, 1)
  61. # send a lot of data
  62. def send_data2(self, data):
  63. epdconfig.digital_write(self.dc_pin, 1)
  64. epdconfig.digital_write(self.cs_pin, 0)
  65. epdconfig.spi_writebyte2(data)
  66. epdconfig.digital_write(self.cs_pin, 1)
  67. def ReadBusy(self):
  68. logger.debug("e-Paper busy")
  69. self.send_command(0X71)
  70. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  71. epdconfig.delay_ms(200)
  72. logger.debug("e-Paper busy release")
  73. def TurnOnDisplay(self):
  74. self.send_command(0x22) #Display Update Control
  75. self.send_data(0xF7)
  76. self.send_command(0x20) #Activate Display Update Sequence
  77. self.ReadBusy()
  78. def TurnOnDisplay_Base(self):
  79. self.send_command(0x22) #Display Update Control
  80. self.send_data(0xF4)
  81. self.send_command(0x20) #Activate Display Update Sequence
  82. self.ReadBusy()
  83. def TurnOnDisplay_Fast(self):
  84. self.send_command(0x22) #Display Update Control
  85. self.send_data(0xC7)
  86. self.send_command(0x20) #Activate Display Update Sequence
  87. self.ReadBusy()
  88. def TurnOnDisplay_Partial(self):
  89. self.send_command(0x22) #Display Update Control
  90. self.send_data(0x1C)
  91. self.send_command(0x20) #Activate Display Update Sequence
  92. self.ReadBusy()
  93. def init(self):
  94. if (epdconfig.module_init() != 0):
  95. return -1
  96. # EPD hardware init start
  97. self.reset()
  98. self.ReadBusy()
  99. self.send_command(0x12) #SWRESET
  100. self.ReadBusy()
  101. self.send_command(0x01) #Driver output control
  102. self.send_data((self.height-1)%256)
  103. self.send_data((self.height-1)//256)
  104. self.send_data(0x00)
  105. self.send_command(0x11) #data entry mode
  106. self.send_data(0x03)
  107. self.send_command(0x44) #set Ram-X address start/end position
  108. self.send_data(0x00)
  109. self.send_data(self.width//8-1)
  110. self.send_command(0x45) #set Ram-Y address start/end position
  111. self.send_data(0x00)
  112. self.send_data(0x00)
  113. self.send_data((self.height-1)%256)
  114. self.send_data((self.height-1)//256)
  115. self.send_command(0x3C) #BorderWavefrom
  116. self.send_data(0x05)
  117. self.send_command(0x21) # Display update control
  118. self.send_data(0x00)
  119. self.send_data(0x80)
  120. self.send_command(0x18) #Read built-in temperature sensor
  121. self.send_data(0x80)
  122. self.send_command(0x4E) # set RAM x address count to 0
  123. self.send_data(0x00)
  124. self.send_command(0x4F) # set RAM y address count to 0X199
  125. self.send_data(0x00)
  126. self.send_data(0x00)
  127. self.ReadBusy()
  128. return 0
  129. def init_Fast(self):
  130. if (epdconfig.module_init() != 0):
  131. return -1
  132. # EPD hardware init start
  133. self.reset()
  134. self.ReadBusy()
  135. self.send_command(0x12) #SWRESET
  136. self.ReadBusy()
  137. self.send_command(0x18) #Read built-in temperature sensor
  138. self.send_data(0x80)
  139. self.send_command(0x22) # Load temperature value
  140. self.send_data(0xB1)
  141. self.send_command(0x20)
  142. self.ReadBusy()
  143. self.send_command(0x1A) # Write to temperature register
  144. self.send_data(0x5a) # 90
  145. self.send_data(0x00)
  146. self.send_command(0x22) # Load temperature value
  147. self.send_data(0x91)
  148. self.send_command(0x20)
  149. self.ReadBusy()
  150. self.send_command(0x01) #Driver output control
  151. self.send_data((self.height-1)%256)
  152. self.send_data((self.height-1)//256)
  153. self.send_data(0x00)
  154. self.send_command(0x11) #data entry mode
  155. self.send_data(0x03)
  156. self.send_command(0x44) #set Ram-X address start/end position
  157. self.send_data(0x00)
  158. self.send_data(self.width//8-1)
  159. self.send_command(0x45) #set Ram-Y address start/end position
  160. self.send_data(0x00)
  161. self.send_data(0x00)
  162. self.send_data((self.height-1)%256)
  163. self.send_data((self.height-1)//256)
  164. self.send_command(0x4E) # set RAM x address count to 0
  165. self.send_data(0x00)
  166. self.send_command(0x4F) # set RAM y address count to 0X199
  167. self.send_data(0x00)
  168. self.send_data(0x00)
  169. self.ReadBusy()
  170. return 0
  171. def getbuffer(self, image):
  172. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  173. buf = [0xFF] * (int(self.width/8) * self.height)
  174. image_monocolor = image.convert('1')
  175. imwidth, imheight = image_monocolor.size
  176. pixels = image_monocolor.load()
  177. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  178. if(imwidth == self.width and imheight == self.height):
  179. logger.debug("Vertical")
  180. for y in range(imheight):
  181. for x in range(imwidth):
  182. # Set the bits for the column of pixels at the current position.
  183. if pixels[x, y] == 0:
  184. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  185. elif(imwidth == self.height and imheight == self.width):
  186. logger.debug("Horizontal")
  187. for y in range(imheight):
  188. for x in range(imwidth):
  189. newx = y
  190. newy = self.height - x - 1
  191. if pixels[x, y] == 0:
  192. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  193. return buf
  194. def display(self, blackimage, ryimage): # ryimage: red or yellow image
  195. if(self.width % 8 == 0):
  196. Width = self.width // 8
  197. else:
  198. Width = self.width // 8 +1
  199. Height = self.height
  200. if (blackimage != None):
  201. self.send_command(0x24)
  202. self.send_data2(blackimage)
  203. if (ryimage != None):
  204. for j in range(Height):
  205. for i in range(Width):
  206. ryimage[i + j * Width] = ~ryimage[i + j * Width]
  207. self.send_command(0x26)
  208. self.send_data2(ryimage)
  209. self.TurnOnDisplay()
  210. def display_Fast(self, blackimage, ryimage): # ryimage: red or yellow image
  211. if(self.width % 8 == 0):
  212. Width = self.width // 8
  213. else:
  214. Width = self.width // 8 +1
  215. Height = self.height
  216. if (blackimage != None):
  217. self.send_command(0x24)
  218. self.send_data2(blackimage)
  219. if (ryimage != None):
  220. for j in range(Height):
  221. for i in range(Width):
  222. ryimage[i + j * Width] = ~ryimage[i + j * Width]
  223. self.send_command(0x26)
  224. self.send_data2(ryimage)
  225. self.TurnOnDisplay_Fast()
  226. def Clear(self):
  227. self.send_command(0x24)
  228. self.send_data2([0xff] * int(self.width * self.height // 8))
  229. self.send_command(0x26)
  230. self.send_data2([0x00] * int(self.width * self.height // 8))
  231. self.TurnOnDisplay()
  232. def Clear_Fast(self):
  233. self.send_command(0x24)
  234. self.send_data2([0xff] * int(self.width * self.height // 8))
  235. self.send_command(0x26)
  236. self.send_data2([0x00] * int(self.width * self.height // 8))
  237. self.TurnOnDisplay_Fast()
  238. def display_Base(self, blackimage, ryimage):
  239. if(self.width % 8 == 0):
  240. Width = self.width // 8
  241. else:
  242. Width = self.width // 8 +1
  243. Height = self.height
  244. if (blackimage != None):
  245. self.send_command(0x24)
  246. self.send_data2(blackimage)
  247. if (ryimage != None):
  248. for j in range(Height):
  249. for i in range(Width):
  250. ryimage[i + j * Width] = ~ryimage[i + j * Width]
  251. self.send_command(0x26)
  252. self.send_data2(ryimage)
  253. self.TurnOnDisplay_Base()
  254. if (blackimage != None):
  255. for j in range(Height):
  256. for i in range(Width):
  257. blackimage[i + j * Width] = ~blackimage[i + j * Width]
  258. self.send_command(0x26)
  259. self.send_data2(blackimage)
  260. else:
  261. self.send_command(0x26)
  262. self.send_data2(blackimage)
  263. def display_Base_color(self, color):
  264. if(self.width % 8 == 0):
  265. Width = self.width // 8
  266. else:
  267. Width = self.width // 8 +1
  268. Height = self.height
  269. self.send_command(0x24) #Write Black and White image to RAM
  270. for j in range(Height):
  271. for i in range(Width):
  272. self.send_data(color)
  273. self.send_command(0x26) #Write Black and White image to RAM
  274. for j in range(Height):
  275. for i in range(Width):
  276. self.send_data(~color)
  277. self.TurnOnDisplay_Base()
  278. self.send_command(0x26) #Write Black and White image to RAM
  279. for j in range(Height):
  280. for i in range(Width):
  281. self.send_data(color)
  282. def display_Partial(self, Image, Xstart, Ystart, Xend, Yend):
  283. if((Xstart % 8 + Xend % 8 == 8 & Xstart % 8 > Xend % 8) | Xstart % 8 + Xend % 8 == 0 | (Xend - Xstart)%8 == 0):
  284. Xstart = Xstart // 8
  285. Xend = Xend // 8
  286. else:
  287. Xstart = Xstart // 8
  288. if Xend % 8 == 0:
  289. Xend = Xend // 8
  290. else:
  291. Xend = Xend // 8 + 1
  292. if(self.width % 8 == 0):
  293. Width = self.width // 8
  294. else:
  295. Width = self.width // 8 +1
  296. Height = self.height
  297. Xend -= 1
  298. Yend -= 1
  299. self.send_command(0x44) # set RAM x address start/end, in page 35
  300. self.send_data(Xstart & 0xff) # RAM x address start at 00h
  301. self.send_data(Xend & 0xff) # RAM x address end at 0fh(15+1)*8->128
  302. self.send_command(0x45) # set RAM y address start/end, in page 35
  303. self.send_data(Ystart & 0xff) # RAM y address start at 0127h
  304. self.send_data((Ystart>>8) & 0x01) # RAM y address start at 0127h
  305. self.send_data(Yend & 0xff) # RAM y address end at 00h
  306. self.send_data((Yend>>8) & 0x01)
  307. self.send_command(0x4E) # set RAM x address count to 0
  308. self.send_data(Xstart & 0xff)
  309. self.send_command(0x4F) # set RAM y address count to 0X127
  310. self.send_data(Ystart & 0xff)
  311. self.send_data((Ystart>>8) & 0x01)
  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. if((j > Ystart-1) & (j < (Yend + 1)) & (i > Xstart-1) & (i < (Xend + 1))):
  316. self.send_data(Image[i + j * Width])
  317. self.TurnOnDisplay_Partial()
  318. def sleep(self):
  319. self.send_command(0x10) # deep sleep
  320. self.send_data(0x01)
  321. epdconfig.delay_ms(2000)
  322. epdconfig.module_exit()
  323. ### END OF FILE ###