added long press to turn off:

This commit is contained in:
Daan Meijer 2025-02-10 00:01:51 +01:00
parent 1c9398681d
commit 712d1bac78
5 changed files with 72 additions and 21 deletions

View File

@ -58,7 +58,7 @@ void Controls_loop(void *pvParameters){
if(CONTROLS_DEBUG){
Serial.printf("Key: %d\n", duration);
}
Controls_send_event(Key);
Controls_send_event({Key, (int)duration});
}else{
if(CONTROLS_DEBUG){
Serial.println("Too short");
@ -92,7 +92,7 @@ void Controls_loop(void *pvParameters){
if(CONTROLS_DEBUG){
Serial.println("Dir A");
}
Controls_send_event(Counterclockwise);
Controls_send_event({Counterclockwise, NULL});
}else if(
pinChanges[base + 0] == PIN_B &&
pinChanges[base + 1] == PIN_A &&
@ -103,7 +103,7 @@ void Controls_loop(void *pvParameters){
if(CONTROLS_DEBUG){
Serial.println("Dir B");
}
Controls_send_event(Clockwise);
Controls_send_event({Clockwise, NULL});
}else{
if(CONTROLS_DEBUG){
Serial.println("Unknown direction");
@ -136,5 +136,5 @@ void Controls_setup(){
TaskHandle_t taskControls = NULL;
xTaskCreate(Controls_loop, "Controls_loop", 1000, NULL, tskIDLE_PRIORITY, &taskControls);
xTaskCreate(Controls_loop, "Controls_loop", 2048, NULL, tskIDLE_PRIORITY, &taskControls);
}

View File

@ -6,16 +6,22 @@
#define PIN_B 3
#define PIN_KEY 4
#define KEY_DURATION_MS 120
#define KEY_DURATION_MS 150
#define CONTROLS_DEBUG false
enum ControlEvent {
enum ControlEventType {
Clockwise,
Counterclockwise,
Key
};
struct ControlEvent {
ControlEventType type;
int data;
};
typedef void (* ControlsCallback)( ControlEvent ) ;
void Controls_setup();

View File

@ -3,21 +3,17 @@
CRGB colorTable[] = {
CRGB(255, 243, 218),
CRGB(255, 250, 237),
CRGB(255, 255, 255),
CRGB(250, 233, 213),
CRGB::Red,
CRGB::Blue,
CRGB::Green,
CRGB::Purple,
CRGB::Turquoise,
CRGB::Yellow,
// CRGB(255, 100, 4),
// CRGB(255, 128, 4),
// CRGB(255, 100, 32),
// CRGB(255, 128, 32),
CRGB::White,
CRGB(255, 142, 32), // "warm wit"
};
Hanglamp::Hanglamp(){
turnOn();
}
void Hanglamp::setup(){
@ -57,8 +53,8 @@ void Hanglamp::adjustBrightness(int add){
}
void Hanglamp::controlsCallback(ControlEvent event){
Serial.printf("ControlEvent: %d\n", event);
switch(event){
Serial.printf("ControlEventType: %d\n", event);
switch(event.type){
case Clockwise:
adjustBrightness(4);
break;
@ -66,11 +62,51 @@ void Hanglamp::controlsCallback(ControlEvent event){
adjustBrightness(-4);
break;
case Key:
nextColor();
Serial.printf("key press %d\n", event.data);
if(!isOn()){
turnOn();
}else{
auto msDuration = event.data/1000;
Serial.printf("msDuration %d\n", msDuration);
if(msDuration > 800){
turnOff();
}else{
nextColor();
}
}
break;
}
}
void Hanglamp::status(CRGB color){
Led_status(color);
}
}
void Hanglamp::setOn(bool on){
Serial.printf("Hanglamp::setOn(%d -> %d)\n", this->on, on);
this->on = on;
}
bool Hanglamp::isOn(){
return this->on;
}
void Hanglamp::turnOn(){
if(!this->on){
FastLED.setBrightness(this->brightness);
FastLED.show();
this->setOn(true);
}
}
void Hanglamp::turnOff(){
if(this->on){
FastLED.setBrightness(0);
FastLED.show();
this->setOn(false);
}
}

View File

@ -16,9 +16,15 @@ class Hanglamp {
void status(CRGB color);
void setColor(CRGB color);
bool isOn();
void turnOn();
void turnOff();
private:
char colorIndex = 0;
int brightness = 128;
bool on = false;
void setOn(bool on);
};

View File

@ -1,3 +1,4 @@
#pragma once
#include "settings.h"
@ -5,6 +6,8 @@
#include <WiFi.h>
#include <vector>
using namespace std;