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