Skip to content

Commit

Permalink
CircularBuffer update
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jun 21, 2024
1 parent 182f78e commit cb94964
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
5 changes: 5 additions & 0 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <LittleFS.h>

#include <MycilaAppInfo.h>
#include <MycilaCircularBuffer.h>
#include <MycilaConfig.h>
#include <MycilaDS18.h>
#include <MycilaDimmer.h>
Expand Down Expand Up @@ -77,6 +78,10 @@ extern Mycila::RouterRelay routerRelay2;
extern Mycila::TrafficLight lights;
extern Mycila::ZCD zcd;

extern Mycila::CircularBuffer<float, YASOLR_GRAPH_POINTS> gridPowerHistory;
extern Mycila::CircularBuffer<float, YASOLR_GRAPH_POINTS> routedPowerHistory;
extern Mycila::CircularBuffer<float, YASOLR_GRAPH_POINTS> routerTHDiHistory;

extern Mycila::TaskManager ioTaskManager;
extern Mycila::Task haDiscoveryTask;
extern Mycila::Task mqttPublishTask;
Expand Down
7 changes: 4 additions & 3 deletions include/YaSolRDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

// YASOLR

#define YASOLR_LANG_EN 1
#define YASOLR_LANG_FR 2
#define TAG "YASOLR"

#define YASOLR_TRUE "true"
#define YASOLR_FALSE "false"
Expand All @@ -28,7 +27,8 @@
#define YASOLR_BOOL(a) ((a) ? YASOLR_TRUE : YASOLR_FALSE)
#define YASOLR_STATE(a) ((a) ? YASOLR_ON : YASOLR_OFF)

#define TAG "YASOLR"
#define YASOLR_LANG_EN 1
#define YASOLR_LANG_FR 2

#ifndef YASOLR_LANG
#define YASOLR_LANG YASOLR_LANG_EN
Expand Down Expand Up @@ -150,6 +150,7 @@
#define YASOLR_RELAY_TYPE_NO "NO"
#define YASOLR_SERIAL_BAUDRATE 115200
#define YASOLR_WEEK_DAYS "sun,mon,tue,wed,thu,fri,sat"
#define YASOLR_GRAPH_POINTS 30

// pinout

Expand Down
22 changes: 11 additions & 11 deletions lib/DimmableLight/src/thyristor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ int Thyristor::semiPeriodShrinkMargin = 50;
int Thyristor::semiPeriodExpandMargin = 50;

static uint32_t lastTime = 0;
static Mycila::CircularBuffer<uint32_t, 5> queue(0, UINT32_MAX);
static Mycila::CircularBuffer<uint32_t, 5> pulses(0, UINT32_MAX);
static Mycila::CircularBuffer<uint32_t, 5> queue;
static Mycila::CircularBuffer<uint32_t, 5> pulses;

void ARDUINO_ISR_ATTR zero_cross_pulse_int() {
if (!lastTime) {
Expand Down Expand Up @@ -271,8 +271,8 @@ void ARDUINO_ISR_ATTR zero_cross_pulse_int() {
// can be considered as lost for a while and I must reset my moving average.
// I decided to use "16" because is a power of 2, very fast to be computed.
if (semiPeriodLength && diff > semiPeriodLength * 16) {
queue.reset(0, UINT32_MAX);
pulses.reset(0, UINT32_MAX);
queue.reset();
pulses.reset();
} else {
// If filtering has passed, I can update the moving average
queue.add(diff);
Expand Down Expand Up @@ -439,8 +439,8 @@ void Thyristor::begin() {
void Thyristor::end() {
detachInterrupt(digitalPinToInterrupt(syncPin));
pinMode(syncPin, INPUT);
queue.reset(0, UINT32_MAX);
pulses.reset(0, UINT32_MAX);
queue.reset();
pulses.reset();
setFrequency(0);
}

Expand Down Expand Up @@ -472,7 +472,7 @@ uint16_t Thyristor::getPulseWidth() {
noInterrupts();
uint32_t diff = micros() - lastTime;
if (diff > semiPeriodLength) {
pulses.reset(0, UINT32_MAX);
pulses.reset();
}
uint16_t avg = pulses.avg();
interrupts();
Expand All @@ -483,7 +483,7 @@ uint16_t Thyristor::getMaxPulseWidth() {
noInterrupts();
uint32_t diff = micros() - lastTime;
if (diff > semiPeriodLength) {
pulses.reset(0, UINT32_MAX);
pulses.reset();
}
uint16_t max = pulses.max();
interrupts();
Expand All @@ -494,7 +494,7 @@ uint16_t Thyristor::getMinPulseWidth() {
noInterrupts();
uint32_t diff = micros() - lastTime;
if (diff > semiPeriodLength) {
pulses.reset(0, UINT32_MAX);
pulses.reset();
}
uint16_t min = pulses.min();
interrupts();
Expand All @@ -505,7 +505,7 @@ uint16_t Thyristor::getLastPulseWidth() {
noInterrupts();
uint32_t diff = micros() - lastTime;
if (diff > semiPeriodLength) {
pulses.reset(0, UINT32_MAX);
pulses.reset();
}
uint16_t last = pulses.last();
interrupts();
Expand All @@ -526,7 +526,7 @@ float Thyristor::getDetectedFrequency() {
// can be considered as lost for a while.
// I decided to use "16" because is a power of 2, very fast to be computed.
if (semiPeriodLength && diff > semiPeriodLength * 16) {
queue.reset(0, UINT32_MAX);
queue.reset();
}

c = queue.count();
Expand Down
9 changes: 5 additions & 4 deletions lib/MycilaCircularBuffer/MycilaCircularBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
#pragma once

#include <stddef.h>
#include <limits>

namespace Mycila {
template <typename T, int N>
class CircularBuffer {
public:
CircularBuffer(T typeMin, T typeMax) { reset(typeMin, typeMax); }
CircularBuffer() { reset(); }

size_t count() const { return _count; };
T avg() const { return _count == 0 ? 0 : _sum / _count; }
Expand All @@ -36,11 +37,11 @@ namespace Mycila {
return current;
};

void reset(T typeMin, T typeMax) {
void reset() {
_sum = 0;
_last = 0;
_min = typeMax;
_max = typeMin;
_min = std::numeric_limits<T>::max();
_max = std::numeric_limits<T>::min();
_index = 0;
_count = 0;
for (int i = 0; i < N; i++)
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Mycila::RouterOutput output2("output2", dimmerO2, bypassRelayO2, ds18O2, grid, p
Mycila::RouterRelay routerRelay1(relay1);
Mycila::RouterRelay routerRelay2(relay2);

Mycila::CircularBuffer<float, YASOLR_GRAPH_POINTS> gridPowerHistory;
Mycila::CircularBuffer<float, YASOLR_GRAPH_POINTS> routedPowerHistory;
Mycila::CircularBuffer<float, YASOLR_GRAPH_POINTS> routerTHDiHistory;

AsyncWebServer webServer(80);
ESPDash dashboard = ESPDash(&webServer, "/dashboard", false);

Expand Down

0 comments on commit cb94964

Please sign in to comment.