u8x8_d_max7219.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. u8x8_d_max7219.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2017, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "u8x8.h"
  28. static const uint8_t u8x8_d_max7219_init_seq[] = {
  29. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  30. U8X8_CA(15, 0), /* test mode off */
  31. U8X8_CA(15, 0), /* test mode off */
  32. U8X8_CA(15, 0), /* test mode off */
  33. U8X8_CA(15, 0), /* test mode off */
  34. U8X8_END_TRANSFER(), /* disable chip */
  35. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  36. U8X8_CA(12, 0), /* */
  37. U8X8_CA(12, 0), /* */
  38. U8X8_CA(12, 0), /* */
  39. U8X8_CA(12, 0), /* */
  40. U8X8_END_TRANSFER(), /* disable chip */
  41. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  42. U8X8_CA(9, 0), /* decode mode: graphics */
  43. U8X8_CA(9, 0), /* decode mode: graphics */
  44. U8X8_CA(9, 0), /* decode mode: graphics */
  45. U8X8_CA(9, 0), /* decode mode: graphics */
  46. U8X8_END_TRANSFER(), /* disable chip */
  47. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  48. U8X8_CA(10, 10), /* medium high intensity */
  49. U8X8_CA(10, 10), /* medium high intensity */
  50. U8X8_CA(10, 10), /* medium high intensity */
  51. U8X8_CA(10, 10), /* medium high intensity */
  52. U8X8_END_TRANSFER(), /* disable chip */
  53. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  54. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  55. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  56. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  57. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  58. U8X8_END_TRANSFER(), /* disable chip */
  59. //U8X8_CA(12, 0), /* shutdown */
  60. //U8X8_END_TRANSFER(), /* disable chip */
  61. U8X8_END() /* end of sequence */
  62. };
  63. static const uint8_t u8x8_d_max7219_powersave0_seq[] = {
  64. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  65. U8X8_CA(12, 1), /* display on */
  66. U8X8_CA(12, 1), /* display on */
  67. U8X8_CA(12, 1), /* display on */
  68. U8X8_CA(12, 1), /* display on */
  69. U8X8_END_TRANSFER(), /* disable chip */
  70. U8X8_END() /* end of sequence */
  71. };
  72. static const uint8_t u8x8_d_max7219_powersave1_seq[] = {
  73. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  74. U8X8_CA(12, 0), /* shutdown */
  75. U8X8_CA(12, 0), /* shutdown */
  76. U8X8_CA(12, 0), /* shutdown */
  77. U8X8_CA(12, 0), /* shutdown */
  78. U8X8_END_TRANSFER(), /* disable chip */
  79. U8X8_END() /* end of sequence */
  80. };
  81. static uint8_t u8x8_d_max7219_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  82. {
  83. uint8_t c, j, i;
  84. uint8_t *ptr;
  85. switch(msg)
  86. {
  87. /* handled by the calling function
  88. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  89. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_pcf8812_96x65_display_info);
  90. break;
  91. case U8X8_MSG_DISPLAY_INIT:
  92. u8x8_d_helper_display_init(u8x8);
  93. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_init_seq);
  94. break;
  95. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  96. if ( arg_int == 0 )
  97. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave0_seq);
  98. else
  99. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave1_seq);
  100. break;
  101. */
  102. /* not supported by MAX7219
  103. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  104. break;
  105. */
  106. #ifdef U8X8_WITH_SET_CONTRAST
  107. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  108. u8x8_cad_StartTransfer(u8x8);
  109. for( i = 0; i < u8x8->display_info->tile_width; i++ )
  110. {
  111. u8x8_cad_SendCmd(u8x8, 10 ); /* brightness */
  112. u8x8_cad_SendArg(u8x8, (arg_int>>4) ); /* 0..15 for contrast */
  113. }
  114. u8x8_cad_EndTransfer(u8x8);
  115. break;
  116. #endif
  117. case U8X8_MSG_DISPLAY_DRAW_TILE:
  118. /* transfer always has to start at x pos 0 (u8x8 is not supported) */
  119. /* also y pos has to be 0 */
  120. /* arg_int is ignored */
  121. //x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  122. c = ((u8x8_tile_t *)arg_ptr)->cnt; /* number of tiles */
  123. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr; /* data ptr to the tiles */
  124. for( i = 0; i < 8; i++ )
  125. {
  126. u8x8_cad_StartTransfer(u8x8);
  127. for( j = 0; j < c; j++ )
  128. {
  129. u8x8_cad_SendCmd(u8x8, i+1); /* commands 1..8 select the byte */
  130. u8x8_cad_SendArg(u8x8, *ptr );
  131. ptr++;
  132. }
  133. u8x8_cad_EndTransfer(u8x8);
  134. }
  135. break;
  136. default:
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. /*==============================*/
  142. static const u8x8_display_info_t u8x8_max7219_32x8_display_info =
  143. {
  144. /* chip_enable_level = */ 0,
  145. /* chip_disable_level = */ 1,
  146. /* post_chip_enable_wait_ns = */ 100,
  147. /* pre_chip_disable_wait_ns = */ 100,
  148. /* reset_pulse_width_ms = */ 100,
  149. /* post_reset_wait_ms = */ 100,
  150. /* sda_setup_time_ns = */ 100,
  151. /* sck_pulse_width_ns = */ 100,
  152. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  153. /* spi_mode = */ 0, /* active high, rising edge */
  154. /* i2c_bus_clock_100kHz = */ 4,
  155. /* data_setup_time_ns = */ 40,
  156. /* write_pulse_width_ns = */ 150,
  157. /* tile_width = */ 4,
  158. /* tile_height = */ 1,
  159. /* default_x_offset = */ 0,
  160. /* flipmode_x_offset = */ 0,
  161. /* pixel_width = */ 32,
  162. /* pixel_height = */ 8
  163. };
  164. uint8_t u8x8_d_max7219_32x8(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  165. {
  166. switch(msg)
  167. {
  168. case U8X8_MSG_DISPLAY_SETUP_MEMORY :
  169. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_max7219_32x8_display_info);
  170. return 1;
  171. case U8X8_MSG_DISPLAY_INIT:
  172. u8x8_d_helper_display_init(u8x8);
  173. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_init_seq);
  174. return 1;
  175. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  176. if ( arg_int == 0 )
  177. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave0_seq);
  178. else
  179. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave1_seq);
  180. return 1;
  181. }
  182. return u8x8_d_max7219_generic(u8x8, msg, arg_int, arg_ptr);
  183. }
  184. /*==============================*/
  185. static const u8x8_display_info_t u8x8_max7219_16x16_display_info =
  186. {
  187. /* chip_enable_level = */ 0,
  188. /* chip_disable_level = */ 1,
  189. /* post_chip_enable_wait_ns = */ 100,
  190. /* pre_chip_disable_wait_ns = */ 100,
  191. /* reset_pulse_width_ms = */ 100,
  192. /* post_reset_wait_ms = */ 100,
  193. /* sda_setup_time_ns = */ 100,
  194. /* sck_pulse_width_ns = */ 100,
  195. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  196. /* spi_mode = */ 0, /* active high, rising edge */
  197. /* i2c_bus_clock_100kHz = */ 4,
  198. /* data_setup_time_ns = */ 40,
  199. /* write_pulse_width_ns = */ 150,
  200. /* tile_width = */ 2,
  201. /* tile_height = */ 2,
  202. /* default_x_offset = */ 0,
  203. /* flipmode_x_offset = */ 0,
  204. /* pixel_width = */ 16,
  205. /* pixel_height = */ 16
  206. };
  207. /*
  208. Multiple page rows are not supported, so 16x16 will not work.
  209. Due to the hardware structure of such displays all tiles of the display
  210. must be written at once.
  211. This is not possible with the current u8g2 structure.
  212. So u8x8_d_max7219_16x16 will not work.
  213. */
  214. uint8_t u8x8_d_max7219_16x16(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  215. {
  216. switch(msg)
  217. {
  218. case U8X8_MSG_DISPLAY_SETUP_MEMORY :
  219. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_max7219_16x16_display_info);
  220. return 1;
  221. case U8X8_MSG_DISPLAY_INIT:
  222. u8x8_d_helper_display_init(u8x8);
  223. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_init_seq);
  224. return 1;
  225. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  226. if ( arg_int == 0 )
  227. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave0_seq);
  228. else
  229. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave1_seq);
  230. return 1;
  231. }
  232. return u8x8_d_max7219_generic(u8x8, msg, arg_int, arg_ptr);
  233. }
  234. /*==============================*/
  235. static const u8x8_display_info_t u8x8_max7219_8x8_display_info =
  236. {
  237. /* chip_enable_level = */ 0,
  238. /* chip_disable_level = */ 1,
  239. /* post_chip_enable_wait_ns = */ 100,
  240. /* pre_chip_disable_wait_ns = */ 100,
  241. /* reset_pulse_width_ms = */ 100,
  242. /* post_reset_wait_ms = */ 100,
  243. /* sda_setup_time_ns = */ 100,
  244. /* sck_pulse_width_ns = */ 100,
  245. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  246. /* spi_mode = */ 0, /* active high, rising edge */
  247. /* i2c_bus_clock_100kHz = */ 4,
  248. /* data_setup_time_ns = */ 40,
  249. /* write_pulse_width_ns = */ 150,
  250. /* tile_width = */ 1,
  251. /* tile_height = */ 1,
  252. /* default_x_offset = */ 0,
  253. /* flipmode_x_offset = */ 0,
  254. /* pixel_width = */ 8,
  255. /* pixel_height = */ 8
  256. };
  257. uint8_t u8x8_d_max7219_8x8(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  258. {
  259. switch(msg)
  260. {
  261. case U8X8_MSG_DISPLAY_SETUP_MEMORY :
  262. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_max7219_8x8_display_info);
  263. return 1;
  264. case U8X8_MSG_DISPLAY_INIT:
  265. u8x8_d_helper_display_init(u8x8);
  266. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_init_seq);
  267. return 1;
  268. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  269. if ( arg_int == 0 )
  270. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave0_seq);
  271. else
  272. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_powersave1_seq);
  273. return 1;
  274. }
  275. return u8x8_d_max7219_generic(u8x8, msg, arg_int, arg_ptr);
  276. }
  277. /*==============================*/
  278. static const uint8_t u8x8_d_max7219_8_init_seq[] = {
  279. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  280. U8X8_CA(15, 0), /* test mode off */
  281. U8X8_CA(15, 0), /* test mode off */
  282. U8X8_CA(15, 0), /* test mode off */
  283. U8X8_CA(15, 0), /* test mode off */
  284. U8X8_CA(15, 0), /* test mode off */
  285. U8X8_CA(15, 0), /* test mode off */
  286. U8X8_CA(15, 0), /* test mode off */
  287. U8X8_CA(15, 0), /* test mode off */
  288. U8X8_END_TRANSFER(), /* disable chip */
  289. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  290. U8X8_CA(12, 0), /* */
  291. U8X8_CA(12, 0), /* */
  292. U8X8_CA(12, 0), /* */
  293. U8X8_CA(12, 0), /* */
  294. U8X8_CA(12, 0), /* */
  295. U8X8_CA(12, 0), /* */
  296. U8X8_CA(12, 0), /* */
  297. U8X8_CA(12, 0), /* */
  298. U8X8_END_TRANSFER(), /* disable chip */
  299. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  300. U8X8_CA(9, 0), /* decode mode: graphics */
  301. U8X8_CA(9, 0), /* decode mode: graphics */
  302. U8X8_CA(9, 0), /* decode mode: graphics */
  303. U8X8_CA(9, 0), /* decode mode: graphics */
  304. U8X8_CA(9, 0), /* decode mode: graphics */
  305. U8X8_CA(9, 0), /* decode mode: graphics */
  306. U8X8_CA(9, 0), /* decode mode: graphics */
  307. U8X8_CA(9, 0), /* decode mode: graphics */
  308. U8X8_END_TRANSFER(), /* disable chip */
  309. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  310. U8X8_CA(10, 10), /* medium high intensity */
  311. U8X8_CA(10, 10), /* medium high intensity */
  312. U8X8_CA(10, 10), /* medium high intensity */
  313. U8X8_CA(10, 10), /* medium high intensity */
  314. U8X8_CA(10, 10), /* medium high intensity */
  315. U8X8_CA(10, 10), /* medium high intensity */
  316. U8X8_CA(10, 10), /* medium high intensity */
  317. U8X8_CA(10, 10), /* medium high intensity */
  318. U8X8_END_TRANSFER(), /* disable chip */
  319. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  320. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  321. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  322. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  323. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  324. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  325. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  326. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  327. U8X8_CA(11, 7), /* scan limit: display all digits (assuming a 8x8 matrix) */
  328. U8X8_END_TRANSFER(), /* disable chip */
  329. //U8X8_CA(12, 0), /* shutdown */
  330. //U8X8_END_TRANSFER(), /* disable chip */
  331. U8X8_END() /* end of sequence */
  332. };
  333. static const uint8_t u8x8_d_max7219_8_powersave0_seq[] = {
  334. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  335. U8X8_CA(12, 1), /* display on */
  336. U8X8_CA(12, 1), /* display on */
  337. U8X8_CA(12, 1), /* display on */
  338. U8X8_CA(12, 1), /* display on */
  339. U8X8_CA(12, 1), /* display on */
  340. U8X8_CA(12, 1), /* display on */
  341. U8X8_CA(12, 1), /* display on */
  342. U8X8_CA(12, 1), /* display on */
  343. U8X8_END_TRANSFER(), /* disable chip */
  344. U8X8_END() /* end of sequence */
  345. };
  346. static const uint8_t u8x8_d_max7219_8_powersave1_seq[] = {
  347. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  348. U8X8_CA(12, 0), /* shutdown */
  349. U8X8_CA(12, 0), /* shutdown */
  350. U8X8_CA(12, 0), /* shutdown */
  351. U8X8_CA(12, 0), /* shutdown */
  352. U8X8_CA(12, 0), /* shutdown */
  353. U8X8_CA(12, 0), /* shutdown */
  354. U8X8_CA(12, 0), /* shutdown */
  355. U8X8_CA(12, 0), /* shutdown */
  356. U8X8_END_TRANSFER(), /* disable chip */
  357. U8X8_END() /* end of sequence */
  358. };
  359. static const u8x8_display_info_t u8x8_max7219_64x8_display_info =
  360. {
  361. /* chip_enable_level = */ 0,
  362. /* chip_disable_level = */ 1,
  363. /* post_chip_enable_wait_ns = */ 100,
  364. /* pre_chip_disable_wait_ns = */ 100,
  365. /* reset_pulse_width_ms = */ 100,
  366. /* post_reset_wait_ms = */ 100,
  367. /* sda_setup_time_ns = */ 100,
  368. /* sck_pulse_width_ns = */ 100,
  369. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  370. /* spi_mode = */ 0, /* active high, rising edge */
  371. /* i2c_bus_clock_100kHz = */ 4,
  372. /* data_setup_time_ns = */ 40,
  373. /* write_pulse_width_ns = */ 150,
  374. /* tile_width = */ 8,
  375. /* tile_height = */ 1,
  376. /* default_x_offset = */ 0,
  377. /* flipmode_x_offset = */ 0,
  378. /* pixel_width = */ 64,
  379. /* pixel_height = */ 8
  380. };
  381. uint8_t u8x8_d_max7219_64x8(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  382. {
  383. switch(msg)
  384. {
  385. case U8X8_MSG_DISPLAY_SETUP_MEMORY :
  386. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_max7219_64x8_display_info);
  387. return 1;
  388. case U8X8_MSG_DISPLAY_INIT:
  389. u8x8_d_helper_display_init(u8x8);
  390. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_8_init_seq);
  391. return 1;
  392. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  393. if ( arg_int == 0 )
  394. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_8_powersave0_seq);
  395. else
  396. u8x8_cad_SendSequence(u8x8, u8x8_d_max7219_8_powersave1_seq);
  397. return 1;
  398. }
  399. return u8x8_d_max7219_generic(u8x8, msg, arg_int, arg_ptr);
  400. }