-
Notifications
You must be signed in to change notification settings - Fork 8
/
a50_knob.ino
131 lines (111 loc) · 4.61 KB
/
a50_knob.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************
# #
# An alternative firmware for Xiaomi Desk Lamp (Yeelight) #
# #
# #
# Copyright (C) 2018 Tom Stöveken #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
********************************************************************************/
/* circumvent bug: https://github.com/PaulStoffregen/Encoder/issues/40 */
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
#include <OneButton.h>
#define ENCODER_A_PIN 12
#define ENCODER_B_PIN 13
#define ENCODER_SWITCH_PIN 2
#define RESET_BUTTON_PIN 14
Encoder knob(ENCODER_A_PIN, ENCODER_B_PIN);
OneButton button(ENCODER_SWITCH_PIN, true);
OneButton resetSwitch(RESET_BUTTON_PIN, true);
// remember the last time user double clicked onto knob
static unsigned long LastDoubleClick = 0;
/******************************************************************************
Description.: prepare the rotary knob encoder and switch
Input Value.: -
Return Value: -
******************************************************************************/
void setup_knob() {
knob.write(0);
button.attachClick([](){
Log("Single Click!");
// change state of desk lamp
if( state == CONSTANTCOLOR ) {
state = LIGHTSOFF;
} else if( state == LIGHTSOFF ) {
state = CONSTANTCOLOR;
// come up with a bit of light if dimmed very low
if ( g_brightness <= 0.01 ) {
g_brightness = 0.02;
}
}
});
button.attachDoubleClick([](){
Log("Double Click!");
// double click twice within 2 seconds toggles WiFi
if( millis()-LastDoubleClick <= 2000 ) {
Log("doubleclicked knob-button twice, this toggles WiFi on/off");
configuration.disable_WiFi = !configuration.disable_WiFi;
writeConfig();
delay(200);
Log("restart now");
ESP.restart();
}
LastDoubleClick = millis();
});
resetSwitch.attachLongPressStart([](){
Log("Reset button was pressed long, resetting configuration");
state = RESET_CONFIGURATION;
});
resetSwitch.attachDoubleClick([](){
Log("Reset button was pressed twice, this toggles WiFi on/off");
configuration.disable_WiFi = !configuration.disable_WiFi;
writeConfig();
delay(200);
Log("restart now");
ESP.restart();
});
}
/******************************************************************************
Description.: read the rotary knob, change values if required
Input Value.: -
Return Value: -
******************************************************************************/
void loop_knob() {
long knob_delta = knob.read();
knob.write(0);
button.tick();
resetSwitch.tick();
if(knob_delta == 0)
return;
Log("Knob changed by: " + String(knob_delta) + ".");
switch(state) {
case CONSTANTCOLOR:
// if Button is held down change color otherwise change brightness
if( !digitalRead(ENCODER_SWITCH_PIN) ) {
g_ratio = constrain(g_ratio + 0.01*knob_delta, 0.0, 1.0);
Log("ratio changed to: " + String(g_ratio) + ".");
} else {
g_brightness = constrain(g_brightness + 0.01*knob_delta, 0.0, 1.0);
Log("brightness changed to: " + String(g_brightness) + ".");
}
break;
case LIGHTSOFF:
// knob turning ignored when light is off
break;
default:
;
}
}