Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
fireplace.hpp
Go to the documentation of this file.
1#ifndef FIREPLACE_MODE_H
2#define FIREPLACE_MODE_H
3
5
6#include "src/system/ext/math8.h"
7#include "src/system/ext/noise.h"
8
10
12
13#include <cstdint>
14
17
23struct FireMode : public BasicMode
24{
26 static constexpr bool hasCustomRamp = true;
27
28 static constexpr uint16_t xScale = 60;
29 static constexpr uint16_t yScale = 60;
30
32 static constexpr auto palette = colors::PaletteHeatColors;
33
34 // this is too heavy to run at full, speed, display every other pixels instead of refreshing amm
35 static constexpr uint32_t everyNIndex = 2;
36
37 struct StateTy
38 {
41
42 // flag that we have just been reseted
43 bool isResetted = false;
44 };
45
46 static void on_enter_mode(auto& ctx)
47 {
48 ctx.state.isResetted = true;
49 ctx.state.soundEvent.reset(ctx);
50
51 ctx.template set_config_bool<ConfigKeys::rampSaturates>(true);
52 }
53
54 static void loop(auto& ctx)
55 {
56 if (ctx.state.isResetted)
57 {
58 ctx.state.isResetted = false;
59
60 // load animation
61 for (uint32_t i = 0; i <= everyNIndex; ++i)
62 {
63 fire_display(ctx, i);
64 }
65 return;
66 }
67
68 fire_display(ctx, ctx.lamp.tick);
69 }
70
71 static void fire_display(auto& ctx, const uint32_t tick)
72 {
73 // tick forward
74 const int16_t zSpeed = tick % INT16_MAX;
75 const int16_t ySpeed = zSpeed * ctx.lamp.frameDurationMs;
76
77 // measure custom ramp for fire sound sensitivity
78 float soundAverageDelta = 0.0;
79 const uint8_t index = ctx.get_active_custom_ramp();
80 if (index > 16)
81 {
82 ctx.state.soundEvent.update(ctx);
83 soundAverageDelta = ctx.state.soundEvent.avgDelta * 100.0;
84 }
85
86 // measure sound level for sound-sensitive fire
87 float shakeness = 1.0 + (index * soundAverageDelta) / 255.0;
88 float intensity = 128 + 256 / (1 + shakeness) - 1;
89
90 // precompute "fire intensity" line per line
91 intensity *= ctx.lamp.maxHeight;
92
93 const size_t firstIndex = tick % everyNIndex;
94
95 // for each line, generate noise & set pixels
96 for (uint16_t j = firstIndex; j <= ctx.lamp.maxHeight; j += everyNIndex)
97 {
98 const float here = std::max<float>(intensity - j * 255.0, 0.0);
99 const uint8_t decay = std::min<uint8_t>(here / ctx.lamp.maxHeight, 255.0);
100
101 for (uint16_t i = 0; i <= ctx.lamp.maxWidth; ++i)
102 {
103 const auto flame = noise8::inoise(i * xScale, j * yScale + ySpeed, zSpeed);
104 const auto pixel = std::min<uint8_t>(223, qsub8(flame, decay));
105 const auto color = modes::colors::from_palette<false, uint8_t>(pixel, palette);
106
107 ctx.lamp.setPixelColorXY(i, j, color);
108 }
109 }
110 }
111};
112
113} // namespace lampda::modes::default_modes
114
115#endif
Define the audio handle object.
static constexpr PaletteTy PaletteHeatColors
Heat black body.
Definition: palettes.hpp:415
Basic "default" modes included with the hardware.
Definition: aurora.hpp:12
Define some useful color palettes, and tools to use them.
Parent object for all custom user modes.
Definition: mode_type.hpp:53
Sound processor able to detect sound level events.
Definition: utils.hpp:92
void reset(auto &ctx)
Call this once inside the mode on_enter_mode callback.
Definition: utils.hpp:122
audio::SoundEventTy soundEvent
handle sound event
Definition: fireplace.hpp:40
Emulate a fireplace, optionally sound-sensitive. Based on "Perlin noise fire procedure" - ldirko....
Definition: fireplace.hpp:24
static constexpr auto palette
Palette used for fire colors.
Definition: fireplace.hpp:32
static constexpr uint16_t xScale
Noise scaling (X direction)
Definition: fireplace.hpp:28
static constexpr uint16_t yScale
Noise scaling (Y direction)
Definition: fireplace.hpp:29
static constexpr bool hasCustomRamp
Fire custom ramp sets how sensitive it is to ambiant sound.
Definition: fireplace.hpp:26