This commit is contained in:
Tropicananass 2020-11-08 18:22:58 +01:00
parent ec724575a5
commit a68d774d71
2 changed files with 167 additions and 177 deletions

Binary file not shown.

View File

@ -1,47 +1,46 @@
#include <Arduino.h> #include <Arduino.h>
// TODO : use core built-in lib tinyNeoPixel
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
#include <Encoder.h> #include <Encoder.h>
#include "pin_map.hpp"
#include "Button.hpp" #include "Button.hpp"
#include "pin_map.hpp"
/* Types and Constants */
/* Types and Constants */ /*----------------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------------------------- */
/* State machine */ /* State machine */
#define FOREACH_MODE(MODE) \ #define FOREACH_MODE(MODE) \
MODE(ControlOff) \ MODE(ControlOff) \
MODE(LightOn) \ MODE(LightOn) \
MODE(SetColor) \ MODE(SetColor) \
MODE(SetSaturation) \ MODE(SetSaturation) \
MODE(SetBrightness) \ MODE(SetBrightness) \
MODE(Shift) \ MODE(Shift)
#define GENERATE_MODE_ENUM(ENUM) ENUM, #define GENERATE_MODE_ENUM(ENUM) ENUM,
enum class Mode_e {FOREACH_MODE(GENERATE_MODE_ENUM)}; enum class Mode_e { FOREACH_MODE(GENERATE_MODE_ENUM) };
Mode_e& operator++(Mode_e& mode) Mode_e &operator++(Mode_e &mode) {
{ if (mode == Mode_e::Shift) {
if (mode == Mode_e::Shift) { return mode = Mode_e::ControlOff;
return mode = Mode_e::ControlOff; }
}
return mode = static_cast<Mode_e>(static_cast<int>(mode) + 1); return mode = static_cast<Mode_e>(static_cast<int>(mode) + 1);
} }
#ifdef DEBUG #ifdef DEBUG
#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 /*DEBUG*/ #endif /*DEBUG*/
/* Leds */ /* Leds */
uint16_t static const LedCount{24}; uint16_t static const LedCount{24};
uint8_t static const ColorSaturation{128}; uint8_t static const ColorSaturation{128};
uint8_t static const ColorArray[3][3] = uint8_t static const ColorArray[3][3] = {
{{ColorSaturation, 0, 0}, {0, ColorSaturation, 0}, {0, 0, ColorSaturation}}; {ColorSaturation, 0, 0}, {0, ColorSaturation, 0}, {0, 0, ColorSaturation}};
/* Led ring state */ /* Led ring state */
typedef struct { typedef struct {
@ -55,20 +54,19 @@ typedef struct {
/* Button */ /* Button */
int const AnalogButtonVRef{970}; int const AnalogButtonVRef{970};
/* Global variables */
/* Global variables */ /*----------------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------------------------- */
/* State machine */ /* State machine */
Mode_e currentMode{Mode_e::ControlOff}; Mode_e currentMode{Mode_e::ControlOff};
/* Led ring state */ /* Led ring state */
LedRingState_t ledRingState { LedRingState_t ledRingState{
.ledOnCounter = 1, .ledOnCounter = LedCount / 2,
.ledOnShift = 0, .ledOnShift = LedCount / 4,
.hue = 0, .hue = 0,
.saturation = 255, .saturation = 255,
.brightness = 16, .brightness = 16,
}; };
/* IO Objects */ /* IO Objects */
@ -78,22 +76,21 @@ AnalogButton button{AnalogButtonPin, AnalogButtonVRef};
/* debug Serial definition */ /* debug Serial definition */
#ifdef DEBUG #ifdef DEBUG
#ifdef ARDUINO_AVR_ATTINYX5 #ifdef ARDUINO_AVR_ATTINYX5
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
SoftwareSerial debugSerial{RxPin, TxPin}; // RX, TX SoftwareSerial debugSerial{RxPin, TxPin};
#endif /*ARDUINO_AVR_ATTINYX5*/ #endif /*ARDUINO_AVR_ATTINYX5*/
#ifdef ARDUINO_AVR_UNO #ifdef ARDUINO_AVR_UNO
#include <HardwareSerial.h> #include <HardwareSerial.h>
HardwareSerial &debugSerial = Serial; HardwareSerial &debugSerial = Serial;
#endif /*ARDUINO_AVR_UNO*/ #endif /*ARDUINO_AVR_UNO*/
#endif /*DEBUG*/ #endif /*DEBUG*/
/* Private function declarations */
/* Private function declarations */ /*----------------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------------------------- */
/* State machine */ /* State machine */
#define GENERATE_MODE_EXEC_DECLARTION(MODE) void MODE ## _execute (int shift); #define GENERATE_MODE_EXEC_DECLARTION(MODE) void MODE##_execute(int shift);
FOREACH_MODE(GENERATE_MODE_EXEC_DECLARTION) FOREACH_MODE(GENERATE_MODE_EXEC_DECLARTION)
/* Encoder */ /* Encoder */
@ -103,70 +100,69 @@ void refresh_led_ring(void);
void analog_button_calibration(void); void analog_button_calibration(void);
/* Function definitions */ /* Function definitions */
/* ---------------------------------------------------------------------------------------------- */ /*----------------------------------------------------------------------------------------------*/
void setup() void setup() {
{ #ifdef DEBUG
#ifdef DEBUG debugSerial.begin(9600);
debugSerial.begin(9600); while (!debugSerial.available())
while(!debugSerial.available()); ;
debugSerial.println("Init ... "); debugSerial.println("Init ... ");
#endif /*DEBUG*/ #endif /*DEBUG*/
ledRing.begin();
refresh_led_ring();
analog_button_calibration(); analog_button_calibration();
ledRing.begin();
ledRing.setBrightness(50);
ledRing.show();
get_encoder_shift(); get_encoder_shift();
#ifdef DEBUG #ifdef DEBUG
debugSerial.println(mode_str[static_cast<int>(currentMode)]); debugSerial.println(mode_str[static_cast<int>(currentMode)]);
#endif /*DEBUG*/ #endif /*DEBUG*/
} }
void loop() { void loop() {
int shift{get_encoder_shift()}; int shift{get_encoder_shift()};
if (button.is_button_pressed()) { if (button.is_button_pressed()) {
while(button.is_button_pressed()){ while (button.is_button_pressed()) {
delay(150); delay(150);
} }
++currentMode; ++currentMode;
#ifdef DEBUG #ifdef DEBUG
debugSerial.println(mode_str[static_cast<int>(currentMode)]); debugSerial.println(mode_str[static_cast<int>(currentMode)]);
#endif /*DEBUG*/ #endif /*DEBUG*/
} }
if (shift != 0) { if (shift != 0) {
switch (currentMode) { switch (currentMode) {
case Mode_e::ControlOff: case Mode_e::ControlOff:
ControlOff_execute(shift); ControlOff_execute(shift);
break; break;
case Mode_e::LightOn:
LightOn_execute(shift);
break;
case Mode_e::SetColor:
SetColor_execute(shift);
break;
case Mode_e::SetBrightness:
SetBrightness_execute(shift);
break;
case Mode_e::SetSaturation: case Mode_e::LightOn:
SetSaturation_execute(shift); LightOn_execute(shift);
break; break;
case Mode_e::Shift: case Mode_e::SetColor:
Shift_execute(shift); SetColor_execute(shift);
break; break;
default: case Mode_e::SetBrightness:
break; SetBrightness_execute(shift);
break;
case Mode_e::SetSaturation:
SetSaturation_execute(shift);
break;
case Mode_e::Shift:
Shift_execute(shift);
break;
default:
break;
} }
} }
@ -175,11 +171,10 @@ void loop() {
} }
} }
void ControlOff_execute (int shift) { void ControlOff_execute(int shift) {}
}
void LightOn_execute (int shift) { void LightOn_execute(int shift) {
uint16_t & ledOnCounter = ledRingState.ledOnCounter; uint16_t &ledOnCounter = ledRingState.ledOnCounter;
if (shift > 0) { if (shift > 0) {
++ledOnCounter; ++ledOnCounter;
@ -192,52 +187,52 @@ void LightOn_execute (int shift) {
ledOnCounter = LedCount; ledOnCounter = LedCount;
} }
} }
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(DEBUG) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
debugSerial.print(shift, DEC); debugSerial.print(shift, DEC);
debugSerial.print(", ledOnCounter="); debugSerial.print(", ledOnCounter=");
debugSerial.println(ledOnCounter, DEC); debugSerial.println(ledOnCounter, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/
} }
void SetColor_execute (int shift) { void SetColor_execute(int shift) {
uint16_t & hue = ledRingState.hue; uint16_t &hue = ledRingState.hue;
hue += (shift << 8); hue += (shift << 8);
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(DEBUG) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
debugSerial.print(shift, DEC); debugSerial.print(shift, DEC);
debugSerial.print(", hue="); debugSerial.print(", hue=");
debugSerial.println(hue, DEC); debugSerial.println(hue, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/
} }
void SetBrightness_execute (int shift) { void SetBrightness_execute(int shift) {
uint8_t & brightness = ledRingState.brightness; uint8_t &brightness = ledRingState.brightness;
brightness += (shift << 2); brightness += (shift << 2);
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(DEBUG) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
debugSerial.print(shift, DEC); debugSerial.print(shift, DEC);
debugSerial.print(", brightness="); debugSerial.print(", brightness=");
debugSerial.println(brightness, DEC); debugSerial.println(brightness, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/
} }
void Shift_execute (int shift) { void Shift_execute(int shift) {
uint16_t & ledOnShift = ledRingState.ledOnShift; uint16_t &ledOnShift = ledRingState.ledOnShift;
if (shift > 0) { if (shift > 0) {
++ledOnShift; ++ledOnShift;
@ -250,32 +245,32 @@ void Shift_execute (int shift) {
ledOnShift = LedCount - 1; ledOnShift = LedCount - 1;
} }
} }
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(DEBUG) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
debugSerial.print(shift, DEC); debugSerial.print(shift, DEC);
debugSerial.print(", ledOnShift="); debugSerial.print(", ledOnShift=");
debugSerial.println(ledOnShift, DEC); debugSerial.println(ledOnShift, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/
} }
void SetSaturation_execute (int shift) { void SetSaturation_execute(int shift) {
uint8_t & saturation = ledRingState.saturation; uint8_t &saturation = ledRingState.saturation;
saturation += (shift << 2); saturation += (shift << 2);
#if defined(DEBUG) && defined(ARDUINO_AVR_UNO) #if defined(DEBUG) && defined(ARDUINO_AVR_UNO)
if (shift != 0) { if (shift != 0) {
debugSerial.print(__func__); debugSerial.print(__func__);
debugSerial.print(" : shift="); debugSerial.print(" : shift=");
debugSerial.print(shift, DEC); debugSerial.print(shift, DEC);
debugSerial.print(", saturation="); debugSerial.print(", saturation=");
debugSerial.println(saturation, DEC); debugSerial.println(saturation, DEC);
} }
#endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/ #endif /*defined(DEBUG) && defined(ARDUINO_AVR_UNO)*/
} }
int get_encoder_shift(void) { int get_encoder_shift(void) {
@ -304,43 +299,38 @@ void refresh_led_ring(void) {
if (ledId >= LedCount) { if (ledId >= LedCount) {
ledId -= LedCount; ledId -= LedCount;
} }
ledRing.setPixelColor(ledId, Adafruit_NeoPixel::ColorHSV ledRing.setPixelColor(ledId, Adafruit_NeoPixel::ColorHSV(
(ledRingState.hue, ledRingState.saturation, ledRingState.brightness)); ledRingState.hue, ledRingState.saturation,
ledRingState.brightness));
} }
ledRing.show(); ledRing.show();
} }
void analog_button_calibration(void) { void analog_button_calibration(void) {
int hRef; int highRef, lowRef;
do { do {
hRef = analogRead(AnalogButtonPin); highRef = analogRead(AnalogButtonPin);
delay(300); delay(300);
#ifdef DEBUG #ifdef DEBUG
debugSerial.println(hRef); debugSerial.println(highRef);
#endif /*DEBUG*/ #endif /*DEBUG*/
} while (hRef < 1000); } while (highRef < 1000);
lowRef = analogRead(AnalogButtonPin);
while (highRef - lowRef < 50) {
#ifdef DEBUG
debugSerial.println(lowRef);
#endif /*DEBUG*/
int lRef = analogRead(AnalogButtonPin); lowRef = analogRead(AnalogButtonPin);
while (hRef - lRef < 50) { delay(300);
#ifdef DEBUG
debugSerial.println(lRef);
#endif /*DEBUG*/
lRef = analogRead(AnalogButtonPin);
delay(300);
} }
#ifdef DEBUG #ifdef DEBUG
debugSerial.println(lRef); debugSerial.println(lowRef);
#endif /*DEBUG*/ #endif /*DEBUG*/
button = AnalogButton{AnalogButtonPin, lRef + 10};
while(analogRead(AnalogButtonPin) < lRef + 10){
delay(150);
}
button = AnalogButton{AnalogButtonPin, lowRef + 10};
} }