epd2in13_V3.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. # *****************************************************************************
  2. # * | File : epd2in13_V3.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.1
  8. # * | Date : 2021-10-30
  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 = 122
  33. EPD_HEIGHT = 250
  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. lut_partial_update= [
  44. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  45. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  46. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  47. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  48. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  49. 0x14,0x0,0x0,0x0,0x0,0x0,0x0,
  50. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  51. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  52. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  53. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  54. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  55. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  56. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  57. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  58. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  59. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  60. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  61. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  62. 0x22,0x17,0x41,0x00,0x32,0x36,
  63. ]
  64. lut_full_update = [
  65. 0x80,0x4A,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  66. 0x40,0x4A,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  67. 0x80,0x4A,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  68. 0x40,0x4A,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  69. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  70. 0xF,0x0,0x0,0x0,0x0,0x0,0x0,
  71. 0xF,0x0,0x0,0xF,0x0,0x0,0x2,
  72. 0xF,0x0,0x0,0x0,0x0,0x0,0x0,
  73. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  74. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  75. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  76. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  77. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  78. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  79. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  80. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  81. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  82. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  83. 0x22,0x17,0x41,0x0,0x32,0x36,
  84. ]
  85. '''
  86. function :Hardware reset
  87. parameter:
  88. '''
  89. def reset(self):
  90. epdconfig.digital_write(self.reset_pin, 1)
  91. epdconfig.delay_ms(20)
  92. epdconfig.digital_write(self.reset_pin, 0)
  93. epdconfig.delay_ms(2)
  94. epdconfig.digital_write(self.reset_pin, 1)
  95. epdconfig.delay_ms(20)
  96. '''
  97. function :send command
  98. parameter:
  99. command : Command register
  100. '''
  101. def send_command(self, command):
  102. epdconfig.digital_write(self.dc_pin, 0)
  103. epdconfig.digital_write(self.cs_pin, 0)
  104. epdconfig.spi_writebyte([command])
  105. epdconfig.digital_write(self.cs_pin, 1)
  106. '''
  107. function :send data
  108. parameter:
  109. data : Write data
  110. '''
  111. def send_data(self, data):
  112. epdconfig.digital_write(self.dc_pin, 1)
  113. epdconfig.digital_write(self.cs_pin, 0)
  114. epdconfig.spi_writebyte([data])
  115. epdconfig.digital_write(self.cs_pin, 1)
  116. '''
  117. function :Wait until the busy_pin goes LOW
  118. parameter:
  119. '''
  120. def ReadBusy(self):
  121. logger.debug("e-Paper busy")
  122. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  123. epdconfig.delay_ms(10)
  124. logger.debug("e-Paper busy release")
  125. '''
  126. function : Turn On Display
  127. parameter:
  128. '''
  129. def TurnOnDisplay(self):
  130. self.send_command(0x22) # Display Update Control
  131. self.send_data(0xC7)
  132. self.send_command(0x20) # Activate Display Update Sequence
  133. self.ReadBusy()
  134. '''
  135. function : Turn On Display Part
  136. parameter:
  137. '''
  138. def TurnOnDisplayPart(self):
  139. self.send_command(0x22) # Display Update Control
  140. self.send_data(0x0f) # fast:0x0c, quality:0x0f, 0xcf
  141. self.send_command(0x20) # Activate Display Update Sequence
  142. self.ReadBusy()
  143. '''
  144. function : Set lut
  145. parameter:
  146. lut : lut data
  147. '''
  148. def Lut(self, lut):
  149. self.send_command(0x32)
  150. for i in range(0, 153):
  151. self.send_data(lut[i])
  152. self.ReadBusy()
  153. '''
  154. function : Send lut data and configuration
  155. parameter:
  156. lut : lut data
  157. '''
  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. '''
  171. function : Setting the display window
  172. parameter:
  173. xstart : X-axis starting position
  174. ystart : Y-axis starting position
  175. xend : End position of X-axis
  176. yend : End position of Y-axis
  177. '''
  178. def SetWindow(self, x_start, y_start, x_end, y_end):
  179. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  180. # x point must be the multiple of 8 or the last 3 bits will be ignored
  181. self.send_data((x_start>>3) & 0xFF)
  182. self.send_data((x_end>>3) & 0xFF)
  183. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  184. self.send_data(y_start & 0xFF)
  185. self.send_data((y_start >> 8) & 0xFF)
  186. self.send_data(y_end & 0xFF)
  187. self.send_data((y_end >> 8) & 0xFF)
  188. '''
  189. function : Set Cursor
  190. parameter:
  191. x : X-axis starting position
  192. y : Y-axis starting position
  193. '''
  194. def SetCursor(self, x, y):
  195. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  196. # x point must be the multiple of 8 or the last 3 bits will be ignored
  197. self.send_data(x & 0xFF)
  198. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  199. self.send_data(y & 0xFF)
  200. self.send_data((y >> 8) & 0xFF)
  201. '''
  202. function : Initialize the e-Paper register
  203. parameter:
  204. '''
  205. def init(self):
  206. if (epdconfig.module_init() != 0):
  207. return -1
  208. # EPD hardware init start
  209. self.reset()
  210. self.ReadBusy()
  211. self.send_command(0x12) #SWRESET
  212. self.ReadBusy()
  213. self.send_command(0x01) #Driver output control
  214. self.send_data(0xf9)
  215. self.send_data(0x00)
  216. self.send_data(0x00)
  217. self.send_command(0x11) #data entry mode
  218. self.send_data(0x03)
  219. self.SetWindow(0, 0, self.width-1, self.height-1)
  220. self.SetCursor(0, 0)
  221. self.send_command(0x3c)
  222. self.send_data(0x05)
  223. self.send_command(0x21) # Display update control
  224. self.send_data(0x00)
  225. self.send_data(0x80)
  226. self.send_command(0x18)
  227. self.send_data(0x80)
  228. self.ReadBusy()
  229. self.SetLut(self.lut_full_update)
  230. return 0
  231. '''
  232. function : Display images
  233. parameter:
  234. image : Image data
  235. '''
  236. def getbuffer(self, image):
  237. img = image
  238. imwidth, imheight = img.size
  239. if(imwidth == self.width and imheight == self.height):
  240. img = img.convert('1')
  241. elif(imwidth == self.height and imheight == self.width):
  242. # image has correct dimensions, but needs to be rotated
  243. img = img.rotate(90, expand=True).convert('1')
  244. else:
  245. logger.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
  246. # return a blank buffer
  247. return [0x00] * (int(self.width/8) * self.height)
  248. buf = bytearray(img.tobytes('raw'))
  249. return buf
  250. '''
  251. function : Sends the image buffer in RAM to e-Paper and displays
  252. parameter:
  253. image : Image data
  254. '''
  255. def display(self, image):
  256. if self.width%8 == 0:
  257. linewidth = int(self.width/8)
  258. else:
  259. linewidth = int(self.width/8) + 1
  260. self.send_command(0x24)
  261. for j in range(0, self.height):
  262. for i in range(0, linewidth):
  263. self.send_data(image[i + j * linewidth])
  264. self.TurnOnDisplay()
  265. '''
  266. function : Sends the image buffer in RAM to e-Paper and partial refresh
  267. parameter:
  268. image : Image data
  269. '''
  270. def displayPartial(self, image):
  271. if self.width%8 == 0:
  272. linewidth = int(self.width/8)
  273. else:
  274. linewidth = int(self.width/8) + 1
  275. epdconfig.digital_write(self.reset_pin, 0)
  276. epdconfig.delay_ms(1)
  277. epdconfig.digital_write(self.reset_pin, 1)
  278. self.SetLut(self.lut_partial_update)
  279. self.send_command(0x37)
  280. self.send_data(0x00)
  281. self.send_data(0x00)
  282. self.send_data(0x00)
  283. self.send_data(0x00)
  284. self.send_data(0x00)
  285. self.send_data(0x40)
  286. self.send_data(0x00)
  287. self.send_data(0x00)
  288. self.send_data(0x00)
  289. self.send_data(0x00)
  290. self.send_command(0x3C) #BorderWavefrom
  291. self.send_data(0x80)
  292. self.send_command(0x22)
  293. self.send_data(0xC0)
  294. self.send_command(0x20)
  295. self.ReadBusy()
  296. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  297. self.SetCursor(0, 0)
  298. self.send_command(0x24) # WRITE_RAM
  299. for j in range(0, self.height):
  300. for i in range(0, linewidth):
  301. self.send_data(image[i + j * linewidth])
  302. self.TurnOnDisplayPart()
  303. '''
  304. function : Refresh a base image
  305. parameter:
  306. image : Image data
  307. '''
  308. def displayPartBaseImage(self, image):
  309. if self.width%8 == 0:
  310. linewidth = int(self.width/8)
  311. else:
  312. linewidth = int(self.width/8) + 1
  313. self.send_command(0x24)
  314. for j in range(0, self.height):
  315. for i in range(0, linewidth):
  316. self.send_data(image[i + j * linewidth])
  317. self.send_command(0x26)
  318. for j in range(0, self.height):
  319. for i in range(0, linewidth):
  320. self.send_data(image[i + j * linewidth])
  321. self.TurnOnDisplay()
  322. '''
  323. function : Clear screen
  324. parameter:
  325. '''
  326. def Clear(self, color):
  327. if self.width%8 == 0:
  328. linewidth = int(self.width/8)
  329. else:
  330. linewidth = int(self.width/8) + 1
  331. # logger.debug(linewidth)
  332. self.send_command(0x24)
  333. for j in range(0, self.height):
  334. for i in range(0, linewidth):
  335. self.send_data(color)
  336. self.TurnOnDisplay()
  337. '''
  338. function : Enter sleep mode
  339. parameter:
  340. '''
  341. def sleep(self):
  342. self.send_command(0x10) #enter deep sleep
  343. self.send_data(0x01)
  344. epdconfig.delay_ms(2000)
  345. epdconfig.module_exit()
  346. ### END OF FILE ###