Rotary encoder enhancements:
- Added double press action. - Rewritten button handling.
This commit is contained in:
parent
7dc41ab205
commit
20bc3719a4
@ -115,17 +115,20 @@ static int re_qstringCmp(const void *ap, const void *bp) {
|
|||||||
|
|
||||||
class RotaryEncoderUIUsermod : public Usermod {
|
class RotaryEncoderUIUsermod : public Usermod {
|
||||||
private:
|
private:
|
||||||
int fadeAmount = 5; // Amount to change every step (brightness)
|
int8_t fadeAmount = 5; // Amount to change every step (brightness)
|
||||||
unsigned long currentTime;
|
|
||||||
unsigned long loopTime;
|
unsigned long loopTime;
|
||||||
unsigned long buttonHoldTime;
|
|
||||||
|
unsigned long buttonPressedTime = 0;
|
||||||
|
unsigned long buttonWaitTime = 0;
|
||||||
|
bool buttonPressedBefore = false;
|
||||||
|
bool buttonLongPressed = false;
|
||||||
|
|
||||||
int8_t pinA = ENCODER_DT_PIN; // DT from encoder
|
int8_t pinA = ENCODER_DT_PIN; // DT from encoder
|
||||||
int8_t pinB = ENCODER_CLK_PIN; // CLK from encoder
|
int8_t pinB = ENCODER_CLK_PIN; // CLK from encoder
|
||||||
int8_t pinC = ENCODER_SW_PIN; // SW from encoder
|
int8_t pinC = ENCODER_SW_PIN; // SW from encoder
|
||||||
unsigned char select_state = 0; // 0: brightness, 1: effect, 2: effect speed
|
|
||||||
unsigned char button_state = HIGH;
|
unsigned char select_state = 0; // 0: brightness, 1: effect, 2: effect speed, ...
|
||||||
unsigned char prev_button_state = HIGH;
|
|
||||||
bool networkShown = false;
|
|
||||||
uint16_t currentHue1 = 16; // default boot color
|
uint16_t currentHue1 = 16; // default boot color
|
||||||
byte currentSat1 = 255;
|
byte currentSat1 = 255;
|
||||||
|
|
||||||
@ -271,8 +274,7 @@ public:
|
|||||||
pinMode(pinA, INPUT_PULLUP);
|
pinMode(pinA, INPUT_PULLUP);
|
||||||
pinMode(pinB, INPUT_PULLUP);
|
pinMode(pinB, INPUT_PULLUP);
|
||||||
pinMode(pinC, INPUT_PULLUP);
|
pinMode(pinC, INPUT_PULLUP);
|
||||||
currentTime = millis();
|
loopTime = millis();
|
||||||
loopTime = currentTime;
|
|
||||||
|
|
||||||
if (!initDone) sortModesAndPalettes();
|
if (!initDone) sortModesAndPalettes();
|
||||||
|
|
||||||
@ -312,7 +314,8 @@ public:
|
|||||||
*/
|
*/
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
currentTime = millis(); // get the current elapsed time
|
if (!enabled || strip.isUpdating()) return;
|
||||||
|
unsigned long currentTime = millis(); // get the current elapsed time
|
||||||
|
|
||||||
// Initialize effectCurrentIndex and effectPaletteIndex to
|
// Initialize effectCurrentIndex and effectPaletteIndex to
|
||||||
// current state. We do it here as (at least) effectCurrent
|
// current state. We do it here as (at least) effectCurrent
|
||||||
@ -328,17 +331,35 @@ public:
|
|||||||
|
|
||||||
if (currentTime >= (loopTime + 2)) // 2ms since last check of encoder = 500Hz
|
if (currentTime >= (loopTime + 2)) // 2ms since last check of encoder = 500Hz
|
||||||
{
|
{
|
||||||
button_state = digitalRead(pinC);
|
loopTime = currentTime; // Updates loopTime
|
||||||
if (prev_button_state != button_state)
|
|
||||||
{
|
|
||||||
if (button_state == HIGH && (millis()-buttonHoldTime < 3000))
|
|
||||||
{
|
|
||||||
prev_button_state = button_state;
|
|
||||||
|
|
||||||
|
bool buttonPressed = !digitalRead(pinC); //0=pressed, 1=released
|
||||||
|
if (buttonPressed) {
|
||||||
|
if (!buttonPressedBefore) buttonPressedTime = currentTime;
|
||||||
|
buttonPressedBefore = true;
|
||||||
|
if (currentTime-buttonPressedTime > 3000) {
|
||||||
|
if (!buttonLongPressed) displayNetworkInfo(); //long press for network info
|
||||||
|
buttonLongPressed = true;
|
||||||
|
}
|
||||||
|
} else if (!buttonPressed && buttonPressedBefore) {
|
||||||
|
bool doublePress = buttonWaitTime;
|
||||||
|
buttonWaitTime = 0;
|
||||||
|
if (!buttonLongPressed) {
|
||||||
|
if (doublePress) {
|
||||||
|
toggleOnOff();
|
||||||
|
lampUdated();
|
||||||
|
} else {
|
||||||
|
buttonWaitTime = currentTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buttonLongPressed = false;
|
||||||
|
buttonPressedBefore = false;
|
||||||
|
}
|
||||||
|
if (buttonWaitTime && currentTime-buttonWaitTime>350 && !buttonPressedBefore) {
|
||||||
|
buttonWaitTime = 0;
|
||||||
char newState = select_state + 1;
|
char newState = select_state + 1;
|
||||||
if (newState > LAST_UI_STATE || (newState == 7 && presetHigh==0 && presetLow == 0)) newState = 0;
|
|
||||||
|
|
||||||
bool changedState = true;
|
bool changedState = true;
|
||||||
|
if (newState > LAST_UI_STATE || (newState == 7 && presetHigh==0 && presetLow == 0)) newState = 0;
|
||||||
if (display != nullptr) {
|
if (display != nullptr) {
|
||||||
switch(newState) {
|
switch(newState) {
|
||||||
case 0: changedState = changeState(PSTR("Brightness"), 1, 0, 1); break; //1 = sun
|
case 0: changedState = changeState(PSTR("Brightness"), 1, 0, 1); break; //1 = sun
|
||||||
@ -351,22 +372,7 @@ public:
|
|||||||
case 7: changedState = changeState(PSTR("Preset"), 255, 255, 6); break; //6 = moon
|
case 7: changedState = changeState(PSTR("Preset"), 255, 255, 6); break; //6 = moon
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (changedState) {
|
if (changedState) select_state = newState;
|
||||||
select_state = newState;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
prev_button_state = button_state;
|
|
||||||
networkShown = false;
|
|
||||||
if (!prev_button_state) buttonHoldTime = millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!prev_button_state && (millis()-buttonHoldTime > 3000) && !networkShown) {
|
|
||||||
displayNetworkInfo(); //long press for network info
|
|
||||||
loopTime = currentTime; // Updates loopTime
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Enc_A = digitalRead(pinA); // Read encoder pins
|
Enc_A = digitalRead(pinA); // Read encoder pins
|
||||||
@ -401,14 +407,12 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Enc_A_prev = Enc_A; // Store value of A for next time
|
Enc_A_prev = Enc_A; // Store value of A for next time
|
||||||
loopTime = currentTime; // Updates loopTime
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void displayNetworkInfo() {
|
void displayNetworkInfo() {
|
||||||
#ifdef USERMOD_FOUR_LINE_DISPLAY
|
#ifdef USERMOD_FOUR_LINE_DISPLAY
|
||||||
display->networkOverlay(PSTR("NETWORK INFO"), 10000);
|
display->networkOverlay(PSTR("NETWORK INFO"), 10000);
|
||||||
networkShown = true;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,8 +464,7 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (increase) bri = (bri + fadeAmount <= 255) ? (bri + fadeAmount) : 255;
|
bri = max(min((increase ? bri+fadeAmount : bri-fadeAmount), 255), 0);
|
||||||
else bri = (bri - fadeAmount >= 0) ? (bri - fadeAmount) : 0;
|
|
||||||
lampUdated();
|
lampUdated();
|
||||||
#ifdef USERMOD_FOUR_LINE_DISPLAY
|
#ifdef USERMOD_FOUR_LINE_DISPLAY
|
||||||
display->updateBrightness();
|
display->updateBrightness();
|
||||||
@ -543,8 +546,7 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (increase) { if (currentHue1<256) currentHue1 += 4; else currentHue1 = 0; }
|
currentHue1 = max(min((increase ? currentHue1+fadeAmount : currentHue1-fadeAmount), 255), 0);
|
||||||
else { if (currentHue1>3) currentHue1 -= 4; else currentHue1 = 256; }
|
|
||||||
colorHStoRGB(currentHue1*256, currentSat1, col);
|
colorHStoRGB(currentHue1*256, currentSat1, col);
|
||||||
strip.applyToAllSelected = true;
|
strip.applyToAllSelected = true;
|
||||||
strip.setColor(0, colorFromRgbw(col));
|
strip.setColor(0, colorFromRgbw(col));
|
||||||
@ -583,7 +585,7 @@ public:
|
|||||||
if (presetHigh && presetLow && presetHigh > presetLow) {
|
if (presetHigh && presetLow && presetHigh > presetLow) {
|
||||||
String apireq = F("win&PL=~");
|
String apireq = F("win&PL=~");
|
||||||
if (!increase) apireq += '-';
|
if (!increase) apireq += '-';
|
||||||
apireq += F("1&P1=");
|
apireq += F("&P1=");
|
||||||
apireq += presetLow;
|
apireq += presetLow;
|
||||||
apireq += F("&P2=");
|
apireq += F("&P2=");
|
||||||
apireq += presetHigh;
|
apireq += presetHigh;
|
||||||
|
Loading…
Reference in New Issue
Block a user