controls work, rendering on low brightness is wonky

This commit is contained in:
Daan Meijer 2025-01-22 23:38:06 +01:00
parent 59a5fd0770
commit 31c9c740b3
4 changed files with 201 additions and 39 deletions

View File

@ -8,26 +8,130 @@ uint8_t controlPins[] = {
};
void Controls_setup(){
pinMode(PIN_A, INPUT_PULLDOWN);
pinMode(PIN_B, INPUT_PULLDOWN);
pinMode(PIN_KEY, INPUT_PULLDOWN);
uint16_t prevState[3];
auto lastChange = micros();
uint8_t pinChanges[128];
uint8_t pinChangeIndex = 0;
void Controls_direction_reset(){
lastChange = 0;
pinChangeIndex = 0;
}
int16_t prevState[3];
auto lastKey = micros();
void Controls_loop(){
ControlsCallback handler = NULL;
void Controls_handler(ControlsCallback newHandler){
handler = newHandler;
}
void Controls_send_event(ControlEvent event){
if(handler){
handler(event);
}
}
void Controls_loop(void *pvParameters){
lastKey = 0;
lastChange = 0;
while(true){
for(uint8_t a=0; a<sizeof(controlPins); a++){
auto pin = controlPins[a];
auto currentValue = analogRead(pin);
auto currentValue = analogRead(pin) > 3072;
auto prevValue = prevState[a];
Serial.printf("Pin %d: %d->%d\n", pin, prevValue, currentValue);
// Serial.printf("Pin %d: %d->%d\n", pin, prevValue, currentValue);
if(abs(currentValue - prevValue) > 100){
Serial.println("Changed!");
// if(abs(currentValue - prevValue) > 100){
if(currentValue != prevValue){
if(pin == PIN_KEY){
if(currentValue){
auto duration = micros() - lastKey;
if(duration > KEY_DURATION_MS * 1000){
if(CONTROLS_DEBUG){
Serial.printf("Key: %d\n", duration);
}
Controls_send_event(Key);
}else{
if(CONTROLS_DEBUG){
Serial.println("Too short");
}
}
lastKey = 0;
} else {
lastKey = micros();
}
}else{
pinChanges[pinChangeIndex++] = pin;
lastChange = micros();
}
}
prevState[a] = currentValue;
}
//maybe we have a valid sequence
if(pinChangeIndex >= 4){
// Serial.printf("Have %d changes\n", pinChangeIndex);
for(uint8_t base = 0; base + 4 <= pinChangeIndex; base += 4){
if(
pinChanges[base + 0] == PIN_A &&
pinChanges[base + 1] == PIN_B &&
pinChanges[base + 2] == PIN_A &&
pinChanges[base + 3] == PIN_B
){
if(CONTROLS_DEBUG){
Serial.println("Dir A");
}
Controls_send_event(Counterclockwise);
}else if(
pinChanges[base + 0] == PIN_B &&
pinChanges[base + 1] == PIN_A &&
pinChanges[base + 2] == PIN_B &&
pinChanges[base + 3] == PIN_A
){
if(CONTROLS_DEBUG){
Serial.println("Dir B");
}
Controls_send_event(Clockwise);
}else{
if(CONTROLS_DEBUG){
Serial.println("Unknown direction");
}
}
}
Controls_direction_reset();
}
if(lastChange){
if(micros() - lastChange > 100 * 1000){
if(CONTROLS_DEBUG){
Serial.println("changes expired");
}
Controls_direction_reset();
}
}
vTaskDelay(1/portTICK_PERIOD_MS);
}
}
void Controls_setup(){
pinMode(PIN_A, INPUT_PULLDOWN);
pinMode(PIN_B, INPUT_PULLDOWN);
pinMode(PIN_KEY, INPUT_PULLDOWN);
TaskHandle_t taskControls = NULL;
xTaskCreate(Controls_loop, "Controls_loop", 1000, NULL, tskIDLE_PRIORITY, &taskControls);
}

View File

@ -6,6 +6,18 @@
#define PIN_B 3
#define PIN_KEY 4
#define KEY_DURATION_MS 120
#define CONTROLS_DEBUG false
enum ControlEvent {
Clockwise,
Counterclockwise,
Key
};
typedef void (* ControlsCallback)( ControlEvent ) ;
void Controls_setup();
void Controls_loop();
void Controls_handler(ControlsCallback callback);

View File

@ -93,20 +93,23 @@ CRGB colorTable[] = {
};
uint8_t colorIndex = 0;
void colors(void *pvParameters){
while(true){
int8_t colorIndex = 0;
void nextColor(){
colorIndex++;
if(colorIndex >= (sizeof(colorTable) / sizeof(CRGB))){
colorIndex = 0;
}
Serial.printf("colorIndex: %d\n", colorIndex);
jumpTo(colorTable[colorIndex]);
}
void colors(void *pvParameters){
while(true){
nextColor();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
#endif
@ -130,12 +133,52 @@ gpio_num_t sensePins[] = {
long prevStates[sizeof(sensePins)];
#endif
int16_t brightness = 128;
#ifdef ENABLE_CONTROLS
void newBrightness(){
uint8_t newBrightness =
brightness < 0 ? 0 :
brightness > 255 ? 255 :
brightness;
Serial.printf("new brightness: %d\n", newBrightness);
delay(100);
FastLED.setBrightness(newBrightness);
FastLED.show();
}
void controlsCallback(ControlEvent event){
Serial.printf("ControlEvent: %d\n", event);
switch(event){
case Clockwise:
if(brightness < 256){
brightness += 8;
newBrightness();
}
break;
case Counterclockwise:
if(brightness > 0){
brightness -= 8 ;
newBrightness();
}
break;
case Key:
nextColor();
break;
}
}
#endif
void setup() {
Serial.begin(115200);
Serial.println("Waiting...");
delay(1500);
Serial.println("That's long enough.");
#ifdef ENABLE_PIN_SENSING
for(int i=0; i<sizeof(sensePins)/sizeof(gpio_num_t); i++){
@ -154,12 +197,8 @@ void setup() {
#endif
// runner.init();
Serial.println("Initialized scheduler");
Serial.println("Scheduled tasks");
#ifdef ENABLE_PINFINDER
PinFinder_setup();
@ -167,11 +206,15 @@ void setup() {
#ifdef ENABLE_CONTROLS
Controls_setup();
Controls_handler(controlsCallback);
#endif
#ifdef INCLUDE_LED
Led_setup();
FastLED.setBrightness(brightness);
jumpTo(CRGB(0xFF00FF));
#endif
@ -196,15 +239,22 @@ void setup() {
jumpTo(CRGB(0x00FFFF));
#endif
#ifdef ENABLE_COLORS
TaskHandle_t taskColors = NULL;
Serial.println("Scheduling tasks");
// Create the task, storing the handle.
xTaskCreate(colors, "NAME", 1000, NULL, tskIDLE_PRIORITY, &taskColors);
// xTaskCreate(colors, "NAME", 1000, NULL, tskIDLE_PRIORITY, &taskColors);
Serial.println("Scheduled tasks");
#endif
vTaskStartScheduler();
// ESP.wdtEnable(10000);
Serial.println("Initialized scheduler");
}
@ -223,10 +273,6 @@ void loop() {
PinFinder_loop();
#endif
#ifdef ENABLE_CONTROLS
Controls_loop();
#endif
#ifdef ENABLE_PIN_SENSING
for(int i=0; i<sizeof(sensePins)/sizeof(gpio_num_t); i++){
auto pin = sensePins[i];

View File

@ -20,8 +20,8 @@
#include <NeoPixelBus.h>
#endif
#define PIN_STRIP1 5
#define PIN_STRIP2 6
#define PIN_STRIP1 8
#define PIN_STRIP2 9
#define STRIP_LENGTH 46