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
|
|
|
*
|
|
|
|
* This file is part of Asbelos DCC API
|
|
|
|
*
|
|
|
|
* 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
|
|
|
#ifndef Sensor_h
|
|
|
|
#define Sensor_h
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
2021-08-03 23:12:25 +02:00
|
|
|
#include "IODevice.h"
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
// Uncomment the following #define statement to use callback notification
|
|
|
|
// where the driver supports it.
|
|
|
|
// The principle of callback notification is to avoid the Sensor class
|
|
|
|
// having to poll the device driver cyclically for input values, and then scan
|
|
|
|
// for changes. Instead, when the driver scans the inputs, if it detects
|
|
|
|
// a change it invokes a callback function in the Sensor class. In the current
|
|
|
|
// implementation, the advantages are limited because (a) the Sensor class
|
|
|
|
// performs debounce checks, and (b) the Sensor class does not have a
|
|
|
|
// static reference to the output stream for sending <Q>/<q> messages
|
|
|
|
// when a change is detected. These restrictions mean that the checkAll()
|
|
|
|
// method still has to iterate through all of the Sensor objects looking
|
|
|
|
// for changes.
|
|
|
|
#define USE_NOTIFY
|
2020-06-03 15:26:49 +02:00
|
|
|
|
|
|
|
struct SensorData {
|
|
|
|
int snum;
|
2021-08-03 23:12:25 +02:00
|
|
|
VPIN pin;
|
2020-06-03 15:26:49 +02:00
|
|
|
uint8_t pullUp;
|
|
|
|
};
|
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
class Sensor{
|
|
|
|
// The sensor list is a linked list where each sensor's 'nextSensor' field points to the next.
|
|
|
|
// The pointer is null in the last on the list.
|
|
|
|
|
|
|
|
public:
|
|
|
|
SensorData data;
|
|
|
|
struct {
|
|
|
|
uint8_t active:1;
|
|
|
|
uint8_t inputState:1;
|
|
|
|
uint8_t latchDelay:6;
|
|
|
|
}; // bit 7=active; bit 6=input state; bits 5-0=latchDelay
|
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
static Sensor *firstSensor;
|
2021-08-03 23:12:25 +02:00
|
|
|
#ifdef USE_NOTIFY
|
|
|
|
static Sensor *firstPollSensor;
|
|
|
|
static Sensor *lastSensor;
|
|
|
|
#endif
|
|
|
|
// readingSensor points to the next sensor to be polled, or null if the poll cycle is completed for
|
|
|
|
// the period.
|
2020-10-26 00:56:25 +01:00
|
|
|
static Sensor *readingSensor;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
// Constructor
|
|
|
|
Sensor();
|
2020-06-03 15:26:49 +02:00
|
|
|
Sensor *nextSensor;
|
2021-09-23 11:54:27 +02:00
|
|
|
|
2021-08-03 23:12:25 +02:00
|
|
|
void setState(int state);
|
2021-11-08 02:07:21 +01:00
|
|
|
#ifndef DISABLE_EEPROM
|
2020-06-03 15:26:49 +02:00
|
|
|
static void load();
|
|
|
|
static void store();
|
2021-11-08 02:07:21 +01:00
|
|
|
#endif
|
2021-08-03 23:12:25 +02:00
|
|
|
static Sensor *create(int id, VPIN vpin, int pullUp);
|
2024-02-07 23:11:27 +01:00
|
|
|
static void createMultiple(VPIN firstPin, byte count=1);
|
2021-08-03 23:12:25 +02:00
|
|
|
static Sensor* get(int id);
|
|
|
|
static bool remove(int id);
|
2021-12-05 13:08:59 +01:00
|
|
|
static void checkAll();
|
2021-08-03 23:12:25 +02:00
|
|
|
static void printAll(Print *stream);
|
|
|
|
static unsigned long lastReadCycle; // value of micros at start of last read cycle
|
|
|
|
static const unsigned int cycleInterval = 10000; // min time between consecutive reads of each sensor in microsecs.
|
|
|
|
// should not be less than device scan cycle time.
|
|
|
|
static const unsigned int minReadCount = 1; // number of additional scans before acting on change
|
|
|
|
// E.g. 1 means that a change is ignored for one scan and actioned on the next.
|
|
|
|
// Max value is 63
|
2021-09-23 11:54:27 +02:00
|
|
|
bool pollingRequired = true;
|
2021-08-03 23:12:25 +02:00
|
|
|
|
|
|
|
#ifdef USE_NOTIFY
|
|
|
|
static void inputChangeCallback(VPIN vpin, int state);
|
|
|
|
static bool inputChangeCallbackRegistered;
|
|
|
|
#endif
|
|
|
|
|
2020-06-03 15:26:49 +02:00
|
|
|
}; // Sensor
|
|
|
|
|
|
|
|
#endif
|