Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add software serial based logging (implements #33) #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Arduino/SelfomatController/AgreementState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bool AgreementState::processCommand(const uint8_t* buffer, size_t size) {
sendCommand('A');
return true;
}

// Process base commands.
if(BaseState::processCommand(buffer, size))
return true;
Expand All @@ -31,11 +31,11 @@ void AgreementState::animationStep(unsigned long dt) {
}
}


float angleOffset = timeInState()/300.0f;
float anglePerLed = (2.0 * PI) / ring.numPixels();
for(int j = 0; j < ring.numPixels(); j++) {
float angle = (float)j * anglePerLed + angleOffset;
float angle = (float)j * anglePerLed + angleOffset;
float sin_x = sin(angle);
uint8_t brightness = sin_x*sin_x * 255.0f * b;
ring.setPixelColor(j, 0, brightness, 0);
Expand All @@ -48,24 +48,26 @@ BaseState* AgreementState::logicStep() {
shouldExit = true;
sendCommand('c');
}

if(shouldExit && exitAnimationDone) {
sendCommand('a');
logger.println( F("exit to -> IdleState") );
return &IdleState::INSTANCE;
}
return this;
}

void AgreementState::enter() {
BaseState::enter();
logger.println( F("Entering AgreementState") );
b = 0;
frame = 0;
exitAnimationDone = shouldExit = false;
sendCommand('A');
}

void AgreementState::exit() {

logger.println( F("Leaving AgreementState") );
}

bool AgreementState::needsHeartbeat() {
Expand Down
5 changes: 4 additions & 1 deletion Arduino/SelfomatController/BootingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ BaseState* BootingState::logicStep() {
return parentStep;

if(shouldExit && exitAnimationDone) {
logger.println( F("exit to -> IdleState") );
return &IdleState::INSTANCE;
}

Expand All @@ -54,12 +55,14 @@ BaseState* BootingState::logicStep() {
void BootingState::enter() {
frame = 0;
exitAnimationDone = shouldExit = false;
logger.println( F("Entering BootingState") );
}

void BootingState::exit() {
// Booting done, we need a heartbeat. Assume we got it now
heartbeatDeactivated = false;
lastHeartbeat = millis();
lastHeartbeat = millis();
logger.println( F("Leaving BootingState") );
}

bool BootingState::needsHeartbeat() {
Expand Down
8 changes: 6 additions & 2 deletions Arduino/SelfomatController/BusyState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,28 @@ BaseState* BusyState::logicStep() {
if(flashTriggered) {
if(settings.flashDurationMicros < 0) {
delay(10);
} else {
} else {
while(micros() - flashStartMicros < settings.flashDurationMicros);
}
digitalWrite(PIN_FLASH_ON, LOW);
flashTriggered = false;
}

// timeout in busy state
if(timeInState() > 15000 || exitIdle) {
logger.println( F("exit to -> IdleState") );
return &IdleState::INSTANCE;
}
if(exitPrint) {
logger.println( F("exit to -> PrintingState") );
return &PrintingState::INSTANCE;
}
return this;
}

void BusyState::enter() {
BaseState::enter();
logger.println( F("Entering BusyState") );
flashTriggered = exitPrint = exitIdle = false;
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_FLASH_CAM_TRIGGER), triggerFlash, FALLING);
}
Expand All @@ -61,6 +64,7 @@ void BusyState::exit() {
detachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_FLASH_CAM_TRIGGER));
// then turn off the light for safety
digitalWrite(PIN_FLASH_ON, LOW);
logger.println( F("Leaving BusyState") );
}

bool BusyState::needsHeartbeat() {
Expand Down
15 changes: 9 additions & 6 deletions Arduino/SelfomatController/CountDownState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ bool CountDownState::processCommand(const uint8_t* buffer, size_t size) {
void CountDownState::animationStep(unsigned long dt) {
uint32_t color = colors[animationCycle];
float percentage = ((float)timeInState() - animationCycle * oneCycleMillis) / oneCycleMillis;

float pixelBorder = ring.numPixels() - ring.numPixels() * percentage;

for(int8_t i=0; i<ring.numPixels(); i++) {

if(i > pixelBorder) {
Expand All @@ -37,23 +37,26 @@ BaseState* CountDownState::logicStep() {
if(timeInState() > settings.countDownMillis) {
// Trigger the capture and go to idle
sendCommand('t');
//logger.println("Cntdn expired, triggered -> BusyState");
return &BusyState::INSTANCE;
}
return this;
}

void CountDownState::enter() {
BaseState::enter();
logger.println( F("Entering CountDownState") );
oneCycleMillis = (settings.countDownMillis)/3.0f;
animationCycle = 0;
lastColor = 0;
}

void CountDownState::exit() {
for(int8_t i=0; i<ring.numPixels(); i++) {
ring.setPixelColor(i, 0);
}
ring.show();
for(int8_t i=0; i<ring.numPixels(); i++) {
ring.setPixelColor(i, 0);
}
ring.show();
logger.println( F("Leaving CountDownState") );
}

bool CountDownState::needsHeartbeat() {
Expand Down
5 changes: 3 additions & 2 deletions Arduino/SelfomatController/FlashingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void FlashingState::animationStep(unsigned long dt) {

// Flash 10x per second
int on = (timeInState() / 150) & 0x01;

for(int j = 0; j < ring.numPixels(); j++) {
if(on) {
ring.setPixelColor(j, 255, 0, 0);
Expand All @@ -42,11 +42,12 @@ BaseState* FlashingState::logicStep() {

void FlashingState::enter() {
BaseState::enter();
logger.println( F("Entering FlashingState") );
shouldExit = false;
}

void FlashingState::exit() {

logger.println( F("Leaving FlashingState") );
}

bool FlashingState::needsHeartbeat() {
Expand Down
5 changes: 3 additions & 2 deletions Arduino/SelfomatController/IdleState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void IdleState::animationStep(unsigned long dt) {
float brightness = sinx*sinx;
uint8_t g = brightness * 255.0f;
for(int i = 0; i < ring.numPixels(); i++) {
ring.setPixelColor(i, 0, g, g >> 2);
ring.setPixelColor(i, 0, g, g >> 2);
}
ring.show();
}
Expand All @@ -36,12 +36,13 @@ BaseState* IdleState::logicStep() {

void IdleState::enter() {
BaseState::enter();
logger.println( F("Entering IdleState") );
externalTrigger = false;
green = 0;
}

void IdleState::exit() {

logger.println( F("Leaving IdleState") );
}

bool IdleState::needsHeartbeat() {
Expand Down
30 changes: 30 additions & 0 deletions Arduino/SelfomatController/Logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "Logging.h"

#ifdef LOGGING_ACTIVE
#include <NeoSWSerial.h>
#ifndef NEOSWSERIAL_EXTERNAL_PCINT
#error NEOSWSERIAL_EXTERNAL_PCINT needs to be defined inside of NeoSWSerial.h or as compile flag.
#endif
#endif

Logging::Logging(uint8_t transmitPin) {
#ifdef LOGGING_ACTIVE
/* using the same pin for RX and TX seems to work; we won't RX anyways */
pSwSerial = new NeoSWSerial(transmitPin, transmitPin);
#endif
}

void Logging::begin(uint16_t baudRate) {
#ifdef LOGGING_ACTIVE
((NeoSWSerial*)pSwSerial)->begin(baudRate);
((NeoSWSerial*)pSwSerial)->ignore(); // do not use RX
#endif
}

size_t Logging::write(uint8_t txChar) {
#ifdef LOGGING_ACTIVE
return ((NeoSWSerial*)pSwSerial)->write(txChar);
#else
return 0;
#endif
}
20 changes: 20 additions & 0 deletions Arduino/SelfomatController/Logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef LOGGING_H
#define LOGGING_H

#include "Print.h"

//#define LOGGING_ACTIVE /* Comment out when logging is unwanted; could also overwrite class! */

class Logging : public Print {
public:
Logging(uint8_t transmitPin);

void begin(uint16_t baudRate=38400);

size_t write(uint8_t txChar);

private:
void* pSwSerial;
};

#endif
8 changes: 6 additions & 2 deletions Arduino/SelfomatController/OffState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ BaseState* OffState::logicStep() {

if (digitalRead(PIN_SWITCH) == LOW) {
// The switch is on and we're allowed to exit the off state->do it
logger.println( F("HW switch LOW -> booting") );
return &BootingState::INSTANCE;
}
return this;
}

void OffState::enter() {
BaseState::enter();

logger.println( F("Entering OffState") );

// read settings
logger.println( F("Read settings on entry") );
readSettings();

allowExitState = false;


Expand Down Expand Up @@ -58,6 +61,7 @@ void OffState::exit() {
ring.show();

blink(2);
logger.println( F("Leaving OffState") );
}

bool OffState::needsHeartbeat() {
Expand Down
9 changes: 5 additions & 4 deletions Arduino/SelfomatController/PrintingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ void PrintingState::animationStep(unsigned long dt) {
}
}


float angleOffset = timeInState()/300.0f;
float anglePerLed = (2.0 * PI) / ring.numPixels();
for(int j = 0; j < ring.numPixels(); j++) {
float angle = (float)j * anglePerLed + angleOffset;
float angle = (float)j * anglePerLed + angleOffset;
float sin_x = sin(angle);
uint8_t brightness = sin_x*sin_x * 255.0f * b;
ring.setPixelColor(j, 0, brightness, brightness >> 2);
Expand All @@ -48,7 +48,7 @@ BaseState* PrintingState::logicStep() {
cancelSent = true;
sendCommand('c');
}

if(timeInState() > 30000 || (shouldExit && exitAnimationDone)) {
return &IdleState::INSTANCE;
}
Expand All @@ -57,14 +57,15 @@ BaseState* PrintingState::logicStep() {

void PrintingState::enter() {
BaseState::enter();
logger.println( F("Entering PrintingState") );
cancelSent = false;
b = 0;
frame = 0;
exitAnimationDone = shouldExit = false;
}

void PrintingState::exit() {

logger.println( F("Leaving PrintingState") );
}

bool PrintingState::needsHeartbeat() {
Expand Down
Loading