max7219.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2021 Zach White <skullydazed@gmail.com>
  3. * Copyright (c) 2007 Eberhard Fahle
  4. *
  5. * max7219.c - A library for controling Leds with a MAX7219/MAX7221
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * This permission notice shall be included in all copies or
  17. * substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. /*
  29. * This driver started as a port of Arduino's LedControl to QMK. The
  30. * original Arduino code can be found here:
  31. *
  32. * https://github.com/wayoda/LedControl
  33. *
  34. * Unlike LedControl we are using the native SPI support, you will need to
  35. * use the native SPI pins for your MCU. You can set the CS pin with
  36. * `#define MAX7219_LOAD <pin>`.
  37. *
  38. * This has only been tested on AVR, specifically a Teensy 2.0++.
  39. */
  40. #include "max7219.h"
  41. #include "font.h"
  42. // Datastructures
  43. bool max7219_led_scrolling = true;
  44. uint16_t max7219_buffer_end = 0;
  45. uint8_t max7219_spidata[MAX_BYTES];
  46. uint8_t max7219_led_a[8][MAX7219_BUFFER_SIZE];
  47. /* Write max7219_spidata to all the max7219's
  48. */
  49. void max7219_write_all(void) {
  50. dprintf("max7219_write_all()\n");
  51. if (spi_start(MAX7219_LOAD, false, 0, 8)) {
  52. for(int i = MAX_BYTES; i>0; i--) {
  53. dprintf("spi_write(%d)\n", max7219_spidata[i-1]);
  54. spi_write(max7219_spidata[i-1]);
  55. }
  56. spi_stop();
  57. } else {
  58. xprintf("Could not spi_start!\n");
  59. }
  60. }
  61. /* Write the current frame in max7219_led_a to all the max7219's
  62. */
  63. void max7219_write_frame(void) {
  64. dprintf("max7219_write_frame()\n");
  65. // Set our opcode and data
  66. for (int col=0; col<8; col++) {
  67. for (int device_num=0; device_num<MAX7219_CONTROLLERS; device_num++) {
  68. int offset=device_num*2;
  69. max7219_spidata[offset] = max7219_led_a[col][device_num];
  70. max7219_spidata[offset+1] = col+1;
  71. }
  72. max7219_write_all();
  73. }
  74. }
  75. /* Stores a message in the sign buffer.
  76. *
  77. * message should be a 2d array with the outer array having a length of your
  78. * message and the inner array having a length of 6. Use the CHR_<letter>
  79. * macros from font.h to populate your array.
  80. *
  81. * Example:
  82. *
  83. * uint8_t message[10][6] = {CHR_INTERROBANG, CHR_C, CHR_l, CHR_u, CHR_e, CHR_b, CHR_o, CHR_a, CHR_r, CHR_d};
  84. * max7219_message(message, 10);
  85. */
  86. void max7219_message_sign(uint8_t message[][6], size_t message_len) {
  87. uint8_t letter_num = 0;
  88. uint8_t letter_col = 0;
  89. max7219_buffer_end = message_len * 6 + 32;
  90. for (int device_num=0; device_num<MAX7219_BUFFER_SIZE; device_num++) {
  91. for (int col=0; col<8; col++) {
  92. if (letter_num >= message_len) {
  93. max7219_led_a[col][device_num] = 0b00000000;
  94. } else {
  95. max7219_led_a[col][device_num] = message[letter_num][letter_col];
  96. if (letter_col == 5) {
  97. letter_num++;
  98. letter_col = 0;
  99. } else {
  100. letter_col++;
  101. }
  102. }
  103. }
  104. }
  105. max7219_write_frame();
  106. }
  107. /* Scroll the content on the sign left by 1 column.
  108. *
  109. * When loop_message is true columns that slide off the left will be added
  110. * to the right to be displayed again.
  111. */
  112. void max7219_message_sign_task(bool loop_message) {
  113. uint8_t left_col = 0b00000000;
  114. if (!max7219_led_scrolling) {
  115. return;
  116. }
  117. if (loop_message) {
  118. left_col = max7219_led_a[0][0];
  119. }
  120. int i=0;
  121. for (int device_num=0; device_num<MAX7219_BUFFER_SIZE; device_num++) {
  122. for (int col=0; col<8; col++) {
  123. i++;
  124. if (i == max7219_buffer_end) {
  125. max7219_led_a[col][device_num] = left_col;
  126. device_num=MAX7219_BUFFER_SIZE;
  127. break;
  128. } else if (col < 7) {
  129. max7219_led_a[col][device_num] = max7219_led_a[col+1][device_num];
  130. } else if (device_num == MAX7219_BUFFER_SIZE-1) {
  131. max7219_led_a[col][device_num] = left_col;
  132. } else {
  133. max7219_led_a[col][device_num] = max7219_led_a[0][device_num+1];
  134. }
  135. }
  136. }
  137. max7219_write_frame();
  138. }
  139. /* Write data to a single max7219
  140. */
  141. void max7219_write(int device_num, volatile uint8_t opcode, volatile uint8_t data) {
  142. dprintf("max7219_write(%d, %d, %d)\n", device_num, opcode, data);
  143. // Clear the data array
  144. for(int i = MAX_BYTES; i>0; i--) {
  145. max7219_spidata[i-1]=0;
  146. }
  147. // Set our opcode and data
  148. uint8_t offset = device_num*2;
  149. max7219_spidata[offset] = data;
  150. max7219_spidata[offset+1] = opcode;
  151. // Write the data
  152. max7219_write_all();
  153. }
  154. /* Turn off all the LEDs
  155. */
  156. void max7219_clear_display(void) {
  157. dprintf("max7219_clear_display();\n");
  158. for (int col=0; col<8; col++) {
  159. for (int device_num=0; device_num<MAX7219_BUFFER_SIZE; device_num++) {
  160. max7219_led_a[col][device_num] = 0b00000000;
  161. }
  162. }
  163. max7219_write_frame();
  164. }
  165. /* Enable the display test (IE turn on all 64 LEDs)
  166. */
  167. void max7219_display_test(int device_num, bool enabled) {
  168. dprintf("max7219_display_test(%d, %d);\n", device_num, enabled);
  169. if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
  170. return;
  171. }
  172. max7219_write(device_num, OP_DISPLAYTEST, enabled);
  173. }
  174. /* Initialize the max7219 system and set the controller(s) to a default state.
  175. */
  176. void max7219_init(void) {
  177. wait_ms(1500);
  178. dprintf("max7219_init()\n");
  179. setPinOutput(MAX7219_LOAD);
  180. writePinHigh(MAX7219_LOAD);
  181. spi_init();
  182. for (int i=0; i<MAX7219_CONTROLLERS; i++) {
  183. max7219_shutdown(i, true);
  184. }
  185. for (int i=0; i<MAX7219_CONTROLLERS; i++) {
  186. // Reset everything to defaults and enable the display
  187. max7219_display_test(i, false);
  188. max7219_set_scan_limit(i, 7);
  189. max7219_set_decode_mode(i, 0);
  190. max7219_set_intensity(i, MAX7219_LED_INTENSITY);
  191. }
  192. max7219_clear_display();
  193. #ifndef MAX7219_NO_STARTUP_TEST
  194. for (int i=0; i<MAX7219_CONTROLLERS; i++) {
  195. // Test this display
  196. max7219_display_test(i, true);
  197. wait_ms(75);
  198. max7219_display_test(i, false);
  199. }
  200. #endif
  201. for (int i=0; i<MAX7219_CONTROLLERS; i++) {
  202. max7219_shutdown(i, false);
  203. }
  204. }
  205. /* Set the decode mode of the controller. You probably don't want to change this.
  206. */
  207. void max7219_set_decode_mode(int device_num, int mode) {
  208. dprintf("max7219_set_decode_mode(%d, %d);\n", device_num, mode);
  209. if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
  210. return;
  211. }
  212. max7219_write(device_num, OP_DECODEMODE, mode);
  213. }
  214. /* Set the intensity (brightness) for the LEDs.
  215. */
  216. void max7219_set_intensity(int device_num, int intensity) {
  217. dprintf("max7219_set_intensity(%d, %d);\n", device_num, intensity);
  218. if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
  219. return;
  220. }
  221. if (intensity >= 0 && intensity<16) {
  222. max7219_write(device_num, OP_INTENSITY, intensity);
  223. }
  224. }
  225. /* Control a single LED.
  226. */
  227. void max7219_set_led(int row, int column, bool state) {
  228. dprintf("max7219_set_led(%d, %d, %d);\n", row, column, state);
  229. if (column<0 || column>8*MAX7219_CONTROLLERS) {
  230. xprintf("max7219_set_led: column (%d) out of bounds\n", column);
  231. return;
  232. }
  233. if (row<0 || row>7) {
  234. xprintf("max7219_set_led: row (%d) out of bounds\n", row);
  235. return;
  236. }
  237. /* At this point we reverse the sense of row and column to match the
  238. * physical layout of my LEDs.
  239. */
  240. uint8_t device_num = column / 8;
  241. uint8_t col = column % 8;
  242. uint8_t val = 0b10000000 >> row;
  243. if (state) {
  244. max7219_led_a[col][device_num] = max7219_led_a[col][device_num]|val;
  245. } else {
  246. val = ~val;
  247. max7219_led_a[col][device_num] = max7219_led_a[col][device_num]&val;
  248. }
  249. max7219_write(device_num, col+1, max7219_led_a[col][device_num]);
  250. }
  251. /* Set the number of digits (rows) to be scanned.
  252. */
  253. void max7219_set_scan_limit(int device_num, int limit) {
  254. dprintf("max7219_set_scan_limit(%d, %d);\n", device_num, limit);
  255. if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
  256. return;
  257. }
  258. if (limit >= 0 && limit < 8) {
  259. max7219_write(device_num, OP_SCANLIMIT, limit);
  260. }
  261. }
  262. /* Enable (true) or disable (false) the controller.
  263. */
  264. void max7219_shutdown(int device_num, bool shutdown) {
  265. dprintf("max7219_shutdown(%d, %d);\n", device_num, shutdown);
  266. if (device_num<0 || device_num >= MAX7219_CONTROLLERS) {
  267. return;
  268. }
  269. max7219_write(device_num, OP_SHUTDOWN, !shutdown);
  270. }