IoAbstraction
Toggle main menu visibility
Loading...
Searching...
No Matches
src
DeviceEvents.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4
*/
5
6
#ifndef IOABSTRACTION_DEVICEEVENTS_H
7
#define IOABSTRACTION_DEVICEEVENTS_H
8
13
14
#include <TaskManagerIO.h>
15
#include <PlatformDetermination.h>
16
#include <
AnalogDeviceAbstraction.h
>
17
30
class
AnalogInEvent
:
public
BaseEvent {
31
public
:
35
enum
AnalogEventMode
{
37
ANALOGIN_EXCEEDS
,
39
ANALOGIN_BELOW
,
41
ANALOGIN_CHANGE
42
};
43
private
:
44
AnalogDevice
*analogDevice;
45
AnalogEventMode
mode;
46
uint32_t pollInterval;
47
bool
latched;
48
pinid_t analogPin;
49
protected
:
50
float
analogThreshold;
51
float
lastReading;
52
public
:
62
AnalogInEvent
(
AnalogDevice
& device, pinid_t inputPin,
float
threshold,
AnalogEventMode
mode_,
63
uint32_t pollInterval_) : BaseEvent() {
64
analogThreshold = threshold;
65
analogPin = inputPin;
66
lastReading = 0;
67
pollInterval = pollInterval_;
68
analogDevice = &device;
69
latched =
false
;
70
mode = mode_;
71
}
72
82
AnalogInEvent
(
AnalogDevice
*device, pinid_t inputPin,
float
threshold,
AnalogEventMode
mode_,
83
uint32_t pollInterval_) : BaseEvent() {
84
analogThreshold = threshold;
85
analogPin = inputPin;
86
lastReading = 0;
87
pollInterval = pollInterval_;
88
analogDevice = device;
89
latched =
false
;
90
mode = mode_;
91
}
92
97
void
setPollInterval
(uint32_t micros) {
98
pollInterval = micros;
99
}
100
107
uint32_t
timeOfNextCheck
()
override
{
108
lastReading = analogDevice->getCurrentFloat(analogPin);
109
auto
analogTrigger =
isConditionTrue
();
110
if
(analogTrigger && !latched) {
111
setTriggered(
true
);
112
latched =
true
;
113
}
114
else
if
(!analogTrigger && latched) {
115
latched =
false
;
116
}
117
return
pollInterval;
118
}
119
124
bool
isConditionTrue
() {
125
if
(mode ==
ANALOGIN_BELOW
) {
126
return
lastReading < analogThreshold;
127
}
128
else
if
(mode ==
ANALOGIN_EXCEEDS
) {
129
return
lastReading > analogThreshold;
130
}
131
else
{
132
auto
change = abs(analogThreshold - lastReading);
133
return
change > analogThreshold;
134
}
135
}
136
143
void
readingAvailable
() {
144
taskManager.triggerEvents();
145
}
146
};
147
148
#endif
//IOABSTRACTION_DEVICEEVENTS_H
AnalogDeviceAbstraction.h
Contains a series of helper classes for dealing with analog devices, these are compatible across a wi...
AnalogDevice
Definition
AnalogDeviceAbstraction.h:31
AnalogInEvent::isConditionTrue
bool isConditionTrue()
Definition
DeviceEvents.h:124
AnalogInEvent::AnalogInEvent
AnalogInEvent(AnalogDevice *device, pinid_t inputPin, float threshold, AnalogEventMode mode_, uint32_t pollInterval_)
Definition
DeviceEvents.h:82
AnalogInEvent::setPollInterval
void setPollInterval(uint32_t micros)
Definition
DeviceEvents.h:97
AnalogInEvent::AnalogInEvent
AnalogInEvent(AnalogDevice &device, pinid_t inputPin, float threshold, AnalogEventMode mode_, uint32_t pollInterval_)
Definition
DeviceEvents.h:62
AnalogInEvent::readingAvailable
void readingAvailable()
Definition
DeviceEvents.h:143
AnalogInEvent::timeOfNextCheck
uint32_t timeOfNextCheck() override
Definition
DeviceEvents.h:107
AnalogInEvent::AnalogEventMode
AnalogEventMode
Definition
DeviceEvents.h:35
AnalogInEvent::ANALOGIN_BELOW
@ ANALOGIN_BELOW
Definition
DeviceEvents.h:39
AnalogInEvent::ANALOGIN_EXCEEDS
@ ANALOGIN_EXCEEDS
Definition
DeviceEvents.h:37
AnalogInEvent::ANALOGIN_CHANGE
@ ANALOGIN_CHANGE
Definition
DeviceEvents.h:41