2020-07-03 18:35:02 +02:00
|
|
|
/*
|
2022-01-07 02:28:35 +01:00
|
|
|
* © 2021 Neil McKechnie
|
|
|
|
* © 2020-2021 Harald Barth
|
|
|
|
* © 2020-2021 Chris Harlow
|
|
|
|
* All rights reserved.
|
2020-07-03 18:35:02 +02:00
|
|
|
*
|
2022-01-07 02:28:35 +01:00
|
|
|
* This file is part of CommandStation-EX
|
2020-07-03 18:35:02 +02:00
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-06-03 15:26:49 +02:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
DCC++ BASE STATION supports Sensor inputs that can be connected to any Arduino 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.
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
The Sensor code below utilises "de-bounce" logic to remove spikes generated by
|
2020-06-03 15:26:49 +02:00
|
|
|
mechanical switches and transistors. This avoids the need to create smoothing circuitry
|
2021-08-03 23:12:25 +02:00
|
|
|
for each sensor. You may need to change the parameters through trial and error for your specific sensors.
|
2020-06-03 15:26:49 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2020-10-25 23:52:22 +01:00
|
|
|
#include "StringFormatter.h"
|
2021-12-05 13:08:59 +01:00
|
|
|
#include "CommandDistributor.h"
|
2020-06-03 15:26:49 +02:00
|
|
|
#include "Sensors.h"
|
2021-11-08 02:07:21 +01:00
|
|
|
#ifndef DISABLE_EEPROM
|
2020-06-03 15:26:49 +02:00
|
|
|
#include "EEStore.h"
|
2021-11-08 02:07:21 +01:00
|
|
|
#endif
|
2021-08-03 23:12:25 +02:00
|
|
|
#include "IODevice.h"
|
2020-08-15 12:32:32 +02:00
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2020-10-25 23:52:22 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2021-08-03 23:12:25 +02:00
|
|
|
// checks a number of defined sensors per entry and prints _changed_ sensor state
|
2020-10-25 23:52:22 +01:00
|
|
|
// to stream unless stream is NULL in which case only internal
|
2020-10-26 00:56:25 +01:00
|
|
|
// state is updated. Then advances to next sensor which will
|
2021-08-03 23:12:25 +02:00
|
|
|
// be checked at next invocation. Each cycle of reading all sensors will
|
|
|
|
// be initiated no more frequently than the time set by 'cycleInterval' microseconds.
|
2020-10-25 23:52:22 +01:00
|
|
|
//
|
2021-08-03 23:12:25 +02:00
|
|
|
// The list of sensors is divided such that the first part of the list
|
|
|
|
// contains sensors that support change notification via callback, and the second
|
|
|
|
// part of the list contains sensors that require cyclic polling. The start of the
|
|
|
|
// second part of the list is determined from by the 'firstPollSensor' pointer.
|
2020-06-03 15:26:49 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-12-05 13:08:59 +01:00
|
|
|
void Sensor::checkAll(){
|
2021-08-03 23:12:25 +02:00
|
|
|
uint16_t sensorCount = 0;
|
|
|
|
|
|
|
|
#ifdef USE_NOTIFY
|
|
|
|
// Register the event handler ONCE!
|
|
|
|
if (!inputChangeCallbackRegistered)
|
2021-08-18 00:41:34 +02:00
|
|
|
IONotifyCallback::add(inputChangeCallback);
|
2021-08-03 23:12:25 +02:00
|
|
|
inputChangeCallbackRegistered = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (firstSensor == NULL) return; // No sensors to be scanned
|
|
|
|
if (readingSensor == NULL) {
|
|
|
|
// Not currently scanning sensor list
|
|
|
|
unsigned long thisTime = micros();
|
|
|
|
if (thisTime - lastReadCycle >= cycleInterval) {
|
|
|
|
// Required time elapsed since last read cycle started,
|
|
|
|
// so initiate new scan through the sensor list
|
|
|
|
readingSensor = firstSensor;
|
|
|
|
lastReadCycle = thisTime;
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
// Loop until either end of list is encountered or we pause for some reason
|
|
|
|
bool pause = false;
|
|
|
|
while (readingSensor != NULL && !pause) {
|
|
|
|
|
|
|
|
// Where the sensor is attached to a pin, read pin status. For sources such as LCN,
|
|
|
|
// which don't have an input pin to read, the LCN class calls setState() to update inputState when
|
|
|
|
// a message is received. The IODevice::read() call returns 1 for active pins (0v) and 0 for inactive (5v).
|
|
|
|
// Also, on HAL drivers that support change notifications, the driver calls the notification callback
|
|
|
|
// routine when an input signal change is detected, and this updates the inputState directly,
|
|
|
|
// so these inputs don't need to be polled here.
|
|
|
|
VPIN pin = readingSensor->data.pin;
|
2021-09-23 11:54:27 +02:00
|
|
|
if (readingSensor->pollingRequired && pin != VPIN_NONE)
|
|
|
|
readingSensor->inputState = IODevice::read(pin);
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
// Check if changed since last time, and process changes.
|
|
|
|
if (readingSensor->inputState == readingSensor->active) {
|
|
|
|
// no change
|
|
|
|
readingSensor->latchDelay = minReadCount; // Reset counter
|
|
|
|
} else if (readingSensor->latchDelay > 0) {
|
|
|
|
// change detected, but first decrement delay
|
|
|
|
readingSensor->latchDelay--;
|
|
|
|
} else {
|
|
|
|
// change validated, act on it.
|
|
|
|
readingSensor->active = readingSensor->inputState;
|
|
|
|
readingSensor->latchDelay = minReadCount; // Reset counter
|
|
|
|
|
2021-12-05 13:08:59 +01:00
|
|
|
CommandDistributor::broadcastSensor(readingSensor->data.snum,readingSensor->active);
|
|
|
|
pause = true; // Don't check any more sensors on this entry
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
2020-10-26 09:10:45 +01:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
// Move to next sensor in list.
|
|
|
|
readingSensor = readingSensor->nextSensor;
|
|
|
|
|
2021-09-23 11:54:27 +02:00
|
|
|
// Currently process max of 16 sensors per entry.
|
|
|
|
// Performance measurements taken during development indicate that, with 128 sensors configured
|
|
|
|
// on 8x 16-pin MCP23017 GPIO expanders with polling (no change notification), all inputs can be read from the devices
|
|
|
|
// within 1.4ms (400Mhz I2C bus speed), and a full cycle of checking 128 sensors for changes takes under a millisecond.
|
2021-08-03 23:12:25 +02:00
|
|
|
sensorCount++;
|
2021-09-23 11:54:27 +02:00
|
|
|
if (sensorCount >= 16) pause = true;
|
2020-10-26 00:56:25 +01:00
|
|
|
}
|
2020-10-26 09:10:45 +01:00
|
|
|
|
2020-09-06 13:44:19 +02:00
|
|
|
} // Sensor::checkAll
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
#ifdef USE_NOTIFY
|
|
|
|
// Callback from HAL (IODevice class) when a digital input change is recognised.
|
|
|
|
// Updates the inputState field, which is subsequently scanned for changes in the checkAll
|
|
|
|
// method. Ideally the <Q>/<q> message should be sent from here, instead of waiting for
|
|
|
|
// the checkAll method, but the output stream is not available at this point.
|
|
|
|
void Sensor::inputChangeCallback(VPIN vpin, int state) {
|
|
|
|
Sensor *tt;
|
|
|
|
// This bit is not ideal since it has, potentially, to look through the entire list of
|
|
|
|
// sensors to find the one that has changed. Ideally this should be improved somehow.
|
|
|
|
for (tt=firstSensor; tt!=NULL ; tt=tt->nextSensor) {
|
|
|
|
if (tt->data.pin == vpin) break;
|
|
|
|
}
|
|
|
|
if (tt != NULL) { // Sensor found
|
|
|
|
tt->inputState = (state != 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-25 23:52:22 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// prints all sensor states to stream
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void Sensor::printAll(Print *stream){
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
if (stream != NULL) {
|
|
|
|
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
|
2021-03-30 23:01:37 +02:00
|
|
|
StringFormatter::send(stream, F("<%c %d>\n"), tt->active ? 'Q' : 'q', tt->data.snum);
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
2020-10-25 23:52:22 +01:00
|
|
|
} // loop over all sensors
|
|
|
|
} // Sensor::printAll
|
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2021-08-03 23:12:25 +02:00
|
|
|
// Static Function to create/find Sensor object.
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
Sensor *Sensor::create(int snum, VPIN pin, int pullUp){
|
2020-06-03 15:26:49 +02:00
|
|
|
Sensor *tt;
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
if (pin > VPIN_MAX && pin != VPIN_NONE) return NULL;
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
remove(snum); // Unlink and free any existing sensor with the same id, before creating the new one.
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
tt = (Sensor *)calloc(1,sizeof(Sensor));
|
|
|
|
if (!tt) return tt; // memory allocation failure
|
|
|
|
|
2021-09-23 11:54:27 +02:00
|
|
|
if (pin == VPIN_NONE)
|
|
|
|
tt->pollingRequired = false;
|
|
|
|
#ifdef USE_NOTIFY
|
|
|
|
else if (IODevice::hasCallback(pin))
|
|
|
|
tt->pollingRequired = false;
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
tt->pollingRequired = true;
|
|
|
|
|
|
|
|
// Add to the start of the list
|
2021-08-03 23:12:25 +02:00
|
|
|
tt->nextSensor = firstSensor;
|
|
|
|
firstSensor = tt;
|
|
|
|
|
|
|
|
tt->data.snum = snum;
|
|
|
|
tt->data.pin = pin;
|
|
|
|
tt->data.pullUp = pullUp;
|
|
|
|
tt->active = 0;
|
|
|
|
tt->inputState = 0;
|
|
|
|
tt->latchDelay = minReadCount;
|
|
|
|
|
|
|
|
if (pin != VPIN_NONE)
|
2021-09-23 11:54:27 +02:00
|
|
|
IODevice::configureInput(pin, pullUp);
|
2021-08-03 23:12:25 +02:00
|
|
|
// Generally, internal pull-up resistors are not, on their own, sufficient
|
|
|
|
// for external infrared sensors --- each sensor must have its own 1K external pull-up resistor
|
2020-06-03 15:26:49 +02:00
|
|
|
|
|
|
|
return tt;
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Object method to directly change the input state, for sensors such as LCN which are updated
|
|
|
|
// by means other than by polling an input.
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
void Sensor::setState(int value) {
|
|
|
|
// Trigger sensor change to be reported on next checkAll loop.
|
|
|
|
inputState = (value != 0);
|
|
|
|
latchDelay = 0; // Don't wait for anti-jitter logic
|
2020-06-03 15:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Sensor* Sensor::get(int n){
|
|
|
|
Sensor *tt;
|
|
|
|
for(tt=firstSensor;tt!=NULL && tt->data.snum!=n;tt=tt->nextSensor);
|
|
|
|
return tt ;
|
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool Sensor::remove(int n){
|
|
|
|
Sensor *tt,*pp=NULL;
|
|
|
|
|
|
|
|
for(tt=firstSensor;tt!=NULL && tt->data.snum!=n;pp=tt,tt=tt->nextSensor);
|
|
|
|
|
|
|
|
if (tt==NULL) return false;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
// Unlink the sensor from the list
|
|
|
|
if(tt==firstSensor)
|
2020-06-03 15:26:49 +02:00
|
|
|
firstSensor=tt->nextSensor;
|
2021-08-03 23:12:25 +02:00
|
|
|
else
|
2020-06-03 15:26:49 +02:00
|
|
|
pp->nextSensor=tt->nextSensor;
|
2021-08-03 23:12:25 +02:00
|
|
|
#ifdef USE_NOTIFY
|
|
|
|
if (tt==lastSensor)
|
|
|
|
lastSensor = pp;
|
|
|
|
if (tt==firstPollSensor)
|
|
|
|
firstPollSensor = tt->nextSensor;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Check if the sensor being deleted is the next one to be read. If so,
|
|
|
|
// make the following one the next one to be read.
|
2020-10-26 00:56:25 +01:00
|
|
|
if (readingSensor==tt) readingSensor=tt->nextSensor;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
free(tt);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2021-11-08 02:07:21 +01:00
|
|
|
#ifndef DISABLE_EEPROM
|
2020-06-03 15:26:49 +02:00
|
|
|
void Sensor::load(){
|
|
|
|
struct SensorData data;
|
|
|
|
Sensor *tt;
|
|
|
|
|
2021-08-23 12:58:48 +02:00
|
|
|
uint16_t i=EEStore::eeStore->data.nSensors;
|
|
|
|
while(i--){
|
2020-06-03 15:26:49 +02:00
|
|
|
EEPROM.get(EEStore::pointer(),data);
|
2021-08-03 23:12:25 +02:00
|
|
|
tt=create(data.snum, data.pin, data.pullUp);
|
2020-06-03 15:26:49 +02:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 02:07:21 +01:00
|
|
|
#endif
|
2020-06-03 15:26:49 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Sensor *Sensor::firstSensor=NULL;
|
2020-10-26 00:56:25 +01:00
|
|
|
Sensor *Sensor::readingSensor=NULL;
|
2021-08-03 23:12:25 +02:00
|
|
|
unsigned long Sensor::lastReadCycle=0;
|
|
|
|
|
|
|
|
#ifdef USE_NOTIFY
|
|
|
|
Sensor *Sensor::firstPollSensor = NULL;
|
|
|
|
Sensor *Sensor::lastSensor = NULL;
|
|
|
|
bool Sensor::inputChangeCallbackRegistered = false;
|
2021-11-08 02:07:21 +01:00
|
|
|
#endif
|