My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

max7219.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * This module is off by default, but can be enabled to facilitate the display of
  25. * extra debug information during code development.
  26. *
  27. * Just connect up 5V and GND to give it power, then connect up the pins assigned
  28. * in Configuration_adv.h. For example, on the Re-ARM you could use:
  29. *
  30. * #define MAX7219_CLK_PIN 77
  31. * #define MAX7219_DIN_PIN 78
  32. * #define MAX7219_LOAD_PIN 79
  33. *
  34. * max7219.init() is called automatically at startup, and then there are a number of
  35. * support functions available to control the LEDs in the 8x8 grid.
  36. *
  37. * If you are using the Max7219 matrix for firmware debug purposes in time sensitive
  38. * areas of the code, please be aware that the orientation (rotation) of the display can
  39. * affect the speed. The Max7219 can update a single column fairly fast. It is much
  40. * faster to do a Max7219_Set_Column() with a rotation of 90 or 270 degrees than to do
  41. * a Max7219_Set_Row(). The opposite is true for rotations of 0 or 180 degrees.
  42. */
  43. #include "../inc/MarlinConfig.h"
  44. #ifndef MAX7219_ROTATE
  45. #define MAX7219_ROTATE 0
  46. #endif
  47. #define _ROT ((MAX7219_ROTATE + 360) % 360)
  48. #ifndef MAX7219_NUMBER_UNITS
  49. #define MAX7219_NUMBER_UNITS 1
  50. #endif
  51. #define MAX7219_LINES (8 * (MAX7219_NUMBER_UNITS))
  52. //
  53. // MAX7219 registers
  54. //
  55. #define max7219_reg_noop 0x00
  56. #define max7219_reg_digit0 0x01
  57. #define max7219_reg_digit1 0x02
  58. #define max7219_reg_digit2 0x03
  59. #define max7219_reg_digit3 0x04
  60. #define max7219_reg_digit4 0x05
  61. #define max7219_reg_digit5 0x06
  62. #define max7219_reg_digit6 0x07
  63. #define max7219_reg_digit7 0x08
  64. #define max7219_reg_decodeMode 0x09
  65. #define max7219_reg_intensity 0x0A
  66. #define max7219_reg_scanLimit 0x0B
  67. #define max7219_reg_shutdown 0x0C
  68. #define max7219_reg_displayTest 0x0F
  69. #ifdef MAX7219_DEBUG_PROFILE
  70. // This class sums up the amount of time for which its instances exist.
  71. // By default there is one instantiated for the duration of the idle()
  72. // function. But an instance can be created in any code block to measure
  73. // the time spent from the point of instantiation until the CPU leaves
  74. // block. Be careful about having multiple instances of CodeProfiler as
  75. // it does not guard against double counting. In general mixing ISR and
  76. // non-ISR use will require critical sections but note that mode setting
  77. // is atomic so the total or average times can safely be read if you set
  78. // mode to FREEZE first.
  79. class CodeProfiler {
  80. public:
  81. enum Mode : uint8_t { ACCUMULATE_AVERAGE, ACCUMULATE_TOTAL, FREEZE };
  82. private:
  83. static Mode mode;
  84. static uint8_t instance_count;
  85. static uint32_t last_calc_time;
  86. static uint32_t total_time;
  87. static uint8_t time_fraction;
  88. static uint16_t call_count;
  89. uint32_t start_time;
  90. public:
  91. CodeProfiler() : start_time(micros()) { instance_count++; }
  92. ~CodeProfiler() {
  93. instance_count--;
  94. if (mode == FREEZE) return;
  95. call_count++;
  96. const uint32_t now = micros();
  97. total_time += now - start_time;
  98. if (mode == ACCUMULATE_TOTAL) return;
  99. // update time_fraction every hundred milliseconds
  100. if (instance_count == 0 && ELAPSED(now, last_calc_time + 100000)) {
  101. time_fraction = total_time * 128 / (now - last_calc_time);
  102. last_calc_time = now;
  103. total_time = 0;
  104. }
  105. }
  106. static void set_mode(Mode _mode) { mode = _mode; }
  107. static void reset() {
  108. time_fraction = 0;
  109. last_calc_time = micros();
  110. total_time = 0;
  111. call_count = 0;
  112. }
  113. // returns fraction of total time which was measured, scaled from 0 to 128
  114. static uint8_t get_time_fraction() { return time_fraction; }
  115. // returns total time in microseconds
  116. static uint32_t get_total_time() { return total_time; }
  117. static uint16_t get_call_count() { return call_count; }
  118. };
  119. #endif
  120. class Max7219 {
  121. public:
  122. static uint8_t led_line[MAX7219_LINES];
  123. Max7219() {}
  124. static void init();
  125. static void register_setup();
  126. static void putbyte(uint8_t data);
  127. static void pulse_load();
  128. // Set a single register (e.g., a whole native row)
  129. static void send(const uint8_t reg, const uint8_t data);
  130. // Refresh all units
  131. static void refresh() { for (uint8_t i = 0; i < 8; i++) refresh_line(i); }
  132. // Suspend / resume updates to the LED unit
  133. // Use these methods to speed up multiple changes
  134. // or to apply updates from interrupt context.
  135. static void suspend() { suspended++; }
  136. static void resume() { suspended--; suspended |= 0x80; }
  137. // Update a single native line on all units
  138. static void refresh_line(const uint8_t line);
  139. // Update a single native line on just one unit
  140. static void refresh_unit_line(const uint8_t line);
  141. #if ENABLED(MAX7219_NUMERIC)
  142. // Draw an integer with optional leading zeros and optional decimal point
  143. void print(const uint8_t start, int16_t value, uint8_t size, const bool leadzero=false, bool dec=false);
  144. // Draw a float with a decimal point and optional digits
  145. void print(const uint8_t start, const_float_t value, const uint8_t pre_size, const uint8_t post_size, const bool leadzero=false);
  146. #endif
  147. // Set a single LED by XY coordinate
  148. static void led_set(const uint8_t x, const uint8_t y, const bool on, uint8_t * const rcm=nullptr);
  149. static void led_on(const uint8_t x, const uint8_t y, uint8_t * const rcm=nullptr);
  150. static void led_off(const uint8_t x, const uint8_t y, uint8_t * const rcm=nullptr);
  151. static void led_toggle(const uint8_t x, const uint8_t y, uint8_t * const rcm=nullptr);
  152. // Set all LEDs in a single column
  153. static void set_column(const uint8_t col, const uint32_t val);
  154. static void clear_column(const uint8_t col);
  155. // Set all LEDs in a single row
  156. static void set_row(const uint8_t row, const uint32_t val);
  157. static void clear_row(const uint8_t row);
  158. // 16 and 32 bit versions of Row and Column functions
  159. // Multiple rows and columns will be used to display the value if
  160. // the array of matrix LED's is too narrow to accomplish the goal
  161. static void set_rows_16bits(const uint8_t y, uint32_t val);
  162. static void set_rows_32bits(const uint8_t y, uint32_t val);
  163. static void set_columns_16bits(const uint8_t x, uint32_t val);
  164. static void set_columns_32bits(const uint8_t x, uint32_t val);
  165. // Quickly clear the whole matrix
  166. static void clear();
  167. // Quickly fill the whole matrix
  168. static void fill();
  169. // Apply custom code to update the matrix
  170. static void idle_tasks();
  171. private:
  172. static uint8_t suspended;
  173. static void error(FSTR_P const func, const int32_t v1, const int32_t v2=-1);
  174. static void noop();
  175. static void set(const uint8_t line, const uint8_t bits);
  176. static void send_row(const uint8_t row);
  177. static void send_column(const uint8_t col);
  178. static void mark16(const uint8_t y, const uint8_t v1, const uint8_t v2, uint8_t * const rcm=nullptr);
  179. static void range16(const uint8_t y, const uint8_t ot, const uint8_t nt, const uint8_t oh, const uint8_t nh, uint8_t * const rcm=nullptr);
  180. static void quantity(const uint8_t y, const uint8_t ov, const uint8_t nv, uint8_t * const rcm=nullptr);
  181. static void quantity16(const uint8_t y, const uint8_t ov, const uint8_t nv, uint8_t * const rcm=nullptr);
  182. #if MAX7219_INIT_TEST
  183. static void test_pattern();
  184. static void run_test_pattern();
  185. static void start_test_pattern();
  186. #endif
  187. };
  188. extern Max7219 max7219;