| LightHub v4.0.0 Smarthome controller firmware

inputs.h
Go to the documentation of this file.
1 /* Copyright © 2017-2018 Andrey Klimov. All rights reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 Homepage: http://lazyhome.ru
16 GIT: https://github.com/anklimov/lighthub
17 e-mail anklimov@gmail.com
18
19 */
20 #pragma once
21 #include <aJSON.h>
22 #include "modules/in_ccs811_hdc1080.h"
23 #include "itemCmd.h"
24
25 #define IN_ACTIVE_HIGH 2 // High level = PUSHED/ CLOSED/ ON othervise :Low Level. Use INPUT mode instead of INPUT_PULLUP for digital pin
26 #define IN_ANALOG 64 // Analog input
27 #define IN_RE 32 // Rotary Encoder (for further use)
28 #define IN_I2C 128 // MCP23017
29
30 #define IN_PUSH_ON 0 // PUSH - ON, Release - OFF (ovverrided by pcmd/rcmd) - DEFAULT
31 #define IN_PUSH_TOGGLE 1 // Used for push buttons. Every physicall push toggle logical switch on/off. Toggle on leading edge
32
33 #define IN_DHT22 4
34 #define IN_CCS811 5
35 #define IN_HDC1080 6
36
37 #define IN_COUNTER 8
38 #define IN_UPTIME 16
39
40 #define IS_IDLE 0
41 #define IS_PRESSED 1
42 #define IS_RELEASED 2
43 #define IS_LONG 3
44 #define IS_REPEAT 4
45 #define IS_WAITPRESS 5
46 #define IS_PRESSED2 6
47 #define IS_RELEASED2 7
48 #define IS_LONG2 8u
49 #define IS_REPEAT2 9u
50 #define IS_PRESSED3 10u
51 #define IS_LONG3 11u
52 #define IS_REPEAT3 12u
53 #define IS_WAITRELEASE 13u
54 #define IS_REQSTATE 0xFF
55
56
57
58 #define SAME_STATE_ATTEMPTS 2
59 #define ANALOG_STATE_ATTEMPTS 6
60 #define ANALOG_NOIZE 1
61
62 #define CHECK_SENSOR 1
63 #define CHECK_INPUT 2
64 #define CHECK_INTERRUPT 3
65
66
67 #define T_LONG 1000
68 #define T_IDLE 600
69 #define T_RPT 300
70 #define T_RPT_PULSE 150
71
72
73
74 // in syntaxis
75 // "pin": { "T":"N", "emit":"out_emit", item:"out_item", "scmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd":"repeat_command" }
76
77 //
78 //Switch/Restore all
79 //"pin": { "T":"1", "emit":"/all", item:"local_all", "scmd": "OFF", "rcmd": "RESTORE"}
80
81 //
82 //Normal (not button) Switch (toggled mode)
83 //"pin": { "T":"0", "emit":"/light1", item:"light1", "scmd": "TOGGLE", "rcmd": "TOGGLE"}
84 // or
85 // "pin": { "T":"xx", "emit":"/light1", item:"light1"}
86
87 //Use Button
88 //"pin": { "T":"1", "emit":"/light1", item:"light1", "scmd": "ON", "rcmd": "OFF"}
89 // or
90 // "pin": { "T":"1", "emit":"/light1", item:"light1"}
91 //or
92 // "pin": { "emit":"/light1", item:"light1"}
93
94 //1-Button dimmer
95 //"pin": { "T":"1", "emit":"/light1", item:"light1", "scmd": "ON", srcmd:"INCREASE",rrcmd:"DECREASE", "rcmd": "OFF"}
96 // or
97 // "pin": { "T":"xx", "emit":"/light1", item:"light1"}
98
99 //2-Buttons dimmer
100 //"pin1": { "T":"0", "emit":"/light1", item:"light1", "scmd": "ON", repcmd:"INCREASE"}
101 //"pin2": { "T":"0", "emit":"/light1", item:"light1", "scmd": "OFF", repcmd:"INCREASE"}
102
103
104 extern aJsonObject *inputs;
105
106
107 typedef union {
108 long int aslong;
109 uint32_t timestamp;
110 // Analog input structure
111 struct {
112 uint8_t reserved;
113 uint8_t logicState;
114 int16_t currentValue;
115 };
116 // Digital input structure
117 struct {
118 uint8_t toggle1:1;
119 uint8_t toggle2:1;
120 uint8_t toggle3:1;
121 uint8_t lastValue:1;
122 uint8_t delayedState:1;
123 uint8_t bounce:3;
124 uint8_t state:4;
125 uint8_t reqState:4;
126 uint16_t timestamp16;
127
128 };
129
130 } inStore;
131
132 class Input {
133 public:
134 aJsonObject *inputObj;
135 uint8_t inType;
136 uint8_t pin;
137 inStore *store;
138
139 Input(aJsonObject *obj, aJsonObject * configObj = NULL);
140 Input(char *name);
141
142 boolean isValid();
143
144 void onContactChanged(int newValue);
145 void onAnalogChanged(itemCmd newValue);
146
147 int Poll(short cause);
148 void setup();
149
150 static void inline onCounterChanged(int i);
151 static void onCounterChanged0();
152 static void onCounterChanged1();
153 static void onCounterChanged2();
154 static void onCounterChanged3();
155 static void onCounterChanged4();
156 static void onCounterChanged5();
157
158
159
160 protected:
161 void Parse(aJsonObject * configObj = NULL);
162
163 void contactPoll(short cause);
164 void analogPoll(short cause);
165
166 void dht22Poll();
167
168
169 void counterPoll();
170
171 void attachInterruptPinIrq(int realPin, int irq);
172
173 unsigned long nextPollTime() const;
174 void setNextPollTime(unsigned long pollTime);
175
176
177 void uptimePoll();
178
179 bool publishDataToDomoticz(int , aJsonObject *, const char *format, ...);
180
181 char* getIdxField();
182 bool changeState(uint8_t newState, short cause);
183 //bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
184 };
185
186
187
188 class readCache {
189 public:
190 readCache();
191 uint16_t analogReadCached (uint8_t pin);
192 uint8_t digitalReadCached(uint8_t pin);
193 #ifdef MCP23017
194 uint8_t I2CReadBit(uint8_t type, uint8_t addr, uint8_t pin);
195 #endif
196 void invalidateInputCache();
197 protected:
198 uint8_t addr;
199 uint8_t type;
200 uint16_t cached_data;
201 };
202
203 extern readCache inCache;
Input::Poll
int Poll(short cause)
Definition: inputs.cpp:224
itemCmd.h
itemCmd
Definition: itemCmd.h:153
Input::onCounterChanged5
static void onCounterChanged5()
Definition: inputs.cpp:424
inStore::toggle1
uint8_t toggle1
Definition: inputs.h:118
Input::dht22Poll
void dht22Poll()
Definition: inputs.cpp:460
readCache::cached_data
uint16_t cached_data
Definition: inputs.h:200
inStore::timestamp
uint32_t timestamp
Definition: inputs.h:109
Input::onCounterChanged
static void onCounterChanged(int i)
Definition: inputs.cpp:399
Input::publishDataToDomoticz
bool publishDataToDomoticz(int, aJsonObject *, const char *format,...)
Definition: inputs.cpp:1037
inStore::bounce
uint8_t bounce
Definition: inputs.h:123
inStore::reqState
uint8_t reqState
Definition: inputs.h:125
Input::counterPoll
void counterPoll()
Definition: inputs.cpp:291
readCache::readCache
readCache()
Definition: inputs.cpp:1069
Input::analogPoll
void analogPoll(short cause)
Definition: inputs.cpp:867
Input::contactPoll
void contactPoll(short cause)
Definition: inputs.cpp:656
readCache::type
uint8_t type
Definition: inputs.h:199
Input::changeState
bool changeState(uint8_t newState, short cause)
Definition: inputs.cpp:520
Input::store
inStore * store
Definition: inputs.h:137
inStore::toggle3
uint8_t toggle3
Definition: inputs.h:120
Input::onCounterChanged0
static void onCounterChanged0()
Definition: inputs.cpp:409
readCache::addr
uint8_t addr
Definition: inputs.h:198
inputs
aJsonObject * inputs
Definition: main.cpp:93
Input::Parse
void Parse(aJsonObject *configObj=NULL)
Definition: inputs.cpp:96
Input::inType
uint8_t inType
Definition: inputs.h:135
Input::getIdxField
char * getIdxField()
Definition: inputs.cpp:1060
Input::setNextPollTime
void setNextPollTime(unsigned long pollTime)
Definition: inputs.cpp:444
Input::nextPollTime
unsigned long nextPollTime() const
Definition: inputs.cpp:431
Input::setup
void setup()
Definition: inputs.cpp:154
inStore::state
uint8_t state
Definition: inputs.h:124
Input::Input
Input(aJsonObject *obj, aJsonObject *configObj=NULL)
Definition: inputs.cpp:83
Input::isValid
boolean isValid()
Definition: inputs.cpp:90
Input::onContactChanged
void onContactChanged(int newValue)
Definition: inputs.cpp:947
Input
Definition: inputs.h:132
inStore::timestamp16
uint16_t timestamp16
Definition: inputs.h:126
inStore
Definition: inputs.h:107
inStore::currentValue
int16_t currentValue
Definition: inputs.h:114
Input::onCounterChanged3
static void onCounterChanged3()
Definition: inputs.cpp:418
inStore::reserved
uint8_t reserved
Definition: inputs.h:112
readCache
Definition: inputs.h:188
in_ccs811_hdc1080.h
Input::onAnalogChanged
void onAnalogChanged(itemCmd newValue)
Definition: inputs.cpp:1000
inStore::logicState
uint8_t logicState
Definition: inputs.h:113
Input::uptimePoll
void uptimePoll()
Definition: inputs.cpp:377
inStore::delayedState
uint8_t delayedState
Definition: inputs.h:122
readCache::analogReadCached
uint16_t analogReadCached(uint8_t pin)
Definition: inputs.cpp:1075
Input::pin
uint8_t pin
Definition: inputs.h:136
Input::attachInterruptPinIrq
void attachInterruptPinIrq(int realPin, int irq)
Definition: inputs.cpp:339
Input::onCounterChanged1
static void onCounterChanged1()
Definition: inputs.cpp:412
Input::onCounterChanged2
static void onCounterChanged2()
Definition: inputs.cpp:415
inCache
readCache inCache
Definition: inputs.cpp:71
Input::inputObj
aJsonObject * inputObj
Definition: inputs.h:134
inStore::lastValue
uint8_t lastValue
Definition: inputs.h:121
inStore::toggle2
uint8_t toggle2
Definition: inputs.h:119
inStore::aslong
long int aslong
Definition: inputs.h:108
readCache::digitalReadCached
uint8_t digitalReadCached(uint8_t pin)
Definition: inputs.cpp:1084
readCache::invalidateInputCache
void invalidateInputCache()
Definition: inputs.cpp:1103
Input::onCounterChanged4
static void onCounterChanged4()
Definition: inputs.cpp:421