Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
user_signals.h
Go to the documentation of this file.
1
20#ifndef LOGIC_USER_SIGNALS_H
21#define LOGIC_USER_SIGNALS_H
22
23#include <cstdint>
24#include <cstdio>
25#include <cstddef>
26
27#include "src/system/hal/time.h"
28
30
31namespace lampda {
32namespace logic {
33namespace user_signals {
34
37static constexpr size_t maxSignalLenght = 32;
39static constexpr size_t maxFormatLenght = maxSignalLenght - 2;
41static constexpr uint32_t signalIntervalMs = 100;
42
51inline size_t format(char* buffer, const char* name, const uint32_t* values, const size_t valueCount)
52{
53 int len = snprintf(buffer, maxFormatLenght, "$%s", name);
54 for (size_t i = 0; i < valueCount and len > 0 and static_cast<size_t>(len) < maxFormatLenght; ++i)
55 len += snprintf(buffer + len, maxFormatLenght - len, ":%lu", static_cast<unsigned long>(values[i]));
56
57 // snprintf returns the wanted lenght, so truncation shows as len >= buffer size
58 if (len <= 0 or static_cast<size_t>(len) >= maxFormatLenght)
59 {
60 buffer[0] = '\0';
61 return 0;
62 }
63 return static_cast<size_t>(len);
64}
65
67inline void send(const char* name, const uint32_t* values, const size_t valueCount)
68{
69 char buffer[maxFormatLenght];
70 if (format(buffer, name, values, valueCount) > 0)
71 bsp::lampda_print_raw("%s\r\n", buffer);
72}
73
74namespace detail {
75
76static constexpr size_t maxValues = 2;
77
79struct Slot
80{
81 constexpr Slot(const char* signalName, const uint8_t valueCount, const bool skipRepeats) :
82 name(signalName),
83 count(valueCount),
84 dedupe(skipRepeats)
85 {
86 }
87
88 const char* name;
89 uint8_t count;
90 bool dedupe;
91 bool pending = false;
92 bool force = false;
93 bool hasSent = false;
94 uint32_t sent[maxValues] = {0, 0};
95 uint32_t pendingValues[maxValues] = {0, 0};
96 uint32_t sentTimeMs = 0;
97};
98
99enum SlotId : size_t
100{
101 slotBrightness = 0,
102 slotMode,
103 slotRamp,
104 slotIndicator,
105 slotCount
106};
107
108inline Slot& slot(const SlotId id)
109{
110 static Slot slots[slotCount] = {
111 Slot("brgt", 1, true),
112 Slot("mode", 2, false),
113 Slot("ramp", 1, true),
114 Slot("ind", 1, true),
115 };
116 return slots[id];
117}
118
119inline bool same_as_sent(const Slot& s)
120{
121 if (not s.hasSent)
122 return false;
123 for (size_t i = 0; i < s.count; ++i)
124 if (s.sent[i] != s.pendingValues[i])
125 return false;
126 return true;
127}
128
130inline void flush(Slot& s, const uint32_t nowMs)
131{
132 if (not s.pending)
133 return;
134
135 // the latest value is what the receiver already has: nothing to send
136 if (s.dedupe and not s.force and same_as_sent(s))
137 {
138 s.pending = false;
139 return;
140 }
141
142 // unsigned subtraction is safe with counter wrap around
143 if (s.hasSent and (nowMs - s.sentTimeMs) < signalIntervalMs)
144 return;
145
146 send(s.name, s.pendingValues, s.count);
147 for (size_t i = 0; i < s.count; ++i)
148 s.sent[i] = s.pendingValues[i];
149 s.hasSent = true;
150 s.sentTimeMs = nowMs;
151 s.pending = false;
152 s.force = false;
153}
154
156inline void submit(const SlotId id, const uint32_t* values, const bool forceSend = false)
157{
158 Slot& s = slot(id);
159 for (size_t i = 0; i < s.count; ++i)
160 s.pendingValues[i] = values[i];
161 s.pending = true;
162 s.force = s.force or forceSend;
163 flush(s, hal::time_ms());
164}
165
166} // namespace detail
167
172inline void loop()
173{
174 const uint32_t nowMs = hal::time_ms();
175 for (size_t i = 0; i < detail::slotCount; ++i)
176 detail::flush(detail::slot(static_cast<detail::SlotId>(i)), nowMs);
177}
178
179inline void send_brightness(const uint32_t brightness) { detail::submit(detail::slotBrightness, &brightness); }
180
181inline void send_mode(const uint8_t groupId, const uint8_t modeId)
182{
183 const uint32_t values[] = {groupId, modeId};
184 detail::submit(detail::slotMode, values);
185}
186
187inline void send_ramp(const uint32_t ramp, const bool forceSend = false)
188{
189 detail::submit(detail::slotRamp, &ramp, forceSend);
190}
191
192inline void send_indicator(const uint32_t level) { detail::submit(detail::slotIndicator, &level); }
193
194} // namespace user_signals
195} // namespace logic
196} // namespace lampda
197
198#endif
void lampda_print_raw(const char *format,...)
C linkage to print functions.
Definition: text_out.cpp:320
uint32_t time_ms(void)
Returns the number of milliseconds since the Arduino board began running the current program....
Program scope.
Definition: control_fixed_modes.hpp:14
Rate limiting state of one signal.
Definition: user_signals.h:80
uint32_t sentTimeMs
Time of the latest send.
Definition: user_signals.h:96
bool dedupe
Do not send a value equal to the latest sent one.
Definition: user_signals.h:90
bool hasSent
At least one value was sent.
Definition: user_signals.h:93
bool force
The pending value must be sent even if equal to the latest sent one.
Definition: user_signals.h:92
uint32_t pendingValues[maxValues]
Latest requested values.
Definition: user_signals.h:95
bool pending
A value is waiting to be sent.
Definition: user_signals.h:91
uint32_t sent[maxValues]
Latest sent values.
Definition: user_signals.h:94
Convertion header to use the text out functions from C code.
Define all time related platform functions.
static constexpr size_t maxSignalLenght
Max signal lenght with the "\r\n" end, so that a signal is sent as one uart message (never interleave...
Definition: user_signals.h:37
static constexpr size_t maxFormatLenght
Max formatted signal lenght, without the "\r\n" end and with the terminating null.
Definition: user_signals.h:39
void send(const char *name, const uint32_t *values, const size_t valueCount)
Format and send a signal immediately (no rate limiting)
Definition: user_signals.h:67
void submit(const SlotId id, const uint32_t *values, const bool forceSend=false)
Record the latest value of a signal and send it now if the interval allows.
Definition: user_signals.h:156
static constexpr uint32_t signalIntervalMs
Minimum time between two sends of the same signal.
Definition: user_signals.h:41