diff --git a/platformio.ini b/platformio.ini index 52ab1e6..aae3fa1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,7 +31,6 @@ board_build.f_cpu = 16000000L [env:attiny85_debug] build_type = debug extends = env:attiny85 -build_flags = -D DEBUG [env:attiny85_init_debug] build_type = debug diff --git a/src/main.cpp b/src/main.cpp index d41650e..39cf4b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,17 +8,17 @@ #include "pin_map.hpp" /* Types and Constants */ -/*----------------------------------------------------------------------------------------------*/ +/*--------------------------------------------------------------------------------------------------------------------*/ /* State machine */ -#define FOREACH_MODE(MODE) \ - MODE(Init) \ - MODE(LightOnMin) \ - MODE(LightOnMax) \ - MODE(ControlOff) \ - MODE(Preset) \ - MODE(SetColor) \ - MODE(SetSaturation) \ +#define FOREACH_MODE(MODE) \ + MODE(Init) \ + MODE(LightOnMin) \ + MODE(LightOnMax) \ + MODE(ControlOff) \ + MODE(Preset) \ + MODE(SetColor) \ + MODE(SetSaturation) \ MODE(SetBrightness) #define GENERATE_MODE_ENUM(ENUM) ENUM, @@ -34,11 +34,11 @@ Mode_e &operator++(Mode_e &mode) { return mode = static_cast(static_cast(mode) + 1); } -#if defined(DEBUG) +#if defined(__PLATFORMIO_BUILD_DEBUG__) /* Generate string array from Mode_e names */ #define GENERATE_MODE_STRING(STRING) #STRING, char static const *mode_str[] = {FOREACH_MODE(GENERATE_MODE_STRING)}; -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ /* Leds */ 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 // white} -uint16_t const PresetHue[PresetMax] = {0, 7282, 9102, 20025, - 43691, 50972, 56434, 65535}; +uint16_t const PresetHue[PresetMax] = {0, 7282, 9102, 20025, 43691, 50972, 56434, 65535}; uint8_t const PresetLevelMax{3}; @@ -80,15 +79,11 @@ typedef struct { int const AnalogButtonVRef{970}; /* Global variables */ -/*----------------------------------------------------------------------------------------------*/ +/*--------------------------------------------------------------------------------------------------------------------*/ /* Led ring state */ LedRingState_t ledRingState{ - .ledRingColorState = {.ledOnMin = 0, - .ledOnMax = LedCount / 2, - .hue = 0, - .saturation = 255, - .brightness = 16}, + .ledRingColorState = {.ledOnMin = 0, .ledOnMax = LedCount / 2, .hue = 0, .saturation = 255, .brightness = 16}, .presetState = {.index = 0, .level = 0}, .currentMode = Mode_e::Init}; @@ -98,7 +93,7 @@ Encoder encoder{EncoderPinA, EncoderPinB}; AnalogButton button{AnalogButtonPin, AnalogButtonVRef}; /* debug Serial definition */ -#if defined(DEBUG) +#if defined(__PLATFORMIO_BUILD_DEBUG__) #if defined(ARDUINO_AVR_ATTINYX5) #include SoftwareSerial debugSerial{RxPin, TxPin}; @@ -107,10 +102,10 @@ SoftwareSerial debugSerial{RxPin, TxPin}; #include HardwareSerial &debugSerial = Serial; #endif /* defined(ARDUINO_AVR_UNO) */ -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ /* Private function declarations */ -/*----------------------------------------------------------------------------------------------*/ +/*--------------------------------------------------------------------------------------------------------------------*/ /* Generates state machine handlers for each Mode_e */ #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 refresh_led_ring(LedRingColorState_t const &ledRingColorState = - ledRingState.ledRingColorState); +void refresh_led_ring(LedRingColorState_t const &ledRingColorState = ledRingState.ledRingColorState); /* Function definitions */ -/*----------------------------------------------------------------------------------------------*/ +/*--------------------------------------------------------------------------------------------------------------------*/ void setup() { ledRing.begin(); ledRing.fill(Adafruit_NeoPixel::Color(45, 0, 0)); @@ -139,22 +133,22 @@ void setup() { ledRing.fill(Adafruit_NeoPixel::Color(0, 0, 45)); delay(300); -#if defined(DEBUG) +#if defined(__PLATFORMIO_BUILD_DEBUG__) debugSerial.begin(9600); #if defined(DEBUG_INIT) while (!debugSerial.available()) ; #endif /* defined(DEBUG_INIT) */ debugSerial.println("Init ... "); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ analog_button_calibration(); get_encoder_shift(); -#if defined(DEBUG) +#if defined(__PLATFORMIO_BUILD_DEBUG__) debugSerial.println(mode_str[static_cast(ledRingState.currentMode)]); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ } void loop() { @@ -167,9 +161,9 @@ void loop() { } ++ledRingState.currentMode; -#if defined(DEBUG) +#if defined(__PLATFORMIO_BUILD_DEBUG__) debugSerial.println(mode_str[static_cast(ledRingState.currentMode)]); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ display_mode(ledRingState.currentMode); hasModeChanged = true; @@ -210,12 +204,14 @@ void loop() { } } - if (ledRingState.currentMode != Mode_e::ControlOff && - (shift != 0 || hasModeChanged)) { + if (ledRingState.currentMode != Mode_e::ControlOff && (shift != 0 || hasModeChanged)) { display_led_ring(); } } +/* Private function definitions */ +/*--------------------------------------------------------------------------------------------------------------------*/ + /* State Machine functions */ void ControlOff_execute(int shift) {} @@ -249,7 +245,7 @@ void SetColor_execute(int shift) { hue += (shift << 8); -#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) +#if defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO) if (shift != 0) { debugSerial.print(__func__); debugSerial.print(" : shift="); @@ -257,7 +253,7 @@ void SetColor_execute(int shift) { debugSerial.print(", hue="); debugSerial.println(hue, DEC); } -#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ +#endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/ } /* SetColor_execute */ void SetBrightness_execute(int shift) { @@ -265,7 +261,7 @@ void SetBrightness_execute(int shift) { brightness += (shift << 2); -#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) +#if defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO) if (shift != 0) { debugSerial.print(__func__); debugSerial.print(" : shift="); @@ -273,7 +269,7 @@ void SetBrightness_execute(int shift) { debugSerial.print(", brightness="); debugSerial.println(brightness, DEC); } -#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ +#endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/ } /* SetBrightness_execute */ void SetSaturation_execute(int shift) { @@ -281,7 +277,7 @@ void SetSaturation_execute(int shift) { saturation += (shift << 2); -#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) +#if defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO) if (shift != 0) { debugSerial.print(__func__); debugSerial.print(" : shift="); @@ -289,7 +285,7 @@ void SetSaturation_execute(int shift) { debugSerial.print(", saturation="); debugSerial.println(saturation, DEC); } -#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ +#endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/ } /* SetSaturation_execute */ 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) { debugSerial.print(__func__); debugSerial.print(" : shift="); @@ -326,7 +322,7 @@ void LightOnMin_execute(int shift) { debugSerial.print(", ledOnMin="); debugSerial.println(ledOnMin, DEC); } -#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ +#endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/ } /* LightOnMin_execute */ 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) { debugSerial.print(__func__); debugSerial.print(" : shift="); @@ -363,7 +359,7 @@ void LightOnMax_execute(int shift) { debugSerial.print(", ledOnMax="); debugSerial.println(ledOnMax, DEC); } -#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ +#endif /*defined(__PLATFORMIO_BUILD_DEBUG__) && defined(ARDUINO_AVR_UNO)*/ } /* LightOnMax_execute */ /* Others functions */ @@ -371,19 +367,19 @@ void LightOnMax_execute(int shift) { void analog_button_calibration(void) { int highRef, lowRef; -#if defined(DEBUG) && false +#if defined(__PLATFORMIO_BUILD_DEBUG__) && false debugSerial.print(__func__); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ do { highRef = analogRead(AnalogButtonPin); delay(300); } while (highRef < 1000); -#if defined(DEBUG) && false +#if defined(__PLATFORMIO_BUILD_DEBUG__) && false debugSerial.println("highRef="); debugSerial.println(highRef); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ lowRef = analogRead(AnalogButtonPin); while (highRef - lowRef < 50) { @@ -391,10 +387,10 @@ void analog_button_calibration(void) { delay(300); } -#if defined(DEBUG) && false +#if defined(__PLATFORMIO_BUILD_DEBUG__) && false debugSerial.println("lowRef="); debugSerial.println(lowRef); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ button = AnalogButton{AnalogButtonPin, lowRef + 10}; } @@ -438,18 +434,17 @@ void display_led_ring(void) { LedRingColorState_t ledRingColorState; 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); debugSerial.print(","); debugSerial.print(preset.level); debugSerial.println(); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ ledRingColorState = {.ledOnMin = ledRingState.ledRingColorState.ledOnMin, .ledOnMax = ledRingState.ledRingColorState.ledOnMax, .hue = PresetHue[preset.index], - .saturation = - (preset.index == (PresetMax - 1)) ? 0 : UINT8_MAX, + .saturation = (preset.index == (PresetMax - 1)) ? 0 : UINT8_MAX, .brightness = PresetBrightness[preset.level]}; refresh_led_ring(ledRingColorState); } else { @@ -462,14 +457,14 @@ void refresh_led_ring(LedRingColorState_t const &ledRingColorState) { uint16_t const &ledOnMax = ledRingColorState.ledOnMax; uint16_t LedOnCount = 0; -#if defined(DEBUG) +#if defined(__PLATFORMIO_BUILD_DEBUG__) debugSerial.print("Color hue,sat,bri="); debugSerial.print(ledRingColorState.hue); debugSerial.print(","); debugSerial.print(ledRingColorState.saturation); debugSerial.print(","); debugSerial.println(ledRingColorState.brightness); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ /* reset ring */ ledRing.fill(0); @@ -481,25 +476,22 @@ void refresh_led_ring(LedRingColorState_t const &ledRingColorState) { LedOnCount = LedCount - (ledOnMax - ledOnMax); } -#if defined(DEBUG) +#if defined(__PLATFORMIO_BUILD_DEBUG__) debugSerial.print("Led min,max,count="); debugSerial.print(ledOnMin); debugSerial.print(","); debugSerial.print(ledOnMax); debugSerial.print(","); debugSerial.println(LedOnCount); -#endif /* defined(DEBUG) */ +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ - for (size_t i = ledRingColorState.ledOnMin; - i < ledRingColorState.ledOnMin + LedOnCount; ++i) { + for (size_t i = ledRingColorState.ledOnMin; i < ledRingColorState.ledOnMin + LedOnCount; ++i) { unsigned int ledId = i; if (ledId >= LedCount) { ledId -= LedCount; } - ledRing.setPixelColor( - ledId, Adafruit_NeoPixel::ColorHSV(ledRingColorState.hue, - ledRingColorState.saturation, - ledRingColorState.brightness)); + ledRing.setPixelColor(ledId, Adafruit_NeoPixel::ColorHSV(ledRingColorState.hue, ledRingColorState.saturation, + ledRingColorState.brightness)); } ledRing.show(); } \ No newline at end of file diff --git a/src/pin_map.hpp b/src/pin_map.hpp index af9a045..d3f4e4c 100644 --- a/src/pin_map.hpp +++ b/src/pin_map.hpp @@ -1,27 +1,27 @@ -#ifdef ARDUINO_AVR_ATTINYX5 - /* Led output pin */ - uint8_t const LedPin{PB0}; - - /* Button analog input pin */ - uint8_t const AnalogButtonPin{0}; - /* Rotary encoder input pins */ - uint8_t const EncoderPinA{PB3}; - uint8_t const EncoderPinB{PB4}; +#if defined(ARDUINO_AVR_ATTINYX5) +/* Led output pin */ +uint8_t const LedPin{PB0}; - /* Uart for serial debug pins */ - #ifdef DEBUG - uint8_t const TxPin{PB1}; - uint8_t const RxPin{PB2}; - #endif /*DEBUG*/ -#endif /*ARDUINO_AVR_ATTINYX5*/ +/* Button analog input pin */ +uint8_t const AnalogButtonPin{0}; +/* Rotary encoder input pins */ +uint8_t const EncoderPinA{PB3}; +uint8_t const EncoderPinB{PB4}; -#ifdef ARDUINO_AVR_UNO - /* Led output pin */ - uint8_t const LedPin{6}; +/* Uart for serial debug pins */ +#if defined(__PLATFORMIO_BUILD_DEBUG__) +uint8_t const TxPin{PB1}; +uint8_t const RxPin{PB2}; +#endif /* defined(__PLATFORMIO_BUILD_DEBUG__) */ +#endif /* defined(ARDUINO_AVR_ATTINYX5) */ - /* Button analog input pin */ - uint8_t const AnalogButtonPin{A0}; - /* Rotary encoder input pins */ - uint8_t const EncoderPinA{8}; - uint8_t const EncoderPinB{9}; -#endif /*ARDUINO_AVR_UNO*/ \ No newline at end of file +#if defined(ARDUINO_AVR_UNO) +/* Led output pin */ +uint8_t const LedPin{6}; + +/* Button analog input pin */ +uint8_t const AnalogButtonPin{A0}; +/* Rotary encoder input pins */ +uint8_t const EncoderPinA{8}; +uint8_t const EncoderPinB{9}; +#endif /* defined(ARDUINO_AVR_UNO) */ \ No newline at end of file