change button order and light brightness

This commit is contained in:
Philipp Kramer 2025-01-03 16:18:00 +01:00
parent 7908510bec
commit ab3a201674

View file

@ -49,14 +49,20 @@ Adafruit_NeoKey_1x4 neokey; // Create the NeoKey object
uint32_t Wheel(byte WheelPos);
NeoKey1x4Callback blink(keyEvent evt);
int32_t multiplyColor(int32_t color,float v);
#define BTN_IDX_0 3
#define BTN_IDX_1 2
#define BTN_IDX_2 1
#define BTN_IDX_3 0
OneButton button1;
void click1();
void doubleclick1();
void longPressStart1();
Adafruit_MQTT_Publish button1Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button1");
Adafruit_MQTT_Subscribe button1Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button1");
Adafruit_MQTT_Publish button1Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button/1");
Adafruit_MQTT_Subscribe button1Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button/1");
void button1ColorCB(uint32_t c);
uint32_t releasecolor1;
@ -64,8 +70,8 @@ OneButton button2;
void click2();
void doubleclick2();
void longPressStart2();
Adafruit_MQTT_Publish button2Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button2");
Adafruit_MQTT_Subscribe button2Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button2");
Adafruit_MQTT_Publish button2Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button/2");
Adafruit_MQTT_Subscribe button2Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button/2");
void button2ColorCB(uint32_t c);
uint32_t releasecolor2;
@ -73,8 +79,8 @@ OneButton button3;
void click3();
void doubleclick3();
void longPressStart3();
Adafruit_MQTT_Publish button3Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button3");
Adafruit_MQTT_Subscribe button3Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button3");
Adafruit_MQTT_Publish button3Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button/3");
Adafruit_MQTT_Subscribe button3Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button/3");
void button3ColorCB(uint32_t c);
uint32_t releasecolor3;
@ -82,14 +88,14 @@ OneButton button4;
void click4();
void doubleclick4();
void longPressStart4();
Adafruit_MQTT_Publish button4Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button4");
Adafruit_MQTT_Subscribe button4Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button4");
Adafruit_MQTT_Publish button4Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button/4");
Adafruit_MQTT_Subscribe button4Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button/4");
void button4ColorCB(uint32_t c);
uint32_t releasecolor4;
uint32_t presscolor = 0x555555;
//uint32_t presscolor = 0xa07020;
@ -207,24 +213,28 @@ void setup() {
#endif
button1.setDebounceMs(10);
button1.attachClick(click1);
button1.attachDoubleClick(doubleclick1);
button1.attachLongPressStart(longPressStart1);
button1Color.setCallback(button1ColorCB);
mqtt.subscribe(&button1Color);
button2.setDebounceMs(10);
button2.attachClick(click2);
button2.attachDoubleClick(doubleclick2);
button2.attachLongPressStart(longPressStart2);
button2Color.setCallback(button2ColorCB);
mqtt.subscribe(&button2Color);
button4.setDebounceMs(10);
button3.attachClick(click3);
button3.attachDoubleClick(doubleclick3);
button3.attachLongPressStart(longPressStart3);
button3Color.setCallback(button3ColorCB);
mqtt.subscribe(&button3Color);
button4.setDebounceMs(10);
button4.attachClick(click4);
button4.attachDoubleClick(doubleclick4);
button4.attachLongPressStart(longPressStart4);
@ -249,40 +259,44 @@ void loop() {
// Check each button, if pressed, light the matching neopixel
if (buttons & (1<<0)) {
if (buttons & (1<<BTN_IDX_0)) {
//Serial.println("Button A");
neokey.pixels.setPixelColor(0, presscolor);
//neokey.pixels.setPixelColor(BTN_IDX_0, presscolor);
neokey.pixels.setPixelColor(BTN_IDX_0, multiplyColor(releasecolor1,10));
}else{
neokey.pixels.setPixelColor(0, releasecolor1);
neokey.pixels.setPixelColor(BTN_IDX_0, releasecolor1);
}
if (buttons & (1<<1)) {
if (buttons & (1<<BTN_IDX_1)) {
//Serial.println("Button B");
neokey.pixels.setPixelColor(1, presscolor);
//neokey.pixels.setPixelColor(BTN_IDX_1, presscolor);
neokey.pixels.setPixelColor(BTN_IDX_1, multiplyColor(releasecolor2,10));
}else{
neokey.pixels.setPixelColor(1, releasecolor2);
neokey.pixels.setPixelColor(BTN_IDX_1, releasecolor2);
}
if (buttons & (1<<2)) {
if (buttons & (1<<BTN_IDX_2)) {
//Serial.println("Button C");
neokey.pixels.setPixelColor(2, presscolor);
//neokey.pixels.setPixelColor(BTN_IDX_2, presscolor);
neokey.pixels.setPixelColor(BTN_IDX_2, multiplyColor(releasecolor3,10));
}else{
neokey.pixels.setPixelColor(2, releasecolor3);
neokey.pixels.setPixelColor(BTN_IDX_2, releasecolor3);
}
if (buttons & (1<<3)) {
if (buttons & (1<<BTN_IDX_3)) {
//Serial.println("Button D");
neokey.pixels.setPixelColor(3, presscolor);
//neokey.pixels.setPixelColor(BTN_IDX_3, presscolor);
neokey.pixels.setPixelColor(BTN_IDX_3, multiplyColor(releasecolor4,10));
}else{
neokey.pixels.setPixelColor(3, releasecolor4);
neokey.pixels.setPixelColor(BTN_IDX_3, releasecolor4);
}
neokey.pixels.show();
button1.tick(buttons & (1<<0));
button2.tick(buttons & (1<<1));
button3.tick(buttons & (1<<2));
button4.tick(buttons & (1<<3));
button1.tick(buttons & (1<<BTN_IDX_0));
button2.tick(buttons & (1<<BTN_IDX_1));
button3.tick(buttons & (1<<BTN_IDX_2));
button4.tick(buttons & (1<<BTN_IDX_3));
}
static unsigned long last_status_send=0;
@ -304,6 +318,26 @@ void loop() {
}
int32_t multiplyColor(int32_t color,float v) {
byte r = color >> 16;
byte g = color >> 8;
byte b = color;
float maxV=255.0/max(r,max(g,b)); //limit so that no color component clips 255
v=min(v,maxV);
float rnew=constrain(r*v,0,255);
float gnew=constrain(g*v,0,255);
float bnew=constrain(b*v,0,255);
r=byte(rnew);
g=byte(gnew);
b=byte(bnew);
int32_t c=(r<<16) + (g<<8) + b;
return c;
}
// React to Ethernet events:-
#ifndef WIFI
void WiFiEvent(WiFiEvent_t event)