epd2in13_V3.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. # *****************************************************************************
  2. # * | File : epd2in13_V3.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.2
  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 = 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. # send a lot of data
  117. def send_data2(self, data):
  118. epdconfig.digital_write(self.dc_pin, 1)
  119. epdconfig.digital_write(self.cs_pin, 0)
  120. epdconfig.spi_writebyte2(data)
  121. epdconfig.digital_write(self.cs_pin, 1)
  122. '''
  123. function :Wait until the busy_pin goes LOW
  124. parameter:
  125. '''
  126. def ReadBusy(self):
  127. logger.debug("e-Paper busy")
  128. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  129. epdconfig.delay_ms(10)
  130. logger.debug("e-Paper busy release")
  131. '''
  132. function : Turn On Display
  133. parameter:
  134. '''
  135. def TurnOnDisplay(self):
  136. self.send_command(0x22) # Display Update Control
  137. self.send_data(0xC7)
  138. self.send_command(0x20) # Activate Display Update Sequence
  139. self.ReadBusy()
  140. '''
  141. function : Turn On Display Part
  142. parameter:
  143. '''
  144. def TurnOnDisplayPart(self):
  145. self.send_command(0x22) # Display Update Control
  146. self.send_data(0x0f) # fast:0x0c, quality:0x0f, 0xcf
  147. self.send_command(0x20) # Activate Display Update Sequence
  148. self.ReadBusy()
  149. '''
  150. function : Set lut
  151. parameter:
  152. lut : lut data
  153. '''
  154. def Lut(self, lut):
  155. self.send_command(0x32)
  156. for i in range(0, 153):
  157. self.send_data(lut[i])
  158. self.ReadBusy()
  159. '''
  160. function : Send lut data and configuration
  161. parameter:
  162. lut : lut data
  163. '''
  164. def SetLut(self, lut):
  165. self.Lut(lut)
  166. self.send_command(0x3f)
  167. self.send_data(lut[153])
  168. self.send_command(0x03) # gate voltage
  169. self.send_data(lut[154])
  170. self.send_command(0x04) # source voltage
  171. self.send_data(lut[155]) # VSH
  172. self.send_data(lut[156]) # VSH2
  173. self.send_data(lut[157]) # VSL
  174. self.send_command(0x2c) # VCOM
  175. self.send_data(lut[158])
  176. '''
  177. function : Setting the display window
  178. parameter:
  179. xstart : X-axis starting position
  180. ystart : Y-axis starting position
  181. xend : End position of X-axis
  182. yend : End position of Y-axis
  183. '''
  184. def SetWindow(self, x_start, y_start, x_end, y_end):
  185. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  186. # x point must be the multiple of 8 or the last 3 bits will be ignored
  187. self.send_data((x_start>>3) & 0xFF)
  188. self.send_data((x_end>>3) & 0xFF)
  189. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  190. self.send_data(y_start & 0xFF)
  191. self.send_data((y_start >> 8) & 0xFF)
  192. self.send_data(y_end & 0xFF)
  193. self.send_data((y_end >> 8) & 0xFF)
  194. '''
  195. function : Set Cursor
  196. parameter:
  197. x : X-axis starting position
  198. y : Y-axis starting position
  199. '''
  200. def SetCursor(self, x, y):
  201. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  202. # x point must be the multiple of 8 or the last 3 bits will be ignored
  203. self.send_data(x & 0xFF)
  204. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  205. self.send_data(y & 0xFF)
  206. self.send_data((y >> 8) & 0xFF)
  207. '''
  208. function : Initialize the e-Paper register
  209. parameter:
  210. '''
  211. def init(self):
  212. if (epdconfig.module_init() != 0):
  213. return -1
  214. # EPD hardware init start
  215. self.reset()
  216. self.ReadBusy()
  217. self.send_command(0x12) #SWRESET
  218. self.ReadBusy()
  219. self.send_command(0x01) #Driver output control
  220. self.send_data(0xf9)
  221. self.send_data(0x00)
  222. self.send_data(0x00)
  223. self.send_command(0x11) #data entry mode
  224. self.send_data(0x03)
  225. self.SetWindow(0, 0, self.width-1, self.height-1)
  226. self.SetCursor(0, 0)
  227. self.send_command(0x3c)
  228. self.send_data(0x05)
  229. self.send_command(0x21) # Display update control
  230. self.send_data(0x00)
  231. self.send_data(0x80)
  232. self.send_command(0x18)
  233. self.send_data(0x80)
  234. self.ReadBusy()
  235. self.SetLut(self.lut_full_update)
  236. return 0
  237. '''
  238. function : Display images
  239. parameter:
  240. image : Image data
  241. '''
  242. def getbuffer(self, image):
  243. img = image
  244. imwidth, imheight = img.size
  245. if(imwidth == self.width and imheight == self.height):
  246. img = img.convert('1')
  247. elif(imwidth == self.height and imheight == self.width):
  248. # image has correct dimensions, but needs to be rotated
  249. img = img.rotate(90, expand=True).convert('1')
  250. else:
  251. logger.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
  252. # return a blank buffer
  253. return [0x00] * (int(self.width/8) * self.height)
  254. buf = bytearray(img.tobytes('raw'))
  255. return buf
  256. '''
  257. function : Sends the image buffer in RAM to e-Paper and displays
  258. parameter:
  259. image : Image data
  260. '''
  261. def display(self, image):
  262. if self.width%8 == 0:
  263. linewidth = int(self.width/8)
  264. else:
  265. linewidth = int(self.width/8) + 1
  266. self.send_command(0x24)
  267. for j in range(0, self.height):
  268. for i in range(0, linewidth):
  269. self.send_data(image[i + j * linewidth])
  270. self.TurnOnDisplay()
  271. '''
  272. function : Sends the image buffer in RAM to e-Paper and partial refresh
  273. parameter:
  274. image : Image data
  275. '''
  276. def displayPartial(self, image):
  277. epdconfig.digital_write(self.reset_pin, 0)
  278. epdconfig.delay_ms(1)
  279. epdconfig.digital_write(self.reset_pin, 1)
  280. self.SetLut(self.lut_partial_update)
  281. self.send_command(0x37)
  282. self.send_data(0x00)
  283. self.send_data(0x00)
  284. self.send_data(0x00)
  285. self.send_data(0x00)
  286. self.send_data(0x00)
  287. self.send_data(0x40)
  288. self.send_data(0x00)
  289. self.send_data(0x00)
  290. self.send_data(0x00)
  291. self.send_data(0x00)
  292. self.send_command(0x3C) #BorderWavefrom
  293. self.send_data(0x80)
  294. self.send_command(0x22)
  295. self.send_data(0xC0)
  296. self.send_command(0x20)
  297. self.ReadBusy()
  298. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  299. self.SetCursor(0, 0)
  300. self.send_command(0x24) # WRITE_RAM
  301. # for j in range(0, self.height):
  302. # for i in range(0, linewidth):
  303. # self.send_data(image[i + j * linewidth])
  304. self.send_data2(image)
  305. self.TurnOnDisplayPart()
  306. '''
  307. function : Refresh a base image
  308. parameter:
  309. image : Image data
  310. '''
  311. def displayPartBaseImage(self, image):
  312. self.send_command(0x24)
  313. self.send_data2(image)
  314. self.send_command(0x26)
  315. self.send_data2(image)
  316. self.TurnOnDisplay()
  317. '''
  318. function : Clear screen
  319. parameter:
  320. '''
  321. def Clear(self, color=0xFF):
  322. if self.width%8 == 0:
  323. linewidth = int(self.width/8)
  324. else:
  325. linewidth = int(self.width/8) + 1
  326. # logger.debug(linewidth)
  327. self.send_command(0x24)
  328. self.send_data2([color] * int(self.height * linewidth))
  329. self.TurnOnDisplay()
  330. '''
  331. function : Enter sleep mode
  332. parameter:
  333. '''
  334. def sleep(self):
  335. self.send_command(0x10) #enter deep sleep
  336. self.send_data(0x01)
  337. epdconfig.delay_ms(2000)
  338. epdconfig.module_exit()
  339. ### END OF FILE ###