EPD_4in2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*****************************************************************************
  2. * | File : EPD_4in2.c
  3. * | Author : Waveshare team
  4. * | Function : 4.2inch e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V3.1
  8. * | Date : 2019-11-14
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. * V3.1(2019-11-14):
  12. * 1.Add 4 grayscale drive and display program
  13. * Add EPD_4IN2_4Gray_lut_vcom[]
  14. * Add EPD_4IN2_4Gray_lut_ww[]
  15. * Add EPD_4IN2_4Gray_lut_bw[]
  16. * Add EPD_4IN2_4Gray_lut_wb
  17. * Add EPD_4IN2_4Gray_lut_bb
  18. * Add EPD_4IN2_Partial_SetLut()
  19. * Add EPD_4IN2_4Gray_lut()
  20. * Add EPD_4IN2_Init_4Gray()
  21. * Add EPD_4IN2_4GrayDisplay(....)
  22. * 2.Add partial refresh display
  23. * Add EPD_4IN2_Partial_lut_vcom1[]
  24. * Add EPD_4IN2_Partial_lut_ww1[]
  25. * Add EPD_4IN2_Partial_lut_bw1[]
  26. * Add EPD_4IN2_Partial_lut_wb1[]
  27. * Add EPD_4IN2_Partial_lut_bb1[]
  28. * Add EPD_4IN2_Partial_SetLut()
  29. * Add EPD_4IN2_PartialDisplay(...)
  30. * Poor display, no display function by default
  31. *
  32. * V3.0(2019-06-13):
  33. * 1.Change:
  34. * lut_vcomDC[] => EPD_4IN2_lut_vcomDC[]
  35. * lut_ww[] => EPD_4IN2_lut_ww[]
  36. * lut_bw[] => EPD_4IN2_lut_bw[]
  37. * lut_wb[] => EPD_4IN2_lut_wb[]
  38. * lut_bb[] => EPD_4IN2_lut_bb[]
  39. * EPD_Reset() => EPD_4IN2_Reset()
  40. * EPD_SendCommand() => EPD_4IN2_SendCommand()
  41. * EPD_SendData() => EPD_4IN2_SendData()
  42. * EPD_WaitUntilIdle() => EPD_4IN2_ReadBusy()
  43. * EPD_SetFullReg() => EPD_4IN2_SetFullReg()
  44. * EPD_SetPartReg() => EPD_4IN2_SetPartReg()
  45. * EPD_TurnOnDisplay() => EPD_4IN2_TurnOnDisplay()
  46. * EPD_Init() => EPD_4IN2_Init()
  47. * EPD_Clear() => EPD_4IN2_Clear()
  48. * EPD_Display() => EPD_4IN2_Display()
  49. * EPD_Sleep() => EPD_4IN2_Sleep()
  50. * 2.remove commands define:
  51. * #define PANEL_SETTING 0x00
  52. * #define POWER_SETTING 0x01
  53. * #define POWER_OFF 0x02
  54. * #define POWER_OFF_SEQUENCE_SETTING 0x03
  55. * #define POWER_ON 0x04
  56. * #define POWER_ON_MEASURE 0x05
  57. * #define BOOSTER_SOFT_START 0x06
  58. * #define DEEP_SLEEP 0x07
  59. * #define DATA_START_TRANSMISSION_1 0x10
  60. * #define DATA_STOP 0x11
  61. * #define DISPLAY_REFRESH 0x12
  62. * #define DATA_START_TRANSMISSION_2 0x13
  63. * #define VCOM_LUT 0x20
  64. * #define W2W_LUT 0x21
  65. * #define B2W_LUT 0x22
  66. * #define W2B_LUT 0x23
  67. * #define B2B_LUT 0x24
  68. * #define PLL_CONTROL 0x30
  69. * #define TEMPERATURE_SENSOR_CALIBRATION 0x40
  70. * #define TEMPERATURE_SENSOR_SELECTION 0x41
  71. * #define TEMPERATURE_SENSOR_WRITE 0x42
  72. * #define TEMPERATURE_SENSOR_READ 0x43
  73. * #define VCOM_AND_DATA_INTERVAL_SETTING 0x50
  74. * #define LOW_POWER_DETECTION 0x51
  75. * #define TCON_SETTING 0x60
  76. * #define RESOLUTION_SETTING 0x61
  77. * #define GET_STATUS 0x71
  78. * #define AUTO_MEASURE_VCOM 0x80
  79. * #define READ_VCOM_VALUE 0x81
  80. * #define VCM_DC_SETTING 0x82
  81. * #define PARTIAL_WINDOW 0x90
  82. * #define PARTIAL_IN 0x91
  83. * #define PARTIAL_OUT 0x92
  84. * #define PROGRAM_MODE 0xA0
  85. * #define ACTIVE_PROGRAM 0xA1
  86. * #define READ_OTP_DATA 0xA2
  87. * #define POWER_SAVING 0xE3
  88. * V2.0(2018-10-30):
  89. * 1.Remove:ImageBuff[EPD_HEIGHT * EPD_WIDTH / 8]
  90. * 2.Change:EPD_Display(UBYTE *Image)
  91. * Need to pass parameters: pointer to cached data
  92. #
  93. # Permission is hereby granted, free of charge, to any person obtaining a copy
  94. # of this software and associated documnetation files (the "Software"), to deal
  95. # in the Software without restriction, including without limitation the rights
  96. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  97. # copies of the Software, and to permit persons to whom the Software is
  98. # furished to do so, subject to the following conditions:
  99. #
  100. # The above copyright notice and this permission notice shall be included in
  101. # all copies or substantial portions of the Software.
  102. #
  103. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  104. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  105. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  106. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  107. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  108. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  109. # THE SOFTWARE.
  110. #
  111. ******************************************************************************/
  112. #include "EPD_4in2.h"
  113. #include "Debug.h"
  114. static const unsigned char EPD_4IN2_lut_vcom0[] = {
  115. 0x00, 0x17, 0x00, 0x00, 0x00, 0x02,
  116. 0x00, 0x17, 0x17, 0x00, 0x00, 0x02,
  117. 0x00, 0x0A, 0x01, 0x00, 0x00, 0x01,
  118. 0x00, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. };
  123. static const unsigned char EPD_4IN2_lut_ww[] = {
  124. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  125. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  126. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  127. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. };
  132. static const unsigned char EPD_4IN2_lut_bw[] = {
  133. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  134. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  135. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  136. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  140. };
  141. static const unsigned char EPD_4IN2_lut_wb[] = {
  142. 0x80, 0x17, 0x00, 0x00, 0x00, 0x02,
  143. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  144. 0x80, 0x0A, 0x01, 0x00, 0x00, 0x01,
  145. 0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  147. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149. };
  150. static const unsigned char EPD_4IN2_lut_bb[] = {
  151. 0x80, 0x17, 0x00, 0x00, 0x00, 0x02,
  152. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  153. 0x80, 0x0A, 0x01, 0x00, 0x00, 0x01,
  154. 0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  155. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  156. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  157. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  158. };
  159. /******************************partial screen update LUT*********************************/
  160. const unsigned char EPD_4IN2_Partial_lut_vcom1[] = {
  161. 0x00 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  162. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  163. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  164. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  165. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  166. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  167. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  168. 0x00 ,0x00,
  169. };
  170. const unsigned char EPD_4IN2_Partial_lut_ww1[] = {
  171. 0x00 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  172. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  173. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  174. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  175. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  176. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  177. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  178. };
  179. const unsigned char EPD_4IN2_Partial_lut_bw1[] = {
  180. 0x80 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  181. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  182. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  183. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  184. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  185. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  186. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  187. };
  188. const unsigned char EPD_4IN2_Partial_lut_wb1[] = {
  189. 0x40 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  190. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  191. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  192. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  193. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  194. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  195. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  196. };
  197. const unsigned char EPD_4IN2_Partial_lut_bb1[] = {
  198. 0x00 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  199. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  200. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  201. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  202. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  203. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  204. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  205. };
  206. /******************************gray*********************************/
  207. //0~3 gray
  208. const unsigned char EPD_4IN2_4Gray_lut_vcom[] = {
  209. 0x00 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  210. 0x60 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  211. 0x00 ,0x14 ,0x00 ,0x00 ,0x00 ,0x01,
  212. 0x00 ,0x13 ,0x0A ,0x01 ,0x00 ,0x01,
  213. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  214. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  215. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
  216. };
  217. //R21
  218. const unsigned char EPD_4IN2_4Gray_lut_ww[] = {
  219. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  220. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  221. 0x10 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  222. 0xA0 ,0x13 ,0x01 ,0x00 ,0x00 ,0x01,
  223. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  224. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  225. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  226. };
  227. //R22H r
  228. const unsigned char EPD_4IN2_4Gray_lut_bw[] = {
  229. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  230. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  231. 0x00 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  232. 0x99 ,0x0C ,0x01 ,0x03 ,0x04 ,0x01,
  233. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  234. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  235. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  236. };
  237. //R23H w
  238. const unsigned char EPD_4IN2_4Gray_lut_wb[] = {
  239. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  240. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  241. 0x00 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  242. 0x99 ,0x0B ,0x04 ,0x04 ,0x01 ,0x01,
  243. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  244. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  245. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  246. };
  247. //R24H b
  248. const unsigned char EPD_4IN2_4Gray_lut_bb[] = {
  249. 0x80 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  250. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  251. 0x20 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  252. 0x50 ,0x13 ,0x01 ,0x00 ,0x00 ,0x01,
  253. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  254. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  255. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  256. };
  257. /******************************************************************************
  258. function : Software reset
  259. parameter:
  260. ******************************************************************************/
  261. static void EPD_4IN2_Reset(void)
  262. {
  263. DEV_Digital_Write(EPD_RST_PIN, 1);
  264. DEV_Delay_ms(20);
  265. DEV_Digital_Write(EPD_RST_PIN, 0);
  266. DEV_Delay_ms(2);
  267. DEV_Digital_Write(EPD_RST_PIN, 1);
  268. DEV_Delay_ms(20);
  269. DEV_Digital_Write(EPD_RST_PIN, 0);
  270. DEV_Delay_ms(2);
  271. DEV_Digital_Write(EPD_RST_PIN, 1);
  272. DEV_Delay_ms(20);
  273. DEV_Digital_Write(EPD_RST_PIN, 0);
  274. DEV_Delay_ms(2);
  275. DEV_Digital_Write(EPD_RST_PIN, 1);
  276. DEV_Delay_ms(20);
  277. }
  278. /******************************************************************************
  279. function : send command
  280. parameter:
  281. Reg : Command register
  282. ******************************************************************************/
  283. static void EPD_4IN2_SendCommand(UBYTE Reg)
  284. {
  285. DEV_Digital_Write(EPD_DC_PIN, 0);
  286. DEV_Digital_Write(EPD_CS_PIN, 0);
  287. DEV_SPI_WriteByte(Reg);
  288. DEV_Digital_Write(EPD_CS_PIN, 1);
  289. }
  290. /******************************************************************************
  291. function : send data
  292. parameter:
  293. Data : Write data
  294. ******************************************************************************/
  295. static void EPD_4IN2_SendData(UBYTE Data)
  296. {
  297. DEV_Digital_Write(EPD_DC_PIN, 1);
  298. DEV_Digital_Write(EPD_CS_PIN, 0);
  299. DEV_SPI_WriteByte(Data);
  300. DEV_Digital_Write(EPD_CS_PIN, 1);
  301. }
  302. /******************************************************************************
  303. function : Wait until the busy_pin goes LOW
  304. parameter:
  305. ******************************************************************************/
  306. void EPD_4IN2_ReadBusy(void)
  307. {
  308. Debug("e-Paper busy\r\n");
  309. EPD_4IN2_SendCommand(0x71);
  310. while(DEV_Digital_Read(EPD_BUSY_PIN) == 0) { //LOW: idle, HIGH: busy
  311. EPD_4IN2_SendCommand(0x71);
  312. DEV_Delay_ms(100);
  313. }
  314. Debug("e-Paper busy release\r\n");
  315. }
  316. /******************************************************************************
  317. function : Turn On Display
  318. parameter:
  319. ******************************************************************************/
  320. static void EPD_4IN2_TurnOnDisplay(void)
  321. {
  322. EPD_4IN2_SendCommand(0x12);
  323. DEV_Delay_ms(100);
  324. EPD_4IN2_ReadBusy();
  325. }
  326. /******************************************************************************
  327. function : set the look-up tables
  328. parameter:
  329. ******************************************************************************/
  330. static void EPD_4IN2_SetLut(void)
  331. {
  332. UWORD count;
  333. EPD_4IN2_SendCommand(0x20); //g vcom
  334. for(count=0; count<44; count++) {
  335. EPD_4IN2_SendData(EPD_4IN2_lut_vcom0[count]);
  336. }
  337. EPD_4IN2_SendCommand(0x21);
  338. for(count=0; count<42; count++) {
  339. EPD_4IN2_SendData(EPD_4IN2_lut_ww[count]);
  340. }
  341. EPD_4IN2_SendCommand(0x22);
  342. for(count=0; count<42; count++) {
  343. EPD_4IN2_SendData(EPD_4IN2_lut_bw[count]);
  344. }
  345. EPD_4IN2_SendCommand(0x23);
  346. for(count=0; count<42; count++) {
  347. EPD_4IN2_SendData(EPD_4IN2_lut_wb[count]);
  348. }
  349. EPD_4IN2_SendCommand(0x24);
  350. for(count=0; count<42; count++) {
  351. EPD_4IN2_SendData(EPD_4IN2_lut_bb[count]);
  352. }
  353. }
  354. static void EPD_4IN2_Partial_SetLut(void)
  355. {
  356. unsigned int count;
  357. EPD_4IN2_SendCommand(0x20);
  358. for(count=0; count<44; count++) {
  359. EPD_4IN2_SendData(EPD_4IN2_Partial_lut_vcom1[count]);
  360. }
  361. EPD_4IN2_SendCommand(0x21);
  362. for(count=0; count<42; count++) {
  363. EPD_4IN2_SendData(EPD_4IN2_Partial_lut_ww1[count]);
  364. }
  365. EPD_4IN2_SendCommand(0x22);
  366. for(count=0; count<42; count++) {
  367. EPD_4IN2_SendData(EPD_4IN2_Partial_lut_bw1[count]);
  368. }
  369. EPD_4IN2_SendCommand(0x23);
  370. for(count=0; count<42; count++) {
  371. EPD_4IN2_SendData(EPD_4IN2_Partial_lut_wb1[count]);
  372. }
  373. EPD_4IN2_SendCommand(0x24);
  374. for(count=0; count<42; count++) {
  375. EPD_4IN2_SendData(EPD_4IN2_Partial_lut_bb1[count]);
  376. }
  377. }
  378. //LUT download
  379. static void EPD_4IN2_4Gray_lut(void)
  380. {
  381. unsigned int count;
  382. {
  383. EPD_4IN2_SendCommand(0x20); //vcom
  384. for(count=0; count<42; count++) {
  385. EPD_4IN2_SendData(EPD_4IN2_4Gray_lut_vcom[count]);
  386. }
  387. EPD_4IN2_SendCommand(0x21); //red not use
  388. for(count=0; count<42; count++) {
  389. EPD_4IN2_SendData(EPD_4IN2_4Gray_lut_ww[count]);
  390. }
  391. EPD_4IN2_SendCommand(0x22); //bw r
  392. for(count=0; count<42; count++) {
  393. EPD_4IN2_SendData(EPD_4IN2_4Gray_lut_bw[count]);
  394. }
  395. EPD_4IN2_SendCommand(0x23); //wb w
  396. for(count=0; count<42; count++) {
  397. EPD_4IN2_SendData(EPD_4IN2_4Gray_lut_wb[count]);
  398. }
  399. EPD_4IN2_SendCommand(0x24); //bb b
  400. for(count=0; count<42; count++) {
  401. EPD_4IN2_SendData(EPD_4IN2_4Gray_lut_bb[count]);
  402. }
  403. EPD_4IN2_SendCommand(0x25); //vcom
  404. for(count=0; count<42; count++) {
  405. EPD_4IN2_SendData(EPD_4IN2_4Gray_lut_ww[count]);
  406. }
  407. }
  408. }
  409. /******************************************************************************
  410. function : Initialize the e-Paper register
  411. parameter:
  412. ******************************************************************************/
  413. void EPD_4IN2_Init(void)
  414. {
  415. EPD_4IN2_Reset();
  416. EPD_4IN2_SendCommand(0x01); // POWER SETTING
  417. EPD_4IN2_SendData(0x03);
  418. EPD_4IN2_SendData(0x00);
  419. EPD_4IN2_SendData(0x2b);
  420. EPD_4IN2_SendData(0x2b);
  421. EPD_4IN2_SendCommand(0x06); // boost soft start
  422. EPD_4IN2_SendData(0x17); //A
  423. EPD_4IN2_SendData(0x17); //B
  424. EPD_4IN2_SendData(0x17); //C
  425. EPD_4IN2_SendCommand(0x04); // POWER_ON
  426. EPD_4IN2_ReadBusy();
  427. EPD_4IN2_SendCommand(0x00); // panel setting
  428. EPD_4IN2_SendData(0xbf); // KW-BF KWR-AF BWROTP 0f BWOTP 1f
  429. EPD_4IN2_SendData(0x0d);
  430. EPD_4IN2_SendCommand(0x30); // PLL setting
  431. EPD_4IN2_SendData(0x3C); // 3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  432. EPD_4IN2_SendCommand(0x61); // resolution setting
  433. EPD_4IN2_SendData(0x01);
  434. EPD_4IN2_SendData(0x90); //128
  435. EPD_4IN2_SendData(0x01); //
  436. EPD_4IN2_SendData(0x2c);
  437. EPD_4IN2_SendCommand(0x82); // vcom_DC setting
  438. EPD_4IN2_SendData(0x28);
  439. EPD_4IN2_SendCommand(0X50); // VCOM AND DATA INTERVAL SETTING
  440. EPD_4IN2_SendData(0x97); // 97white border 77black border VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7
  441. EPD_4IN2_SetLut();
  442. }
  443. void EPD_4IN2_Init_4Gray(void)
  444. {
  445. EPD_4IN2_Reset();
  446. EPD_4IN2_SendCommand(0x01); //POWER SETTING
  447. EPD_4IN2_SendData (0x03);
  448. EPD_4IN2_SendData (0x00); //VGH=20V,VGL=-20V
  449. EPD_4IN2_SendData (0x2b); //VDH=15V
  450. EPD_4IN2_SendData (0x2b); //VDL=-15V
  451. EPD_4IN2_SendData (0x13);
  452. EPD_4IN2_SendCommand(0x06); //booster soft start
  453. EPD_4IN2_SendData (0x17); //A
  454. EPD_4IN2_SendData (0x17); //B
  455. EPD_4IN2_SendData (0x17); //C
  456. EPD_4IN2_SendCommand(0x04);
  457. EPD_4IN2_ReadBusy();
  458. EPD_4IN2_SendCommand(0x00); //panel setting
  459. EPD_4IN2_SendData(0x3f); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  460. EPD_4IN2_SendCommand(0x30); //PLL setting
  461. EPD_4IN2_SendData (0x3c); //100hz
  462. EPD_4IN2_SendCommand(0x61); //resolution setting
  463. EPD_4IN2_SendData (0x01); //400
  464. EPD_4IN2_SendData (0x90);
  465. EPD_4IN2_SendData (0x01); //300
  466. EPD_4IN2_SendData (0x2c);
  467. EPD_4IN2_SendCommand(0x82); //vcom_DC setting
  468. EPD_4IN2_SendData (0x12);
  469. EPD_4IN2_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  470. EPD_4IN2_SendData(0x97);
  471. }
  472. /******************************************************************************
  473. function : Clear screen
  474. parameter:
  475. ******************************************************************************/
  476. void EPD_4IN2_Clear(void)
  477. {
  478. UWORD Width, Height;
  479. Width = (EPD_4IN2_WIDTH % 8 == 0)? (EPD_4IN2_WIDTH / 8 ): (EPD_4IN2_WIDTH / 8 + 1);
  480. Height = EPD_4IN2_HEIGHT;
  481. EPD_4IN2_SendCommand(0x10);
  482. for (UWORD j = 0; j < Height; j++) {
  483. for (UWORD i = 0; i < Width; i++) {
  484. EPD_4IN2_SendData(0xFF);
  485. }
  486. }
  487. EPD_4IN2_SendCommand(0x13);
  488. for (UWORD j = 0; j < Height; j++) {
  489. for (UWORD i = 0; i < Width; i++) {
  490. EPD_4IN2_SendData(0xFF);
  491. }
  492. }
  493. EPD_4IN2_SendCommand(0x12); //DISPLAY REFRESH
  494. DEV_Delay_ms(10);
  495. EPD_4IN2_TurnOnDisplay();
  496. }
  497. /******************************************************************************
  498. function : Sends the image buffer in RAM to e-Paper and displays
  499. parameter:
  500. ******************************************************************************/
  501. void EPD_4IN2_Display(UBYTE *Image)
  502. {
  503. UWORD Width, Height;
  504. Width = (EPD_4IN2_WIDTH % 8 == 0)? (EPD_4IN2_WIDTH / 8 ): (EPD_4IN2_WIDTH / 8 + 1);
  505. Height = EPD_4IN2_HEIGHT;
  506. EPD_4IN2_SendCommand(0x13);
  507. for (UWORD j = 0; j < Height; j++) {
  508. for (UWORD i = 0; i < Width; i++) {
  509. EPD_4IN2_SendData(Image[i + j * Width]);
  510. }
  511. }
  512. EPD_4IN2_TurnOnDisplay();
  513. }
  514. void EPD_4IN2_PartialDisplay(UWORD X_start,UWORD Y_start,UWORD X_end,UWORD Y_end, UBYTE *Image)
  515. {
  516. UWORD Width;
  517. Width = (EPD_4IN2_WIDTH % 8 == 0)? (EPD_4IN2_WIDTH / 8 ): (EPD_4IN2_WIDTH / 8 + 1);
  518. X_start = (X_start % 8 == 0)? (X_start): (X_start/8*8+8);
  519. X_end = (X_end % 8 == 0)? (X_end): (X_end/8*8+8);
  520. EPD_4IN2_SendCommand(0X50);
  521. EPD_4IN2_SendData(0xf7);
  522. DEV_Delay_ms(100);
  523. EPD_4IN2_SendCommand(0x82); //vcom_DC setting
  524. EPD_4IN2_SendData (0x08);
  525. EPD_4IN2_SendCommand(0X50);
  526. EPD_4IN2_SendData(0x47);
  527. EPD_4IN2_Partial_SetLut();
  528. EPD_4IN2_SendCommand(0x91); //This command makes the display enter partial mode
  529. EPD_4IN2_SendCommand(0x90); //resolution setting
  530. EPD_4IN2_SendData ((X_start)/256);
  531. EPD_4IN2_SendData ((X_start)%256); //x-start
  532. EPD_4IN2_SendData ((X_end )/256);
  533. EPD_4IN2_SendData ((X_end )%256-1); //x-end
  534. EPD_4IN2_SendData (Y_start/256);
  535. EPD_4IN2_SendData (Y_start%256); //y-start
  536. EPD_4IN2_SendData (Y_end/256);
  537. EPD_4IN2_SendData (Y_end%256-1); //y-end
  538. EPD_4IN2_SendData (0x28);
  539. EPD_4IN2_SendCommand(0x10); //writes Old data to SRAM for programming
  540. for (UWORD j = 0; j < Y_end - Y_start; j++) {
  541. for (UWORD i = 0; i < (X_end - X_start)/8; i++) {
  542. EPD_4IN2_SendData(Image[(Y_start + j)*Width + X_start/8 + i]);
  543. }
  544. }
  545. EPD_4IN2_SendCommand(0x13); //writes New data to SRAM.
  546. for (UWORD j = 0; j < Y_end - Y_start; j++) {
  547. for (UWORD i = 0; i < (X_end - X_start)/8; i++) {
  548. EPD_4IN2_SendData(~Image[(Y_start + j)*Width + X_start/8 + i]);
  549. }
  550. }
  551. EPD_4IN2_SendCommand(0x12); //DISPLAY REFRESH
  552. DEV_Delay_ms(10); //The delay here is necessary, 200uS at least!!!
  553. EPD_4IN2_TurnOnDisplay();
  554. }
  555. void EPD_4IN2_4GrayDisplay(const UBYTE *Image)
  556. {
  557. UDOUBLE i,j,k;
  558. UBYTE temp1,temp2,temp3;
  559. /****Color display description****
  560. white gray1 gray2 black
  561. 0x10| 01 01 00 00
  562. 0x13| 01 00 01 00
  563. *********************************/
  564. EPD_4IN2_SendCommand(0x10);
  565. for(i=0; i<15000; i++) { //5808*4 46464
  566. temp3=0;
  567. for(j=0; j<2; j++) {
  568. temp1 = Image[i*2+j];
  569. for(k=0; k<2; k++) {
  570. temp2 = temp1&0xC0 ;
  571. if(temp2 == 0xC0)
  572. temp3 |= 0x01;//white
  573. else if(temp2 == 0x00)
  574. temp3 |= 0x00; //black
  575. else if(temp2 == 0x80)
  576. temp3 |= 0x01; //gray1
  577. else //0x40
  578. temp3 |= 0x00; //gray2
  579. temp3 <<= 1;
  580. temp1 <<= 2;
  581. temp2 = temp1&0xC0 ;
  582. if(temp2 == 0xC0) //white
  583. temp3 |= 0x01;
  584. else if(temp2 == 0x00) //black
  585. temp3 |= 0x00;
  586. else if(temp2 == 0x80)
  587. temp3 |= 0x01; //gray1
  588. else //0x40
  589. temp3 |= 0x00; //gray2
  590. if(j!=1 || k!=1)
  591. temp3 <<= 1;
  592. temp1 <<= 2;
  593. }
  594. }
  595. EPD_4IN2_SendData(temp3);
  596. }
  597. // new data
  598. EPD_4IN2_SendCommand(0x13);
  599. for(i=0; i<15000; i++) { //5808*4 46464
  600. temp3=0;
  601. for(j=0; j<2; j++) {
  602. temp1 = Image[i*2+j];
  603. for(k=0; k<2; k++) {
  604. temp2 = temp1&0xC0 ;
  605. if(temp2 == 0xC0)
  606. temp3 |= 0x01;//white
  607. else if(temp2 == 0x00)
  608. temp3 |= 0x00; //black
  609. else if(temp2 == 0x80)
  610. temp3 |= 0x00; //gray1
  611. else //0x40
  612. temp3 |= 0x01; //gray2
  613. temp3 <<= 1;
  614. temp1 <<= 2;
  615. temp2 = temp1&0xC0 ;
  616. if(temp2 == 0xC0) //white
  617. temp3 |= 0x01;
  618. else if(temp2 == 0x00) //black
  619. temp3 |= 0x00;
  620. else if(temp2 == 0x80)
  621. temp3 |= 0x00; //gray1
  622. else //0x40
  623. temp3 |= 0x01; //gray2
  624. if(j!=1 || k!=1)
  625. temp3 <<= 1;
  626. temp1 <<= 2;
  627. }
  628. }
  629. EPD_4IN2_SendData(temp3);
  630. }
  631. EPD_4IN2_4Gray_lut();
  632. EPD_4IN2_TurnOnDisplay();
  633. }
  634. /******************************************************************************
  635. function : Enter sleep mode
  636. parameter:
  637. ******************************************************************************/
  638. void EPD_4IN2_Sleep(void)
  639. {
  640. EPD_4IN2_SendCommand(0x02); // POWER_OFF
  641. EPD_4IN2_ReadBusy();
  642. EPD_4IN2_SendCommand(0x07); // DEEP_SLEEP
  643. EPD_4IN2_SendData(0XA5);
  644. }