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,12 +186,25 @@ void adalight(){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
|
||||||
case MODE_DATA:
|
|
||||||
|
|
||||||
|
void dataMode(){
|
||||||
if(bytesRemaining > 0) {
|
if(bytesRemaining > 0) {
|
||||||
if (outPos < sizeof(leds)){
|
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
|
#ifdef CALIBRATE
|
||||||
if(outPos < 3)
|
if(outPos < 3)
|
||||||
ledsRaw[outPos++] = c;
|
ledsRaw[outPos++] = c;
|
||||||
|
@ -192,19 +216,8 @@ void adalight(){
|
||||||
ledsRaw[outPos++] = c; // Issue next byte
|
ledsRaw[outPos++] = c; // Issue next byte
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
bytesRemaining--;
|
|
||||||
}
|
void timeouts(){
|
||||||
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 {
|
|
||||||
// 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