/
_P059_Encoder.ino
200 lines (166 loc) · 6.34 KB
/
_P059_Encoder.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "_Plugin_Helper.h"
#ifdef USES_P059
//#######################################################################################################
//#################################### Plugin 059: Rotary Encoder #######################################
//#######################################################################################################
// ESPEasy Plugin to process the quadrature encoder interface signals (e.g. rotary encoder)
// written by Jochen Krapf (jk@nerd2nerd.org)
// Connection:
// Use 1st and 2nd GPIO for encoders A and B signal.
// Optional use 3rd GPIO for encoders I signal to reset counter to 0 at first trigger.
// If counter runs in wrong direction, change A and B GPIOs in settings page
// Note: Up to 4 encoders can be used simultaneously
#define PLUGIN_059
#define PLUGIN_ID_059 59
#define PLUGIN_NAME_059 "Switch Input - Rotary Encoder"
#define PLUGIN_VALUENAME1_059 "Counter"
#include <QEIx4.h>
std::map<unsigned int, std::shared_ptr<QEIx4> > P_059_sensordefs;
boolean Plugin_059(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_059;
Device[deviceCount].Type = DEVICE_TYPE_TRIPLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_SWITCH;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].TimerOptional = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_059);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_059));
break;
}
case PLUGIN_GET_DEVICEGPIONAMES:
{
event->String1 = formatGpioName_input(F("A (CLK)"));
event->String2 = formatGpioName_input(F("B (DT)"));
event->String3 = formatGpioName_input_optional(F("I (Z)"));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
// default values
if (PCONFIG_LONG(0) == 0 && PCONFIG_LONG(1) == 0)
PCONFIG_LONG(1) = 100;
String options[3] = { F("1 pulse per cycle"), F("2 pulses per cycle"), F("4 pulses per cycle") };
int optionValues[3] = { 1, 2, 4 };
addFormSelector(F("Mode"), F("qei_mode"), 3, options, optionValues, PCONFIG(0));
addFormNumericBox(F("Limit min."), F("qei_limitmin"), PCONFIG_LONG(0));
addFormNumericBox(F("Limit max."), F("qei_limitmax"), PCONFIG_LONG(1));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
PCONFIG(0) = getFormItemInt(F("qei_mode"));
PCONFIG_LONG(0) = getFormItemInt(F("qei_limitmin"));
PCONFIG_LONG(1) = getFormItemInt(F("qei_limitmax"));
success = true;
break;
}
case PLUGIN_INIT:
{
portStatusStruct newStatus;
// create sensor instance and add to std::map
P_059_sensordefs.erase(event->TaskIndex);
P_059_sensordefs[event->TaskIndex] = std::shared_ptr<QEIx4>(new QEIx4);
P_059_sensordefs[event->TaskIndex]->begin(CONFIG_PIN1,CONFIG_PIN2,CONFIG_PIN3,PCONFIG(0));
P_059_sensordefs[event->TaskIndex]->setLimit(PCONFIG_LONG(0), PCONFIG_LONG(1));
P_059_sensordefs[event->TaskIndex]->setIndexTrigger(true);
ExtraTaskSettings.TaskDeviceValueDecimals[event->BaseVarIndex] = 0;
String log = F("QEI : GPIO: ");
for (byte i=0; i<3; i++)
{
int pin = PIN(i);
if (pin >= 0)
{
//pinMode(pin, (Settings.TaskDevicePin1PullUp[event->TaskIndex]) ? INPUT_PULLUP : INPUT);
const uint32_t key = createKey(PLUGIN_ID_059,pin);
// WARNING: operator [] creates an entry in the map if key does not exist
newStatus = globalMapPortStatus[key];
newStatus.task++; // add this GPIO/port as a task
newStatus.mode = PIN_MODE_INPUT;
newStatus.state = 0;
savePortStatus(key,newStatus);
//setPinState(PLUGIN_ID_059, pin, PIN_MODE_INPUT, 0);
}
log += pin;
log += ' ';
}
addLog(LOG_LEVEL_INFO, log);
success = true;
break;
}
case PLUGIN_EXIT:
{
P_059_sensordefs.erase(event->TaskIndex);
break;
}
case PLUGIN_TEN_PER_SECOND:
{
if (P_059_sensordefs.count(event->TaskIndex) != 0)
{
if (P_059_sensordefs[event->TaskIndex]->hasChanged())
{
long c = P_059_sensordefs[event->TaskIndex]->read();
UserVar[event->BaseVarIndex] = (float)c;
event->sensorType = Sensor_VType::SENSOR_TYPE_SWITCH;
String log = F("QEI : ");
log += c;
addLog(LOG_LEVEL_INFO, log);
sendData(event);
}
}
success = true;
break;
}
case PLUGIN_READ:
{
if (P_059_sensordefs.count(event->TaskIndex) != 0)
{
UserVar[event->BaseVarIndex] = (float)P_059_sensordefs[event->TaskIndex]->read();
}
success = true;
break;
}
case PLUGIN_WRITE:
{
if (P_059_sensordefs.count(event->TaskIndex) != 0)
{
String log = "";
String command = parseString(string, 1);
if (command == F("encwrite"))
{
if (event->Par1 >= 0)
{
log = String(F("QEI : ")) + string;
addLog(LOG_LEVEL_INFO, log);
P_059_sensordefs[event->TaskIndex]->write(event->Par1);
Scheduler.schedule_task_device_timer(event->TaskIndex, millis());
}
success = true; // Command is handled.
}
}
break;
}
}
return success;
}
#endif // USES_P059