Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
(cherry picked from commit 510cffba350b1b10b248cb5f9d0131a88b817f12)
  • Loading branch information
mathieucarbou committed Jul 12, 2024
1 parent 5065178 commit 3068a30
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 27 deletions.
2 changes: 1 addition & 1 deletion include/i18n/en.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
#define YASOLR_LBL_176 "D Term (W)"
#define YASOLR_LBL_177 "Chart Reset"
#define YASOLR_LBL_178 "Debug Information"
#define YASOLR_LBL_179
#define YASOLR_LBL_179 "ZCD Disabled"
#define YASOLR_LBL_180
#define YASOLR_LBL_181
#define YASOLR_LBL_182
Expand Down
2 changes: 1 addition & 1 deletion include/i18n/fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
#define YASOLR_LBL_176 "Terme D (W)"
#define YASOLR_LBL_177 "Réinitialisation graphique"
#define YASOLR_LBL_178 "Informations de debug"
#define YASOLR_LBL_179
#define YASOLR_LBL_179 "ZCD Désactivé"
#define YASOLR_LBL_180
#define YASOLR_LBL_181
#define YASOLR_LBL_182
Expand Down
17 changes: 9 additions & 8 deletions lib/MycilaDimmer/MycilaDimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern Mycila::Logger logger;
static const uint16_t TABLE_PHASE_DELAY[TABLE_PHASE_LEN] PROGMEM{0xefea, 0xdfd4, 0xd735, 0xd10d, 0xcc12, 0xc7cc, 0xc403, 0xc094, 0xbd6a, 0xba78, 0xb7b2, 0xb512, 0xb291, 0xb02b, 0xaddc, 0xaba2, 0xa97a, 0xa762, 0xa557, 0xa35a, 0xa167, 0x9f7f, 0x9da0, 0x9bc9, 0x99fa, 0x9831, 0x966e, 0x94b1, 0x92f9, 0x9145, 0x8f95, 0x8de8, 0x8c3e, 0x8a97, 0x88f2, 0x8750, 0x85ae, 0x840e, 0x826e, 0x80cf, 0x7f31, 0x7d92, 0x7bf2, 0x7a52, 0x78b0, 0x770e, 0x7569, 0x73c2, 0x7218, 0x706b, 0x6ebb, 0x6d07, 0x6b4f, 0x6992, 0x67cf, 0x6606, 0x6437, 0x6260, 0x6081, 0x5e99, 0x5ca6, 0x5aa9, 0x589e, 0x5686, 0x545e, 0x5224, 0x4fd5, 0x4d6f, 0x4aee, 0x484e, 0x4588, 0x4296, 0x3f6c, 0x3bfd, 0x3834, 0x33ee, 0x2ef3, 0x28cb, 0x202c, 0x1016};

void Mycila::Dimmer::begin(const int8_t pin) {
if (_enabled)
if (_dimmer)
return;

if (GPIO_IS_VALID_OUTPUT_GPIO(pin)) {
Expand All @@ -52,15 +52,13 @@ void Mycila::Dimmer::begin(const int8_t pin) {
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);

_dimmer = new Thyristor(_pin);
_duty = 0;
_enabled = true;
_dimmer = new Thyristor(_pin);
}

void Mycila::Dimmer::end() {
if (_enabled) {
if (_dimmer) {
LOGI(TAG, "Disable Dimmer on pin %" PRId8, _pin);
_enabled = false;
_duty = 0;
_dimmer->turnOff();
digitalWrite(_pin, LOW);
Expand All @@ -71,7 +69,12 @@ void Mycila::Dimmer::end() {
}

void Mycila::Dimmer::setDuty(uint16_t newDuty) {
if (!_enabled)
if (!_dimmer)
return;

const uint16_t semiPeriod = _zcd->getSemiPeriod();

if (semiPeriod == 0)
return;

// ensure newDuty is within bounds
Expand All @@ -81,8 +84,6 @@ void Mycila::Dimmer::setDuty(uint16_t newDuty) {
if (_duty == newDuty)
return;

const uint16_t semiPeriod = _zcd->getSemiPeriod();

if (newDuty == 0) {
_dimmer->setDelay(semiPeriod);

Expand Down
11 changes: 5 additions & 6 deletions lib/MycilaDimmer/MycilaDimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Mycila {
bool isOnAtFullPower() const { return _duty >= MYCILA_DIMMER_MAX_DUTY; }

gpio_num_t getPin() const { return _pin; }
bool isEnabled() const { return _enabled; }
bool isEnabled() const { return _dimmer != nullptr; }

void toJson(const JsonObject& root) const {
const float angle = getPhaseAngle();
Expand All @@ -39,9 +39,8 @@ namespace Mycila {
root["delay"] = getFiringDelay();
root["duty"] = _duty;
root["duty_cycle"] = getDutyCycle();
root["enabled"] = _enabled;
root["enabled"] = _dimmer != nullptr;
root["state"] = _duty > 0 ? "on" : "off";
_zcd->toJson(root["zcd"].to<JsonObject>());
}

// Power Duty Cycle [0, MYCILA_DIMMER_MAX_DUTY]
Expand All @@ -58,20 +57,20 @@ namespace Mycila {
// Where semi-period = 1000000 / 2 / frequency (50h: 10000 us, 60Hz: 8333 us)
// At 0% power, delay is equal to the semi-period
// At 100% power, the delay is 0 us
uint16_t getFiringDelay() const { return _dimmer->getDelay(); }
uint16_t getFiringDelay() const { return isEnabled() ? _dimmer->getDelay() : 0; }

// Phase angle [0, PI] rad
// At 0% power, the phase angle is equal to PI
// At 100% power, the phase angle is equal to 0
float getPhaseAngle() const {
// angle_rad = PI * delay_us / period_us
return PI * getFiringDelay() / _zcd->getSemiPeriod();
uint16_t semiPeriod = _zcd->getSemiPeriod();
return semiPeriod == 0 ? PI : PI * getFiringDelay() / semiPeriod;
}

private:
ZCD* _zcd;
gpio_num_t _pin = GPIO_NUM_NC;
bool _enabled = false;
Thyristor* _dimmer = nullptr;
uint16_t _duty = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/MycilaDimmer/MycilaZCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void Mycila::ZCD::end() {
}
}

uint16_t Mycila::ZCD::getSemiPeriod() const { return _enabled ? Thyristor::getSemiPeriod() : 0; }
uint16_t Mycila::ZCD::getSemiPeriod() const { return Thyristor::getSemiPeriod(); }

float Mycila::ZCD::getPulseFrequency() const { return _enabled ? Thyristor::getPulseFrequency() : 0; }
uint16_t Mycila::ZCD::getAvgPulseWidth() const { return _enabled ? Thyristor::getPulseWidth() : 0; }
Expand Down
5 changes: 2 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ board = esp32dev
monitor_filters = esp32_exception_decoder, log2file
monitor_speed = 115200
upload_protocol = esptool
upload_speed = 921600
; upload_speed = 921600
; upload_protocol = custom
; upload_url = http://192.168.125.119
extra_scripts =
Expand All @@ -48,8 +48,7 @@ lib_deps =
bblanchon/ArduinoJson @ 7.1.0
olikraus/U8g2 @ 2.35.19
robtillaart/CRC @ 1.0.3
# mathieucarbou/Async TCP @ 3.1.4
https://github.com/mathieucarbou/AsyncTCP#dev
mathieucarbou/Async TCP @ 3.1.4
mathieucarbou/ESP Async WebServer @ 3.0.6
mathieucarbou/MycilaConfig @ 3.0.1
mathieucarbou/MycilaDS18 @ 3.0.4
Expand Down
4 changes: 2 additions & 2 deletions src/Website.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,10 @@ void YaSolR::WebsiteClass::updateCards() {
// Hardware (status)
_status(_jsy, KEY_ENABLE_JSY, jsy.isEnabled(), jsy.isConnected(), YASOLR_LBL_110);
_status(_mqtt, KEY_ENABLE_MQTT, mqtt.isEnabled(), mqtt.isConnected(), mqtt.getLastError() ? mqtt.getLastError() : YASOLR_LBL_113);
_status(_output1Dimmer, KEY_ENABLE_OUTPUT1_DIMMER, dimmerO1.isEnabled());
_status(_output1Dimmer, KEY_ENABLE_OUTPUT1_DIMMER, dimmerO1.isEnabled(), zcd.getSemiPeriod() > 0, YASOLR_LBL_179);
_status(_output1DS18, KEY_ENABLE_OUTPUT1_DS18, ds18O1.isEnabled(), ds18O1.getLastTime() > 0, YASOLR_LBL_114);
_status(_output1PZEM, KEY_ENABLE_OUTPUT1_PZEM, pzemO1.isEnabled(), pzemO1.isConnected(), YASOLR_LBL_110);
_status(_output2Dimmer, KEY_ENABLE_OUTPUT2_DIMMER, dimmerO2.isEnabled());
_status(_output2Dimmer, KEY_ENABLE_OUTPUT2_DIMMER, dimmerO2.isEnabled(), zcd.getSemiPeriod() > 0, YASOLR_LBL_179);
_status(_output2DS18, KEY_ENABLE_OUTPUT2_DS18, ds18O2.isEnabled(), ds18O2.getLastTime() > 0, YASOLR_LBL_114);
_status(_output2PZEM, KEY_ENABLE_OUTPUT2_PZEM, pzemO2.isEnabled(), pzemO2.isConnected(), YASOLR_LBL_110);
_status(_routerDS18, KEY_ENABLE_DS18_SYSTEM, ds18Sys.isEnabled(), ds18Sys.getLastTime() > 0, YASOLR_LBL_114);
Expand Down
34 changes: 31 additions & 3 deletions src/init/REST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
// debug

webServer
.on("/api/debug", HTTP_GET, [](AsyncWebServerRequest* request) {
.on("/api/debug/router", HTTP_GET, [](AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();

ds18Sys.toJson(root["ds18Sys"].to<JsonObject>());
grid.toJson(root["grid"].to<JsonObject>());
lights.toJson(root["leds"].to<JsonObject>());
relay1.toJson(root["relay1"].to<JsonObject>());
relay2.toJson(root["relay2"].to<JsonObject>());
router.toJson(root["router"].to<JsonObject>(), grid.getVoltage());
zcd.toJson(root["zcd"].to<JsonObject>());

response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));

webServer
.on("/api/debug/system", HTTP_GET, [](AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();

ds18Sys.toJson(root["ds18Sys"].to<JsonObject>());
lights.toJson(root["leds"].to<JsonObject>());

Mycila::TaskMonitor.toJson(root["stack"].to<JsonObject>());
core0TaskManager.toJson(root[core0TaskManager.getName()].to<JsonObject>());
Expand All @@ -37,6 +49,22 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));

webServer
.on("/api/debug", HTTP_GET, [](AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();

String base = "http://";
base.concat(ESPConnect.getIPAddress().toString());
base.concat("/api/debug");

root["router"] = base + "/router";
root["system"] = base + "/system";

response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
// config

webServer
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ void setup() {
initMqttSubscribersTask.forceRun();
initDashboardCards.forceRun();

assert( core0TaskManager.asyncStart(512 * 6, 1, 0, 100, true)); // NOLINT
assert( core1TaskManager.asyncStart(512 * 6, 1, 1, 100, true)); // NOLINT
assert( core0TaskManager.asyncStart(512 * 7, 1, 0, 100, true)); // NOLINT
assert( core1TaskManager.asyncStart(512 * 7, 1, 1, 100, true)); // NOLINT
assert( ioTaskManager.asyncStart(512 * 9, 1, 1, 100, false)); // NOLINT
assert( jsyTaskManager.asyncStart(512 * 4, 5, 0, 100, true)); // NOLINT
assert( pzemTaskManager.asyncStart(512 * 4, 5, 0, 100, true)); // NOLINT
Expand Down

0 comments on commit 3068a30

Please sign in to comment.