@ -21,199 +21,160 @@
*/
*/
/**
/**
* This module is normally not enabled . It can be enabled to facilitate
* This module is off by default , but can be enabled to facilitate the display of
* the display of extra debug information during code development .
* extra debug information during code development . It assumes the existence of a
* It assumes the existance of a Max7219 LED Matrix . A suitable
* Max7219 LED Matrix . A suitable device can be obtained on eBay similar to this :
* device can be obtained on eBay similar to this : http : //www.ebay.com/itm/191781645249
* http : //www.ebay.com/itm/191781645249 for under $2.00 including shipping.
* for under $ 2.00 including shipping .
*
*
* Just connect up + 5 v and Gnd to give it power . And then 3 wires declared in the
* Just connect up + 5 v and GND to give it power , then connect up the pins assigned
* # define ' s below . Actual pin assignments can be changed in MAX7219_DEBUG section
* in Configuration_adv . h . For example , on the Re - ARM you could use :
* of configuration_adv . h
*
*
* # define M ax7219_clock 77
* # define M AX7219_CLK_PIN 77
* # define M ax7219_data_in 78
* # define M AX7219_DIN_PIN 78
* # define M ax7219_load 79
* # define M AX7219_LOAD_PIN 79
*
*
* First call Max7219_init ( ) and then there are a number of support functions available
* Max7219_init( ) is called automatically at startup , and then there are a number of
* to control the LED ' s in the 8 x8 grid .
* support functions available to control the LED s in the 8 x8 grid .
*
*
* void Max7219_init ( ) ;
* void Max7219_init ( ) ;
* void Max7219_PutByte ( uint8_t data ) ;
* void Max7219_PutByte ( uint8_t data ) ;
* void Max7219 ( uint8_t reg , uint8_t data ) ;
* void Max7219 ( uint8_t reg , uint8_t data ) ;
* void Max7219_LED_On ( int8_t row , int8_t col ) ;
* void Max7219_LED_On ( uint8_t row , uint8_t col ) ;
* void Max7219_LED_Off ( int8_t row , int8_t col ) ;
* void Max7219_LED_Off ( uint8_t row , uint8_t col ) ;
* void Max7219_LED_Toggle ( int8_t row , int8_t col ) ;
* void Max7219_LED_Toggle ( uint8_t row , uint8_t col ) ;
* void Max7219_Clear_Row ( int8_t row ) ;
* void Max7219_Clear_Row ( uint8_t row ) ;
* void Max7219_Clear_Column ( int8_t col ) ;
* void Max7219_Clear_Column ( uint8_t col ) ;
* void Max7219_Set_Row ( uint8_t row , uint8_t val ) ;
* void Max7219_Set_Column ( uint8_t col , uint8_t val ) ;
* void Max7219_idle_tasks ( ) ;
*/
*/
# include "MarlinConfig.h"
# include "Marlin.h"
# if ENABLED(MAX7219_DEBUG)
# if ENABLED(MAX7219_DEBUG)
# include "Marlin.h"
# include "planner.h"
# include "planner.h"
# include "stepper.h"
# include "stepper.h"
# include "Max7219_Debug_LEDs.h"
# include "Max7219_Debug_LEDs.h"
static uint8_t LEDs [ 8 ] = { 0 } ;
static uint8_t LEDs [ 8 ] = { 0 } ;
void Max7219_PutByte ( uint8_t data ) {
void Max7219_PutByte ( uint8_t data ) {
uint8_t i = 8 ;
for ( uint8_t i = 8 ; i - - ; ) {
while ( i > 0 ) {
WRITE ( MAX7219_CLK_PIN , LOW ) ; // tick
digitalWrite ( Max7219_clock , LOW ) ; // tick
WRITE ( MAX7219_DIN_PIN , ( data & 0x80 ) ? HIGH : LOW ) ; // send 1 or 0 based on data bit
if ( data & 0x80 ) // check bit
WRITE ( MAX7219_CLK_PIN , HIGH ) ; // tock
digitalWrite ( Max7219_data_in , HIGH ) ; // send 1
data < < = 1 ;
else
digitalWrite ( Max7219_data_in , LOW ) ; // send 0
digitalWrite ( Max7219_clock , HIGH ) ; // tock
data = data < < 0x01 ;
- - i ; // move to lesser bit
}
}
}
}
void Max7219 ( uint8_t reg , uint8_t data ) {
void Max7219 ( const uint8_t reg , const uint8_t data ) {
digitalWrite( Max7219_load , LOW ) ; // begin
WRITE ( MAX7219_LOAD_PIN , LOW ) ; // begin
Max7219_PutByte ( reg ) ; // specify register
Max7219_PutByte ( reg ) ; // specify register
Max7219_PutByte ( data ) ; // put data
Max7219_PutByte ( data ) ; // put data
digitalWrite( Max7219_load , LOW ) ; // and tell the chip to load the data
WRITE( MAX7219_LOAD_PIN , LOW ) ; // and tell the chip to load the data
digitalWrite( Max7219_load , HIGH ) ;
WRITE( MAX7219_LOAD_PIN , HIGH ) ;
}
}
void Max7219_LED_On ( int8_t row , int8_t col ) {
void Max7219_LED_Set ( const uint8_t row , const uint8_t col , const bool on ) {
int x_index ;
if ( row > 7 | | col > 7 ) return ;
if ( row > = 8 | | row < 0 | | col > = 8 | | col < 0 )
if ( TEST ( LEDs [ row ] , col ) = = on ) return ; // if LED is already on/off, leave alone
return ;
if ( on ) SBI ( LEDs [ row ] , col ) ; else CBI ( LEDs [ row ] , col ) ;
if ( LEDs [ row ] & ( 0x01 < < col ) ) // if LED is already on, just leave
Max7219 ( 8 - row , LEDs [ row ] ) ;
return ;
LEDs [ row ] | = ( 0x01 < < col ) ;
x_index = 7 - row ;
Max7219 ( x_index + 1 , LEDs [ row ] ) ;
}
}
void Max7219_LED_Off ( int8_t row , int8_t col ) {
void Max7219_LED_On ( const uint8_t row , const uint8_t col ) {
int x_index ;
Max7219_LED_Set ( row , col , true ) ;
if ( row > = 8 | | row < 0 | | col > = 8 | | col < 0 )
}
return ;
if ( ! ( LEDs [ row ] & ( 0x01 < < col ) ) ) // if LED is already off, just leave
return ;
LEDs [ row ] ^ = ( 0x01 < < col ) ;
void Max7219_LED_Off ( const uint8_t row , const uint8_t col ) {
x_index = 7 - row ;
Max7219_LED_Set ( row , col , false ) ;
Max7219 ( x_index + 1 , LEDs [ row ] ) ;
}
}
void Max7219_LED_Toggle ( int8_t row , int8_t col ) {
void Max7219_LED_Toggle ( const uint8_t row , const uint8_t col ) {
if ( row > = 8 | | row < 0 | | col > = 8 | | col < 0 )
if ( row > 7 | | col > 7 ) return ;
return ;
if ( TEST ( LEDs [ row ] , col ) )
if ( ( LEDs [ row ] & ( 0x01 < < col ) ) )
Max7219_LED_Off ( row , col ) ;
Max7219_LED_Off ( row , col ) ;
else
else
Max7219_LED_On ( row , col ) ;
Max7219_LED_On ( row , col ) ;
}
}
void Max7219_Clear_Column ( int8_t col ) {
void Max7219_Clear_Column ( const uint8_t col ) {
int x_index ;
if ( col > 7 ) return ;
if ( col > = 8 | | col < 0 )
return ;
LEDs [ col ] = 0 ;
LEDs [ col ] = 0 ;
x_index = 7 - col ;
Max7219 ( 8 - col , LEDs [ col ] ) ;
Max7219 ( x_index + 1 , LEDs [ col ] ) ;
}
}
void Max7219_Clear_Row ( int8_t row ) {
void Max7219_Clear_Row ( const uint8_t row ) {
int c ;
if ( row > 7 ) return ;
if ( row > = 8 | | row < 0 )
for ( uint8_t c = 0 ; c < = 7 ; c + + )
return ;
Max7219_LED_Off ( c , row ) ;
for ( c = 0 ; c < 8 ; c + + )
Max7219_LED_Off ( c , row ) ;
}
}
void Max7219_Set_Row ( int8_t row , uint8_t val ) {
void Max7219_Set_Row ( const uint8_t row , const uint8_t val ) {
int b ;
if ( row > 7 ) return ;
for ( uint8_t b = 0 ; b < = 7 ; b + + )
if ( row < 0 | | row > 7 )
if ( TEST ( val , b ) )
return ;
Max7219_LED_On ( 7 - b , row ) ;
if ( val < 0 | | val > 255 )
return ;
for ( b = 0 ; b < 8 ; b + + )
if ( val & ( 0x01 < < b ) )
Max7219_LED_On ( 7 - b , row ) ;
else
else
Max7219_LED_Off ( 7 - b , row ) ;
Max7219_LED_Off ( 7 - b , row ) ;
}
}
void Max7219_Set_Column ( int8_t col , uint8_t val ) {
void Max7219_Set_Column ( const uint8_t col , const uint8_t val ) {
int x_index ;
if ( col > 7 ) return ;
if ( col > = 8 | | col < 0 )
return ;
if ( val < 0 | | val > 255 )
return ;
LEDs [ col ] = val ;
LEDs [ col ] = val ;
x_index = 7 - col ;
Max7219 ( 8 - col , LEDs [ col ] ) ;
Max7219 ( x_index + 1 , LEDs [ col ] ) ;
}
}
void Max7219_init ( ) {
void Max7219_init ( ) {
int i , x , y ;
uint8_t i , x , y ;
pinMode ( Max7219_data_in , OUTPUT ) ;
SET_OUTPUT ( MAX7219_DIN_PIN ) ;
pinMode ( Max7219_clock , OUTPUT ) ;
SET_OUTPUT ( MAX7219_CLK_PIN ) ;
pinMode ( Max7219_load , OUTPUT ) ;
digitalWrite( Max7219_load , HIGH ) ;
OUT_WRITE( MAX7219_LOAD_PIN , HIGH ) ;
//initiation of the max 7219
//initiation of the max 7219
Max7219 ( max7219_reg_scanLimit , 0x07 ) ;
Max7219 ( max7219_reg_scanLimit , 0x07 ) ;
Max7219 ( max7219_reg_decodeMode , 0x00 ) ; // using an led matrix (not digits)
Max7219 ( max7219_reg_decodeMode , 0x00 ) ; // using an led matrix (not digits)
Max7219 ( max7219_reg_shutdown , 0x01 ) ; // not in shutdown mode
Max7219 ( max7219_reg_shutdown , 0x01 ) ; // not in shutdown mode
Max7219 ( max7219_reg_displayTest , 0x00 ) ; // no display test
Max7219 ( max7219_reg_displayTest , 0x00 ) ; // no display test
Max7219 ( max7219_reg_intensity , 0x01 & 0x0 f) ; // the first 0x0f is the value you can set
Max7219 ( max7219_reg_intensity , 0x01 & 0x0 F) ; // the first 0x0F is the value you can set
// range: 0x00 to 0x0 f
// range: 0x00 to 0x0 F
for ( i = 0 ; i < 8 ; i + + ) { // empty registers, turn all LEDs off
for ( i = 0 ; i < = 7 ; i + + ) { // empty registers, turn all LEDs off
LEDs [ i ] = 0x00 ;
LEDs [ i ] = 0x00 ;
Max7219 ( i + 1 , 0 ) ;
Max7219 ( i + 1 , 0 ) ;
}
}
for ( x = 0 ; x < 8 ; x + + ) { // Do an aust etically pleasing pattern to fully test
for ( x = 0 ; x < = 7 ; x + + ) // Do an aesth etically pleasing pattern to fully test
for ( y = 0 ; y < 8 ; y + + ) { // the Max7219 module and LED ' s. First, turn them
for ( y = 0 ; y < = 7 ; y + + ) { // the Max7219 module and LED s. First, turn them
Max7219_LED_On ( x , y ) ; // all on.
Max7219_LED_On ( x , y ) ; // all on.
delay ( 3 ) ;
delay ( 3 ) ;
}
}
}
for ( x = 0 ; x < 8 ; x + + ) { // Now, turn them all off.
for ( x = 0 ; x < = 7 ; x + + ) // Now, turn them all off.
for ( y = 0 ; y < 8 ; y + + ) {
for ( y = 0 ; y < = 7 ; y + + ) {
Max7219_LED_Off ( x , y ) ;
Max7219_LED_Off ( x , y ) ;
delay ( 3 ) ; // delay() is OK here. Max7219_init() is only called from
delay ( 3 ) ; // delay() is OK here. Max7219_init() is only called from
} // setup() and nothing is running yet.
} // setup() and nothing is running yet.
}
delay ( 150 ) ;
delay ( 150 ) ;
for ( x = 7 ; x > = 0 ; x - - ) { // Now, do the same thing from the opposite direction
for ( x = 8 ; x - - ; ) // Now, do the same thing from the opposite direction
for ( y = 0 ; y < 8 ; y + + ) {
for ( y = 0 ; y < = 7 ; y + + ) {
Max7219_LED_On ( x , y ) ;
Max7219_LED_On ( x , y ) ;
delay ( 2 ) ;
delay ( 2 ) ;
}
}
}
for ( x = 7 ; x > = 0 ; x - - ) {
for ( x = 8 ; x - - ; )
for ( y = 0 ; y < 8 ; y + + ) {
for ( y = 0 ; y < = 7 ; y + + ) {
Max7219_LED_Off ( x , y ) ;
Max7219_LED_Off ( x , y ) ;
delay ( 2 ) ;
delay ( 2 ) ;
}
}
}
}
}
/*
/* *
* These are sample debug features to demonstrate the usage of the 8 x8 LED Matrix for debug purposes .
* These are sample debug features to demonstrate the usage of the 8 x8 LED Matrix for debug purposes .
* There is very little CPU burden added to the system by displaying information within the idle ( )
* There is very little CPU burden added to the system by displaying information within the idle ( )
* task .
* task .
@ -223,17 +184,17 @@
* or clear a row is not very significant .
* or clear a row is not very significant .
*/
*/
void Max7219_idle_tasks ( ) {
void Max7219_idle_tasks ( ) {
# if def MAX7219_DEBUG_PRINTER_ALIVE
# if ENABLED( MAX7219_DEBUG_PRINTER_ALIVE)
static int debug_cnt = 0 ;
static int debug_cnt = 0 ;
if ( debug_cnt + + > 100 ) {
if ( debug_cnt + + > 100 ) {
Max7219_LED_Toggle ( 7 , 7 ) ;
Max7219_LED_Toggle ( 7 , 7 ) ;
debug_cnt = 0 ;
debug_cnt = 0 ;
}
}
# endif
# endif
# ifdef MAX7219_DEBUG_STEPPER_HEAD
# ifdef MAX7219_DEBUG_STEPPER_HEAD
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_HEAD ) ;
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_HEAD ) ;
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_HEAD + 1 ) ;
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_HEAD + 1 ) ;
if ( planner . block_buffer_head < 8 )
if ( planner . block_buffer_head < 8 )
Max7219_LED_On ( planner . block_buffer_head , MAX7219_DEBUG_STEPPER_HEAD ) ;
Max7219_LED_On ( planner . block_buffer_head , MAX7219_DEBUG_STEPPER_HEAD ) ;
else
else
@ -242,7 +203,7 @@
# ifdef MAX7219_DEBUG_STEPPER_TAIL
# ifdef MAX7219_DEBUG_STEPPER_TAIL
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_TAIL ) ;
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_TAIL ) ;
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_TAIL + 1 ) ;
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_TAIL + 1 ) ;
if ( planner . block_buffer_tail < 8 )
if ( planner . block_buffer_tail < 8 )
Max7219_LED_On ( planner . block_buffer_tail , MAX7219_DEBUG_STEPPER_TAIL ) ;
Max7219_LED_On ( planner . block_buffer_tail , MAX7219_DEBUG_STEPPER_TAIL ) ;
else
else
@ -250,37 +211,26 @@
# endif
# endif
# ifdef MAX7219_DEBUG_STEPPER_QUEUE
# ifdef MAX7219_DEBUG_STEPPER_QUEUE
static int16_t last_depth = 0 , current_depth ;
static int16_t last_depth = 0 ;
uint8_t i ;
int16_t current_depth = planner . block_buffer_head - planner . block_buffer_tail ;
current_depth = planner . block_buffer_head - planner . block_buffer_tail ;
if ( current_depth ! = last_depth ) { // usually, no update will be needed.
if ( current_depth ! = last_depth ) { // usually, no update will be needed.
if ( current_depth < 0 ) current_depth + = BLOCK_BUFFER_SIZE ;
if ( current_depth < 0 )
NOMORE ( current_depth , BLOCK_BUFFER_SIZE ) ;
current_depth + = BLOCK_BUFFER_SIZE ;
NOMORE ( current_depth , 16 ) ; // if the BLOCK_BUFFER_SIZE is greater than 16, two lines
// of LEDs is enough to see if the buffer is draining
if ( current_depth > = BLOCK_BUFFER_SIZE )
current_depth = BLOCK_BUFFER_SIZE ;
const uint8_t st = min ( current_depth , last_depth ) ,
en = max ( current_depth , last_depth ) ;
if ( current_depth > 16 ) // if the BLOCK_BUFFER_SIZE is greater than 16 two lines
if ( current_depth < last_depth )
current_depth = 16 ; // of LED's is enough to see if the buffer is draining
for ( uint8_t i = st ; i < = en ; i + + ) // clear the highest order LEDs
Max7219_LED_Off ( i > > 1 , MAX7219_DEBUG_STEPPER_QUEUE + ( i & 1 ) ) ;
if ( current_depth < last_depth )
for ( i = current_depth ; i < = last_depth ; i + + ) { // clear the highest order LED's
if ( i & 1 )
Max7219_LED_Off ( i > > 1 , MAX7219_DEBUG_STEPPER_QUEUE + 1 ) ;
else
Max7219_LED_Off ( i > > 1 , MAX7219_DEBUG_STEPPER_QUEUE + 0 ) ;
}
else
else
for ( i = last_depth ; i < = current_depth ; i + + ) { // light up the highest order LED's
for ( uint8_t i = st ; i < = en ; i + + ) // set the highest order LEDs
if ( i & 1 )
Max7219_LED_On ( i > > 1 , MAX7219_DEBUG_STEPPER_QUEUE + ( i & 1 ) ) ;
Max7219_LED_On ( i > > 1 , MAX7219_DEBUG_STEPPER_QUEUE + 1 ) ;
else
Max7219_LED_On ( i > > 1 , MAX7219_DEBUG_STEPPER_QUEUE + 0 ) ;
}
last_depth = current_depth ;
last_depth = current_depth ;
}
}
# endif
# endif
}
}
# endif //MAX7219_DEBUG
# endif // MAX7219_DEBUG