edits + travis CI exaple

This commit is contained in:
Tropicananass 2021-03-27 13:12:23 +01:00
parent 62c58a6a9b
commit f6c4f27efa
3 changed files with 79 additions and 88 deletions

View File

@ -31,7 +31,6 @@ board_build.f_cpu = 16000000L
[env:attiny85_debug] [env:attiny85_debug]
build_type = debug build_type = debug
extends = env:attiny85 extends = env:attiny85
build_flags = -D DEBUG
[env:attiny85_init_debug] [env:attiny85_init_debug]
build_type = debug build_type = debug

View File

@ -8,7 +8,7 @@
#include "pin_map.hpp" #include "pin_map.hpp"
/* Types and Constants */ /* Types and Constants */
/*----------------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------------------------------------*/
/* State machine */ /* State machine */
#define FOREACH_MODE(MODE) \ #define FOREACH_MODE(MODE) \
@ -34,11 +34,11 @@ Mode_e &operator++(Mode_e &mode) {
return mode = static_cast<Mode_e>(static_cast<int>(mode) + 1); return mode = static_cast<Mode_e>(static_cast<int>(mode) + 1);
} }
#if defined(DEBUG) #if defined(__PLATFORMIO_BUILD_DEBUG__)
/* Generate string array from Mode_e names */ /* Generate string array from Mode_e names */
#define GENERATE_MODE_STRING(STRING) #STRING, #define GENERATE_MODE_STRING(STRING) #STRING,
char static const *mode_str[] = {FOREACH_MODE(GENERATE_MODE_STRING)}; char static const *mode_str[] = {FOREACH_MODE(GENERATE_MODE_STRING)};
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
/* Leds */ /* Leds */
uint16_t static const LedCount{24}; uint16_t static const LedCount{24};
@ -56,8 +56,7 @@ uint8_t const PresetMax{8};
// https://hslpicker.com/ = {0, 40, 50, 110, 240, 280, 310, 360 for // https://hslpicker.com/ = {0, 40, 50, 110, 240, 280, 310, 360 for
// white} // white}
uint16_t const PresetHue[PresetMax] = {0, 7282, 9102, 20025, uint16_t const PresetHue[PresetMax] = {0, 7282, 9102, 20025, 43691, 50972, 56434, 65535};
43691, 50972, 56434, 65535};
uint8_t const PresetLevelMax{3}; uint8_t const PresetLevelMax{3};
@ -80,15 +79,11 @@ typedef struct {
int const AnalogButtonVRef{970}; int const AnalogButtonVRef{970};
/* Global variables */ /* Global variables */
/*----------------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------------------------------------*/
/* Led ring state */ /* Led ring state */
LedRingState_t ledRingState{ LedRingState_t ledRingState{
.ledRingColorState = {.ledOnMin = 0, .ledRingColorState = {.ledOnMin = 0, .ledOnMax = LedCount / 2, .hue = 0, .saturation = 255, .brightness = 16},
.ledOnMax = LedCount / 2,
.hue = 0,
.saturation = 255,
.brightness = 16},
.presetState = {.index = 0, .level = 0}, .presetState = {.index = 0, .level = 0},
.currentMode = Mode_e::Init}; .currentMode = Mode_e::Init};
@ -98,7 +93,7 @@ Encoder encoder{EncoderPinA, EncoderPinB};
AnalogButton button{AnalogButtonPin, AnalogButtonVRef}; AnalogButton button{AnalogButtonPin, AnalogButtonVRef};
/* debug Serial definition */ /* debug Serial definition */
#if defined(DEBUG) #if defined(__PLATFORMIO_BUILD_DEBUG__)
#if defined(ARDUINO_AVR_ATTINYX5) #if defined(ARDUINO_AVR_ATTINYX5)
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
SoftwareSerial debugSerial{RxPin, TxPin}; SoftwareSerial debugSerial{RxPin, TxPin};
@ -107,10 +102,10 @@ SoftwareSerial debugSerial{RxPin, TxPin};
#include <HardwareSerial.h> #include <HardwareSerial.h>
HardwareSerial &debugSerial = Serial; HardwareSerial &debugSerial = Serial;
#endif /* defined(ARDUINO_AVR_UNO) */ #endif /* defined(ARDUINO_AVR_UNO) */
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
/* Private function declarations */ /* Private function declarations */
/*----------------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------------------------------------*/
/* Generates state machine handlers for each Mode_e */ /* Generates state machine handlers for each Mode_e */
#define GENERATE_MODE_EXEC_DECLARTION(MODE) void MODE##_execute(int shift); #define GENERATE_MODE_EXEC_DECLARTION(MODE) void MODE##_execute(int shift);
@ -125,11 +120,10 @@ void display_mode(Mode_e mode);
void display_led_ring(void); void display_led_ring(void);
void refresh_led_ring(LedRingColorState_t const &ledRingColorState = void refresh_led_ring(LedRingColorState_t const &ledRingColorState = ledRingState.ledRingColorState);
ledRingState.ledRingColorState);
/* Function definitions */ /* Function definitions */
/*----------------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------------------------------------*/
void setup() { void setup() {
ledRing.begin(); ledRing.begin();
ledRing.fill(Adafruit_NeoPixel::Color(45, 0, 0)); ledRing.fill(Adafruit_NeoPixel::Color(45, 0, 0));
@ -139,22 +133,22 @@ void setup() {
ledRing.fill(Adafruit_NeoPixel::Color(0, 0, 45)); ledRing.fill(Adafruit_NeoPixel::Color(0, 0, 45));
delay(300); delay(300);
#if defined(DEBUG) #if defined(__PLATFORMIO_BUILD_DEBUG__)
debugSerial.begin(9600); debugSerial.begin(9600);
#if defined(DEBUG_INIT) #if defined(DEBUG_INIT)
while (!debugSerial.available()) while (!debugSerial.available())
; ;
#endif /* defined(DEBUG_INIT) */ #endif /* defined(DEBUG_INIT) */
debugSerial.println("Init ... "); debugSerial.println("Init ... ");
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
analog_button_calibration(); analog_button_calibration();
get_encoder_shift(); get_encoder_shift();
#if defined(DEBUG) #if defined(__PLATFORMIO_BUILD_DEBUG__)
debugSerial.println(mode_str[static_cast<int>(ledRingState.currentMode)]); debugSerial.println(mode_str[static_cast<int>(ledRingState.currentMode)]);
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
} }
void loop() { void loop() {
@ -167,9 +161,9 @@ void loop() {
} }
++ledRingState.currentMode; ++ledRingState.currentMode;
#if defined(DEBUG) #if defined(__PLATFORMIO_BUILD_DEBUG__)
debugSerial.println(mode_str[static_cast<int>(ledRingState.currentMode)]); debugSerial.println(mode_str[static_cast<int>(ledRingState.currentMode)]);
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
display_mode(ledRingState.currentMode); display_mode(ledRingState.currentMode);
hasModeChanged = true; hasModeChanged = true;
@ -210,12 +204,14 @@ void loop() {
} }
} }
if (ledRingState.currentMode != Mode_e::ControlOff && if (ledRingState.currentMode != Mode_e::ControlOff && (shift != 0 || hasModeChanged)) {
(shift != 0 || hasModeChanged)) {
display_led_ring(); display_led_ring();
} }
} }
/* Private function definitions */
/*--------------------------------------------------------------------------------------------------------------------*/
/* State Machine functions */ /* State Machine functions */
void ControlOff_execute(int shift) {} void ControlOff_execute(int shift) {}
@ -249,7 +245,7 @@ void SetColor_execute(int shift) {
hue += (shift << 8); hue += (shift << 8);
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
@ -257,7 +253,7 @@ void SetColor_execute(int shift) {
debugSerial.print(", hue="); debugSerial.print(", hue=");
debugSerial.println(hue, DEC); debugSerial.println(hue, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/
} /* SetColor_execute */ } /* SetColor_execute */
void SetBrightness_execute(int shift) { void SetBrightness_execute(int shift) {
@ -265,7 +261,7 @@ void SetBrightness_execute(int shift) {
brightness += (shift << 2); brightness += (shift << 2);
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
@ -273,7 +269,7 @@ void SetBrightness_execute(int shift) {
debugSerial.print(", brightness="); debugSerial.print(", brightness=");
debugSerial.println(brightness, DEC); debugSerial.println(brightness, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/
} /* SetBrightness_execute */ } /* SetBrightness_execute */
void SetSaturation_execute(int shift) { void SetSaturation_execute(int shift) {
@ -281,7 +277,7 @@ void SetSaturation_execute(int shift) {
saturation += (shift << 2); saturation += (shift << 2);
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
@ -289,7 +285,7 @@ void SetSaturation_execute(int shift) {
debugSerial.print(", saturation="); debugSerial.print(", saturation=");
debugSerial.println(saturation, DEC); debugSerial.println(saturation, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/
} /* SetSaturation_execute */ } /* SetSaturation_execute */
void LightOnMin_execute(int shift) { void LightOnMin_execute(int shift) {
@ -318,7 +314,7 @@ void LightOnMin_execute(int shift) {
} }
} }
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
@ -326,7 +322,7 @@ void LightOnMin_execute(int shift) {
debugSerial.print(", ledOnMin="); debugSerial.print(", ledOnMin=");
debugSerial.println(ledOnMin, DEC); debugSerial.println(ledOnMin, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/
} /* LightOnMin_execute */ } /* LightOnMin_execute */
void LightOnMax_execute(int shift) { void LightOnMax_execute(int shift) {
@ -355,7 +351,7 @@ void LightOnMax_execute(int shift) {
} }
} }
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
@ -363,7 +359,7 @@ void LightOnMax_execute(int shift) {
debugSerial.print(", ledOnMax="); debugSerial.print(", ledOnMax=");
debugSerial.println(ledOnMax, DEC); debugSerial.println(ledOnMax, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/
} /* LightOnMax_execute */ } /* LightOnMax_execute */
/* Others functions */ /* Others functions */
@ -371,19 +367,19 @@ void LightOnMax_execute(int shift) {
void analog_button_calibration(void) { void analog_button_calibration(void) {
int highRef, lowRef; int highRef, lowRef;
#if defined(DEBUG) && false #if defined(__PLATFORMIO_BUILD_DEBUG__) && false
debugSerial.print(__func__); debugSerial.print(__func__);
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
do { do {
highRef = analogRead(AnalogButtonPin); highRef = analogRead(AnalogButtonPin);
delay(300); delay(300);
} while (highRef < 1000); } while (highRef < 1000);
#if defined(DEBUG) && false #if defined(__PLATFORMIO_BUILD_DEBUG__) && false
debugSerial.println("highRef="); debugSerial.println("highRef=");
debugSerial.println(highRef); debugSerial.println(highRef);
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
lowRef = analogRead(AnalogButtonPin); lowRef = analogRead(AnalogButtonPin);
while (highRef - lowRef < 50) { while (highRef - lowRef < 50) {
@ -391,10 +387,10 @@ void analog_button_calibration(void) {
delay(300); delay(300);
} }
#if defined(DEBUG) && false #if defined(__PLATFORMIO_BUILD_DEBUG__) && false
debugSerial.println("lowRef="); debugSerial.println("lowRef=");
debugSerial.println(lowRef); debugSerial.println(lowRef);
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
button = AnalogButton{AnalogButtonPin, lowRef + 10}; button = AnalogButton{AnalogButtonPin, lowRef + 10};
} }
@ -438,18 +434,17 @@ void display_led_ring(void) {
LedRingColorState_t ledRingColorState; LedRingColorState_t ledRingColorState;
if (ledRingState.currentMode == Mode_e::Preset) { if (ledRingState.currentMode == Mode_e::Preset) {
#if defined(DEBUG) && false #if defined(__PLATFORMIO_BUILD_DEBUG__) && false
debugSerial.print("Preset index,level="); debugSerial.print("Preset index,level=");
debugSerial.print(preset.index); debugSerial.print(preset.index);
debugSerial.print(","); debugSerial.print(",");
debugSerial.print(preset.level); debugSerial.print(preset.level);
debugSerial.println(); debugSerial.println();
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
ledRingColorState = {.ledOnMin = ledRingState.ledRingColorState.ledOnMin, ledRingColorState = {.ledOnMin = ledRingState.ledRingColorState.ledOnMin,
.ledOnMax = ledRingState.ledRingColorState.ledOnMax, .ledOnMax = ledRingState.ledRingColorState.ledOnMax,
.hue = PresetHue[preset.index], .hue = PresetHue[preset.index],
.saturation = .saturation = (preset.index == (PresetMax - 1)) ? 0 : UINT8_MAX,
(preset.index == (PresetMax - 1)) ? 0 : UINT8_MAX,
.brightness = PresetBrightness[preset.level]}; .brightness = PresetBrightness[preset.level]};
refresh_led_ring(ledRingColorState); refresh_led_ring(ledRingColorState);
} else { } else {
@ -462,14 +457,14 @@ void refresh_led_ring(LedRingColorState_t const &ledRingColorState) {
uint16_t const &ledOnMax = ledRingColorState.ledOnMax; uint16_t const &ledOnMax = ledRingColorState.ledOnMax;
uint16_t LedOnCount = 0; uint16_t LedOnCount = 0;
#if defined(DEBUG) #if defined(__PLATFORMIO_BUILD_DEBUG__)
debugSerial.print("Color hue,sat,bri="); debugSerial.print("Color hue,sat,bri=");
debugSerial.print(ledRingColorState.hue); debugSerial.print(ledRingColorState.hue);
debugSerial.print(","); debugSerial.print(",");
debugSerial.print(ledRingColorState.saturation); debugSerial.print(ledRingColorState.saturation);
debugSerial.print(","); debugSerial.print(",");
debugSerial.println(ledRingColorState.brightness); debugSerial.println(ledRingColorState.brightness);
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
/* reset ring */ /* reset ring */
ledRing.fill(0); ledRing.fill(0);
@ -481,24 +476,21 @@ void refresh_led_ring(LedRingColorState_t const &ledRingColorState) {
LedOnCount = LedCount - (ledOnMax - ledOnMax); LedOnCount = LedCount - (ledOnMax - ledOnMax);
} }
#if defined(DEBUG) #if defined(__PLATFORMIO_BUILD_DEBUG__)
debugSerial.print("Led min,max,count="); debugSerial.print("Led min,max,count=");
debugSerial.print(ledOnMin); debugSerial.print(ledOnMin);
debugSerial.print(","); debugSerial.print(",");
debugSerial.print(ledOnMax); debugSerial.print(ledOnMax);
debugSerial.print(","); debugSerial.print(",");
debugSerial.println(LedOnCount); debugSerial.println(LedOnCount);
#endif /* defined(DEBUG) */ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
for (size_t i = ledRingColorState.ledOnMin; for (size_t i = ledRingColorState.ledOnMin; i < ledRingColorState.ledOnMin + LedOnCount; ++i) {
i < ledRingColorState.ledOnMin + LedOnCount; ++i) {
unsigned int ledId = i; unsigned int ledId = i;
if (ledId >= LedCount) { if (ledId >= LedCount) {
ledId -= LedCount; ledId -= LedCount;
} }
ledRing.setPixelColor( ledRing.setPixelColor(ledId, Adafruit_NeoPixel::ColorHSV(ledRingColorState.hue, ledRingColorState.saturation,
ledId, Adafruit_NeoPixel::ColorHSV(ledRingColorState.hue,
ledRingColorState.saturation,
ledRingColorState.brightness)); ledRingColorState.brightness));
} }
ledRing.show(); ledRing.show();

View File

@ -1,27 +1,27 @@
#ifdef ARDUINO_AVR_ATTINYX5 #if defined(ARDUINO_AVR_ATTINYX5)
/* Led output pin */ /* Led output pin */
uint8_t const LedPin{PB0}; uint8_t const LedPin{PB0};
/* Button analog input pin */ /* Button analog input pin */
uint8_t const AnalogButtonPin{0}; uint8_t const AnalogButtonPin{0};
/* Rotary encoder input pins */ /* Rotary encoder input pins */
uint8_t const EncoderPinA{PB3}; uint8_t const EncoderPinA{PB3};
uint8_t const EncoderPinB{PB4}; uint8_t const EncoderPinB{PB4};
/* Uart for serial debug pins */ /* Uart for serial debug pins */
#ifdef DEBUG #if defined(__PLATFORMIO_BUILD_DEBUG__)
uint8_t const TxPin{PB1}; uint8_t const TxPin{PB1};
uint8_t const RxPin{PB2}; uint8_t const RxPin{PB2};
#endif /*DEBUG*/ #endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */
#endif /*ARDUINO_AVR_ATTINYX5*/ #endif /* defined(ARDUINO_AVR_ATTINYX5) */
#ifdef ARDUINO_AVR_UNO #if defined(ARDUINO_AVR_UNO)
/* Led output pin */ /* Led output pin */
uint8_t const LedPin{6}; uint8_t const LedPin{6};
/* Button analog input pin */ /* Button analog input pin */
uint8_t const AnalogButtonPin{A0}; uint8_t const AnalogButtonPin{A0};
/* Rotary encoder input pins */ /* Rotary encoder input pins */
uint8_t const EncoderPinA{8}; uint8_t const EncoderPinA{8};
uint8_t const EncoderPinB{9}; uint8_t const EncoderPinB{9};
#endif /*ARDUINO_AVR_UNO*/ #endif /* defined(ARDUINO_AVR_UNO) */