1#ifndef FIREPLACE_MODE_H
2#define FIREPLACE_MODE_H
6#include "src/system/ext/math8.h"
7#include "src/system/ext/noise.h"
28 static constexpr uint16_t
xScale = 60;
29 static constexpr uint16_t
yScale = 60;
35 static constexpr uint32_t everyNIndex = 2;
43 bool isResetted =
false;
46 static void on_enter_mode(
auto& ctx)
48 ctx.state.isResetted =
true;
49 ctx.state.soundEvent.
reset(ctx);
51 ctx.template set_config_bool<ConfigKeys::rampSaturates>(
true);
54 static void loop(
auto& ctx)
56 if (ctx.state.isResetted)
58 ctx.state.isResetted =
false;
61 for (uint32_t i = 0; i <= everyNIndex; ++i)
68 fire_display(ctx, ctx.lamp.tick);
71 static void fire_display(
auto& ctx,
const uint32_t tick)
74 const int16_t zSpeed = tick % INT16_MAX;
75 const int16_t ySpeed = zSpeed * ctx.lamp.frameDurationMs;
78 float soundAverageDelta = 0.0;
79 const uint8_t index = ctx.get_active_custom_ramp();
82 ctx.state.soundEvent.update(ctx);
83 soundAverageDelta = ctx.state.soundEvent.avgDelta * 100.0;
87 float shakeness = 1.0 + (index * soundAverageDelta) / 255.0;
88 float intensity = 128 + 256 / (1 + shakeness) - 1;
91 intensity *= ctx.lamp.maxHeight;
93 const size_t firstIndex = tick % everyNIndex;
96 for (uint16_t j = firstIndex; j <= ctx.lamp.maxHeight; j += everyNIndex)
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);
101 for (uint16_t i = 0; i <= ctx.lamp.maxWidth; ++i)
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);
107 ctx.lamp.setPixelColorXY(i, j, color);
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
Definition: fireplace.hpp:38
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