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_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
#ifdef DEBUG_LED
#define ON 1
@ -109,23 +122,6 @@ void setup(){
}
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
lastByteTime = lastAckTime = millis();
@ -134,7 +130,6 @@ void adalight(){
// has a measurable impact on this code's overall throughput.
for(;;) {
// Implementation is a simple finite-state machine.
// Regardless of mode, check for serial input each time:
t = millis();
@ -143,8 +138,24 @@ void adalight(){
lastByteTime = lastAckTime = t; // Reset timeout counters
switch(mode) {
case MODE_HEADER:
headerMode();
break;
case MODE_DATA:
dataMode();
break;
}
}
else {
timeouts();
}
}
}
void headerMode(){
static uint8_t
headPos,
hi, lo, chk;
if(headPos < MAGICSIZE){
if(c == magic[headPos]) headPos++;
@ -175,22 +186,12 @@ void adalight(){
break;
}
}
break;
case MODE_DATA:
}
void dataMode(){
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
dataSet();
}
bytesRemaining--;
}
@ -201,10 +202,22 @@ void adalight(){
D_FPS;
D_LED(OFF);
}
break;
} // end switch
} // end serial if
else {
}
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) {
@ -217,12 +230,9 @@ void adalight(){
FastLED.show();
lastByteTime = t; // Reset counter
}
} // end else
} // end for(;;)
}
void loop()
{
void loop(){
// loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput.
}