Separated adalight into functions
Makes the code more readable and easier to modify
This commit is contained in:
parent
ec44b9335f
commit
6910e162d7
|
@ -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,8 +138,24 @@ 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;
|
||||||
|
case MODE_DATA:
|
||||||
|
dataMode();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
timeouts();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void headerMode(){
|
||||||
|
static uint8_t
|
||||||
|
headPos,
|
||||||
|
hi, lo, chk;
|
||||||
|
|
||||||
if(headPos < MAGICSIZE){
|
if(headPos < MAGICSIZE){
|
||||||
if(c == magic[headPos]) headPos++;
|
if(c == magic[headPos]) headPos++;
|
||||||
|
@ -175,22 +186,12 @@ void adalight(){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
|
||||||
case MODE_DATA:
|
|
||||||
|
|
||||||
|
void dataMode(){
|
||||||
if(bytesRemaining > 0) {
|
if(bytesRemaining > 0) {
|
||||||
if (outPos < sizeof(leds)){
|
if (outPos < sizeof(leds)){
|
||||||
#ifdef CALIBRATE
|
dataSet();
|
||||||
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--;
|
bytesRemaining--;
|
||||||
}
|
}
|
||||||
|
@ -201,10 +202,22 @@ void adalight(){
|
||||||
D_FPS;
|
D_FPS;
|
||||||
D_LED(OFF);
|
D_LED(OFF);
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
} // end switch
|
|
||||||
} // end serial if
|
void dataSet(){
|
||||||
else {
|
#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
|
// No data received. If this persists, send an ACK packet
|
||||||
// to host once every second to alert it to our presence.
|
// to host once every second to alert it to our presence.
|
||||||
if((t - lastAckTime) > 1000) {
|
if((t - lastAckTime) > 1000) {
|
||||||
|
@ -217,12 +230,9 @@ void adalight(){
|
||||||
FastLED.show();
|
FastLED.show();
|
||||||
lastByteTime = t; // Reset counter
|
lastByteTime = t; // Reset counter
|
||||||
}
|
}
|
||||||
} // end else
|
|
||||||
} // end for(;;)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
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.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue