libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
PotMonitor.h
Go to the documentation of this file.
1#pragma once
2#include <stdint.h>
3#include "UiEventQueue.h"
4#include "../sys/system.h"
5
6namespace daisy
7{
36template <typename BackendType, uint32_t numPots>
38{
39 public:
41 : queue_(nullptr),
42 backend_(nullptr),
43 deadBand_(1.0 / (1 << 12)),
44 deadBandIdle_(1.0 / (1 << 10)),
45 timeout_(0)
46 {
47 }
48
62 float deadBandIdle = 1.0 / (1 << 10),
63 float deadBand = 1.0 / (1 << 12))
64 {
65 queue_ = &queueToAddEventsTo;
66 deadBand_ = deadBand;
67 backend_ = &backend;
68 deadBandIdle_ = deadBandIdle;
69 timeout_ = idleTimeoutMs;
70
71 for(uint32_t i = 0; i < numPots; i++)
72 {
73 lastValue_[i] = 0.0;
74 timeoutCounterMs_[i] = 0;
75 }
76
77 lastCallSysTime_ = System::GetNow();
78 }
79
83 void Process()
84 {
85 const auto now = System::GetNow();
86 const auto timeDiff = now - lastCallSysTime_;
87 lastCallSysTime_ = now;
88
89 for(uint32_t i = 0; i < numPots; i++)
90 ProcessPot(i, backend_->GetPotValue(i), timeDiff);
91 }
92
97 {
98 if(potId >= numPots)
99 return false;
100 else
101 return timeoutCounterMs_[potId] < timeout_;
102 }
103
109 {
110 if(potId >= numPots)
111 return -1.0f;
112 else
113 return lastValue_[potId];
114 }
115
117 BackendType& GetBackend() { return backend_; }
118
121
122 private:
130 void ProcessPot(uint16_t id, float value, uint32_t timeDiffMs)
131 {
132 // currently moving?
133 if(timeoutCounterMs_[id] < timeout_)
134 {
135 // check if pot has left the deadband. If so, add a new message
136 // to the queue.
137 float delta = lastValue_[id] - value;
138 if((delta > deadBand_) || (delta < -deadBand_))
139 {
140 lastValue_[id] = value;
141 queue_->AddPotMoved(id, value);
142 timeoutCounterMs_[id] = 0;
143 }
144 // no movement, increment timeout counter
145 else
146 {
147 timeoutCounterMs_[id] += timeDiffMs;
148 // post activity changed event after timeout expired.
149 if(timeoutCounterMs_[id] == timeout_)
150 {
151 queue_->AddPotActivityChanged(id, false);
152 }
153 }
154 }
155 // not moving right now
156 else
157 {
158 // check if pot has left the idle deadband. If so, add a new message
159 // to the queue and restart the timeout
160 float delta = lastValue_[id] - value;
161 if((delta > deadBandIdle_) || (delta < -deadBandIdle_))
162 {
163 lastValue_[id] = value;
164 queue_->AddPotActivityChanged(id, true);
165 queue_->AddPotMoved(id, value);
166 timeoutCounterMs_[id] = 0;
167 }
168 }
169 }
170
171 PotMonitor(const PotMonitor&) = delete;
172 PotMonitor& operator=(const PotMonitor&) = delete;
173
174 UiEventQueue* queue_;
175 BackendType* backend_;
176 float deadBand_;
177 float deadBandIdle_;
178 uint16_t timeout_;
179 float lastValue_[numPots];
180 uint16_t timeoutCounterMs_[numPots];
181 uint32_t lastCallSysTime_;
182};
183
184} // namespace daisy
Definition leddriver.h:33
A potentiometer monitor that generates events in a UiEventQueue.
Definition PotMonitor.h:38
bool IsMoving(uint16_t potId) const
Definition PotMonitor.h:96
void Init(UiEventQueue &queueToAddEventsTo, BackendType &backend, uint16_t idleTimeoutMs=500, float deadBandIdle=1.0/(1<< 10), float deadBand=1.0/(1<< 12))
Definition PotMonitor.h:59
void Process()
Definition PotMonitor.h:83
float GetCurrentPotValue(uint16_t potId) const
Definition PotMonitor.h:108
PotMonitor()
Definition PotMonitor.h:40
BackendType & GetBackend()
Definition PotMonitor.h:117
uint16_t GetNumPotsMonitored() const
Definition PotMonitor.h:120
static uint32_t GetNow()
A queue that holds user input events in the UI system.
Definition UiEventQueue.h:18
void AddPotMoved(uint16_t potId, float newPosition)
Definition UiEventQueue.h:168
void AddPotActivityChanged(uint16_t potId, bool isActive)
Definition UiEventQueue.h:179
Hardware defines and helpers for daisy field platform.
Definition index.h:2