Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
(cherry picked from commit b2ca545244b7efa9b870219b6e293fc13ac19e49)
  • Loading branch information
mathieucarbou committed Jun 25, 2024
1 parent 3c6c7d1 commit 3551118
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 46 deletions.
17 changes: 10 additions & 7 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <AsyncUDP.h>
#include <ESPAsyncWebServer.h>
#include <FastCRC32.h>
#include <HardwareSerial.h>
#include <LittleFS.h>

Expand Down Expand Up @@ -35,17 +36,17 @@
#include <MycilaZCD.h>

#ifdef APP_MODEL_TRIAL
#include <MycilaTrial.h>
#include <MycilaTrial.h>
#endif

#ifdef APP_MODEL_PRO
#include <ESPDashPro.h>
#include <ElegantOTAPro.h>
#include <WebSerialPro.h>
#include <ESPDashPro.h>
#include <ElegantOTAPro.h>
#include <WebSerialPro.h>
#else
#include <ESPDash.h>
#include <ElegantOTA.h>
#include <WebSerial.h>
#include <ESPDash.h>
#include <ElegantOTA.h>
#include <WebSerial.h>
#endif

#include <YaSolRDefines.h>
Expand Down Expand Up @@ -127,3 +128,5 @@ extern Mycila::Task initLoggingTask;
extern Mycila::Task initMqttSubscribersTask;
extern Mycila::Task initRestApiTask;
extern Mycila::Task initWebTask;

extern Mycila::CircularBuffer<float, 50> jsyRemoteUdpRate;
2 changes: 2 additions & 0 deletions include/YaSolRWebsite.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ namespace YaSolR {
Statistic _relay1SwitchCount = Statistic(&dashboard, YASOLR_LBL_032);
Statistic _relay2SwitchCount = Statistic(&dashboard, YASOLR_LBL_033);

Statistic _jsyRemoteUdpRate = Statistic(&dashboard, YASOLR_LBL_157);

Statistic _time = Statistic(&dashboard, YASOLR_LBL_034);
Statistic _uptime = Statistic(&dashboard, YASOLR_LBL_035);
#ifdef APP_MODEL_TRIAL
Expand Down
2 changes: 1 addition & 1 deletion include/i18n/en.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
#define YASOLR_LBL_154 "Invalid"
#define YASOLR_LBL_155 "Input Only"
#define YASOLR_LBL_156 "I/O"
#define YASOLR_LBL_157
#define YASOLR_LBL_157 "JSY Remote UDP: Reception Rate"
#define YASOLR_LBL_158
#define YASOLR_LBL_159
#define YASOLR_LBL_160
Expand Down
2 changes: 1 addition & 1 deletion include/i18n/fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
#define YASOLR_LBL_154 "Invalide"
#define YASOLR_LBL_155 "Entrée uniquement"
#define YASOLR_LBL_156 "E/S"
#define YASOLR_LBL_157
#define YASOLR_LBL_157 "JSY Remote UDP: Vitesse de réception"
#define YASOLR_LBL_158
#define YASOLR_LBL_159
#define YASOLR_LBL_160
Expand Down
8 changes: 4 additions & 4 deletions lib/DimmableLight/src/thyristor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ void Thyristor::setFrequency(float frequency) {
uint16_t Thyristor::getPulseWidth() {
noInterrupts();
uint32_t diff = micros() - lastTime;
if (diff > semiPeriodLength) {
if (semiPeriodLength && diff > semiPeriodLength) {
pulses.reset();
}
uint16_t avg = pulses.avg();
Expand All @@ -482,7 +482,7 @@ uint16_t Thyristor::getPulseWidth() {
uint16_t Thyristor::getMaxPulseWidth() {
noInterrupts();
uint32_t diff = micros() - lastTime;
if (diff > semiPeriodLength) {
if (semiPeriodLength && diff > semiPeriodLength) {
pulses.reset();
}
uint16_t max = pulses.max();
Expand All @@ -493,7 +493,7 @@ uint16_t Thyristor::getMaxPulseWidth() {
uint16_t Thyristor::getMinPulseWidth() {
noInterrupts();
uint32_t diff = micros() - lastTime;
if (diff > semiPeriodLength) {
if (semiPeriodLength && diff > semiPeriodLength) {
pulses.reset();
}
uint16_t min = pulses.min();
Expand All @@ -504,7 +504,7 @@ uint16_t Thyristor::getMinPulseWidth() {
uint16_t Thyristor::getLastPulseWidth() {
noInterrupts();
uint32_t diff = micros() - lastTime;
if (diff > semiPeriodLength) {
if (semiPeriodLength && diff > semiPeriodLength) {
pulses.reset();
}
uint16_t last = pulses.last();
Expand Down
10 changes: 6 additions & 4 deletions lib/MycilaCircularBuffer/MycilaCircularBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ namespace Mycila {

size_t count() const { return _count; };
T avg() const { return _count == 0 ? 0 : _sum / _count; }
T last() const { return _last; }
T first() const { return _buffer[_index]; }
T last() const { return _buffer[_index == 0 ? N - 1 : _index - 1]; }
T sum() const { return _sum; }
T max() const { return _max; }
T min() const { return _min; }
T rate() const {
T diff = last() - first();
return diff == 0 ? 0 : _count / diff;
}

T add(T value) {
T current = _buffer[_index];
_buffer[_index++] = value;
_last = value;
if (value > _max)
_max = value;
if (value < _min)
Expand All @@ -49,7 +53,6 @@ namespace Mycila {

void reset() {
_sum = 0;
_last = 0;
_min = std::numeric_limits<T>::max();
_max = std::numeric_limits<T>::min();
_index = 0;
Expand All @@ -73,7 +76,6 @@ namespace Mycila {
private:
T _buffer[N];
T _sum;
T _last;
T _min;
T _max;
size_t _index = 0;
Expand Down
4 changes: 2 additions & 2 deletions lib/MycilaDimmer/MycilaDimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace Mycila {

gpio_num_t getPin() const { return _pin; }
bool isEnabled() const { return _enabled; }
bool isConnected() const { return _zcd->isConnected(); }
bool isConnected() const { return _enabled && _zcd->isConnected(); }

void toJson(const JsonObject& root) const {
const float angle = getPhaseAngle();
Expand Down Expand Up @@ -74,7 +74,7 @@ namespace Mycila {
// At 100% power, the phase angle is equal to 0
float getPhaseAngle() const {
// angle_rad = PI * delay_s / period_s
return PI * _zcd->getFrequency() * getFiringDelay() / 1000000;
return PI * _zcd->getPulseFrequency() * getFiringDelay() / 1000000;
}

private:
Expand Down
5 changes: 2 additions & 3 deletions lib/MycilaDimmer/MycilaZCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ void Mycila::ZCD::begin(const int8_t pin, const uint8_t frequency) {
Thyristor::setSyncPin(_pin);
Thyristor::begin();

// attachInterrupt(digitalPinToInterrupt(_pin), onEdge, CHANGE);
_frequencyUpdater.attach(1, +[](ZCD* instance) { instance->_measuredGridFrequency = Thyristor::getDetectedFrequency(); }, this);

_enabled = true;
}

void Mycila::ZCD::end() {
if (_enabled) {
LOGI(TAG, "Disable Zero-Cross Detection...");
_frequencyUpdater.detach();
_enabled = false;
Thyristor::setFrequency(0);
Thyristor::end();
Expand All @@ -67,8 +68,6 @@ void Mycila::ZCD::end() {
}

uint16_t Mycila::ZCD::getSemiPeriod() const { return _enabled ? Thyristor::getSemiPeriod() : 0; }
float Mycila::ZCD::getFrequency() const { return getCurrentFrequency() * 2; }
float Mycila::ZCD::getCurrentFrequency() const { return _enabled ? Thyristor::getDetectedFrequency() : 0; }
uint16_t Mycila::ZCD::getAvgPulseWidth() const { return _enabled ? Thyristor::getPulseWidth() : 0; }
uint16_t Mycila::ZCD::getMaxPulseWidth() const { return _enabled ? Thyristor::getMaxPulseWidth() : 0; }
uint16_t Mycila::ZCD::getLastPulseWidth() const { return _enabled ? Thyristor::getLastPulseWidth() : 0; }
Expand Down
16 changes: 9 additions & 7 deletions lib/MycilaDimmer/MycilaZCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#pragma once

#include <ArduinoJson.h>
#include <Ticker.h>

#include <esp32-hal-gpio.h>

namespace Mycila {
Expand All @@ -15,28 +17,26 @@ namespace Mycila {

gpio_num_t getPin() const { return _pin; }
bool isEnabled() const { return _enabled; }
bool isConnected() const { return getFrequency() > 0; }
bool isConnected() const { return _measuredGridFrequency > 0; }

// in microseconds
// 50Hz => 10000
// 60Hz => 8333
uint16_t getSemiPeriod() const;

// Detect the Zero-Cross pulse frequency in Hz
float getFrequency() const;

// Grid frequency in Hz based on the Zero-Cross pulse frequency
float getCurrentFrequency() const;
float getCurrentFrequency() const { return _enabled ? _measuredGridFrequency : 0; }

// Detect the Zero-Cross pulse width in microseconds
// Zero-Cross pulse information
float getPulseFrequency() const { return _enabled ? _measuredGridFrequency * 2 : 0; }
uint16_t getAvgPulseWidth() const;
uint16_t getLastPulseWidth() const;
uint16_t getMaxPulseWidth() const;
uint16_t getMinPulseWidth() const;

void toJson(const JsonObject& root) const {
root["enabled"] = isEnabled();
root["pulse_freq"] = getFrequency();
root["pulse_freq"] = getPulseFrequency();
root["pulse_width_avg"] = getAvgPulseWidth();
root["pulse_width_last"] = getLastPulseWidth();
root["pulse_width_max"] = getMaxPulseWidth();
Expand All @@ -46,6 +46,8 @@ namespace Mycila {

private:
bool _enabled = false;
volatile float _measuredGridFrequency = 0;
Ticker _frequencyUpdater;
gpio_num_t _pin = GPIO_NUM_NC;
};
} // namespace Mycila
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ lib_deps =
Update
bblanchon/ArduinoJson @ 7.0.4
olikraus/U8g2 @ 2.35.19
robtillaart/CRC @ 1.0.3
mathieucarbou/Async TCP @ 3.1.4
mathieucarbou/ESP Async WebServer @ 2.10.8
mathieucarbou/MycilaConfig @ 3.0.1
mathieucarbou/MycilaDS18 @ 3.0.2
mathieucarbou/MycilaESPConnect @ 4.3.1
mathieucarbou/MycilaESPConnect @ 4.3.2
mathieucarbou/MycilaEasyDisplay @ 3.0.1
mathieucarbou/MycilaHADiscovery @ 2.2.1
mathieucarbou/MycilaJSY @ 9.0.8
Expand Down
1 change: 1 addition & 0 deletions src/Website.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ void YaSolR::WebsiteClass::updateCards() {
_networkWiFiSSID.set(ESPConnect.getWiFiSSID().c_str());
_relay1SwitchCount.set(String(relay1.getSwitchCount()).c_str());
_relay2SwitchCount.set(String(relay2.getSwitchCount()).c_str());
_jsyRemoteUdpRate.set((String(jsyRemoteUdpRate.rate()) + " msg/s").c_str());
_time.set(Mycila::Time::getLocalStr().c_str());
_uptime.set(Mycila::Time::toDHHMMSS(Mycila::System.getUptime()).c_str());
#ifdef APP_MODEL_TRIAL
Expand Down
42 changes: 26 additions & 16 deletions src/init/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,25 +346,35 @@ Mycila::Task initEventsTask("Init Events", [](void* params) {
});

udp.onPacket([](AsyncUDPPacket packet) {
// [0] uint8_t == message type
// [1] sizeof(Mycila::JSYData)
// [sizeOfBody] uint32_t == CRC32
constexpr size_t sizeOfJSYData = sizeof(Mycila::JSYData);
constexpr size_t sizeOfBody = 1 + sizeOfJSYData;
constexpr size_t sizeOfCRC32 = sizeof(uint32_t);
constexpr size_t sizeOfUDP = sizeOfBody + sizeOfCRC32;

size_t len = packet.length();
uint8_t* data = packet.data();
if (!len)
if (len != sizeOfUDP)
return;

uint8_t* data = packet.data();
const uint8_t type = data[0];
data++;
len--;
switch (type) {
case YASOLR_UDP_MSG_TYPE_JSY_DATA:
if (len == sizeof(Mycila::JSYData)) {
Mycila::JSYData jsyData;
memcpy(&jsyData, data, sizeof(Mycila::JSYData));
if (grid.updateRemoteJSYData(jsyData)) {
routingTask.resume();
}
}
break;
default:
break;

if (type != YASOLR_UDP_MSG_TYPE_JSY_DATA)
return;

FastCRC32 crc32;
crc32.add(data, sizeOfBody);
uint32_t crc = crc32.calc();
if (memcmp(&crc, data + sizeOfBody, sizeOfCRC32) != 0)
return;

Mycila::JSYData jsyData;
memcpy(&jsyData, data + 1, sizeOfJSYData);
jsyRemoteUdpRate.add(millis() / 1000.0f);
if (grid.updateRemoteJSYData(jsyData)) {
routingTask.resume();
}
});
});
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Mycila::RouterOutput output2("output2", dimmerO2, bypassRelayO2, ds18O2, grid, p
Mycila::RouterRelay routerRelay1(relay1);
Mycila::RouterRelay routerRelay2(relay2);

Mycila::CircularBuffer<float, 50> jsyRemoteUdpRate;

AsyncWebServer webServer(80);
AsyncUDP udp;
ESPDash dashboard = ESPDash(&webServer, "/dashboard", false);
Expand Down

0 comments on commit 3551118

Please sign in to comment.