Separated adalight into functions

Makes the code more readable and easier to modify
This commit is contained in:
David Madison 2017-05-05 07:28:29 -04:00
parent ec44b9335f
commit 6910e162d7
1 changed files with 107 additions and 97 deletions

View File

@ -65,6 +65,19 @@ static const uint8_t magic[] = {
#define MODE_HEADER 0 #define MODE_HEADER 0
#define MODE_DATA 1 #define MODE_DATA 1
static uint8_t
mode = MODE_HEADER;
static int16_t
c;
static uint16_t
outPos;
static uint32_t
bytesRemaining;
static unsigned long
t,
lastByteTime,
lastAckTime;
// Debug macros initialized // Debug macros initialized
#ifdef DEBUG_LED #ifdef DEBUG_LED
#define ON 1 #define ON 1
@ -109,23 +122,6 @@ void setup(){
} }
void adalight(){ void adalight(){
static uint8_t
mode = MODE_HEADER;
static uint8_t
headPos,
hi, lo, chk;
int16_t
c;
static uint16_t
outPos;
static uint32_t
bytesRemaining;
unsigned long
t;
static unsigned long
lastByteTime,
lastAckTime;
Serial.print("Ada\n"); // Send ACK string to host Serial.print("Ada\n"); // Send ACK string to host
lastByteTime = lastAckTime = millis(); lastByteTime = lastAckTime = millis();
@ -134,7 +130,6 @@ void adalight(){
// has a measurable impact on this code's overall throughput. // has a measurable impact on this code's overall throughput.
for(;;) { for(;;) {
// Implementation is a simple finite-state machine. // Implementation is a simple finite-state machine.
// Regardless of mode, check for serial input each time: // Regardless of mode, check for serial input each time:
t = millis(); t = millis();
@ -143,86 +138,101 @@ void adalight(){
lastByteTime = lastAckTime = t; // Reset timeout counters lastByteTime = lastAckTime = t; // Reset timeout counters
switch(mode) { switch(mode) {
case MODE_HEADER:
case MODE_HEADER: headerMode();
break;
if(headPos < MAGICSIZE){ case MODE_DATA:
if(c == magic[headPos]) headPos++; dataMode();
else headPos = 0; break;
} }
else{ }
switch(headPos){
case HICHECK:
hi = c;
headPos++;
break;
case LOCHECK:
lo = c;
headPos++;
break;
case CHECKSUM:
chk = c;
if(chk == (hi ^ lo ^ 0x55)) {
// Checksum looks valid. Get 16-bit LED count, add 1
// (# LEDs is always > 0) and multiply by 3 for R,G,B.
D_LED(ON);
bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
outPos = 0;
memset(leds, 0, Num_Leds * sizeof(struct CRGB));
mode = MODE_DATA; // Proceed to latch wait mode
}
headPos = 0; // Reset header position regardless of checksum result
break;
}
}
break;
case MODE_DATA:
if(bytesRemaining > 0) {
if (outPos < sizeof(leds)){
#ifdef CALIBRATE
if(outPos < 3)
ledsRaw[outPos++] = c;
else{
ledsRaw[outPos] = ledsRaw[outPos%3]; // Sets RGB data to first LED color
outPos++;
}
#else
ledsRaw[outPos++] = c; // Issue next byte
#endif
}
bytesRemaining--;
}
if(bytesRemaining == 0) {
// End of data -- issue latch:
mode = MODE_HEADER; // Begin next header search
FastLED.show();
D_FPS;
D_LED(OFF);
}
break;
} // end switch
} // end serial if
else { else {
// No data received. If this persists, send an ACK packet timeouts();
// to host once every second to alert it to our presence. }
if((t - lastAckTime) > 1000) { }
Serial.print("Ada\n"); // Send ACK string to host
lastAckTime = t; // Reset counter
}
// If no data received for an extended time, turn off all LEDs.
if((t - lastByteTime) > SerialTimeout) {
memset(leds, 0, Num_Leds * sizeof(struct CRGB)); //filling Led array by zeroes
FastLED.show();
lastByteTime = t; // Reset counter
}
} // end else
} // end for(;;)
} }
void loop() void headerMode(){
{ static uint8_t
headPos,
hi, lo, chk;
if(headPos < MAGICSIZE){
if(c == magic[headPos]) headPos++;
else headPos = 0;
}
else{
switch(headPos){
case HICHECK:
hi = c;
headPos++;
break;
case LOCHECK:
lo = c;
headPos++;
break;
case CHECKSUM:
chk = c;
if(chk == (hi ^ lo ^ 0x55)) {
// Checksum looks valid. Get 16-bit LED count, add 1
// (# LEDs is always > 0) and multiply by 3 for R,G,B.
D_LED(ON);
bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
outPos = 0;
memset(leds, 0, Num_Leds * sizeof(struct CRGB));
mode = MODE_DATA; // Proceed to latch wait mode
}
headPos = 0; // Reset header position regardless of checksum result
break;
}
}
}
void dataMode(){
if(bytesRemaining > 0) {
if (outPos < sizeof(leds)){
dataSet();
}
bytesRemaining--;
}
if(bytesRemaining == 0) {
// End of data -- issue latch:
mode = MODE_HEADER; // Begin next header search
FastLED.show();
D_FPS;
D_LED(OFF);
}
}
void dataSet(){
#ifdef CALIBRATE
if(outPos < 3)
ledsRaw[outPos++] = c;
else{
ledsRaw[outPos] = ledsRaw[outPos%3]; // Sets RGB data to first LED color
outPos++;
}
#else
ledsRaw[outPos++] = c; // Issue next byte
#endif
}
void timeouts(){
// No data received. If this persists, send an ACK packet
// to host once every second to alert it to our presence.
if((t - lastAckTime) > 1000) {
Serial.print("Ada\n"); // Send ACK string to host
lastAckTime = t; // Reset counter
}
// If no data received for an extended time, turn off all LEDs.
if((t - lastByteTime) > SerialTimeout) {
memset(leds, 0, Num_Leds * sizeof(struct CRGB)); //filling Led array by zeroes
FastLED.show();
lastByteTime = t; // Reset counter
}
}
void loop(){
// loop() is avoided as even that small bit of function overhead // loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput. // has a measurable impact on this code's overall throughput.
} }