-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSensor.cpp
280 lines (206 loc) · 8.55 KB
/
Sensor.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**********************************************************************
Sensor.cpp
COPYRIGHT (c) 2013-2015 Gregg E. Berman
Part of DCC++ BASE STATION for the Arduino
**********************************************************************/
/**********************************************************************
DCC++ BASE STATION supports Sensor inputs that can be connected to any Aruidno Pin
not in use by this program. Sensors can be of any type (infrared, magentic, mechanical...).
The only requirement is that when "activated" the Sensor must force the specified Arduino
Pin LOW (i.e. to ground), and when not activated, this Pin should remain HIGH (e.g. 5V),
or be allowed to float HIGH if use of the Arduino Pin's internal pull-up resistor is specified.
To ensure proper voltage levels, some part of the Sensor circuitry
MUST be tied back to the same ground as used by the Arduino.
The Sensor code below utilizes exponential smoothing to "de-bounce" spikes generated by
mechanical switches and transistochers. This avoids the need to create smoothing circuitry
for each sensor. You may need to change these parameters through trial and error for your specific sensors.
To have this sketch monitor one or more Arduino pins for sensor triggers, first define/edit/delete
sensor definitions using the following variation of the "S" command:
<S ID PIN PULLUP>: creates a new sensor ID, with specified PIN and PULLUP
if sensor ID already exists, it is updated with specificed PIN and PULLUP
returns: <O> if successful and <X> if unsuccessful (e.g. out of memory)
<S ID>: deletes definition of sensor ID
returns: <O> if successful and <X> if unsuccessful (e.g. ID does not exist)
<S>: lists all defined sensors
returns: <Q ID PIN PULLUP> for each defined sensor or <X> if no sensors defined
where
ID: the numeric ID (0-32767) of the sensor
PIN: the arduino pin number the sensor is connected to
PULLUP: 1=use internal pull-up resistor for PIN, 0=don't use internal pull-up resistor for PIN
Once all sensors have been properly defined, use the <E> command to store their definitions to EEPROM.
If you later make edits/additions/deletions to the sensor definitions, you must invoke the <E> command if you want those
new definitions updated in the EEPROM. You can also clear everything stored in the EEPROM by invoking the <e> command.
All sensors defined as per above are repeatedly and sequentially checked within the main loop of this sketch.
If a Sensor Pin is found to have transitioned from one state to another, one of the following serial messages are generated:
<Q ID> - for transition of Sensor ID from HIGH state to LOW state (i.e. the sensor is triggered)
<q ID> - for transition of Sensor ID from LOW state to HIGH state (i.e. the sensor is no longer triggered)
Depending on whether the physical sensor is acting as an "event-trigger" or a "detection-sensor," you may
decide to ignore the <q ID> return and only react to <Q ID> triggers.
**********************************************************************/
#include "DCCpp_Uno.h"
#include "Sensor.h"
#include "EEStore.h"
#include <EEPROM.h>
#include "Comm.h"
#include "S88.h"
///////////////////////////////////////////////////////////////////////////////
static S88 s88Interface;
void Sensor::check(){
Sensor *tt;
for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
tt->signal=tt->signal*(1.0-SENSOR_DECAY)+digitalRead(tt->data.pin)*SENSOR_DECAY;
if(!tt->active && tt->signal<0.5){
tt->active=true;
INTERFACE.print("<Q");
INTERFACE.print(tt->data.snum);
INTERFACE.print(">");
} else if(tt->active && tt->signal>0.9){
tt->active=false;
INTERFACE.print("<q");
INTERFACE.print(tt->data.snum);
INTERFACE.print(">");
}
} // loop over all sensors
//Check S88
s88Interface.cycle();
} // Sensor::check
///////////////////////////////////////////////////////////////////////////////
Sensor *Sensor::create(int snum, int pin, int pullUp, int v){
Sensor *tt;
if(snum<S88AdrMax && pin<S88AdrMax && snum>1 && pin>1)//address not higher than Basisaddress of S88 and not effecting TX/RX at 0/1
{
if(firstSensor==NULL){
firstSensor=(Sensor *)calloc(1,sizeof(Sensor));
tt=firstSensor;
} else if((tt=get(snum))==NULL){
tt=firstSensor;
while(tt->nextSensor!=NULL)
tt=tt->nextSensor;
tt->nextSensor=(Sensor *)calloc(1,sizeof(Sensor));
tt=tt->nextSensor;
}
if(tt==NULL){ // problem allocating memory
if(v==1)
INTERFACE.print("<X>");
return(tt);
}
tt->data.snum=snum;
tt->data.pin=pin;
tt->data.pullUp=(pullUp==0?LOW:HIGH);
tt->active=false;
tt->signal=1;
pinMode(pin,INPUT); // set mode to input
digitalWrite(pin,pullUp); // don't use Arduino's internal pull-up resistors for external infrared sensors --- each sensor must have its own 1K external pull-up resistor
if(v==1)
INTERFACE.print("<O>");
s88Interface.init(snum);//execute Init for S88
}
else//value lies above 100 so we won't save it but we have to return tt
{
if(firstSensor==NULL){
tt=firstSensor;
} else if((tt=get(snum))==NULL){
tt=firstSensor;
while(tt->nextSensor!=NULL)
tt=tt->nextSensor;
}
if(v==1)
INTERFACE.print("<X>");
}
return(tt);
}
///////////////////////////////////////////////////////////////////////////////
Sensor* Sensor::get(int n){
Sensor *tt;
for(tt=firstSensor;tt!=NULL && tt->data.snum!=n;tt=tt->nextSensor);
return(tt);
}
///////////////////////////////////////////////////////////////////////////////
void Sensor::remove(int n){
Sensor *tt,*pp;
for(tt=firstSensor;tt!=NULL && tt->data.snum!=n;pp=tt,tt=tt->nextSensor);
if(tt==NULL){
INTERFACE.print("<X>");
return;
}
if(tt==firstSensor)
firstSensor=tt->nextSensor;
else
pp->nextSensor=tt->nextSensor;
free(tt);
s88Interface.deactivate(n);
INTERFACE.print("<O>");
}
///////////////////////////////////////////////////////////////////////////////
void Sensor::show(){
Sensor *tt;
if(firstSensor==NULL){
INTERFACE.print("<X>");
return;
}
for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
INTERFACE.print("<Q");
INTERFACE.print(tt->data.snum);
INTERFACE.print(" ");
INTERFACE.print(tt->data.pin);
INTERFACE.print(" ");
INTERFACE.print(tt->data.pullUp);
INTERFACE.print(">");
}
}
///////////////////////////////////////////////////////////////////////////////
void Sensor::status(){
Sensor *tt;
if(firstSensor==NULL){
INTERFACE.print("<X>");
return;
}
for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
INTERFACE.print(tt->active?"<Q":"<q");
INTERFACE.print(tt->data.snum);
INTERFACE.print(">");
}
}
///////////////////////////////////////////////////////////////////////////////
void Sensor::parse(char *c){
int n,s,m;
Sensor *t;
switch(sscanf(c,"%d %d %d",&n,&s,&m)){
case 3: // argument is string with id number of sensor followed by a pin number and pullUp indicator (0=LOW/1=HIGH)
create(n,s,m,1);
break;
case 1: // argument is a string with id number only
remove(n);
break;
case -1: // no arguments
show();
break;
case 2: // invalid number of arguments
INTERFACE.print("<X>");
break;
}
}
///////////////////////////////////////////////////////////////////////////////
void Sensor::load(){
struct SensorData data;
Sensor *tt;
for(int i=0;i<EEStore::eeStore->data.nSensors;i++){
EEPROM.get(EEStore::pointer(),data);
tt=create(data.snum,data.pin,data.pullUp);
EEStore::advance(sizeof(tt->data));
}
}
///////////////////////////////////////////////////////////////////////////////
void Sensor::store(){
Sensor *tt;
tt=firstSensor;
EEStore::eeStore->data.nSensors=0;
while(tt!=NULL){
EEPROM.put(EEStore::pointer(),tt->data);
EEStore::advance(sizeof(tt->data));
tt=tt->nextSensor;
EEStore::eeStore->data.nSensors++;
}
}
///////////////////////////////////////////////////////////////////////////////
Sensor *Sensor::firstSensor=NULL;