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_Debug_LEDs.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * This module is normally not enabled. It can be enabled to facilitate
  24. * the display of extra debug information during code development.
  25. * It assumes the existance of a Max7219 LED Matrix. A suitable
  26. * device can be obtained on eBay similar to this: http://www.ebay.com/itm/191781645249
  27. * for under $2.00 including shipping.
  28. *
  29. * Just connect up +5v and Gnd to give it power. And then 3 wires declared in the
  30. * #define's below. Actual pin assignments can be changed in MAX7219_DEBUG section
  31. * of configuration_adv.h
  32. *
  33. * #define Max7219_clock 77
  34. * #define Max7219_data_in 78
  35. * #define Max7219_load 79
  36. *
  37. * First call Max7219_init() and then there are a number of support functions available
  38. * to control the LED's in the 8x8 grid.
  39. *
  40. * void Max7219_init();
  41. * void Max7219_PutByte(uint8_t data);
  42. * void Max7219(uint8_t reg, uint8_t data);
  43. * void Max7219_LED_On( int8_t row, int8_t col);
  44. * void Max7219_LED_Off( int8_t row, int8_t col);
  45. * void Max7219_LED_Toggle( int8_t row, int8_t col);
  46. * void Max7219_Clear_Row( int8_t row);
  47. * void Max7219_Clear_Column( int8_t col);
  48. */
  49. #include "Marlin.h"
  50. #if ENABLED(MAX7219_DEBUG)
  51. #include "planner.h"
  52. #include "stepper.h"
  53. #include "Max7219_Debug_LEDs.h"
  54. static uint8_t LEDs[8] = {0};
  55. void Max7219_PutByte(uint8_t data) {
  56. uint8_t i = 8;
  57. while(i > 0) {
  58. digitalWrite( Max7219_clock, LOW); // tick
  59. if (data & 0x80) // check bit
  60. digitalWrite(Max7219_data_in,HIGH); // send 1
  61. else
  62. digitalWrite(Max7219_data_in,LOW); // send 0
  63. digitalWrite(Max7219_clock, HIGH); // tock
  64. data = data << 0x01;
  65. --i; // move to lesser bit
  66. }
  67. }
  68. void Max7219( uint8_t reg, uint8_t data) {
  69. digitalWrite(Max7219_load, LOW); // begin
  70. Max7219_PutByte(reg); // specify register
  71. Max7219_PutByte(data); // put data
  72. digitalWrite(Max7219_load, LOW); // and tell the chip to load the data
  73. digitalWrite(Max7219_load,HIGH);
  74. }
  75. void Max7219_LED_On( int8_t row, int8_t col) {
  76. int x_index;
  77. if ( row>=8 || row<0 || col>=8 || col<0)
  78. return;
  79. if ( LEDs[row] & (0x01<<col) ) // if LED is already on, just leave
  80. return;
  81. LEDs[row] |= (0x01<<col);
  82. x_index = 7-row;
  83. Max7219( x_index+1, LEDs[row] );
  84. }
  85. void Max7219_LED_Off( int8_t row, int8_t col) {
  86. int x_index;
  87. if ( row>=8 || row<0 || col>=8 || col<0)
  88. return;
  89. if ( !(LEDs[row] & (0x01<<col)) ) // if LED is already off, just leave
  90. return;
  91. LEDs[row] ^= (0x01<<col);
  92. x_index = 7-row;
  93. Max7219( x_index+1, LEDs[row] );
  94. }
  95. void Max7219_LED_Toggle( int8_t row, int8_t col) {
  96. if ( row>=8 || row<0 || col>=8 || col<0)
  97. return;
  98. if ( (LEDs[row] & (0x01<<col)) )
  99. Max7219_LED_Off( row, col);
  100. else
  101. Max7219_LED_On( row, col);
  102. }
  103. void Max7219_Clear_Column( int8_t col) {
  104. int x_index;
  105. if ( col>=8 || col<0 )
  106. return;
  107. LEDs[col] = 0;
  108. x_index = 7-col;
  109. Max7219( x_index+1, LEDs[col] );
  110. }
  111. void Max7219_Clear_Row( int8_t row) {
  112. int c;
  113. if ( row>=8 || row<0 )
  114. return;
  115. for(c=0; c<8; c++)
  116. Max7219_LED_Off( c, row);
  117. }
  118. void Max7219_Set_Row( int8_t row, uint8_t val) {
  119. int b;
  120. if ( row<0 || row>7 )
  121. return;
  122. if ( val<0 || val>255 )
  123. return;
  124. for(b=0; b<8; b++)
  125. if ( val & (0x01 << b) )
  126. Max7219_LED_On( 7-b, row);
  127. else
  128. Max7219_LED_Off( 7-b, row);
  129. }
  130. void Max7219_Set_Column( int8_t col, uint8_t val) {
  131. int x_index;
  132. if ( col>=8 || col<0 )
  133. return;
  134. if ( val<0 || val>255 )
  135. return;
  136. LEDs[col] = val;
  137. x_index = 7-col;
  138. Max7219( x_index+1, LEDs[col] );
  139. }
  140. void Max7219_init() {
  141. int i, x, y;
  142. pinMode(Max7219_data_in, OUTPUT);
  143. pinMode(Max7219_clock, OUTPUT);
  144. pinMode(Max7219_load, OUTPUT);
  145. digitalWrite(Max7219_load, HIGH);
  146. //initiation of the max 7219
  147. Max7219(max7219_reg_scanLimit, 0x07);
  148. Max7219(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
  149. Max7219(max7219_reg_shutdown, 0x01); // not in shutdown mode
  150. Max7219(max7219_reg_displayTest, 0x00); // no display test
  151. Max7219(max7219_reg_intensity, 0x01 & 0x0f); // the first 0x0f is the value you can set
  152. // range: 0x00 to 0x0f
  153. for (i=0; i<8; i++) { // empty registers, turn all LEDs off
  154. LEDs[i] = 0x00;
  155. Max7219(i+1,0);
  156. }
  157. for(x=0; x<8; x++) { // Do an austetically pleasing pattern to fully test
  158. for(y=0; y<8; y++) { // the Max7219 module and LED's. First, turn them
  159. Max7219_LED_On( x, y); // all on.
  160. delay(3);
  161. }
  162. }
  163. for(x=0; x<8; x++) { // Now, turn them all off.
  164. for(y=0; y<8; y++) {
  165. Max7219_LED_Off( x, y);
  166. delay(3); // delay() is OK here. Max7219_init() is only called from
  167. } // setup() and nothing is running yet.
  168. }
  169. delay(150);
  170. for(x=7; x>=0; x--) { // Now, do the same thing from the opposite direction
  171. for(y=0; y<8; y++) {
  172. Max7219_LED_On( x, y);
  173. delay(2);
  174. }
  175. }
  176. for(x=7; x>=0; x--) {
  177. for(y=0; y<8; y++) {
  178. Max7219_LED_Off( x, y);
  179. delay(2);
  180. }
  181. }
  182. }
  183. /*
  184. * These are sample debug features to demonstrate the usage of the 8x8 LED Matrix for debug purposes.
  185. * There is very little CPU burden added to the system by displaying information within the idle()
  186. * task.
  187. *
  188. * But with that said, if your debugging can be facilitated by making calls into the library from
  189. * other places in the code, feel free to do it. The CPU burden for a few calls to toggle an LED
  190. * or clear a row is not very significant.
  191. */
  192. void Max7219_idle_tasks() {
  193. #ifdef MAX7219_DEBUG_PRINTER_ALIVE
  194. static int debug_cnt=0;
  195. if (debug_cnt++ > 100) {
  196. Max7219_LED_Toggle(7,7);
  197. debug_cnt = 0;
  198. }
  199. #endif
  200. #ifdef MAX7219_DEBUG_STEPPER_HEAD
  201. Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_HEAD);
  202. Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_HEAD+1);
  203. if ( planner.block_buffer_head < 8)
  204. Max7219_LED_On( planner.block_buffer_head, MAX7219_DEBUG_STEPPER_HEAD);
  205. else
  206. Max7219_LED_On( planner.block_buffer_head-8, MAX7219_DEBUG_STEPPER_HEAD+1);
  207. #endif
  208. #ifdef MAX7219_DEBUG_STEPPER_TAIL
  209. Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_TAIL);
  210. Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_TAIL+1);
  211. if ( planner.block_buffer_tail < 8)
  212. Max7219_LED_On( planner.block_buffer_tail, MAX7219_DEBUG_STEPPER_TAIL );
  213. else
  214. Max7219_LED_On( planner.block_buffer_tail-8, MAX7219_DEBUG_STEPPER_TAIL+1 );
  215. #endif
  216. #ifdef MAX7219_DEBUG_STEPPER_QUEUE
  217. static int16_t last_depth=0, current_depth;
  218. uint8_t i;
  219. current_depth = planner.block_buffer_head - planner.block_buffer_tail;
  220. if (current_depth != last_depth) { // usually, no update will be needed.
  221. if ( current_depth < 0 )
  222. current_depth += BLOCK_BUFFER_SIZE;
  223. if ( current_depth >= BLOCK_BUFFER_SIZE )
  224. current_depth = BLOCK_BUFFER_SIZE;
  225. if ( current_depth > 16 ) // if the BLOCK_BUFFER_SIZE is greater than 16 two lines
  226. current_depth = 16; // of LED's is enough to see if the buffer is draining
  227. if ( current_depth < last_depth )
  228. for(i=current_depth; i<=last_depth; i++) { // clear the highest order LED's
  229. if ( i & 1)
  230. Max7219_LED_Off(i>>1, MAX7219_DEBUG_STEPPER_QUEUE+1);
  231. else
  232. Max7219_LED_Off(i>>1, MAX7219_DEBUG_STEPPER_QUEUE+0);
  233. }
  234. else
  235. for(i=last_depth; i<=current_depth; i++) { // light up the highest order LED's
  236. if ( i & 1)
  237. Max7219_LED_On(i>>1, MAX7219_DEBUG_STEPPER_QUEUE+1);
  238. else
  239. Max7219_LED_On(i>>1, MAX7219_DEBUG_STEPPER_QUEUE+0);
  240. }
  241. last_depth = current_depth;
  242. }
  243. #endif
  244. }
  245. #endif //MAX7219_DEBUG