5#ifndef MODES_DRAW_GRID_RULE_HPP
6#define MODES_DRAW_GRID_RULE_HPP
10namespace lampda::modes::draw::grid {
12using LampTy = hardware::LampTy;
48template<
typename ConfigTy = LineRuleConfig>
struct LineRule
50 static constexpr uint8_t dstBufIdx = ConfigTy::dstBufIdx;
51 static constexpr uint8_t srcBufIdx = ConfigTy::srcBufIdx;
52 static constexpr uint8_t tempBufIdx = ConfigTy::tempBufIdx;
54 static constexpr bool loadFullOnReset = ConfigTy::loadFullOnReset;
58 using LineTy = std::array<uint32_t, width>;
70 lamp.template fillTempBuffer(0);
71 auto& buffer = lamp.template getTempBuffer<dstBufIdx>();
85 auto& buffer = lamp.template getTempBuffer<dstBufIdx>();
88 uint16_t nextStart = nextLine * fwidth;
89 bool endReached = (nextStart + width) > buffer.size();
92 if (endReached && ConfigTy::scrollAmount == 0)
101 scrollBy(lamp, ConfigTy::scrollAmount, ConfigTy::scrollSkewed);
104 nextLine -= ConfigTy::scrollAmount;
105 nextStart = nextLine * fwidth;
114 if (assertBool(nextStart + width <= buffer.size()))
115 std::copy(newLine.begin(), newLine.end(), buffer.begin() + nextStart);
125 lamp.template setColorsFromBufferReversed<dstBufIdx>(
true);
129 lamp.template setColorsFromBuffer<dstBufIdx>();
136 lamp.template copyBuffer<tempBufIdx, dstBufIdx>();
138 lamp.template copyBuffer<srcBufIdx, tempBufIdx>();
148 void LMBD_INLINE
smoothDisplay(
LampTy& lamp, uint16_t counter, uint16_t maxCounter, uint16_t maxValue = 256)
150 uint16_t increment = maxValue / maxCounter;
151 float phase = counter * increment + increment / 2.0;
158 const auto& buffer = lamp.template getTempBuffer<dstBufIdx>();
163 uint16_t lineStart = lineIndex * fwidth;
164 if (assertBool(lineStart + width <= buffer.size()))
165 std::copy(buffer.cbegin() + lineStart, buffer.cbegin() + lineStart + width, newLine.begin());
174 auto& buffer = lamp.template getTempBuffer<dstBufIdx>();
176 for (uint16_t I = 0; I <
nbLines; ++I)
179 uint16_t shiftStart = I * fwidth;
180 uint16_t shiftTweak = I * fwidth + 0.5;
183 if (skewed && shiftStart == shiftTweak && (I > 2 && I != lamp.shiftResidue))
186 std::copy(line.begin() + 1, line.end(), line.begin());
187 line[line.size() - 1] = last;
191 if (skewed && I == 2)
194 if (assertBool(shiftStart + line.size() <= buffer.size()))
195 std::copy(line.begin() + ((I || !skewed) ? 0 : 1), line.end(), buffer.begin() + shiftStart);
212 template<
bool hasCustomRamp, u
int8_t rampSubstitute = 70, u
int8_t baseSmooth = 4>
213 void LMBD_INLINE
loop(
auto& ctx,
auto& callback)
215 auto& lamp = ctx.lamp;
223 if constexpr (loadFullOnReset)
226 for (
size_t i = 0; i <
nbLines; ++i)
231 if constexpr (ConfigTy::renderBlurAmount)
232 lamp.
blur(ConfigTy::renderBlurAmount);
237 uint8_t rampValue = (hasCustomRamp ? ctx.get_active_custom_ramp() : rampSubstitute);
240 uint16_t smoothFrame = baseSmooth + rampValue / 16;
248 if constexpr (ConfigTy::renderBlurAmount)
249 lamp.
blur(ConfigTy::renderBlurAmount);
264 if constexpr (ConfigTy::renderBlurAmount)
265 lamp.
blur(ConfigTy::renderBlurAmount);
286template<u
int8_t ruleNo,
bool straight = false,
bool leftBound = false,
bool rightBound = false, u
int8_t nbBits = 24>
287void wolframRule(
const auto& before,
auto& after)
289 constexpr uint8_t pattern[8] = {0b1 & (ruleNo >> 7),
296 0b1 & (ruleNo >> 0)};
298 uint16_t start = leftBound ? 1 : 0;
299 uint16_t end = rightBound ? after.size() - 1 : after.size();
301 for (uint16_t I = start; I < end; ++I)
303 uint16_t J = (I + (straight ? 1 : 0)) % end;
306 for (uint32_t P = 0; P < nbBits; ++P)
308 uint32_t mask = 1 << P;
309 uint32_t l = before[(I + before.size() - 1) % before.size()] & mask;
310 uint32_t m = before[I] & mask;
311 uint32_t r = before[(I + 1) % before.size()] & mask;
static constexpr N abs(const N a)
absolute of a number
Definition: utils.h:46
Define the rules for a line.
Definition: grid_rule.hpp:16
static constexpr uint8_t srcBufIdx
Sec. buf. used for smooth grid.
Definition: grid_rule.hpp:18
static constexpr uint8_t renderBlurAmount
Blur before display.
Definition: grid_rule.hpp:21
static constexpr uint8_t scrollAmount
Scroll each update.
Definition: grid_rule.hpp:20
static constexpr uint8_t tempBufIdx
Temporary buffer for copies.
Definition: grid_rule.hpp:19
static constexpr bool scrollSkewed
Scroll skewed to the left.
Definition: grid_rule.hpp:22
static constexpr uint8_t dstBufIdx
Main buf. used for the grid.
Definition: grid_rule.hpp:17
static constexpr bool loadFullOnReset
If true, the first call of loop will load the whole grid. False will load gradually.
Definition: grid_rule.hpp:23
Implement a line-based grid pattern, update line per line.
Definition: grid_rule.hpp:49
void display(LampTy &lamp, bool reversed=false)
Display the line-rule grid onto screen (if reversed reverse it)
Definition: grid_rule.hpp:121
std::array< uint32_t, width > LineTy
Type of a line array.
Definition: grid_rule.hpp:58
void LMBD_INLINE smoothDisplay(LampTy &lamp, uint16_t counter, uint16_t maxCounter, uint16_t maxValue=256)
For a counter between 0 and maxCounter display a smooth frame.
Definition: grid_rule.hpp:148
void update(LampTy &lamp, auto &callback)
When called, pass before and after line to callback for processing.
Definition: grid_rule.hpp:83
static constexpr uint16_t nbLines
Number of lines in grid.
Definition: grid_rule.hpp:61
void LMBD_INLINE loop(auto &ctx, auto &callback)
Default loop function, update and display, smooth display if fast.
Definition: grid_rule.hpp:213
void LMBD_INLINE smoothUpdate(LampTy &lamp, auto &callback)
Same as .update() but uses two buffers, required for smoothDisplay()
Definition: grid_rule.hpp:134
void LMBD_INLINE reset(LampTy &lamp)
Reset grip with an empty (black) first line.
Definition: grid_rule.hpp:76
bool isResetted
flag to signal that the grid just been resetted
Definition: grid_rule.hpp:273
uint16_t lastLine
latest treated line
Definition: grid_rule.hpp:271
void scrollBy(LampTy &lamp, uint8_t amount, bool skewed)
Scroll grid by amount upward (optionnaly skewed to the left)
Definition: grid_rule.hpp:172
void reset(LampTy &lamp, LineTy &firstLine)
To be called in the parent .reset() to set firstLine as first line.
Definition: grid_rule.hpp:64
uint32_t lastUpdate
latest update time, in milliseconds
Definition: grid_rule.hpp:272
LineTy LMBD_INLINE lineAtIndex(LampTy &lamp, uint16_t lineIndex)
Return a copy of line at lineAtIndex (may be empty if out of screen)
Definition: grid_rule.hpp:156
LineTy currentLine
Current line being processed.
Definition: grid_rule.hpp:270
void LMBD_INLINE smoothDisplay(LampTy &lamp, float phasis)
Display grid buffers as an in-between frame, at phasis (from 0 to 1)
Definition: grid_rule.hpp:142
Main interface between the user and the hardware of the lamp.
Definition: lamp_type.hpp:114
void setColorsFromMixedBuffers(float phase)
Mix two colors from a buffer.
Definition: lamp_type.hpp:989
void LMBD_INLINE blur(uint8_t blurBy)
Blur currently displayed content by blurBy units /!\ Only applied along the strip,...
Definition: lamp_type.hpp:753
static constexpr uint16_t maxHeight
(indexable) Height of "pixel space" w/ lamp taken as a LED matrix
Definition: lamp_type.hpp:387
static constexpr float maxWidthFloat
Width as a precise floating point number, equal to stripXCoordinates.
Definition: lamp_type.hpp:369
volatile const uint32_t now
(physical) The "now" on milliseconds, updated just before loop.
Definition: lamp_type.hpp:1075
static constexpr uint32_t frameDurationMs
Hardward try to call .loop() every frameDurationMs (12ms for 83.3fps)
Definition: lamp_type.hpp:339
static constexpr uint16_t maxWidth
(indexable) Width of "pixel space" w/ lamp taken as a LED matrix
Definition: lamp_type.hpp:366
brightness_t LMBD_INLINE getBrightness(const bool readPreviousBrightness=false)
Get brightness of the lamp.
Definition: lamp_type.hpp:694
volatile const uint32_t raw_frame_count
(physical) Raw frame count, incremented at an undefined rate
Definition: lamp_type.hpp:1092