IoAbstraction
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 
14 #include "TaskManagerIO.h"
15 #include "PlatformDetermination.h"
17 
30 class AnalogInEvent : public BaseEvent {
31 public:
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 
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 
144  taskManager.triggerEvents();
145  }
146 };
147 
148 #endif //IOABSTRACTION_DEVICEEVENTS_H
Contains a series of helper classes for dealing with analog devices, these are compatible across a wi...
Definition: AnalogDeviceAbstraction.h:31
Definition: DeviceEvents.h:30
bool isConditionTrue()
Definition: DeviceEvents.h:124
AnalogInEvent(AnalogDevice *device, pinid_t inputPin, float threshold, AnalogEventMode mode_, uint32_t pollInterval_)
Definition: DeviceEvents.h:82
void setPollInterval(uint32_t micros)
Definition: DeviceEvents.h:97
AnalogInEvent(AnalogDevice &device, pinid_t inputPin, float threshold, AnalogEventMode mode_, uint32_t pollInterval_)
Definition: DeviceEvents.h:62
void readingAvailable()
Definition: DeviceEvents.h:143
uint32_t timeOfNextCheck() override
Definition: DeviceEvents.h:107
AnalogEventMode
Definition: DeviceEvents.h:35
@ ANALOGIN_BELOW
Definition: DeviceEvents.h:39
@ ANALOGIN_EXCEEDS
Definition: DeviceEvents.h:37
@ ANALOGIN_CHANGE
Definition: DeviceEvents.h:41