First version

This commit is contained in:
Christoph Hagen
2022-08-29 11:17:03 +02:00
commit e338a5fd0f
5 changed files with 546 additions and 0 deletions

66
include/config.h Normal file
View File

@ -0,0 +1,66 @@
#pragma once
// MARK: Conversions
#define MS_PER_MIN 60000
// MARK: Hardware
#define NUM_LEDS 12
#define LED_SPIN_MAX_COUNT (NUM_LEDS * 2)
#define LED_BATCH_COUNT 2
// MARK: Demo
// Enable the demo mode
//#define ENABLE_DEMO
// MARK: Logging
// Enable logging to serial connection
// #define SERIAL_LOGGING
// Hardware pins
#define LED_STRIP_PIN 5
#define BUTTON_PIN 2
// Storage
#define DEMO_MODE_BYTE 0
// Colors
#define MAX_GREEN 90
#define MAX_RED 5
// Brightness
#define MAX_BRIGHTNESS 250
#define PULSING_BRIGHTNESS 50
// MARK: Durations
#define STAY_GREEN_DURATION_DEMO 20000
#define FADE_TO_RED_DURATION_DEMO 30000
#define PULSING_UNTIL_OFF_DURATION_DEMO 60000
#define STAY_GREEN_DURATION 30*MS_PER_MIN
#define FADE_TO_RED_DURATION 30*MS_PER_MIN
#define PULSING_UNTIL_OFF_DURATION 20*MS_PER_MIN
#define SHORT_FADE_DURATION 1500
// Pulsing config
#define PULSING_START_INTERVAL 3500
#define PULSING_END_INTERVAL 500
#define PULSE_VARIANCE (PULSING_START_INTERVAL-PULSING_END_INTERVAL)
// Spin config
#define SPIN_SATURATION 255
#define SPIN_DEMO_ENABLED_HUE 0
#define SPIN_DEMO_DISABLED_HUE 90
#define MAXIMUM_DELAY 250

86
include/definitions.h Normal file
View File

@ -0,0 +1,86 @@
#pragma once
// MARK: State definition
#include <Arduino.h>
#include "config.h"
enum class ProgramState {
fadeToGreen = 1,
stayGreen = 2,
fadeToRed = 3,
fadePulseLow = 4,
fadePulseHigh = 5,
fadeToSleeping = 6,
sleeping = 7,
toggleDemoMode = 8,
};
struct FadeComponent {
// Timestamps for next fade step
uint32_t nextFade;
// Intervals in ms for each fade step
uint32_t interval;
// Fade direction for each component
uint8_t direction;
// Indicator to alternate between LED batches
uint8_t skip;
// Current color
uint8_t color;
// Target color
uint8_t endColor;
uint8_t needsStep(uint32_t time) {
if (isDone()) {
return 0;
}
return time >= nextFade;
}
uint8_t isDone() {
return color == endColor;
}
uint8_t nextColor() {
return color + direction;
}
uint8_t advanceSkipAndColorIfNeeded() {
skip = (skip + 1) % LED_BATCH_COUNT;
nextFade += interval;
if (skip) {
// Only update color when all batches are done
// i.e. when skip is zero
return interval;
}
if (isDone()) {
return 255;
}
color += direction;
return interval;
}
void setNewColor(uint8_t newColor, uint32_t time, uint32_t duration) {
endColor = newColor;
nextFade = time;
skip = 0;
if (isDone()) {
interval = 255;
direction = 0;
return;
}
uint8_t steps;
if (endColor > color) {
steps = endColor - color;
direction = 1;
} else {
steps = color - endColor;
direction = -1;
}
interval = (duration / steps) / LED_BATCH_COUNT;
}
};

15
include/log.h Normal file
View File

@ -0,0 +1,15 @@
#ifdef SERIAL_LOGGING
#define INIT_LOGGING Serial.begin(115200)
#define LOG_DEBUG(X) \
Serial.print(X);
#define LOG_DEBUG_LN(X) \
Serial.println(X);
#else
#define INIT_LOGGING
#define LOG_DEBUG(X);
#define LOG_DEBUG_LN(X);
#endif