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>
// 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 */
/*----------------------------------------------------------------------------------------------*/
@ -17,14 +17,13 @@
MODE(SetColor) \
MODE(SetSaturation) \
MODE(SetBrightness) \
MODE(Shift) \
MODE(Shift)
#define GENERATE_MODE_ENUM(ENUM) 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) {
return mode = Mode_e::ControlOff;
}
@ -40,8 +39,8 @@ Mode_e& operator++(Mode_e& mode)
/* 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,7 +54,6 @@ typedef struct {
/* Button */
int const AnalogButtonVRef{970};
/* Global variables */
/*----------------------------------------------------------------------------------------------*/
@ -64,8 +62,8 @@ Mode_e currentMode{Mode_e::ControlOff};
/* Led ring state */
LedRingState_t ledRingState{
.ledOnCounter = 1,
.ledOnShift = 0,
.ledOnCounter = LedCount / 2,
.ledOnShift = LedCount / 4,
.hue = 0,
.saturation = 255,
.brightness = 16,
@ -80,7 +78,7 @@ AnalogButton button{AnalogButtonPin, AnalogButtonVRef};
#ifdef DEBUG
#ifdef ARDUINO_AVR_ATTINYX5
#include <SoftwareSerial.h>
SoftwareSerial debugSerial{RxPin, TxPin}; // RX, TX
SoftwareSerial debugSerial{RxPin, TxPin};
#endif /*ARDUINO_AVR_ATTINYX5*/
#ifdef ARDUINO_AVR_UNO
#include <HardwareSerial.h>
@ -88,7 +86,6 @@ AnalogButton button{AnalogButtonPin, AnalogButtonVRef};
#endif /*ARDUINO_AVR_UNO*/
#endif /*DEBUG*/
/* Private function declarations */
/*----------------------------------------------------------------------------------------------*/
@ -105,19 +102,18 @@ void analog_button_calibration(void);
/* Function definitions */
/*----------------------------------------------------------------------------------------------*/
void setup()
{
void setup() {
#ifdef DEBUG
debugSerial.begin(9600);
while(!debugSerial.available());
while (!debugSerial.available())
;
debugSerial.println("Init ... ");
#endif /*DEBUG*/
analog_button_calibration();
ledRing.begin();
ledRing.setBrightness(50);
ledRing.show();
refresh_led_ring();
analog_button_calibration();
get_encoder_shift();
@ -175,8 +171,7 @@ void loop() {
}
}
void ControlOff_execute (int shift) {
}
void ControlOff_execute(int shift) {}
void LightOn_execute(int shift) {
uint16_t &ledOnCounter = ledRingState.ledOnCounter;
@ -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);
highRef = analogRead(AnalogButtonPin);
delay(300);
#ifdef DEBUG
debugSerial.println(hRef);
debugSerial.println(highRef);
#endif /*DEBUG*/
} while (hRef < 1000);
} while (highRef < 1000);
int lRef = analogRead(AnalogButtonPin);
while (hRef - lRef < 50) {
lowRef = analogRead(AnalogButtonPin);
while (highRef - lowRef < 50) {
#ifdef DEBUG
debugSerial.println(lRef);
debugSerial.println(lowRef);
#endif /*DEBUG*/
lRef = analogRead(AnalogButtonPin);
lowRef = analogRead(AnalogButtonPin);
delay(300);
}
#ifdef DEBUG
debugSerial.println(lRef);
debugSerial.println(lowRef);
#endif /*DEBUG*/
button = AnalogButton{AnalogButtonPin, lRef + 10};
while(analogRead(AnalogButtonPin) < lRef + 10){
delay(150);
}
button = AnalogButton{AnalogButtonPin, lowRef + 10};
}