LightHub 4.6.0
Open-source smart home controller firmware
Loading...
Searching...
No Matches
inputs.h
Go to the documentation of this file.
1/* Copyright © 2017-2025 Andrey Klimov. All rights reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14
15Homepage: http://lazyhome.ru
16GIT: https://github.com/anklimov/lighthub
17e-mail anklimov@gmail.com
18
19*/
31#pragma once
32#include <aJSON.h>
34#include "itemCmd.h"
35#ifdef ROTARYENCODER
36#include "RotaryEncoder.h"
37#endif
38
56#define IN_ACTIVE_HIGH 2 // High level = PUSHED/ CLOSED/ ON othervise :Low Level. Use INPUT mode instead of INPUT_PULLUP for digital pin
57#define IN_ANALOG 64 // Analog input
58#define IN_RE 32 // Rotary Encoder (for further use)
59#define IN_I2C 128 // MCP23017
60
61#define IN_PUSH_ON 0 // PUSH - ON, Release - OFF (ovverrided by pcmd/rcmd) - DEFAULT
62#define IN_PUSH_TOGGLE 1 // Used for push buttons. Every physicall push toggle logical switch on/off. Toggle on leading edge
63
64#define IN_DHT22 4 // DHT22 temperature/humidity sensor
65#define IN_CCS811 5 // CCS811 eCO2 sensor
66#define IN_HDC1080 6 // HDC1080 temperature/humidity sensor
67#define IN_ULTRASONIC 7 // Ultrasonic distance sensor
68
69#define IN_COUNTER 8 // Hardware counter (IRQ) input
70#define IN_UPTIME 16 // Publishes system uptime
71
83#define IS_IDLE 0
84#define IS_PRESSED 1
85#define IS_RELEASED 2
86#define IS_LONG 3
87#define IS_REPEAT 4
88#define IS_WAITPRESS 5
89#define IS_PRESSED2 6
90#define IS_RELEASED2 7
91#define IS_LONG2 8u
92#define IS_REPEAT2 9u
93#define IS_PRESSED3 10u
94#define IS_LONG3 11u
95#define IS_REPEAT3 12u
96#define IS_WAITRELEASE 13u
97#define IS_REQSTATE 0xFF
98#define IS_NOP 0xF
99
100
101
103#define SAME_STATE_ATTEMPTS 2
104#define ANALOG_STATE_ATTEMPTS 6
106#define ANALOG_NOIZE 1
107
115#define CHECK_SENSOR 1
116#define CHECK_INPUT 2
117#define CHECK_INTERRUPT 3
118#define CHECK_ULTRASONIC 4
119
120
122#define T_LONG 1000
123#define T_IDLE 600
124#define T_RPT 300
125#define T_RPT_PULSE 150
126#define T_REPEAT 30000
127
128
129
130// in syntaxis
131// "pin": { "T":"N", "emit":"out_emit", item:"out_item", "scmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd":"repeat_command" }
132
133//
134//Switch/Restore all
135//"pin": { "T":"1", "emit":"/all", item:"local_all", "scmd": "OFF", "rcmd": "RESTORE"}
136
137//
138//Normal (not button) Switch (toggled mode)
139//"pin": { "T":"0", "emit":"/light1", item:"light1", "scmd": "TOGGLE", "rcmd": "TOGGLE"}
140// or
141// "pin": { "T":"xx", "emit":"/light1", item:"light1"}
142
143//Use Button
144//"pin": { "T":"1", "emit":"/light1", item:"light1", "scmd": "ON", "rcmd": "OFF"}
145// or
146// "pin": { "T":"1", "emit":"/light1", item:"light1"}
147//or
148// "pin": { "emit":"/light1", item:"light1"}
149
150//1-Button dimmer
151//"pin": { "T":"1", "emit":"/light1", item:"light1", "scmd": "ON", srcmd:"INCREASE",rrcmd:"DECREASE", "rcmd": "OFF"}
152// or
153// "pin": { "T":"xx", "emit":"/light1", item:"light1"}
154
155//2-Buttons dimmer
156//"pin1": { "T":"0", "emit":"/light1", item:"light1", "scmd": "ON", repcmd:"INCREASE"}
157//"pin2": { "T":"0", "emit":"/light1", item:"light1", "scmd": "OFF", repcmd:"INCREASE"}
158
159
160extern aJsonObject *inputs;
161
162
170typedef union {
171 uint32_t aslong;
172 uint32_t timestamp;
173 // Analog input structure
174 struct {
175 uint8_t reserved;
176 uint8_t logicState;
177 int16_t currentValue;
178 };
179 // Digital input structure
180 struct {
181 uint8_t toggle1:1;
182 uint8_t toggle2:1;
183 uint8_t toggle3:1;
184 uint8_t lastValue:1;
185 uint8_t delayedState:1;
186 uint8_t bounce:3;
187 uint8_t state:4;
188 uint8_t reqState:4;
189 uint16_t timestamp16;
190
191 };
192
193} inStore;
194
204class Input {
205public:
207 aJsonObject *inputObj;
209 uint8_t inType;
211 uint8_t pin;
213 uint8_t pin2;
216
222 Input(aJsonObject *obj, aJsonObject * configObj = NULL);
224 Input(char *name);
225
227 boolean isValid();
228
230 void onContactChanged(int newValue);
232 void onAnalogChanged(itemCmd newValue);
233
238 int Poll(short cause);
240 void setup();
242 void stop();
243
245 static void inline onCounterChanged(int i);
247 static void onCounterChanged0();
248 static void onCounterChanged1();
249 static void onCounterChanged2();
250 static void onCounterChanged3();
251 static void onCounterChanged4();
252 static void onCounterChanged5();
253
254
255
256protected:
258 void Parse(aJsonObject * configObj = NULL);
259
261 #ifdef ROTARYENCODER
262 void contactPoll(short cause, RotaryEncoder * re = NULL);
263 #else
264 void contactPoll(short cause);
265 #endif
266
268 void analogPoll(short cause);
269
271 void dht22Poll();
272
273
275 void counterPoll();
276
278 void attachInterruptPinIrq(int realPin, int irq);
279
281 unsigned long nextPollTime() const;
283 void setNextPollTime(unsigned long pollTime);
284
285
287 void uptimePoll();
288
290 bool publishDataToDomoticz(int , aJsonObject *, const char *format, ...);
291
293 char* getIdxField();
298 bool changeState(uint8_t newState, short cause, aJsonObject * currentInputObject, bool contactState, bool calledOnTimer = false);
300 void setupRotaryEncoder();
301
303 aJsonObject * getCurrentInput();
304
306 bool setCurrentInput(aJsonObject * obj);
308 bool setCurrentInput(int n);
310 bool setCurrentInput(char * name);
312 bool checkInstructions(aJsonObject * obj);
313
314 //bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
315};
316
317
318
324public:
325 readCache();
327 uint16_t analogReadCached (uint8_t pin, uint8_t trigPin=0, uint8_t _type = IN_ANALOG);
329 uint8_t digitalReadCached(uint8_t pin);
330 #ifdef MCP23017
332 uint8_t I2CReadBit(uint8_t type, uint8_t addr, uint8_t pin);
333 #endif
334
337protected:
339 uint8_t addr;
340 uint8_t type;
341 uint16_t cached_data;
342};
343
344extern readCache inCache;
uint8_t inType
Input type (IN_* flags).
Definition inputs.h:209
char * getIdxField()
Definition inputs.cpp:1311
static void onCounterChanged(int i)
Definition inputs.cpp:481
inStore * store
Runtime state, packed in the JSON object's valueint.
Definition inputs.h:215
void onContactChanged(int newValue)
Definition inputs.cpp:1171
boolean isValid()
Definition inputs.cpp:106
void counterPoll()
Definition inputs.cpp:373
int Poll(short cause)
Definition inputs.cpp:287
static void onCounterChanged1()
Definition inputs.cpp:494
bool checkInstructions(aJsonObject *obj)
Definition inputs.cpp:603
void uptimePoll()
Definition inputs.cpp:459
void contactPoll(short cause)
Definition inputs.cpp:849
aJsonObject * inputObj
Pointer to the input's JSON object in the global inputs tree.
Definition inputs.h:207
void stop()
Definition inputs.cpp:152
Input(aJsonObject *obj, aJsonObject *configObj=NULL)
Create an input from its JSON object.
Definition inputs.cpp:99
static void onCounterChanged5()
Definition inputs.cpp:506
static void onCounterChanged4()
Definition inputs.cpp:503
void dht22Poll()
Definition inputs.cpp:542
void onAnalogChanged(itemCmd newValue)
Definition inputs.cpp:1244
bool setCurrentInput(aJsonObject *obj)
Definition inputs.cpp:636
static void onCounterChanged0()
Definition inputs.cpp:491
void setupRotaryEncoder()
Definition inputs.cpp:195
static void onCounterChanged2()
Definition inputs.cpp:497
void setNextPollTime(unsigned long pollTime)
Definition inputs.cpp:526
uint8_t pin
First pin number.
Definition inputs.h:211
bool changeState(uint8_t newState, short cause, aJsonObject *currentInputObject, bool contactState, bool calledOnTimer=false)
Definition inputs.cpp:686
void Parse(aJsonObject *configObj=NULL)
Definition inputs.cpp:112
void attachInterruptPinIrq(int realPin, int irq)
Definition inputs.cpp:421
bool publishDataToDomoticz(int, aJsonObject *, const char *format,...)
Definition inputs.cpp:1288
void analogPoll(short cause)
Definition inputs.cpp:1091
unsigned long nextPollTime() const
Definition inputs.cpp:513
static void onCounterChanged3()
Definition inputs.cpp:500
uint8_t pin2
Second pin number (rotary encoder, ...).
Definition inputs.h:213
aJsonObject * getCurrentInput()
Definition inputs.cpp:628
void setup()
Definition inputs.cpp:215
Command + value container passed between items, drivers and MQTT.
Definition itemCmd.h:306
Cache of raw pin reads to avoid multiple GPIO/I2C accesses within one poll cycle.
Definition inputs.h:323
uint16_t cached_data
Definition inputs.h:341
uint8_t digitalReadCached(uint8_t pin)
Definition inputs.cpp:1346
uint8_t type
Definition inputs.h:340
void invalidateInputCache()
Definition inputs.cpp:1365
readCache()
Definition inputs.cpp:1320
uint8_t addr
Cached I2C address / type / value.
Definition inputs.h:339
uint16_t analogReadCached(uint8_t pin, uint8_t trigPin=0, uint8_t _type=IN_ANALOG)
Definition inputs.cpp:1327
readCache inCache
Definition inputs.cpp:87
aJsonObject * inputs
Definition main.cpp:121
#define IN_ANALOG
Definition inputs.h:57
Per-input runtime state, packed into the JSON object's valueint.
Definition inputs.h:170
uint32_t timestamp
Definition inputs.h:172
uint32_t aslong
Definition inputs.h:171