-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathSensor.h
154 lines (150 loc) · 7.4 KB
/
Sensor.h
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
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <[email protected]>
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/
#ifndef Sensor_h
#define Sensor_h
/******************************************
Sensor: provide functionalities common to all the sensors
*/
#include "Node.h"
#include "InternalTimer.h"
#include "Child.h"
#if NODEMANAGER_POWER_MANAGER == ON
#include "PowerManager.h"
#endif
class Sensor {
public:
Sensor();
Sensor(int8_t pin = -1);
// return the name of the sensor
const char* getName();
// [1] where the sensor is attached to (default: not set)
void setPin(int8_t value);
// [5] For some sensors, the measurement can be queried multiple times and an average is returned (default: 1)
void setSamples(unsigned int value);
// [6] If more then one sample has to be taken, set the interval in milliseconds between measurements (default: 0)
void setSamplesInterval(unsigned long value);
// [17] After how many seconds the sensor will report back its measure (default: 10 minutes)
void setReportIntervalSeconds(unsigned long value);
// [16] After how many minutes the sensor will report back its measure (default: 10 minutes)
void setReportIntervalMinutes(unsigned long value);
// [19] After how many hours the sensor will report back its measure (default: 10 minutes)
void setReportIntervalHours(unsigned int value);
// [20] After how many days the sensor will report back its measure (default: 10 minutes)
void setReportIntervalDays(uint8_t value);
// [24] Set the way the timer used for reporting to the gateway should operate. It can be either TIME_INTERVAL (e.g. report every X seconds with the amount of time set with setReportTimerValue()), IMMEDIATELY (e.g. report at every cycle, useful for sensors like actuators which should report as soon as the value has changed), DO_NOT_REPORT (e.g. never report, useful for when there is no need to report, like a Display) and when NODEMANAGER_TIME is ON, EVERY_MINUTE/EVERY_HOUR/EVERY_DAY (e.g. to report the value set in the previous timeframe, useful for sensors reporting an accumulated value linked to a timeframe at regular intervals), AT_MINUTE/AT_HOUR/AT_DAY (e.g. report at a given minute/hour/day, useful if the measure is expected at a specified time, set with setReportTimerValue())
void setReportTimerMode(nm_timer_mode value);
// [25] Set the value for the reporting timer's mode which has been set with setReportTimerMode()
void setReportTimerValue(unsigned long value);
// [26] Set the way the timer used for taking measures should operate. Takes the same parameters as setReportTimerMode(). If not set explicitly, will be set as the reporting timer
void setMeasureTimerMode(nm_timer_mode value);
// [27] Set the value for the reporting timer's mode which has been set with setReportTimerMode() If not set explicitely, will be set with the same value as the reporting timer
void setMeasureTimerValue(unsigned long value);
// return true if it is the first execution of loop on this sensor
bool getFirstRun();
// list of configured child
List<Child*> children;
// return the child object based on the provided child_id
Child* getChild(uint8_t child_id);
Child* getChild(uint8_t child_id, uint8_t ctype);
// register a child
void registerChild(Child* child);
// [28] enabler/disable the sensor (default: true)
void setEnabled(bool value, bool just_set = false);
bool getEnabled();
#if NODEMANAGER_INTERRUPTS == ON
// return the pin the interrupt is attached to
int8_t getInterruptPin();
// set initial value of the configured pin. Can be used for internal pull up
void setPinInitialValue(int8_t value);
// for interrupt-based sensor, set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
void setInterruptMode(uint8_t value);
// [22] for interrupt-based sensor, milliseconds to wait/sleep after the interrupt before reporting (default: 0)
void setWaitAfterInterrupt(unsigned long value);
// [23] for interrupt-based sensor, the value of the pin is checked and the interrupt ignored if RISING and not HIGH or FALLING and not LOW (default: true)
void setInterruptStrict(bool value);
#endif
#if NODEMANAGER_POWER_MANAGER == ON
// set a previously configured PowerManager to the sensor so to powering it up with custom pins
void setPowerManager(PowerManager& powerManager);
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int8_t ground_pin, int8_t vcc_pin, unsigned long wait_time = 50);
// [13] manually turn the power on
void powerOn();
// [14] manually turn the power off
void powerOff();
#endif
#if NODEMANAGER_HOOKING == ON
// set a custom hook function to be called when the sensor executes its setup() function
void setSetupHook(void (*function)(Sensor* sensor));
// set a custom hook function to be called just before the sensor executes its loop() function
void setPreLoopHook(void (*function)(Sensor* sensor));
// set a custom hook function to be called just after the sensor executes its loop() function
void setPostLoopHook(void (*function)(Sensor* sensor));
// set a custom hook function to be called when the sensor executes its interrupt() function
void setInterruptHook(void (*function)(Sensor* sensor));
// set a custom hook function to be called when the sensor executes its receive() function
void setReceiveHook(void (*function)(Sensor* sensor, MyMessage* message));
#endif
// define what to do at each stage of the sketch
void presentation();
void setup();
void loop(MyMessage* message);
#if NODEMANAGER_INTERRUPTS == ON
bool interrupt();
#endif
#if NODEMANAGER_RECEIVE == ON
void receive(MyMessage* message);
#endif
// abstract functions, subclasses may implement
virtual void onSetup();
virtual void onLoop(Child* child);
virtual void onReceive(MyMessage* message);
virtual void onInterrupt();
#if NODEMANAGER_OTA_CONFIGURATION == ON
virtual void onOTAConfiguration(ConfigurationRequest* request);
#endif
protected:
const char* _name = "";
int8_t _pin = -1;
unsigned int _samples = 1;
unsigned long _samples_interval = 0;
bool _first_run = true;
InternalTimer* _report_timer;
InternalTimer* _measure_timer;
bool _evaluateTimer(InternalTimer* timer);
bool _enabled = true;
#if NODEMANAGER_INTERRUPTS == ON
int8_t _interrupt_pin = -1;
uint8_t _interrupt_mode = MODE_NOT_DEFINED;
unsigned long _wait_after_interrupt = 0;
int8_t _initial_value = -1;
bool _interrupt_strict = true;
#endif
#if NODEMANAGER_POWER_MANAGER == ON
PowerManager* _powerManager = nullptr;
#endif
#if NODEMANAGER_HOOKING == ON
void (*_setup_hook)(Sensor* sensor);
void (*_pre_loop_hook)(Sensor* sensor);
void (*_post_loop_hook)(Sensor* sensor);
void (*_interrupt_hook)(Sensor* sensor);
void (*_receive_hook)(Sensor* sensor, MyMessage* message);
#endif
};
#endif