LightHub
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>
23 
24 #define IN_ACTIVE_HIGH 2 // High level = PUSHED/ CLOSED/ ON othervise :Low Level. Use INPUT mode instead of INPUT_PULLUP for digital pin
25 #define IN_ANALOG 64 // Analog input
26 #define IN_RE 32 // Rotary Encoder (for further use)
27 #define IN_I2C 128 // MCP23017
28 
29 #define IN_PUSH_ON 0 // PUSH - ON, Release - OFF (ovverrided by pcmd/rcmd) - DEFAULT
30 #define IN_PUSH_TOGGLE 1 // Used for push buttons. Every physicall push toggle logical switch on/off. Toggle on leading edge
31 
32 #define IN_DHT22 4
33 #define IN_CCS811 5
34 #define IN_HDC1080 6
35 
36 #define IN_COUNTER 8
37 #define IN_UPTIME 16
38 
39 #define IS_IDLE 0
40 #define IS_PRESSED 1
41 #define IS_RELEASED 2
42 #define IS_LONG 3
43 #define IS_REPEAT 4
44 #define IS_WAITPRESS 5
45 #define IS_PRESSED2 6
46 #define IS_RELEASED2 7
47 #define IS_LONG2 8u
48 #define IS_REPEAT2 9u
49 #define IS_PRESSED3 10u
50 #define IS_LONG3 11u
51 #define IS_REPEAT3 12u
52 #define IS_WAITRELEASE 13u
53 #define IS_REQSTATE 0xFF
54 
55 
56 
57 #define SAME_STATE_ATTEMPTS 3
58 #define ANALOG_STATE_ATTEMPTS 6
59 #define ANALOG_NOIZE 1
60 
61 #define CHECK_SENSOR 1
62 #define CHECK_INPUT 2
63 #define CHECK_INTERRUPT 3
64 
65 
66 #define T_LONG 1000
67 #define T_IDLE 600
68 #define T_RPT 300
69 #define T_RPT_PULSE 150
70 
71 
72 
73 // in syntaxis
74 // "pin": { "T":"N", "emit":"out_emit", item:"out_item", "scmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd":"repeat_command" }
75 
76 //
77 //Switch/Restore all
78 //"pin": { "T":"1", "emit":"/all", item:"local_all", "scmd": "OFF", "rcmd": "RESTORE"}
79 
80 //
81 //Normal (not button) Switch (toggled mode)
82 //"pin": { "T":"0", "emit":"/light1", item:"light1", "scmd": "TOGGLE", "rcmd": "TOGGLE"}
83 // or
84 // "pin": { "T":"xx", "emit":"/light1", item:"light1"}
85 
86 //Use Button
87 //"pin": { "T":"1", "emit":"/light1", item:"light1", "scmd": "ON", "rcmd": "OFF"}
88 // or
89 // "pin": { "T":"1", "emit":"/light1", item:"light1"}
90 //or
91 // "pin": { "emit":"/light1", item:"light1"}
92 
93 //1-Button dimmer
94 //"pin": { "T":"1", "emit":"/light1", item:"light1", "scmd": "ON", srcmd:"INCREASE",rrcmd:"DECREASE", "rcmd": "OFF"}
95 // or
96 // "pin": { "T":"xx", "emit":"/light1", item:"light1"}
97 
98 //2-Buttons dimmer
99 //"pin1": { "T":"0", "emit":"/light1", item:"light1", "scmd": "ON", repcmd:"INCREASE"}
100 //"pin2": { "T":"0", "emit":"/light1", item:"light1", "scmd": "OFF", repcmd:"INCREASE"}
101 
102 
103 extern aJsonObject *inputs;
104 
105 
106 typedef union {
107  long int aslong;
108  uint32_t timestamp;
109  // Analog input structure
110  struct {
111  uint8_t reserved;
112  uint8_t logicState;
113  int16_t currentValue;
114  };
115  // Digital input structure
116  struct {
117  uint8_t toggle1:1;
118  uint8_t toggle2:1;
119  uint8_t toggle3:1;
120  uint8_t lastValue:1;
121  uint8_t delayedState:1;
122  uint8_t bounce:3;
123  uint8_t state:4;
124  uint8_t reqState:4;
125  uint16_t timestamp16;
126 
127  };
128 
129 } inStore;
130 
131 class Input {
132 public:
133  aJsonObject *inputObj;
134  uint8_t inType;
135  uint8_t pin;
137 
138  Input(aJsonObject *obj, aJsonObject * configObj = NULL);
139  Input(char *name);
140 
141  boolean isValid();
142 
143  void onContactChanged(int newValue);
144  void onAnalogChanged(float newValue);
145 
146  int Poll(short cause);
147  void setup();
148 
149  static void inline onCounterChanged(int i);
150  static void onCounterChanged0();
151  static void onCounterChanged1();
152  static void onCounterChanged2();
153  static void onCounterChanged3();
154  static void onCounterChanged4();
155  static void onCounterChanged5();
156 
157 
158 
159 protected:
160  void Parse(aJsonObject * configObj = NULL);
161 
162  void contactPoll(short cause);
163  void analogPoll(short cause);
164 
165  void dht22Poll();
166 
167 
168  void counterPoll();
169 
170  void attachInterruptPinIrq(int realPin, int irq);
171 
172  unsigned long nextPollTime() const;
173  void setNextPollTime(unsigned long pollTime);
174 
175 
176  void uptimePoll();
177 
178  bool publishDataToDomoticz(int , aJsonObject *, const char *format, ...);
179 
180  char* getIdxField();
181  bool changeState(uint8_t newState, short cause);
182  //bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
183 };
184 
185 
186 
187 class readCache {
188 public:
189  readCache();
190  uint16_t analogReadCached (uint8_t pin);
191  uint8_t digitalReadCached(uint8_t pin);
192  #ifdef MCP23017
193  uint8_t I2CReadBit(uint8_t type, uint8_t addr, uint8_t pin);
194  #endif
195  void invalidateInputCache();
196 protected:
197  uint8_t addr;
198  uint8_t type;
199  uint16_t cached_data;
200 };
201 
202 extern readCache inCache;
Input::Poll
int Poll(short cause)
Definition: inputs.cpp:224
Input::onCounterChanged5
static void onCounterChanged5()
Definition: inputs.cpp:422
inStore::toggle1
uint8_t toggle1
Definition: inputs.h:117
Input::dht22Poll
void dht22Poll()
Definition: inputs.cpp:458
readCache::cached_data
uint16_t cached_data
Definition: inputs.h:199
inStore::timestamp
uint32_t timestamp
Definition: inputs.h:108
Input::onCounterChanged
static void onCounterChanged(int i)
Definition: inputs.cpp:397
Input::publishDataToDomoticz
bool publishDataToDomoticz(int, aJsonObject *, const char *format,...)
Definition: inputs.cpp:1063
inStore::bounce
uint8_t bounce
Definition: inputs.h:122
Input::onAnalogChanged
void onAnalogChanged(float newValue)
Definition: inputs.cpp:1028
inStore::reqState
uint8_t reqState
Definition: inputs.h:124
Input::counterPoll
void counterPoll()
Definition: inputs.cpp:291
readCache::readCache
readCache()
Definition: inputs.cpp:1095
Input::analogPoll
void analogPoll(short cause)
Definition: inputs.cpp:897
Input::contactPoll
void contactPoll(short cause)
Definition: inputs.cpp:706
readCache::type
uint8_t type
Definition: inputs.h:198
Input::changeState
bool changeState(uint8_t newState, short cause)
Definition: inputs.cpp:591
Input::store
inStore * store
Definition: inputs.h:136
inStore::toggle3
uint8_t toggle3
Definition: inputs.h:119
Input::onCounterChanged0
static void onCounterChanged0()
Definition: inputs.cpp:407
readCache::addr
uint8_t addr
Definition: inputs.h:197
inputs
aJsonObject * inputs
Definition: main.cpp:141
Input::Parse
void Parse(aJsonObject *configObj=NULL)
Definition: inputs.cpp:96
Input::inType
uint8_t inType
Definition: inputs.h:134
Input::getIdxField
char * getIdxField()
Definition: inputs.cpp:1086
Input::setNextPollTime
void setNextPollTime(unsigned long pollTime)
Definition: inputs.cpp:442
Input::nextPollTime
unsigned long nextPollTime() const
Definition: inputs.cpp:429
Input::setup
void setup()
Definition: inputs.cpp:154
inStore::state
uint8_t state
Definition: inputs.h:123
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:976
Input
Definition: inputs.h:131
inStore::timestamp16
uint16_t timestamp16
Definition: inputs.h:125
inStore
Definition: inputs.h:106
inStore::currentValue
int16_t currentValue
Definition: inputs.h:113
Input::onCounterChanged3
static void onCounterChanged3()
Definition: inputs.cpp:416
inStore::reserved
uint8_t reserved
Definition: inputs.h:111
readCache
Definition: inputs.h:187
in_ccs811_hdc1080.h
inStore::logicState
uint8_t logicState
Definition: inputs.h:112
Input::uptimePoll
void uptimePoll()
Definition: inputs.cpp:376
inStore::delayedState
uint8_t delayedState
Definition: inputs.h:121
readCache::analogReadCached
uint16_t analogReadCached(uint8_t pin)
Definition: inputs.cpp:1101
Input::pin
uint8_t pin
Definition: inputs.h:135
Input::attachInterruptPinIrq
void attachInterruptPinIrq(int realPin, int irq)
Definition: inputs.cpp:338
Input::onCounterChanged1
static void onCounterChanged1()
Definition: inputs.cpp:410
Input::onCounterChanged2
static void onCounterChanged2()
Definition: inputs.cpp:413
inCache
readCache inCache
Definition: inputs.cpp:71
Input::inputObj
aJsonObject * inputObj
Definition: inputs.h:133
inStore::lastValue
uint8_t lastValue
Definition: inputs.h:120
inStore::toggle2
uint8_t toggle2
Definition: inputs.h:118
inStore::aslong
long int aslong
Definition: inputs.h:107
readCache::digitalReadCached
uint8_t digitalReadCached(uint8_t pin)
Definition: inputs.cpp:1110
readCache::invalidateInputCache
void invalidateInputCache()
Definition: inputs.cpp:1128
Input::onCounterChanged4
static void onCounterChanged4()
Definition: inputs.cpp:419