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,11 +1,11 @@
#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 */
/*----------------------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------------------*/
@ -17,14 +17,13 @@
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;
} }
@ -40,8 +39,8 @@ Mode_e& operator++(Mode_e& mode)
/* 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,7 +54,6 @@ typedef struct {
/* Button */ /* Button */
int const AnalogButtonVRef{970}; int const AnalogButtonVRef{970};
/* Global variables */ /* Global variables */
/*----------------------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------------------*/
@ -64,8 +62,8 @@ 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,
@ -80,7 +78,7 @@ AnalogButton button{AnalogButtonPin, AnalogButtonVRef};
#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>
@ -88,7 +86,6 @@ AnalogButton button{AnalogButtonPin, AnalogButtonVRef};
#endif /*ARDUINO_AVR_UNO*/ #endif /*ARDUINO_AVR_UNO*/
#endif /*DEBUG*/ #endif /*DEBUG*/
/* Private function declarations */ /* Private function declarations */
/*----------------------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------------------*/
@ -105,19 +102,18 @@ 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*/
analog_button_calibration();
ledRing.begin(); ledRing.begin();
ledRing.setBrightness(50); refresh_led_ring();
ledRing.show();
analog_button_calibration();
get_encoder_shift(); get_encoder_shift();
@ -175,8 +171,7 @@ 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;
@ -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);
int lRef = analogRead(AnalogButtonPin); while (highRef - lowRef < 50) {
while (hRef - lRef < 50) {
#ifdef DEBUG #ifdef DEBUG
debugSerial.println(lRef); debugSerial.println(lowRef);
#endif /*DEBUG*/ #endif /*DEBUG*/
lRef = analogRead(AnalogButtonPin); lowRef = analogRead(AnalogButtonPin);
delay(300); delay(300);
} }
#ifdef DEBUG #ifdef DEBUG
debugSerial.println(lRef); debugSerial.println(lowRef);
#endif /*DEBUG*/ #endif /*DEBUG*/
button = AnalogButton{AnalogButtonPin, lRef + 10}; button = AnalogButton{AnalogButtonPin, lowRef + 10};
while(analogRead(AnalogButtonPin) < lRef + 10){
delay(150);
}
} }