5#include <MycilaDimmer.h>
7#define DIMMER_RESOLUTION 12
8#define FIRING_DELAYS_LEN 200U
10static constexpr uint32_t FIRING_DELAY_MAX = (1 << DIMMER_RESOLUTION) - 1;
11static constexpr uint32_t FIRING_DELAYS_SCALE = (FIRING_DELAYS_LEN - 1U) * (1UL << (16 - DIMMER_RESOLUTION));
12static constexpr uint16_t FIRING_DELAYS[FIRING_DELAYS_LEN] = {
218uint16_t Mycila::Dimmer::_lookupFiringDelay(
float dutyCycle, uint16_t semiPeriod) {
219 uint32_t duty = dutyCycle * FIRING_DELAY_MAX;
220 uint32_t slot = duty * FIRING_DELAYS_SCALE + (FIRING_DELAYS_SCALE >> 1);
221 uint32_t index = slot >> 16;
222 uint32_t a = FIRING_DELAYS[index];
223 uint32_t b = FIRING_DELAYS[index + 1];
224 uint32_t delay = a - (((a - b) * (slot & 0xffff)) >> 16);
225 return (delay * semiPeriod) >> 16;
228#ifdef MYCILA_JSON_SUPPORT
229static const char* H_LEVELS[] = {
"H1",
"H3",
"H5",
"H7",
"H9",
"H11",
"H13",
"H15",
"H17",
"H19",
"H21"};
236void Mycila::Dimmer::toJson(
const JsonObject& root)
const {
237 root[
"type"] = type();
238 root[
"enabled"] = _enabled;
239 root[
"online"] = _online;
240 root[
"state"] = isOn() ?
"on" :
"off";
241 root[
"duty_cycle"] = _dutyCycle;
242 root[
"duty_cycle_mapped"] = getDutyCycleMapped();
243 root[
"duty_cycle_fire"] = _dutyCycleFire;
244 root[
"duty_cycle_limit"] = _dutyCycleLimit;
245 root[
"duty_cycle_min"] = _dutyCycleMin;
246 root[
"duty_cycle_max"] = _dutyCycleMax;
247 root[
"power_lut"] = _powerLUTEnabled;
248 root[
"power_lut_semi_period"] = _semiPeriod;
249 JsonObject harmonics = root[
"harmonics"].to<JsonObject>();
250 float* output =
new float[11];
251 if (calculateHarmonics(output, 11)) {
252 for (
size_t i = 0; i < 11; i++) {
253 if (!std::isnan(output[i])) {
254 harmonics[H_LEVELS[i]] = output[i];