u8x8_cad.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. u8x8_cad.c
  3. "command arg data" interface to the graphics controller
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. The following sequence must be used for any data, which is set to the display:
  28. uint8_t u8x8_cad_StartTransfer(u8x8_t *u8x8)
  29. any of the following calls
  30. uint8_t u8x8_cad_SendCmd(u8x8_t *u8x8, uint8_t cmd)
  31. uint8_t u8x8_cad_SendArg(u8x8_t *u8x8, uint8_t arg)
  32. uint8_t u8x8_cad_SendData(u8x8_t *u8x8, uint8_t cnt, uint8_t *data)
  33. uint8_t u8x8_cad_EndTransfer(u8x8_t *u8x8)
  34. */
  35. /*
  36. uint8_t u8x8_cad_template(u8x8_t *u8x8, uint8_t msg, uint16_t arg_int, void *arg_ptr)
  37. {
  38. uint8_t i;
  39. switch(msg)
  40. {
  41. case U8X8_MSG_CAD_SEND_CMD:
  42. u8x8_mcd_byte_SetDC(mcd->next, 1);
  43. u8x8_mcd_byte_Send(mcd->next, arg_int);
  44. break;
  45. case U8X8_MSG_CAD_SEND_ARG:
  46. u8x8_mcd_byte_SetDC(mcd->next, 1);
  47. u8x8_mcd_byte_Send(mcd->next, arg_int);
  48. break;
  49. case U8X8_MSG_CAD_SEND_DATA:
  50. u8x8_mcd_byte_SetDC(mcd->next, 0);
  51. for( i = 0; i < 8; i++ )
  52. u8x8_mcd_byte_Send(mcd->next, ((uint8_t *)arg_ptr)[i]);
  53. break;
  54. case U8X8_MSG_CAD_RESET:
  55. return mcd->next->cb(mcd->next, msg, arg_int, arg_ptr);
  56. case U8X8_MSG_CAD_START_TRANSFER:
  57. return mcd->next->cb(mcd->next, msg, arg_int, arg_ptr);
  58. case U8X8_MSG_CAD_END_TRANSFER:
  59. return mcd->next->cb(mcd->next, msg, arg_int, arg_ptr);
  60. default:
  61. break;
  62. }
  63. return 1;
  64. }
  65. */
  66. #include "u8x8.h"
  67. uint8_t u8x8_cad_SendCmd(u8x8_t *u8x8, uint8_t cmd)
  68. {
  69. return u8x8->cad_cb(u8x8, U8X8_MSG_CAD_SEND_CMD, cmd, NULL);
  70. }
  71. uint8_t u8x8_cad_SendArg(u8x8_t *u8x8, uint8_t arg)
  72. {
  73. return u8x8->cad_cb(u8x8, U8X8_MSG_CAD_SEND_ARG, arg, NULL);
  74. }
  75. uint8_t u8x8_cad_SendMultipleArg(u8x8_t *u8x8, uint8_t cnt, uint8_t arg)
  76. {
  77. while( cnt > 0 )
  78. {
  79. u8x8->cad_cb(u8x8, U8X8_MSG_CAD_SEND_ARG, arg, NULL);
  80. cnt--;
  81. }
  82. return 1;
  83. }
  84. uint8_t u8x8_cad_SendData(u8x8_t *u8x8, uint8_t cnt, uint8_t *data)
  85. {
  86. return u8x8->cad_cb(u8x8, U8X8_MSG_CAD_SEND_DATA, cnt, data);
  87. }
  88. uint8_t u8x8_cad_StartTransfer(u8x8_t *u8x8)
  89. {
  90. return u8x8->cad_cb(u8x8, U8X8_MSG_CAD_START_TRANSFER, 0, NULL);
  91. }
  92. uint8_t u8x8_cad_EndTransfer(u8x8_t *u8x8)
  93. {
  94. return u8x8->cad_cb(u8x8, U8X8_MSG_CAD_END_TRANSFER, 0, NULL);
  95. }
  96. void u8x8_cad_vsendf(u8x8_t * u8x8, const char *fmt, va_list va)
  97. {
  98. uint8_t d;
  99. u8x8_cad_StartTransfer(u8x8);
  100. while( *fmt != '\0' )
  101. {
  102. d = (uint8_t)va_arg(va, int);
  103. switch(*fmt)
  104. {
  105. case 'a': u8x8_cad_SendArg(u8x8, d); break;
  106. case 'c': u8x8_cad_SendCmd(u8x8, d); break;
  107. case 'd': u8x8_cad_SendData(u8x8, 1, &d); break;
  108. }
  109. fmt++;
  110. }
  111. u8x8_cad_EndTransfer(u8x8);
  112. }
  113. void u8x8_SendF(u8x8_t * u8x8, const char *fmt, ...)
  114. {
  115. va_list va;
  116. va_start(va, fmt);
  117. u8x8_cad_vsendf(u8x8, fmt, va);
  118. va_end(va);
  119. }
  120. /*
  121. 21 c send command c
  122. 22 a send arg a
  123. 23 d send data d
  124. 24 CS on
  125. 25 CS off
  126. 254 milli delay by milliseconds
  127. 255 end of sequence
  128. */
  129. void u8x8_cad_SendSequence(u8x8_t *u8x8, uint8_t const *data)
  130. {
  131. uint8_t cmd;
  132. uint8_t v;
  133. for(;;)
  134. {
  135. cmd = *data;
  136. data++;
  137. switch( cmd )
  138. {
  139. case U8X8_MSG_CAD_SEND_CMD:
  140. case U8X8_MSG_CAD_SEND_ARG:
  141. v = *data;
  142. u8x8->cad_cb(u8x8, cmd, v, NULL);
  143. data++;
  144. break;
  145. case U8X8_MSG_CAD_SEND_DATA:
  146. v = *data;
  147. u8x8_cad_SendData(u8x8, 1, &v);
  148. data++;
  149. break;
  150. case U8X8_MSG_CAD_START_TRANSFER:
  151. case U8X8_MSG_CAD_END_TRANSFER:
  152. u8x8->cad_cb(u8x8, cmd, 0, NULL);
  153. break;
  154. case 0x0fe:
  155. v = *data;
  156. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_MILLI, v);
  157. data++;
  158. break;
  159. default:
  160. return;
  161. }
  162. }
  163. }
  164. uint8_t u8x8_cad_empty(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  165. {
  166. switch(msg)
  167. {
  168. case U8X8_MSG_CAD_SEND_CMD:
  169. u8x8_byte_SendByte(u8x8, arg_int);
  170. break;
  171. case U8X8_MSG_CAD_SEND_ARG:
  172. u8x8_byte_SendByte(u8x8, arg_int);
  173. break;
  174. case U8X8_MSG_CAD_SEND_DATA:
  175. case U8X8_MSG_CAD_INIT:
  176. case U8X8_MSG_CAD_START_TRANSFER:
  177. case U8X8_MSG_CAD_END_TRANSFER:
  178. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  179. default:
  180. return 0;
  181. }
  182. return 1;
  183. }
  184. /*
  185. convert to bytes by using
  186. dc = 1 for commands and args and
  187. dc = 0 for data
  188. */
  189. uint8_t u8x8_cad_110(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  190. {
  191. switch(msg)
  192. {
  193. case U8X8_MSG_CAD_SEND_CMD:
  194. u8x8_byte_SetDC(u8x8, 1);
  195. u8x8_byte_SendByte(u8x8, arg_int);
  196. break;
  197. case U8X8_MSG_CAD_SEND_ARG:
  198. u8x8_byte_SetDC(u8x8, 1);
  199. u8x8_byte_SendByte(u8x8, arg_int);
  200. break;
  201. case U8X8_MSG_CAD_SEND_DATA:
  202. u8x8_byte_SetDC(u8x8, 0);
  203. //u8x8_byte_SendBytes(u8x8, arg_int, arg_ptr);
  204. //break;
  205. /* fall through */
  206. case U8X8_MSG_CAD_INIT:
  207. case U8X8_MSG_CAD_START_TRANSFER:
  208. case U8X8_MSG_CAD_END_TRANSFER:
  209. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  210. default:
  211. return 0;
  212. }
  213. return 1;
  214. }
  215. /*
  216. convert to bytes by using
  217. dc = 1 for commands and args and
  218. dc = 0 for data
  219. t6963
  220. */
  221. uint8_t u8x8_cad_100(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  222. {
  223. switch(msg)
  224. {
  225. case U8X8_MSG_CAD_SEND_CMD:
  226. u8x8_byte_SetDC(u8x8, 1);
  227. u8x8_byte_SendByte(u8x8, arg_int);
  228. break;
  229. case U8X8_MSG_CAD_SEND_ARG:
  230. u8x8_byte_SetDC(u8x8, 0);
  231. u8x8_byte_SendByte(u8x8, arg_int);
  232. break;
  233. case U8X8_MSG_CAD_SEND_DATA:
  234. u8x8_byte_SetDC(u8x8, 0);
  235. //u8x8_byte_SendBytes(u8x8, arg_int, arg_ptr);
  236. //break;
  237. /* fall through */
  238. case U8X8_MSG_CAD_INIT:
  239. case U8X8_MSG_CAD_START_TRANSFER:
  240. case U8X8_MSG_CAD_END_TRANSFER:
  241. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  242. default:
  243. return 0;
  244. }
  245. return 1;
  246. }
  247. /*
  248. convert to bytes by using
  249. dc = 0 for commands and args and
  250. dc = 1 for data
  251. */
  252. uint8_t u8x8_cad_001(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  253. {
  254. switch(msg)
  255. {
  256. case U8X8_MSG_CAD_SEND_CMD:
  257. u8x8_byte_SetDC(u8x8, 0);
  258. u8x8_byte_SendByte(u8x8, arg_int);
  259. break;
  260. case U8X8_MSG_CAD_SEND_ARG:
  261. u8x8_byte_SetDC(u8x8, 0);
  262. u8x8_byte_SendByte(u8x8, arg_int);
  263. break;
  264. case U8X8_MSG_CAD_SEND_DATA:
  265. u8x8_byte_SetDC(u8x8, 1);
  266. //u8x8_byte_SendBytes(u8x8, arg_int, arg_ptr);
  267. //break;
  268. /* fall through */
  269. case U8X8_MSG_CAD_INIT:
  270. case U8X8_MSG_CAD_START_TRANSFER:
  271. case U8X8_MSG_CAD_END_TRANSFER:
  272. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  273. default:
  274. return 0;
  275. }
  276. return 1;
  277. }
  278. /*
  279. convert to bytes by using
  280. dc = 0 for commands
  281. dc = 1 for args and data
  282. */
  283. uint8_t u8x8_cad_011(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  284. {
  285. switch(msg)
  286. {
  287. case U8X8_MSG_CAD_SEND_CMD:
  288. u8x8_byte_SetDC(u8x8, 0);
  289. u8x8_byte_SendByte(u8x8, arg_int);
  290. break;
  291. case U8X8_MSG_CAD_SEND_ARG:
  292. u8x8_byte_SetDC(u8x8, 1);
  293. u8x8_byte_SendByte(u8x8, arg_int);
  294. break;
  295. case U8X8_MSG_CAD_SEND_DATA:
  296. u8x8_byte_SetDC(u8x8, 1);
  297. //u8x8_byte_SendBytes(u8x8, arg_int, arg_ptr);
  298. //break;
  299. /* fall through */
  300. case U8X8_MSG_CAD_INIT:
  301. case U8X8_MSG_CAD_START_TRANSFER:
  302. case U8X8_MSG_CAD_END_TRANSFER:
  303. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  304. default:
  305. return 0;
  306. }
  307. return 1;
  308. }
  309. /* cad procedure for the ST7920 in SPI mode */
  310. /* u8x8_byte_SetDC is not used */
  311. uint8_t u8x8_cad_st7920_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  312. {
  313. uint8_t *data;
  314. uint8_t b;
  315. uint8_t i;
  316. static uint8_t buf[16];
  317. uint8_t *ptr;
  318. switch(msg)
  319. {
  320. case U8X8_MSG_CAD_SEND_CMD:
  321. u8x8_byte_SendByte(u8x8, 0x0f8);
  322. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, 1);
  323. u8x8_byte_SendByte(u8x8, arg_int & 0x0f0);
  324. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, 1);
  325. u8x8_byte_SendByte(u8x8, arg_int << 4);
  326. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, 1);
  327. break;
  328. case U8X8_MSG_CAD_SEND_ARG:
  329. u8x8_byte_SendByte(u8x8, 0x0f8);
  330. u8x8_byte_SendByte(u8x8, arg_int & 0x0f0);
  331. u8x8_byte_SendByte(u8x8, arg_int << 4);
  332. break;
  333. case U8X8_MSG_CAD_SEND_DATA:
  334. u8x8_byte_SendByte(u8x8, 0x0fa);
  335. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, 1);
  336. /* this loop should be optimized: multiple bytes should be sent */
  337. /* u8x8_byte_SendBytes(u8x8, arg_int, arg_ptr); */
  338. data = (uint8_t *)arg_ptr;
  339. /* the following loop increases speed by 20% */
  340. while( arg_int >= 8 )
  341. {
  342. i = 8;
  343. ptr = buf;
  344. do
  345. {
  346. b = *data++;
  347. *ptr++= b & 0x0f0;
  348. b <<= 4;
  349. *ptr++= b;
  350. i--;
  351. } while( i > 0 );
  352. arg_int -= 8;
  353. u8x8_byte_SendBytes(u8x8, 16, buf);
  354. }
  355. while( arg_int > 0 )
  356. {
  357. b = *data;
  358. u8x8_byte_SendByte(u8x8, b & 0x0f0);
  359. u8x8_byte_SendByte(u8x8, b << 4);
  360. data++;
  361. arg_int--;
  362. }
  363. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, 1);
  364. break;
  365. case U8X8_MSG_CAD_INIT:
  366. case U8X8_MSG_CAD_START_TRANSFER:
  367. case U8X8_MSG_CAD_END_TRANSFER:
  368. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  369. default:
  370. return 0;
  371. }
  372. return 1;
  373. }
  374. /* cad procedure for the SSD13xx family in I2C mode */
  375. /* this procedure is also used by the ST7588 */
  376. /* u8x8_byte_SetDC is not used */
  377. /* U8X8_MSG_BYTE_START_TRANSFER starts i2c transfer, U8X8_MSG_BYTE_END_TRANSFER stops transfer */
  378. /* After transfer start, a full byte indicates command or data mode */
  379. static void u8x8_i2c_data_transfer(u8x8_t *u8x8, uint8_t arg_int, void *arg_ptr) U8X8_NOINLINE;
  380. static void u8x8_i2c_data_transfer(u8x8_t *u8x8, uint8_t arg_int, void *arg_ptr)
  381. {
  382. u8x8_byte_StartTransfer(u8x8);
  383. u8x8_byte_SendByte(u8x8, 0x040);
  384. u8x8->byte_cb(u8x8, U8X8_MSG_CAD_SEND_DATA, arg_int, arg_ptr);
  385. u8x8_byte_EndTransfer(u8x8);
  386. }
  387. /* classic version: will put a start/stop condition around each command and arg */
  388. uint8_t u8x8_cad_ssd13xx_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  389. {
  390. uint8_t *p;
  391. switch(msg)
  392. {
  393. case U8X8_MSG_CAD_SEND_CMD:
  394. case U8X8_MSG_CAD_SEND_ARG:
  395. /* 7 Nov 2016: Can this be improved? */
  396. //u8x8_byte_SetDC(u8x8, 0);
  397. u8x8_byte_StartTransfer(u8x8);
  398. //u8x8_byte_SendByte(u8x8, u8x8_GetI2CAddress(u8x8));
  399. u8x8_byte_SendByte(u8x8, 0x000);
  400. u8x8_byte_SendByte(u8x8, arg_int);
  401. u8x8_byte_EndTransfer(u8x8);
  402. break;
  403. case U8X8_MSG_CAD_SEND_DATA:
  404. //u8x8_byte_SetDC(u8x8, 1);
  405. /* the FeatherWing OLED with the 32u4 transfer of long byte */
  406. /* streams was not possible. This is broken down to */
  407. /* smaller streams, 32 seems to be the limit... */
  408. /* I guess this is related to the size of the Wire buffers in Arduino */
  409. /* Unfortunately, this can not be handled in the byte level drivers, */
  410. /* so this is done here. Even further, only 24 bytes will be sent, */
  411. /* because there will be another byte (DC) required during the transfer */
  412. p = arg_ptr;
  413. while( arg_int > 24 )
  414. {
  415. u8x8_i2c_data_transfer(u8x8, 24, p);
  416. arg_int-=24;
  417. p+=24;
  418. }
  419. u8x8_i2c_data_transfer(u8x8, arg_int, p);
  420. break;
  421. case U8X8_MSG_CAD_INIT:
  422. /* apply default i2c adr if required so that the start transfer msg can use this */
  423. if ( u8x8->i2c_address == 255 )
  424. u8x8->i2c_address = 0x078;
  425. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  426. case U8X8_MSG_CAD_START_TRANSFER:
  427. case U8X8_MSG_CAD_END_TRANSFER:
  428. /* cad transfer commands are ignored */
  429. break;
  430. default:
  431. return 0;
  432. }
  433. return 1;
  434. }
  435. /* fast version with reduced data start/stops, issue 735 */
  436. uint8_t u8x8_cad_ssd13xx_fast_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  437. {
  438. static uint8_t in_transfer = 0;
  439. uint8_t *p;
  440. switch(msg)
  441. {
  442. case U8X8_MSG_CAD_SEND_CMD:
  443. /* improved version, takeover from ld7032 */
  444. /* assumes, that the args of a command is not longer than 31 bytes */
  445. /* speed improvement is about 4% compared to the classic version */
  446. if ( in_transfer != 0 )
  447. u8x8_byte_EndTransfer(u8x8);
  448. u8x8_byte_StartTransfer(u8x8);
  449. u8x8_byte_SendByte(u8x8, 0x000); /* cmd byte for ssd13xx controller */
  450. u8x8_byte_SendByte(u8x8, arg_int);
  451. in_transfer = 1;
  452. /* lightning version: can replace the improved version from above */
  453. /* the drawback of the lightning version is this: The complete init sequence */
  454. /* must fit into the 32 byte Arduino Wire buffer, which might not always be the case */
  455. /* speed improvement is about 6% compared to the classic version */
  456. // if ( in_transfer == 0 )
  457. // {
  458. // u8x8_byte_StartTransfer(u8x8);
  459. // u8x8_byte_SendByte(u8x8, 0x000); /* cmd byte for ssd13xx controller */
  460. // in_transfer = 1;
  461. // }
  462. //u8x8_byte_SendByte(u8x8, arg_int);
  463. break;
  464. case U8X8_MSG_CAD_SEND_ARG:
  465. u8x8_byte_SendByte(u8x8, arg_int);
  466. break;
  467. case U8X8_MSG_CAD_SEND_DATA:
  468. if ( in_transfer != 0 )
  469. u8x8_byte_EndTransfer(u8x8);
  470. /* the FeatherWing OLED with the 32u4 transfer of long byte */
  471. /* streams was not possible. This is broken down to */
  472. /* smaller streams, 32 seems to be the limit... */
  473. /* I guess this is related to the size of the Wire buffers in Arduino */
  474. /* Unfortunately, this can not be handled in the byte level drivers, */
  475. /* so this is done here. Even further, only 24 bytes will be sent, */
  476. /* because there will be another byte (DC) required during the transfer */
  477. p = arg_ptr;
  478. while( arg_int > 24 )
  479. {
  480. u8x8_i2c_data_transfer(u8x8, 24, p);
  481. arg_int-=24;
  482. p+=24;
  483. }
  484. u8x8_i2c_data_transfer(u8x8, arg_int, p);
  485. in_transfer = 0;
  486. break;
  487. case U8X8_MSG_CAD_INIT:
  488. /* apply default i2c adr if required so that the start transfer msg can use this */
  489. if ( u8x8->i2c_address == 255 )
  490. u8x8->i2c_address = 0x078;
  491. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  492. case U8X8_MSG_CAD_START_TRANSFER:
  493. in_transfer = 0;
  494. break;
  495. case U8X8_MSG_CAD_END_TRANSFER:
  496. if ( in_transfer != 0 )
  497. u8x8_byte_EndTransfer(u8x8);
  498. in_transfer = 0;
  499. break;
  500. default:
  501. return 0;
  502. }
  503. return 1;
  504. }
  505. /* the st75256 i2c driver is a copy of the ssd13xx driver, but with arg=1 */
  506. /* modified from cad001 (ssd13xx) to cad011 */
  507. uint8_t u8x8_cad_st75256_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  508. {
  509. uint8_t *p;
  510. switch(msg)
  511. {
  512. case U8X8_MSG_CAD_SEND_CMD:
  513. u8x8_byte_StartTransfer(u8x8);
  514. u8x8_byte_SendByte(u8x8, 0x000);
  515. u8x8_byte_SendByte(u8x8, arg_int);
  516. u8x8_byte_EndTransfer(u8x8);
  517. break;
  518. case U8X8_MSG_CAD_SEND_ARG:
  519. u8x8_byte_StartTransfer(u8x8);
  520. u8x8_byte_SendByte(u8x8, 0x040);
  521. u8x8_byte_SendByte(u8x8, arg_int);
  522. u8x8_byte_EndTransfer(u8x8);
  523. break;
  524. case U8X8_MSG_CAD_SEND_DATA:
  525. /* see ssd13xx driver */
  526. p = arg_ptr;
  527. while( arg_int > 24 )
  528. {
  529. u8x8_i2c_data_transfer(u8x8, 24, p);
  530. arg_int-=24;
  531. p+=24;
  532. }
  533. u8x8_i2c_data_transfer(u8x8, arg_int, p);
  534. break;
  535. case U8X8_MSG_CAD_INIT:
  536. /* apply default i2c adr if required so that the start transfer msg can use this */
  537. if ( u8x8->i2c_address == 255 )
  538. u8x8->i2c_address = 0x078; /* ST75256, often this is 0x07e */
  539. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  540. case U8X8_MSG_CAD_START_TRANSFER:
  541. case U8X8_MSG_CAD_END_TRANSFER:
  542. /* cad transfer commands are ignored */
  543. break;
  544. default:
  545. return 0;
  546. }
  547. return 1;
  548. }
  549. /* cad i2c procedure for the ld7032 controller */
  550. /* Issue https://github.com/olikraus/u8g2/issues/865 mentiones, that I2C does not work */
  551. /* Workaround is to remove the while loop (or increase the value in the condition) */
  552. uint8_t u8x8_cad_ld7032_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  553. {
  554. static uint8_t in_transfer = 0;
  555. uint8_t *p;
  556. switch(msg)
  557. {
  558. case U8X8_MSG_CAD_SEND_CMD:
  559. if ( in_transfer != 0 )
  560. u8x8_byte_EndTransfer(u8x8);
  561. u8x8_byte_StartTransfer(u8x8);
  562. u8x8_byte_SendByte(u8x8, arg_int);
  563. in_transfer = 1;
  564. break;
  565. case U8X8_MSG_CAD_SEND_ARG:
  566. u8x8_byte_SendByte(u8x8, arg_int);
  567. break;
  568. case U8X8_MSG_CAD_SEND_DATA:
  569. //u8x8_byte_SetDC(u8x8, 1);
  570. /* the FeatherWing OLED with the 32u4 transfer of long byte */
  571. /* streams was not possible. This is broken down to */
  572. /* smaller streams, 32 seems to be the limit... */
  573. /* I guess this is related to the size of the Wire buffers in Arduino */
  574. /* Unfortunately, this can not be handled in the byte level drivers, */
  575. /* so this is done here. Even further, only 24 bytes will be sent, */
  576. /* because there will be another byte (DC) required during the transfer */
  577. p = arg_ptr;
  578. while( arg_int > 24 )
  579. {
  580. u8x8->byte_cb(u8x8, U8X8_MSG_CAD_SEND_DATA, 24, p);
  581. arg_int-=24;
  582. p+=24;
  583. u8x8_byte_EndTransfer(u8x8);
  584. u8x8_byte_StartTransfer(u8x8);
  585. u8x8_byte_SendByte(u8x8, 0x08); /* data write for LD7032 */
  586. }
  587. u8x8->byte_cb(u8x8, U8X8_MSG_CAD_SEND_DATA, arg_int, p);
  588. break;
  589. case U8X8_MSG_CAD_INIT:
  590. /* apply default i2c adr if required so that the start transfer msg can use this */
  591. if ( u8x8->i2c_address == 255 )
  592. u8x8->i2c_address = 0x060;
  593. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  594. case U8X8_MSG_CAD_START_TRANSFER:
  595. in_transfer = 0;
  596. break;
  597. case U8X8_MSG_CAD_END_TRANSFER:
  598. if ( in_transfer != 0 )
  599. u8x8_byte_EndTransfer(u8x8);
  600. break;
  601. default:
  602. return 0;
  603. }
  604. return 1;
  605. }
  606. /* cad procedure for the UC16xx family in I2C mode */
  607. /* u8x8_byte_SetDC is not used */
  608. /* DC bit is encoded into the adr byte */
  609. uint8_t u8x8_cad_uc16xx_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  610. {
  611. static uint8_t in_transfer = 0;
  612. static uint8_t is_data = 0;
  613. uint8_t *p;
  614. switch(msg)
  615. {
  616. case U8X8_MSG_CAD_SEND_CMD:
  617. case U8X8_MSG_CAD_SEND_ARG:
  618. if ( in_transfer != 0 )
  619. {
  620. if ( is_data != 0 )
  621. {
  622. /* transfer mode is active, but data transfer */
  623. u8x8_byte_EndTransfer(u8x8);
  624. /* clear the lowest two bits of the adr */
  625. u8x8_SetI2CAddress( u8x8, u8x8_GetI2CAddress(u8x8)&0x0fc );
  626. u8x8_byte_StartTransfer(u8x8);
  627. }
  628. }
  629. else
  630. {
  631. /* clear the lowest two bits of the adr */
  632. u8x8_SetI2CAddress( u8x8, u8x8_GetI2CAddress(u8x8)&0x0fc );
  633. u8x8_byte_StartTransfer(u8x8);
  634. }
  635. u8x8_byte_SendByte(u8x8, arg_int);
  636. in_transfer = 1;
  637. break;
  638. case U8X8_MSG_CAD_SEND_DATA:
  639. if ( in_transfer != 0 )
  640. {
  641. if ( is_data == 0 )
  642. {
  643. /* transfer mode is active, but data transfer */
  644. u8x8_byte_EndTransfer(u8x8);
  645. /* clear the lowest two bits of the adr */
  646. u8x8_SetI2CAddress( u8x8, (u8x8_GetI2CAddress(u8x8)&0x0fc)|2 );
  647. u8x8_byte_StartTransfer(u8x8);
  648. }
  649. }
  650. else
  651. {
  652. /* clear the lowest two bits of the adr */
  653. u8x8_SetI2CAddress( u8x8, (u8x8_GetI2CAddress(u8x8)&0x0fc)|2 );
  654. u8x8_byte_StartTransfer(u8x8);
  655. }
  656. in_transfer = 1;
  657. p = arg_ptr;
  658. while( arg_int > 24 )
  659. {
  660. u8x8->byte_cb(u8x8, U8X8_MSG_CAD_SEND_DATA, 24, p);
  661. arg_int-=24;
  662. p+=24;
  663. u8x8_byte_EndTransfer(u8x8);
  664. u8x8_byte_StartTransfer(u8x8);
  665. }
  666. u8x8->byte_cb(u8x8, U8X8_MSG_CAD_SEND_DATA, arg_int, p);
  667. break;
  668. case U8X8_MSG_CAD_INIT:
  669. /* apply default i2c adr if required so that the start transfer msg can use this */
  670. if ( u8x8->i2c_address == 255 )
  671. u8x8->i2c_address = 0x070;
  672. return u8x8->byte_cb(u8x8, msg, arg_int, arg_ptr);
  673. case U8X8_MSG_CAD_START_TRANSFER:
  674. in_transfer = 0;
  675. /* actual start is delayed, because we do not whether this is data or cmd transfer */
  676. break;
  677. case U8X8_MSG_CAD_END_TRANSFER:
  678. if ( in_transfer != 0 )
  679. u8x8_byte_EndTransfer(u8x8);
  680. in_transfer = 0;
  681. break;
  682. default:
  683. return 0;
  684. }
  685. return 1;
  686. }