From ec50135b9aa2a24576089ad64296ed495c123732 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sun, 14 Apr 2024 21:13:11 +0200 Subject: [PATCH 01/31] convert EPD2 to hal to share SPI with other devices --- patches/GxEPD2_HAL.patch | 391 +++++++++++++++++++++++++ scripts/applyPatches.py | 2 + src/defines.h | 2 +- src/hm/nrfHal.h | 2 - src/plugins/Display/Display_ePaper.cpp | 24 +- src/plugins/Display/Display_ePaper.h | 7 + src/plugins/Display/epdHal.h | 261 +++++++++++++++++ 7 files changed, 674 insertions(+), 15 deletions(-) create mode 100644 patches/GxEPD2_HAL.patch create mode 100644 src/plugins/Display/epdHal.h diff --git a/patches/GxEPD2_HAL.patch b/patches/GxEPD2_HAL.patch new file mode 100644 index 00000000..328ec708 --- /dev/null +++ b/patches/GxEPD2_HAL.patch @@ -0,0 +1,391 @@ +diff --git a/src/GxEPD2_EPD.cpp b/src/GxEPD2_EPD.cpp +index 8df8bef..19b210c 100644 +--- a/src/GxEPD2_EPD.cpp ++++ b/src/GxEPD2_EPD.cpp +@@ -17,11 +17,10 @@ + #include + #endif + +-GxEPD2_EPD::GxEPD2_EPD(int16_t cs, int16_t dc, int16_t rst, int16_t busy, int16_t busy_level, uint32_t busy_timeout, ++GxEPD2_EPD::GxEPD2_EPD(GxEPD2_HalInterface *hal, int16_t busy_level, uint32_t busy_timeout, + uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu) : + WIDTH(w), HEIGHT(h), panel(p), hasColor(c), hasPartialUpdate(pu), hasFastPartialUpdate(fpu), +- _cs(cs), _dc(dc), _rst(rst), _busy(busy), _busy_level(busy_level), _busy_timeout(busy_timeout), _diag_enabled(false), +- _pSPIx(&SPI), _spi_settings(4000000, MSBFIRST, SPI_MODE0) ++ _hal(hal), _busy_level(busy_level), _busy_timeout(busy_timeout), _diag_enabled(false) + { + _initial_write = true; + _initial_refresh = true; +@@ -54,44 +53,10 @@ void GxEPD2_EPD::init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset + Serial.begin(serial_diag_bitrate); + _diag_enabled = true; + } +- if (_cs >= 0) +- { +- digitalWrite(_cs, HIGH); // preset (less glitch for any analyzer) +- pinMode(_cs, OUTPUT); +- digitalWrite(_cs, HIGH); // set (needed e.g. for RP2040) +- } +- if (_dc >= 0) +- { +- digitalWrite(_dc, HIGH); // preset (less glitch for any analyzer) +- pinMode(_dc, OUTPUT); +- digitalWrite(_dc, HIGH); // set (needed e.g. for RP2040) +- } +- _reset(); +- if (_busy >= 0) +- { +- pinMode(_busy, INPUT); +- } +- _pSPIx->begin(); +- if (_busy == MISO) // may be overridden +- { +- pinMode(_busy, INPUT); +- } +- if (_dc == MISO) // may be overridden, TTGO T5 V2.66 +- { +- pinMode(_dc, OUTPUT); +- } +- if (_cs == MISO) // may be overridden +- { +- pinMode(_cs, INPUT); +- } + } + + void GxEPD2_EPD::end() + { +- _pSPIx->end(); +- if (_cs >= 0) pinMode(_cs, INPUT); +- if (_dc >= 0) pinMode(_dc, INPUT); +- if (_rst >= 0) pinMode(_rst, INPUT); + } + + void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void* busy_callback_parameter) +@@ -100,34 +65,27 @@ void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void* + _busy_callback_parameter = busy_callback_parameter; + } + +-void GxEPD2_EPD::selectSPI(SPIClass& spi, SPISettings spi_settings) +-{ +- _pSPIx = &spi; +- _spi_settings = spi_settings; +-} +- + void GxEPD2_EPD::_reset() + { +- if (_rst >= 0) + { + if (_pulldown_rst_mode) + { +- digitalWrite(_rst, LOW); +- pinMode(_rst, OUTPUT); +- digitalWrite(_rst, LOW); ++ _hal->rst(LOW); ++ _hal->rstMode(OUTPUT); ++ _hal->rst(LOW); + delay(_reset_duration); +- pinMode(_rst, INPUT_PULLUP); ++ _hal->rstMode(INPUT_PULLUP); + delay(_reset_duration > 10 ? _reset_duration : 10); + } + else + { +- digitalWrite(_rst, HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, preset (less glitch for any analyzer) +- pinMode(_rst, OUTPUT); +- digitalWrite(_rst, HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, set (needed e.g. for RP2040) ++ _hal->rst(HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, preset (less glitch for any analyzer) ++ _hal->rstMode(OUTPUT); ++ _hal->rst(HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, set (needed e.g. for RP2040) + delay(10); // NEEDED for Waveshare "clever" reset circuit, at least delay(2); +- digitalWrite(_rst, LOW); ++ _hal->rst(LOW); + delay(_reset_duration); +- digitalWrite(_rst, HIGH); ++ _hal->rst(HIGH); + delay(_reset_duration > 10 ? _reset_duration : 10); + } + _hibernating = false; +@@ -136,16 +94,15 @@ void GxEPD2_EPD::_reset() + + void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time) + { +- if (_busy >= 0) + { + delay(1); // add some margin to become active + unsigned long start = micros(); + while (1) + { +- if (digitalRead(_busy) != _busy_level) break; ++ if (_hal->getBusy() != _busy_level) break; + if (_busy_callback) _busy_callback(_busy_callback_parameter); + else delay(1); +- if (digitalRead(_busy) != _busy_level) break; ++ if (_hal->getBusy() != _busy_level) break; + if (micros() - start > _busy_timeout) + { + Serial.println("Busy Timeout!"); +@@ -169,120 +126,59 @@ void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time) + } + (void) start; + } +- else delay(busy_time); + } + + void GxEPD2_EPD::_writeCommand(uint8_t c) + { +- _pSPIx->beginTransaction(_spi_settings); +- if (_dc >= 0) digitalWrite(_dc, LOW); +- if (_cs >= 0) digitalWrite(_cs, LOW); +- _pSPIx->transfer(c); +- if (_cs >= 0) digitalWrite(_cs, HIGH); +- if (_dc >= 0) digitalWrite(_dc, HIGH); +- _pSPIx->endTransaction(); ++ _hal->write(c); + } + + void GxEPD2_EPD::_writeData(uint8_t d) + { +- _pSPIx->beginTransaction(_spi_settings); +- if (_cs >= 0) digitalWrite(_cs, LOW); +- _pSPIx->transfer(d); +- if (_cs >= 0) digitalWrite(_cs, HIGH); +- _pSPIx->endTransaction(); ++ _hal->write(d); + } + + void GxEPD2_EPD::_writeData(const uint8_t* data, uint16_t n) + { +- _pSPIx->beginTransaction(_spi_settings); +- if (_cs >= 0) digitalWrite(_cs, LOW); +- for (uint16_t i = 0; i < n; i++) +- { +- _pSPIx->transfer(*data++); +- } +- if (_cs >= 0) digitalWrite(_cs, HIGH); +- _pSPIx->endTransaction(); ++ _hal->write(data, n); + } + + void GxEPD2_EPD::_writeDataPGM(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes) + { +- _pSPIx->beginTransaction(_spi_settings); +- if (_cs >= 0) digitalWrite(_cs, LOW); +- for (uint16_t i = 0; i < n; i++) +- { +- _pSPIx->transfer(pgm_read_byte(&*data++)); +- } +- while (fill_with_zeroes > 0) +- { +- _pSPIx->transfer(0x00); +- fill_with_zeroes--; +- } +- if (_cs >= 0) digitalWrite(_cs, HIGH); +- _pSPIx->endTransaction(); ++ _hal->write(data, n, fill_with_zeroes); + } + + void GxEPD2_EPD::_writeDataPGM_sCS(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes) + { +- _pSPIx->beginTransaction(_spi_settings); +- for (uint8_t i = 0; i < n; i++) +- { +- if (_cs >= 0) digitalWrite(_cs, LOW); +- _pSPIx->transfer(pgm_read_byte(&*data++)); +- if (_cs >= 0) digitalWrite(_cs, HIGH); +- } +- while (fill_with_zeroes > 0) +- { +- if (_cs >= 0) digitalWrite(_cs, LOW); +- _pSPIx->transfer(0x00); +- fill_with_zeroes--; +- if (_cs >= 0) digitalWrite(_cs, HIGH); ++ _hal->write(data, n); ++ if (fill_with_zeroes > 0) { ++ uint8_t buf[fill_with_zeroes]; ++ memset(buf, 0, fill_with_zeroes); ++ _hal->write(buf, fill_with_zeroes); + } +- _pSPIx->endTransaction(); + } + + void GxEPD2_EPD::_writeCommandData(const uint8_t* pCommandData, uint8_t datalen) + { +- _pSPIx->beginTransaction(_spi_settings); +- if (_dc >= 0) digitalWrite(_dc, LOW); +- if (_cs >= 0) digitalWrite(_cs, LOW); +- _pSPIx->transfer(*pCommandData++); +- if (_dc >= 0) digitalWrite(_dc, HIGH); +- for (uint8_t i = 0; i < datalen - 1; i++) // sub the command +- { +- _pSPIx->transfer(*pCommandData++); +- } +- if (_cs >= 0) digitalWrite(_cs, HIGH); +- _pSPIx->endTransaction(); ++ _hal->writeCmd(pCommandData, datalen, false); + } + + void GxEPD2_EPD::_writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen) + { +- _pSPIx->beginTransaction(_spi_settings); +- if (_dc >= 0) digitalWrite(_dc, LOW); +- if (_cs >= 0) digitalWrite(_cs, LOW); +- _pSPIx->transfer(pgm_read_byte(&*pCommandData++)); +- if (_dc >= 0) digitalWrite(_dc, HIGH); +- for (uint8_t i = 0; i < datalen - 1; i++) // sub the command +- { +- _pSPIx->transfer(pgm_read_byte(&*pCommandData++)); +- } +- if (_cs >= 0) digitalWrite(_cs, HIGH); +- _pSPIx->endTransaction(); ++ _hal->writeCmd(pCommandData, datalen, true); + } + + void GxEPD2_EPD::_startTransfer() + { +- _pSPIx->beginTransaction(_spi_settings); +- if (_cs >= 0) digitalWrite(_cs, LOW); ++ _hal->startTransfer(); + } + + void GxEPD2_EPD::_transfer(uint8_t value) + { +- _pSPIx->transfer(value); ++ _hal->transfer(value); + } + + void GxEPD2_EPD::_endTransfer() + { +- if (_cs >= 0) digitalWrite(_cs, HIGH); +- _pSPIx->endTransaction(); ++ _hal->endTransfer(); + } +diff --git a/src/GxEPD2_EPD.h b/src/GxEPD2_EPD.h +index 34c1145..1e8ea64 100644 +--- a/src/GxEPD2_EPD.h ++++ b/src/GxEPD2_EPD.h +@@ -13,9 +13,9 @@ + #define _GxEPD2_EPD_H_ + + #include +-#include + + #include ++#include + + #pragma GCC diagnostic ignored "-Wunused-parameter" + //#pragma GCC diagnostic ignored "-Wsign-compare" +@@ -31,7 +31,7 @@ class GxEPD2_EPD + const bool hasPartialUpdate; + const bool hasFastPartialUpdate; + // constructor +- GxEPD2_EPD(int16_t cs, int16_t dc, int16_t rst, int16_t busy, int16_t busy_level, uint32_t busy_timeout, ++ GxEPD2_EPD(GxEPD2_HalInterface *hal, int16_t busy_level, uint32_t busy_timeout, + uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu); + virtual void init(uint32_t serial_diag_bitrate = 0); // serial_diag_bitrate = 0 : disabled + virtual void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 10, bool pulldown_rst_mode = false); +@@ -97,7 +97,6 @@ class GxEPD2_EPD + { + return (a > b ? a : b); + }; +- void selectSPI(SPIClass& spi, SPISettings spi_settings); + protected: + void _reset(); + void _waitWhileBusy(const char* comment = 0, uint16_t busy_time = 5000); +@@ -112,16 +111,15 @@ class GxEPD2_EPD + void _transfer(uint8_t value); + void _endTransfer(); + protected: +- int16_t _cs, _dc, _rst, _busy, _busy_level; ++ GxEPD2_HalInterface *_hal; ++ int16_t _busy_level; + uint32_t _busy_timeout; + bool _diag_enabled, _pulldown_rst_mode; +- SPIClass* _pSPIx; +- SPISettings _spi_settings; + bool _initial_write, _initial_refresh; + bool _power_is_on, _using_partial_mode, _hibernating; + bool _init_display_done; + uint16_t _reset_duration; +- void (*_busy_callback)(const void*); ++ void (*_busy_callback)(const void*); + const void* _busy_callback_parameter; + }; + +diff --git a/src/GxEPD2_Hal.h b/src/GxEPD2_Hal.h +new file mode 100644 +index 0000000..cb8fb9d +--- /dev/null ++++ b/src/GxEPD2_Hal.h +@@ -0,0 +1,18 @@ ++#pragma once ++ ++class GxEPD2_HalInterface { ++ public: ++ virtual void rstMode(uint8_t mode) = 0; ++ virtual void rst(bool level) = 0; ++ virtual int getBusy(void) = 0; ++ virtual bool isRst(void) = 0; ++ ++ virtual void write(uint8_t buf) = 0; ++ virtual void write(const uint8_t *buf, uint16_t n) = 0; ++ virtual void write(const uint8_t *buf, uint16_t n, int16_t fill_with_zeroes) = 0; ++ virtual void writeCmd(const uint8_t* pCommandData, uint8_t datalen, bool isPGM) = 0; ++ ++ virtual void startTransfer(void) = 0; ++ virtual void endTransfer(void) = 0; ++ virtual void transfer(const uint8_t val) = 0; ++}; +diff --git a/src/epd/GxEPD2_150_BN.cpp b/src/epd/GxEPD2_150_BN.cpp +index bfb3ddf..dba3d78 100644 +--- a/src/epd/GxEPD2_150_BN.cpp ++++ b/src/epd/GxEPD2_150_BN.cpp +@@ -14,8 +14,8 @@ + + #include "GxEPD2_150_BN.h" + +-GxEPD2_150_BN::GxEPD2_150_BN(int16_t cs, int16_t dc, int16_t rst, int16_t busy) : +- GxEPD2_EPD(cs, dc, rst, busy, HIGH, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate) ++GxEPD2_150_BN::GxEPD2_150_BN(GxEPD2_HalInterface *hal) : ++ GxEPD2_EPD(hal, HIGH, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate) + { + } + +@@ -269,7 +269,7 @@ void GxEPD2_150_BN::refresh(int16_t x, int16_t y, int16_t w, int16_t h) + int16_t y1 = y < 0 ? 0 : y; // limit + w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit + h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit +- if ((w1 <= 0) || (h1 <= 0)) return; ++ if ((w1 <= 0) || (h1 <= 0)) return; + // make x1, w1 multiple of 8 + w1 += x1 % 8; + if (w1 % 8 > 0) w1 += 8 - w1 % 8; +@@ -287,7 +287,7 @@ void GxEPD2_150_BN::powerOff() + void GxEPD2_150_BN::hibernate() + { + _PowerOff(); +- if (_rst >= 0) ++ if (_hal->isRst()) + { + _writeCommand(0x10); // deep sleep mode + _writeData(0x1); // enter deep sleep +diff --git a/src/epd/GxEPD2_150_BN.h b/src/epd/GxEPD2_150_BN.h +index bc46a45..954b9c4 100644 +--- a/src/epd/GxEPD2_150_BN.h ++++ b/src/epd/GxEPD2_150_BN.h +@@ -16,6 +16,7 @@ + #define _GxEPD2_150_BN_H_ + + #include "../GxEPD2_EPD.h" ++#include "../GxEPD2_Hal.h" + + class GxEPD2_150_BN : public GxEPD2_EPD + { +@@ -33,7 +34,7 @@ class GxEPD2_150_BN : public GxEPD2_EPD + static const uint16_t full_refresh_time = 4000; // ms, e.g. 3825000us + static const uint16_t partial_refresh_time = 800; // ms, e.g. 736000us + // constructor +- GxEPD2_150_BN(int16_t cs, int16_t dc, int16_t rst, int16_t busy); ++ GxEPD2_150_BN(GxEPD2_HalInterface *hal); + // methods (virtual) + // Support for Bitmaps (Sprites) to Controller Buffer and to Screen + void clearScreen(uint8_t value = 0xFF); // init controller memory and screen (default white) diff --git a/scripts/applyPatches.py b/scripts/applyPatches.py index 91b3498c..3c38c100 100644 --- a/scripts/applyPatches.py +++ b/scripts/applyPatches.py @@ -30,6 +30,8 @@ applyPatch("ESPAsyncWebServer-esphome", "../patches/AsyncWeb_Prometheus.patch") if env['PIOENV'][:13] == "opendtufusion": applyPatch("GxEPD2", "../patches/GxEPD2_SW_SPI.patch") +elif env['PIOENV'][:5] == "esp32": + applyPatch("GxEPD2", "../patches/GxEPD2_HAL.patch") if (env['PIOENV'][:13] == "opendtufusion") or (env['PIOENV'][:5] == "esp32"): applyPatch("RF24", "../patches/RF24_Hal.patch") diff --git a/src/defines.h b/src/defines.h index e7eecc00..cab8676d 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 110 +#define VERSION_PATCH 111 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/hm/nrfHal.h b/src/hm/nrfHal.h index 89fe0885..e1f25439 100644 --- a/src/hm/nrfHal.h +++ b/src/hm/nrfHal.h @@ -9,7 +9,6 @@ #pragma once #include "../utils/spiPatcher.h" - #include #include @@ -79,7 +78,6 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { gpio_set_level(mPinEn, 0); } - bool begin() override { return true; } diff --git a/src/plugins/Display/Display_ePaper.cpp b/src/plugins/Display/Display_ePaper.cpp index 1b52703e..86bb0d32 100644 --- a/src/plugins/Display/Display_ePaper.cpp +++ b/src/plugins/Display/Display_ePaper.cpp @@ -1,17 +1,12 @@ #include "Display_ePaper.h" -#ifdef ESP8266 -#include -#elif defined(ESP32) +#if defined(ESP32) #include -#endif #include "../../utils/helper.h" #include "imagedata.h" #include "defines.h" #include "../plugin_lang.h" -#if defined(ESP32) - static const uint32_t spiClk = 4000000; // 4 MHz #if defined(ESP32) && defined(USE_HSPI_FOR_EPD) @@ -34,13 +29,18 @@ void DisplayEPaper::init(uint8_t type, uint8_t _CS, uint8_t _DC, uint8_t _RST, u if (DISP_TYPE_T10_EPAPER == type) { Serial.begin(115200); - _display = new GxEPD2_BW(GxEPD2_150_BN(_CS, _DC, _RST, _BUSY)); -#if defined(ESP32) && defined(USE_HSPI_FOR_EPD) - hspi.begin(_SCK, _BUSY, _MOSI, _CS); - _display->epd2.selectSPI(hspi, SPISettings(spiClk, MSBFIRST, SPI_MODE0)); -#elif defined(ESP32) && defined(PLUGIN_DISPLAY) - _display->epd2.init(_SCK, _MOSI, 115200, true, 20, false); +#if defined(SPI_HAL) + hal.init(_MOSI, _DC, _SCK, _CS, _RST, _BUSY); + _display = new GxEPD2_BW(GxEPD2_150_BN(&hal)); +#else + _display = new GxEPD2_BW(GxEPD2_150_BN(_CS, _DC, _RST, _BUSY)); + #if defined(USE_HSPI_FOR_EPD) + hspi.begin(_SCK, _BUSY, _MOSI, _CS); + _display->epd2.selectSPI(hspi, SPISettings(spiClk, MSBFIRST, SPI_MODE0)); + #elif defined(PLUGIN_DISPLAY) + _display->epd2.init(_SCK, _MOSI, 115200, true, 20, false); + #endif #endif _display->init(115200, true, 20, false); _display->setRotation(mDisplayRotation); diff --git a/src/plugins/Display/Display_ePaper.h b/src/plugins/Display/Display_ePaper.h index c26d3b42..4fbb0959 100644 --- a/src/plugins/Display/Display_ePaper.h +++ b/src/plugins/Display/Display_ePaper.h @@ -12,7 +12,11 @@ #define EPAPER_MAX_TEXT_LEN 35 #include +#if defined(SPI_HAL) +#include "epdHal.h" +#else #include +#endif // FreeFonts from Adafruit_GFX #include @@ -60,6 +64,9 @@ class DisplayEPaper { const char* _version; RefreshStatus mRefreshState, mNextRefreshState; uint8_t mSecondCnt; + #if defined(SPI_HAL) + epdHal hal; + #endif }; #endif // ESP32 diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h new file mode 100644 index 00000000..3f92498c --- /dev/null +++ b/src/plugins/Display/epdHal.h @@ -0,0 +1,261 @@ +//----------------------------------------------------------------------------- +// 2024 Ahoy, https://github.com/lumpapu/ahoy +// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/4.0/deed +//----------------------------------------------------------------------------- + +#ifndef __EPD_HAL_H__ +#define __EPD_HAL_H__ + +#pragma once +#include "../../utils/spiPatcher.h" +#include +#include + +#define EPD_DEFAULT_SPI_SPEED 4000000 // 4 MHz + +class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { + public: + epdHal() { + mSpiPatcher = SpiPatcher::getInstance(SPI2_HOST); + } + + void patch() override { + esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false); + //esp_rom_gpio_connect_in_signal(mPinDc, spi_periph_signal[mHostDevice].spiq_in, false); + esp_rom_gpio_connect_out_signal(mPinClk, spi_periph_signal[mHostDevice].spiclk_out, false, false); + } + + void unpatch() override { + esp_rom_gpio_connect_out_signal(mPinMosi, SIG_GPIO_OUT_IDX, false, false); + //esp_rom_gpio_connect_in_signal(mPinDc, GPIO_MATRIX_CONST_ZERO_INPUT, false); + esp_rom_gpio_connect_out_signal(mPinClk, SIG_GPIO_OUT_IDX, false, false); + } + + void init(int8_t mosi, int8_t dc, int8_t sclk, int8_t cs, int8_t rst, int8_t busy, int32_t speed = EPD_DEFAULT_SPI_SPEED) { + mPinMosi = static_cast(mosi); + mPinDc = static_cast(dc); + mPinClk = static_cast(sclk); + mPinCs = static_cast(cs); + mPinRst = static_cast(rst); + mPinBusy = static_cast(busy); + mSpiSpeed = speed; + + mHostDevice = mSpiPatcher->getDevice(); + + gpio_reset_pin(mPinMosi); + gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT); + gpio_set_level(mPinMosi, 1); + + gpio_reset_pin(mPinClk); + gpio_set_direction(mPinClk, GPIO_MODE_OUTPUT); + gpio_set_level(mPinClk, 0); + + gpio_reset_pin(mPinCs); + spi_device_interface_config_t devcfg = { + .command_bits = 0, + .address_bits = 0, + .dummy_bits = 0, + .mode = 0, + .duty_cycle_pos = 0, + .cs_ena_pretrans = 0, + .cs_ena_posttrans = 0, + .clock_speed_hz = mSpiSpeed, + .input_delay_ns = 0, + .spics_io_num = mPinCs, + .flags = 0, + .queue_size = 1, + .pre_cb = nullptr, + .post_cb = nullptr + }; + ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg, &spi)); + + if(GPIO_NUM_NC != mPinRst) { + gpio_reset_pin(mPinRst); + gpio_set_direction(mPinRst, GPIO_MODE_OUTPUT); + gpio_set_level(mPinRst, LOW); + } + + gpio_reset_pin(mPinDc); + gpio_set_direction(mPinDc, GPIO_MODE_OUTPUT); + gpio_set_level(mPinDc, HIGH); + + gpio_reset_pin(mPinBusy); + gpio_set_direction(mPinBusy, GPIO_MODE_INPUT); + } + + void rstMode(uint8_t mode) override { + if(GPIO_NUM_NC != mPinRst) + gpio_set_direction(mPinRst, static_cast(mode)); + } + + void rst(bool level) override { + if(GPIO_NUM_NC != mPinRst) + gpio_set_level(mPinRst, level); + } + + int getBusy(void) override { + return gpio_get_level(mPinBusy); + } + + bool isRst(void) override { + return (GPIO_NUM_NC != mPinRst); + } + + void write(uint8_t buf) override { + uint8_t data[1]; + data[0] = buf; + request_spi(); + + spi_transaction_t t = { + .flags = 0, + .cmd = 0, + .addr = 0, + .length = static_cast(1u) << 3, + .rxlength = 0, + .user = NULL, + .tx_buffer = data, + .rx_buffer = NULL + }; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); + + release_spi(); + } + + void write(const uint8_t *buf, uint16_t n) override { + uint8_t data[n]; + std::copy(&buf[0], &buf[n], &data[0]); + + request_spi(); + + spi_transaction_t t = { + .flags = 0, + .cmd = 0, + .addr = 0, + .length = static_cast(n) << 3, + .rxlength = 0, + .user = NULL, + .tx_buffer = data, + .rx_buffer = NULL + }; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); + + release_spi(); + } + + void write(const uint8_t *buf, uint16_t n, int16_t fill_with_zeroes) override { + uint8_t data[n + fill_with_zeroes]; + memset(data, 0, (n + fill_with_zeroes)); + for (uint16_t i = 0; i < n; i++) { + data[i] = pgm_read_byte(&*buf++); + } + + request_spi(); + + spi_transaction_t t = { + .flags = 0, + .cmd = 0, + .addr = 0, + .length = static_cast(n + fill_with_zeroes) << 3, + .rxlength = 0, + .user = NULL, + .tx_buffer = data, + .rx_buffer = NULL + }; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); + + release_spi(); + } + + void writeCmd(const uint8_t *buf, uint8_t n, bool isPGM) override { + uint8_t data[n-1]; + data[0] = (isPGM) ? pgm_read_byte(&*buf++) : buf[0]; + + request_spi(); + gpio_set_level(mPinDc, LOW); + + spi_transaction_t t = { + .flags = 0, + .cmd = 0, + .addr = 0, + .length = static_cast(1u) << 3, + .rxlength = 0, + .user = NULL, + .tx_buffer = data, + .rx_buffer = NULL + }; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); + gpio_set_level(mPinDc, HIGH); + + if(isPGM) { + for (uint16_t i = 0; i < n; i++) { + data[i] = pgm_read_byte(&*buf++); + } + } else + std::copy(&buf[1], &buf[n], &data[0]); + + spi_transaction_t t1 = { + .flags = 0, + .cmd = 0, + .addr = 0, + .length = static_cast(n) << 3, + .rxlength = 0, + .user = NULL, + .tx_buffer = data, + .rx_buffer = NULL + }; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t1)); + + release_spi(); + } + + void startTransfer(void) override { + request_spi(); + gpio_set_level(mPinDc, LOW); + } + + void endTransfer(void) override { + gpio_set_level(mPinDc, HIGH); + release_spi(); + } + + void transfer(const uint8_t val) override { + uint8_t data[1]; + data[0] = val; + + spi_transaction_t t = { + .flags = 0, + .cmd = 0, + .addr = 0, + .length = static_cast(1u) << 3, + .rxlength = 0, + .user = NULL, + .tx_buffer = data, + .rx_buffer = NULL + }; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); + } + + private: + inline void request_spi() { + mSpiPatcher->request(this); + } + + inline void release_spi() { + mSpiPatcher->release(); + } + + private: + gpio_num_t mPinMosi = GPIO_NUM_NC; + gpio_num_t mPinDc = GPIO_NUM_NC; + gpio_num_t mPinClk = GPIO_NUM_NC; + gpio_num_t mPinCs = GPIO_NUM_NC; + gpio_num_t mPinRst = GPIO_NUM_NC; + gpio_num_t mPinBusy = GPIO_NUM_NC; + int32_t mSpiSpeed = EPD_DEFAULT_SPI_SPEED; + + spi_host_device_t mHostDevice; + spi_device_handle_t spi; + SpiPatcher *mSpiPatcher; +}; + +#endif /*__EPD_HAL_H__*/ From c3f2c8bbe87d09cb9094ebe3c80fa0b5c7c340cd Mon Sep 17 00:00:00 2001 From: lumapu Date: Mon, 15 Apr 2024 00:21:45 +0200 Subject: [PATCH 02/31] some fixes, display does not work but no boot loops any more --- src/plugins/Display/epdHal.h | 78 +++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index 3f92498c..ab9a17f8 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -106,15 +106,16 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { data[0] = buf; request_spi(); + size_t spiLen = static_cast(1u) << 3; spi_transaction_t t = { .flags = 0, .cmd = 0, .addr = 0, - .length = static_cast(1u) << 3, - .rxlength = 0, + .length = spiLen, + .rxlength = spiLen, .user = NULL, .tx_buffer = data, - .rx_buffer = NULL + .rx_buffer = data }; ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); @@ -127,15 +128,16 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { request_spi(); + size_t spiLen = static_cast(n) << 3; spi_transaction_t t = { .flags = 0, .cmd = 0, .addr = 0, - .length = static_cast(n) << 3, - .rxlength = 0, + .length = spiLen, + .rxlength = spiLen, .user = NULL, .tx_buffer = data, - .rx_buffer = NULL + .rx_buffer = data }; ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); @@ -151,17 +153,52 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { request_spi(); + size_t spiLen = static_cast(n + fill_with_zeroes) << 3; + spi_transaction_t t = { + .flags = SPI_TRANS_CS_KEEP_ACTIVE, + .cmd = 0, + .addr = 0, + .length = spiLen, + .rxlength = spiLen, + .user = NULL, + .tx_buffer = data, + .rx_buffer = data + }; + + size_t offs = 0; + spi_device_acquire_bus(spi, portMAX_DELAY); + while(offs < (n + fill_with_zeroes)) { + t.length = (64u << 3); + t.rxlength = t.length; + t.tx_buffer = &data[offs]; + offs += 64; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); + } + spi_device_release_bus(spi); + + release_spi(); + } + + void writeCmd(const uint8_t val) override { + uint8_t data[1]; + data[0] = val; + + request_spi(); + gpio_set_level(mPinDc, LOW); + + size_t spiLen = static_cast(1u) << 3; spi_transaction_t t = { .flags = 0, .cmd = 0, .addr = 0, - .length = static_cast(n + fill_with_zeroes) << 3, - .rxlength = 0, + .length = spiLen, + .rxlength = spiLen, .user = NULL, .tx_buffer = data, - .rx_buffer = NULL + .rx_buffer = data }; ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); + gpio_set_level(mPinDc, HIGH); release_spi(); } @@ -173,15 +210,16 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { request_spi(); gpio_set_level(mPinDc, LOW); + size_t spiLen = static_cast(1u) << 3; spi_transaction_t t = { .flags = 0, .cmd = 0, .addr = 0, - .length = static_cast(1u) << 3, - .rxlength = 0, + .length = spiLen, + .rxlength = spiLen, .user = NULL, .tx_buffer = data, - .rx_buffer = NULL + .rx_buffer = data }; ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); gpio_set_level(mPinDc, HIGH); @@ -193,15 +231,16 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { } else std::copy(&buf[1], &buf[n], &data[0]); + spiLen = static_cast(n) << 3; spi_transaction_t t1 = { .flags = 0, .cmd = 0, .addr = 0, - .length = static_cast(n) << 3, - .rxlength = 0, + .length = spiLen, + .rxlength = spiLen, .user = NULL, .tx_buffer = data, - .rx_buffer = NULL + .rx_buffer = data }; ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t1)); @@ -210,11 +249,9 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { void startTransfer(void) override { request_spi(); - gpio_set_level(mPinDc, LOW); } void endTransfer(void) override { - gpio_set_level(mPinDc, HIGH); release_spi(); } @@ -222,15 +259,16 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { uint8_t data[1]; data[0] = val; + size_t spiLen = static_cast(1u) << 3; spi_transaction_t t = { .flags = 0, .cmd = 0, .addr = 0, - .length = static_cast(1u) << 3, - .rxlength = 0, + .length = spiLen, + .rxlength = spiLen, .user = NULL, .tx_buffer = data, - .rx_buffer = NULL + .rx_buffer = data }; ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); } From 0683d1d2f51b6cf843ce5c9b00bc2828cefa5df2 Mon Sep 17 00:00:00 2001 From: lumapu Date: Mon, 15 Apr 2024 00:25:54 +0200 Subject: [PATCH 03/31] update patch --- patches/GxEPD2_HAL.patch | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/patches/GxEPD2_HAL.patch b/patches/GxEPD2_HAL.patch index 328ec708..d7b394eb 100644 --- a/patches/GxEPD2_HAL.patch +++ b/patches/GxEPD2_HAL.patch @@ -1,5 +1,5 @@ diff --git a/src/GxEPD2_EPD.cpp b/src/GxEPD2_EPD.cpp -index 8df8bef..19b210c 100644 +index 8df8bef..e9dfb19 100644 --- a/src/GxEPD2_EPD.cpp +++ b/src/GxEPD2_EPD.cpp @@ -17,11 +17,10 @@ @@ -140,7 +140,7 @@ index 8df8bef..19b210c 100644 - if (_cs >= 0) digitalWrite(_cs, HIGH); - if (_dc >= 0) digitalWrite(_dc, HIGH); - _pSPIx->endTransaction(); -+ _hal->write(c); ++ _hal->writeCmd(c); } void GxEPD2_EPD::_writeData(uint8_t d) @@ -313,10 +313,10 @@ index 34c1145..1e8ea64 100644 diff --git a/src/GxEPD2_Hal.h b/src/GxEPD2_Hal.h new file mode 100644 -index 0000000..cb8fb9d +index 0000000..13424b6 --- /dev/null +++ b/src/GxEPD2_Hal.h -@@ -0,0 +1,18 @@ +@@ -0,0 +1,19 @@ +#pragma once + +class GxEPD2_HalInterface { @@ -329,6 +329,7 @@ index 0000000..cb8fb9d + virtual void write(uint8_t buf) = 0; + virtual void write(const uint8_t *buf, uint16_t n) = 0; + virtual void write(const uint8_t *buf, uint16_t n, int16_t fill_with_zeroes) = 0; ++ virtual void writeCmd(const uint8_t val) = 0; + virtual void writeCmd(const uint8_t* pCommandData, uint8_t datalen, bool isPGM) = 0; + + virtual void startTransfer(void) = 0; From c9fc207afe9b57b4e33d0fa3dc318ce5cfc0c3e9 Mon Sep 17 00:00:00 2001 From: lumapu Date: Mon, 15 Apr 2024 22:54:33 +0200 Subject: [PATCH 04/31] fixed writeCmd, wdt fires --- src/plugins/Display/epdHal.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index ab9a17f8..7f065704 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -209,10 +209,11 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { request_spi(); gpio_set_level(mPinDc, LOW); + spi_device_acquire_bus(spi, portMAX_DELAY); size_t spiLen = static_cast(1u) << 3; spi_transaction_t t = { - .flags = 0, + .flags = SPI_TRANS_CS_KEEP_ACTIVE, .cmd = 0, .addr = 0, .length = spiLen, @@ -231,9 +232,9 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { } else std::copy(&buf[1], &buf[n], &data[0]); - spiLen = static_cast(n) << 3; + spiLen = static_cast(n-1) << 3; spi_transaction_t t1 = { - .flags = 0, + .flags = SPI_TRANS_CS_KEEP_ACTIVE, .cmd = 0, .addr = 0, .length = spiLen, @@ -243,6 +244,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { .rx_buffer = data }; ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t1)); + spi_device_release_bus(spi); release_spi(); } From 26d1bc400d931f80c99a03d61773d5c61e193597 Mon Sep 17 00:00:00 2001 From: lumapu Date: Mon, 15 Apr 2024 23:54:01 +0200 Subject: [PATCH 05/31] removed boot loop, busy has to be MISO --- src/plugins/Display/epdHal.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index 7f065704..01cb4324 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -21,13 +21,13 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { void patch() override { esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false); - //esp_rom_gpio_connect_in_signal(mPinDc, spi_periph_signal[mHostDevice].spiq_in, false); + esp_rom_gpio_connect_in_signal(mPinBusy, spi_periph_signal[mHostDevice].spiq_in, false); esp_rom_gpio_connect_out_signal(mPinClk, spi_periph_signal[mHostDevice].spiclk_out, false, false); } void unpatch() override { esp_rom_gpio_connect_out_signal(mPinMosi, SIG_GPIO_OUT_IDX, false, false); - //esp_rom_gpio_connect_in_signal(mPinDc, GPIO_MATRIX_CONST_ZERO_INPUT, false); + esp_rom_gpio_connect_in_signal(mPinBusy, GPIO_MATRIX_CONST_ZERO_INPUT, false); esp_rom_gpio_connect_out_signal(mPinClk, SIG_GPIO_OUT_IDX, false, false); } @@ -79,8 +79,8 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { gpio_set_direction(mPinDc, GPIO_MODE_OUTPUT); gpio_set_level(mPinDc, HIGH); - gpio_reset_pin(mPinBusy); - gpio_set_direction(mPinBusy, GPIO_MODE_INPUT); + //gpio_reset_pin(mPinBusy); + //gpio_set_direction(mPinBusy, GPIO_MODE_INPUT); } void rstMode(uint8_t mode) override { From 708160b459fcf7ad820c43530bd55914e4c098bc Mon Sep 17 00:00:00 2001 From: lumapu Date: Tue, 16 Apr 2024 00:11:19 +0200 Subject: [PATCH 06/31] display functional if CMT isn't active --- src/plugins/Display/epdHal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index 01cb4324..29948059 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -16,7 +16,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { public: epdHal() { - mSpiPatcher = SpiPatcher::getInstance(SPI2_HOST); + mSpiPatcher = SpiPatcher::getInstance(SPI3_HOST); } void patch() override { @@ -72,7 +72,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { if(GPIO_NUM_NC != mPinRst) { gpio_reset_pin(mPinRst); gpio_set_direction(mPinRst, GPIO_MODE_OUTPUT); - gpio_set_level(mPinRst, LOW); + gpio_set_level(mPinRst, HIGH); } gpio_reset_pin(mPinDc); From ede4fba9973a09553e54a1423e5d891bdf9981d2 Mon Sep 17 00:00:00 2001 From: lumapu Date: Wed, 17 Apr 2024 21:45:33 +0200 Subject: [PATCH 07/31] try to solve issue without success --- src/hm/nrfHal.h | 2 ++ src/hms/cmtHal.h | 2 ++ src/plugins/Display/epdHal.h | 8 ++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/hm/nrfHal.h b/src/hm/nrfHal.h index e1f25439..4bd14763 100644 --- a/src/hm/nrfHal.h +++ b/src/hm/nrfHal.h @@ -55,6 +55,7 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { gpio_set_level(mPinClk, 0); gpio_reset_pin(mPinCs); + request_spi(); spi_device_interface_config_t devcfg = { .command_bits = 0, .address_bits = 0, @@ -72,6 +73,7 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { .post_cb = nullptr }; ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg, &spi)); + release_spi(); gpio_reset_pin(mPinEn); gpio_set_direction(mPinEn, GPIO_MODE_OUTPUT); diff --git a/src/hms/cmtHal.h b/src/hms/cmtHal.h index a4bec587..676b0b71 100644 --- a/src/hms/cmtHal.h +++ b/src/hms/cmtHal.h @@ -50,6 +50,7 @@ class cmtHal : public SpiPatcherHandle { gpio_set_level(mPinClk, 0); gpio_reset_pin(mPinCs); + request_spi(); spi_device_interface_config_t devcfg_reg = { .command_bits = 1, .address_bits = 7, @@ -67,6 +68,7 @@ class cmtHal : public SpiPatcherHandle { .post_cb = nullptr }; ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg_reg, &spi_reg)); + release_spi(); gpio_reset_pin(mPinFcs); spi_device_interface_config_t devcfg_fifo = { diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index 29948059..1219f107 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -51,6 +51,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { gpio_set_level(mPinClk, 0); gpio_reset_pin(mPinCs); + request_spi(); spi_device_interface_config_t devcfg = { .command_bits = 0, .address_bits = 0, @@ -68,6 +69,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { .post_cb = nullptr }; ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg, &spi)); + release_spi(); if(GPIO_NUM_NC != mPinRst) { gpio_reset_pin(mPinRst); @@ -152,14 +154,12 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { } request_spi(); - - size_t spiLen = static_cast(n + fill_with_zeroes) << 3; spi_transaction_t t = { .flags = SPI_TRANS_CS_KEEP_ACTIVE, .cmd = 0, .addr = 0, - .length = spiLen, - .rxlength = spiLen, + .length = 1u, + .rxlength = 1u, .user = NULL, .tx_buffer = data, .rx_buffer = data From 0afe0d7fc99161c292da70a2537f421fbbc2d379 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 18 Apr 2024 23:04:28 +0200 Subject: [PATCH 08/31] fix SPI topics, not completely tested --- .gitattributes | 1 + src/hm/nrfHal.h | 9 ++++----- src/hms/cmtHal.h | 9 ++++----- src/network/AhoyEthernetSpi.h | 27 +++++++------------------- src/plugins/Display/epdHal.h | 9 ++++----- src/utils/dbg.h | 2 +- src/utils/spiPatcher.cpp | 3 ++- src/utils/spiPatcher.h | 36 +++++++++++++++++++++++++---------- 8 files changed, 49 insertions(+), 47 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..3041f8dc --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +patches/GxEPD2_HAL.patch eol=crlf \ No newline at end of file diff --git a/src/hm/nrfHal.h b/src/hm/nrfHal.h index 4bd14763..a9bd89d2 100644 --- a/src/hm/nrfHal.h +++ b/src/hm/nrfHal.h @@ -17,9 +17,7 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { public: - nrfHal() { - mSpiPatcher = SpiPatcher::getInstance(SPI2_HOST); - } + nrfHal() {} void patch() override { esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false); @@ -41,7 +39,8 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { mPinEn = static_cast(en); mSpiSpeed = speed; - mHostDevice = mSpiPatcher->getDevice(); + mHostDevice = (14 == sclk) ? SPI2_HOST : SPI3_HOST; + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); gpio_reset_pin(mPinMosi); gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT); @@ -72,7 +71,7 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { .pre_cb = nullptr, .post_cb = nullptr }; - ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg, &spi)); + mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi); release_spi(); gpio_reset_pin(mPinEn); diff --git a/src/hms/cmtHal.h b/src/hms/cmtHal.h index 676b0b71..58f79da4 100644 --- a/src/hms/cmtHal.h +++ b/src/hms/cmtHal.h @@ -16,9 +16,7 @@ class cmtHal : public SpiPatcherHandle { public: - cmtHal() { - mSpiPatcher = SpiPatcher::getInstance(DEF_CMT_SPI_HOST); - } + cmtHal() {} void patch() override { esp_rom_gpio_connect_out_signal(mPinSdio, spi_periph_signal[mHostDevice].spid_out, false, false); @@ -39,7 +37,8 @@ class cmtHal : public SpiPatcherHandle { mPinFcs = static_cast(fcs); mSpiSpeed = speed; - mHostDevice = mSpiPatcher->getDevice(); + mHostDevice = (14 == clk) ? SPI2_HOST : SPI3_HOST; + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); gpio_reset_pin(mPinSdio); gpio_set_direction(mPinSdio, GPIO_MODE_INPUT_OUTPUT); @@ -67,7 +66,7 @@ class cmtHal : public SpiPatcherHandle { .pre_cb = nullptr, .post_cb = nullptr }; - ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg_reg, &spi_reg)); + mSpiPatcher->addDevice(mHostDevice, &devcfg_reg, &spi_reg); release_spi(); gpio_reset_pin(mPinFcs); diff --git a/src/network/AhoyEthernetSpi.h b/src/network/AhoyEthernetSpi.h index b41431b4..7b5bac85 100644 --- a/src/network/AhoyEthernetSpi.h +++ b/src/network/AhoyEthernetSpi.h @@ -12,7 +12,7 @@ #include #include #include -#include +#include "../utils/spiPatcher.h" // Functions from WiFiGeneric void tcpipInit(); @@ -44,23 +44,8 @@ class AhoyEthernetSpi { gpio_reset_pin(static_cast(pin_int)); gpio_set_pull_mode(static_cast(pin_int), GPIO_PULLUP_ONLY); - - spi_bus_config_t buscfg = { - .mosi_io_num = pin_mosi, - .miso_io_num = pin_miso, - .sclk_io_num = pin_sclk, - .quadwp_io_num = -1, - .quadhd_io_num = -1, - .data4_io_num = -1, - .data5_io_num = -1, - .data6_io_num = -1, - .data7_io_num = -1, - .max_transfer_sz = 0, // uses default value internally - .flags = 0, - .intr_flags = 0 - }; - - ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO)); + mHostDevice = (14 == pin_sclk) ? SPI2_HOST : SPI3_HOST; + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); spi_device_interface_config_t devcfg = { .command_bits = 16, // actually address phase @@ -79,8 +64,7 @@ class AhoyEthernetSpi { .post_cb = nullptr }; - spi_device_handle_t spi; - ESP_ERROR_CHECK(spi_bus_add_device(SPI3_HOST, &devcfg, &spi)); + mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi); // Reset sequence if(-1 != pin_rst) { @@ -137,6 +121,9 @@ class AhoyEthernetSpi { private: esp_eth_handle_t eth_handle; esp_netif_t *eth_netif; + spi_host_device_t mHostDevice; + spi_device_handle_t spi; + SpiPatcher *mSpiPatcher; }; #endif /*__ETH_SPI_H__*/ diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index 1219f107..4d2057dd 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -15,9 +15,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { public: - epdHal() { - mSpiPatcher = SpiPatcher::getInstance(SPI3_HOST); - } + epdHal() {} void patch() override { esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false); @@ -40,7 +38,8 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { mPinBusy = static_cast(busy); mSpiSpeed = speed; - mHostDevice = mSpiPatcher->getDevice(); + mHostDevice = (14 == sclk) ? SPI2_HOST : SPI3_HOST; + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); gpio_reset_pin(mPinMosi); gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT); @@ -68,7 +67,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { .pre_cb = nullptr, .post_cb = nullptr }; - ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg, &spi)); + mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi); release_spi(); if(GPIO_NUM_NC != mPinRst) { diff --git a/src/utils/dbg.h b/src/utils/dbg.h index 9e754ba6..f6dd8012 100644 --- a/src/utils/dbg.h +++ b/src/utils/dbg.h @@ -110,7 +110,7 @@ #if DEBUG_LEVEL >= DBG_ERROR #define PERR(str) DBGPRINT(F("E: ")); DBGPRINT(str); - #define PERRLN(str) DBGPRINT(F("E: ")); DBGPRINTLN(str); + #define PERRLN(str) DBGPRINT(F("E: ")); DBGPRINTLN(str); DSERIAL.flush(); #else #define PERR(str) #define PERRLN(str) diff --git a/src/utils/spiPatcher.cpp b/src/utils/spiPatcher.cpp index 3b7b5681..b3d27482 100644 --- a/src/utils/spiPatcher.cpp +++ b/src/utils/spiPatcher.cpp @@ -5,5 +5,6 @@ #if defined(ESP32) #include "spiPatcher.h" -SpiPatcher *SpiPatcher::mInstance = nullptr; +SpiPatcher *SpiPatcher::InstanceHost2 = nullptr; +SpiPatcher *SpiPatcher::InstanceHost3 = nullptr; #endif diff --git a/src/utils/spiPatcher.h b/src/utils/spiPatcher.h index 210b2a09..23775a36 100644 --- a/src/utils/spiPatcher.h +++ b/src/utils/spiPatcher.h @@ -9,6 +9,7 @@ #if defined(ESP32) +#include "dbg.h" #include "spiPatcherHandle.h" #include @@ -17,7 +18,7 @@ class SpiPatcher { protected: explicit SpiPatcher(spi_host_device_t dev) : - mHostDevice(dev), mCurHandle(nullptr) { + mCurHandle(nullptr) { // Use binary semaphore instead of mutex for performance reasons mutex = xSemaphoreCreateBinaryStatic(&mutex_buffer); xSemaphoreGive(mutex); @@ -36,23 +37,37 @@ class SpiPatcher { .flags = 0, .intr_flags = 0 }; - ESP_ERROR_CHECK(spi_bus_initialize(mHostDevice, &buscfg, SPI_DMA_DISABLED)); + ESP_ERROR_CHECK(spi_bus_initialize(dev, &buscfg, SPI_DMA_DISABLED)); } public: - SpiPatcher(SpiPatcher &other) = delete; + SpiPatcher(const SpiPatcher &other) = delete; void operator=(const SpiPatcher &) = delete; static SpiPatcher* getInstance(spi_host_device_t dev) { - if(nullptr == mInstance) - mInstance = new SpiPatcher(dev); - return mInstance; + if(SPI2_HOST == dev) { + if(nullptr == InstanceHost2) + InstanceHost2 = new SpiPatcher(dev); + return InstanceHost2; + } else { // SPI3_HOST + if(nullptr == InstanceHost3) + InstanceHost3 = new SpiPatcher(dev); + return InstanceHost3; + } } ~SpiPatcher() { vSemaphoreDelete(mutex); } - spi_host_device_t getDevice() { - return mHostDevice; + inline void addDevice(spi_host_device_t host_id, const spi_device_interface_config_t *dev_config, spi_device_handle_t *handle) { + if(SPI2_HOST == host_id) + mHost2Cnt++; + if(SPI3_HOST == host_id) + mHost3Cnt++; + + if((mHost2Cnt > 3) || (mHost3Cnt > 3)) + DPRINTLN(DBG_ERROR, F("maximum number of SPI devices reached (3)")); + + ESP_ERROR_CHECK(spi_bus_add_device(host_id, dev_config, handle)); } inline void request(SpiPatcherHandle* handle) { @@ -74,13 +89,14 @@ class SpiPatcher { } protected: - static SpiPatcher *mInstance; + static SpiPatcher *InstanceHost2; + static SpiPatcher *InstanceHost3; private: - const spi_host_device_t mHostDevice; SpiPatcherHandle* mCurHandle; SemaphoreHandle_t mutex; StaticSemaphore_t mutex_buffer; + uint8_t mHost2Cnt = 0, mHost3Cnt = 0; }; #endif /*ESP32*/ From dc15aae48d325d6ad18dd909dbbc59029a064cca Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 18 Apr 2024 23:07:27 +0200 Subject: [PATCH 09/31] change line endings --- .gitattributes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index 3041f8dc..9fcb3325 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -patches/GxEPD2_HAL.patch eol=crlf \ No newline at end of file +patches/GxEPD2_HAL.patch eol=lf \ No newline at end of file From 9d8ef192904390bf47522d2413d350e02b1b5da4 Mon Sep 17 00:00:00 2001 From: lumapu Date: Mon, 22 Apr 2024 22:00:09 +0200 Subject: [PATCH 10/31] fix ESP32-S3 --- scripts/applyPatches.py | 7 ++++--- src/hm/nrfHal.h | 5 +++++ src/hms/cmtHal.h | 5 +++++ src/network/AhoyEthernetSpi.h | 5 +++++ src/plugins/Display/epdHal.h | 5 +++++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/scripts/applyPatches.py b/scripts/applyPatches.py index 3c38c100..83b28c23 100644 --- a/scripts/applyPatches.py +++ b/scripts/applyPatches.py @@ -28,9 +28,10 @@ def applyPatch(libName, patchFile): # list of patches to apply (relative to /src) applyPatch("ESPAsyncWebServer-esphome", "../patches/AsyncWeb_Prometheus.patch") -if env['PIOENV'][:13] == "opendtufusion": - applyPatch("GxEPD2", "../patches/GxEPD2_SW_SPI.patch") -elif env['PIOENV'][:5] == "esp32": +#if env['PIOENV'][:13] == "opendtufusion": + #applyPatch("GxEPD2", "../patches/GxEPD2_SW_SPI.patch") +#el +if (env['PIOENV'][:5] == "esp32") or (env['PIOENV'][:13] == "opendtufusion"): applyPatch("GxEPD2", "../patches/GxEPD2_HAL.patch") if (env['PIOENV'][:13] == "opendtufusion") or (env['PIOENV'][:5] == "esp32"): diff --git a/src/hm/nrfHal.h b/src/hm/nrfHal.h index a9bd89d2..a9228543 100644 --- a/src/hm/nrfHal.h +++ b/src/hm/nrfHal.h @@ -39,7 +39,12 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { mPinEn = static_cast(en); mSpiSpeed = speed; + #if defined(CONFIG_IDF_TARGET_ESP32S3) + mHostDevice = SPI2_HOST; + #else mHostDevice = (14 == sclk) ? SPI2_HOST : SPI3_HOST; + #endif + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); gpio_reset_pin(mPinMosi); diff --git a/src/hms/cmtHal.h b/src/hms/cmtHal.h index 58f79da4..13a14950 100644 --- a/src/hms/cmtHal.h +++ b/src/hms/cmtHal.h @@ -37,7 +37,12 @@ class cmtHal : public SpiPatcherHandle { mPinFcs = static_cast(fcs); mSpiSpeed = speed; + #if defined(CONFIG_IDF_TARGET_ESP32S3) + mHostDevice = SPI2_HOST; + #else mHostDevice = (14 == clk) ? SPI2_HOST : SPI3_HOST; + #endif + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); gpio_reset_pin(mPinSdio); diff --git a/src/network/AhoyEthernetSpi.h b/src/network/AhoyEthernetSpi.h index 7b5bac85..4848de28 100644 --- a/src/network/AhoyEthernetSpi.h +++ b/src/network/AhoyEthernetSpi.h @@ -44,7 +44,12 @@ class AhoyEthernetSpi { gpio_reset_pin(static_cast(pin_int)); gpio_set_pull_mode(static_cast(pin_int), GPIO_PULLUP_ONLY); + #if defined(CONFIG_IDF_TARGET_ESP32S3) + mHostDevice = SPI3_HOST; + #else mHostDevice = (14 == pin_sclk) ? SPI2_HOST : SPI3_HOST; + #endif + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); spi_device_interface_config_t devcfg = { diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index 4d2057dd..37763288 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -38,7 +38,12 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { mPinBusy = static_cast(busy); mSpiSpeed = speed; + #if defined(CONFIG_IDF_TARGET_ESP32S3) + mHostDevice = SPI3_HOST; + #else mHostDevice = (14 == sclk) ? SPI2_HOST : SPI3_HOST; + #endif + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); gpio_reset_pin(mPinMosi); From b291ad1f4d30e95cf354fcbd813c44e1a91504dd Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 25 Apr 2024 00:25:16 +0200 Subject: [PATCH 11/31] 0.8.112 * improved wizard * converted ePaper and Ethernet to hal-SPI * improved network connection --- patches/GxEPD2_SW_SPI.patch | 362 ---------------------------------- scripts/applyPatches.py | 3 - src/CHANGES.md | 5 + src/defines.h | 2 +- src/network/AhoyEthernet.h | 54 +++-- src/network/AhoyEthernetSpi.h | 3 +- src/network/AhoyNetwork.h | 12 +- src/network/AhoyWifiEsp32.h | 62 ++++-- src/network/AhoyWifiEsp8266.h | 15 +- src/plugins/Display/epdHal.h | 2 - src/utils/spiPatcher.h | 42 ++-- src/web/html/wizard.html | 13 +- 12 files changed, 142 insertions(+), 433 deletions(-) delete mode 100644 patches/GxEPD2_SW_SPI.patch diff --git a/patches/GxEPD2_SW_SPI.patch b/patches/GxEPD2_SW_SPI.patch deleted file mode 100644 index dc3fa9ca..00000000 --- a/patches/GxEPD2_SW_SPI.patch +++ /dev/null @@ -1,362 +0,0 @@ -diff --git a/src/GxEPD2_EPD.cpp b/src/GxEPD2_EPD.cpp -index 8df8bef..91d7f49 100644 ---- a/src/GxEPD2_EPD.cpp -+++ b/src/GxEPD2_EPD.cpp -@@ -19,9 +19,9 @@ - - GxEPD2_EPD::GxEPD2_EPD(int16_t cs, int16_t dc, int16_t rst, int16_t busy, int16_t busy_level, uint32_t busy_timeout, - uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu) : -- WIDTH(w), HEIGHT(h), panel(p), hasColor(c), hasPartialUpdate(pu), hasFastPartialUpdate(fpu), -+ WIDTH(w), HEIGHT(h), panel(p), hasColor(c), hasPartialUpdate(pu), hasFastPartialUpdate(fpu), _sck(-1), _mosi(-1), - _cs(cs), _dc(dc), _rst(rst), _busy(busy), _busy_level(busy_level), _busy_timeout(busy_timeout), _diag_enabled(false), -- _pSPIx(&SPI), _spi_settings(4000000, MSBFIRST, SPI_MODE0) -+ _spi_settings(4000000, MSBFIRST, SPI_MODE0) - { - _initial_write = true; - _initial_refresh = true; -@@ -71,27 +71,30 @@ void GxEPD2_EPD::init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset - { - pinMode(_busy, INPUT); - } -- _pSPIx->begin(); -- if (_busy == MISO) // may be overridden -- { -- pinMode(_busy, INPUT); -- } -- if (_dc == MISO) // may be overridden, TTGO T5 V2.66 -- { -- pinMode(_dc, OUTPUT); -- } -- if (_cs == MISO) // may be overridden -+ if (_sck < 0) SPI.begin(); -+} -+ -+void GxEPD2_EPD::init(int16_t sck, int16_t mosi, uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration, bool pulldown_rst_mode) -+{ -+ if ((sck >= 0) && (mosi >= 0)) - { -- pinMode(_cs, INPUT); -- } -+ _sck = sck; -+ _mosi = mosi; -+ digitalWrite(_sck, LOW); -+ digitalWrite(_mosi, LOW); -+ pinMode(_sck, OUTPUT); -+ pinMode(_mosi, OUTPUT); -+ } else _sck = -1; -+ init(serial_diag_bitrate, initial, reset_duration, pulldown_rst_mode); - } - - void GxEPD2_EPD::end() - { -- _pSPIx->end(); - if (_cs >= 0) pinMode(_cs, INPUT); - if (_dc >= 0) pinMode(_dc, INPUT); - if (_rst >= 0) pinMode(_rst, INPUT); -+ if (_sck >= 0) pinMode(_sck, INPUT); -+ if (_mosi >= 0) pinMode(_mosi, INPUT); - } - - void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void* busy_callback_parameter) -@@ -100,12 +103,6 @@ void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void* - _busy_callback_parameter = busy_callback_parameter; - } - --void GxEPD2_EPD::selectSPI(SPIClass& spi, SPISettings spi_settings) --{ -- _pSPIx = &spi; -- _spi_settings = spi_settings; --} -- - void GxEPD2_EPD::_reset() - { - if (_rst >= 0) -@@ -174,115 +171,201 @@ void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time) - - void GxEPD2_EPD::_writeCommand(uint8_t c) - { -- _pSPIx->beginTransaction(_spi_settings); -+ _beginTransaction(_spi_settings); - if (_dc >= 0) digitalWrite(_dc, LOW); - if (_cs >= 0) digitalWrite(_cs, LOW); -- _pSPIx->transfer(c); -+ _spi_write(c); - if (_cs >= 0) digitalWrite(_cs, HIGH); - if (_dc >= 0) digitalWrite(_dc, HIGH); -- _pSPIx->endTransaction(); -+ _endTransaction(); - } - - void GxEPD2_EPD::_writeData(uint8_t d) - { -- _pSPIx->beginTransaction(_spi_settings); -+ _beginTransaction(_spi_settings); - if (_cs >= 0) digitalWrite(_cs, LOW); -- _pSPIx->transfer(d); -+ _spi_write(d); - if (_cs >= 0) digitalWrite(_cs, HIGH); -- _pSPIx->endTransaction(); -+ _endTransaction(); - } - - void GxEPD2_EPD::_writeData(const uint8_t* data, uint16_t n) - { -- _pSPIx->beginTransaction(_spi_settings); -+ _beginTransaction(_spi_settings); - if (_cs >= 0) digitalWrite(_cs, LOW); -- for (uint16_t i = 0; i < n; i++) -+ for (uint8_t i = 0; i < n; i++) - { -- _pSPIx->transfer(*data++); -+ _spi_write(*data++); - } - if (_cs >= 0) digitalWrite(_cs, HIGH); -- _pSPIx->endTransaction(); -+ _endTransaction(); - } - - void GxEPD2_EPD::_writeDataPGM(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes) - { -- _pSPIx->beginTransaction(_spi_settings); -+ _beginTransaction(_spi_settings); - if (_cs >= 0) digitalWrite(_cs, LOW); -- for (uint16_t i = 0; i < n; i++) -+ for (uint8_t i = 0; i < n; i++) - { -- _pSPIx->transfer(pgm_read_byte(&*data++)); -+ _spi_write(pgm_read_byte(&*data++)); - } - while (fill_with_zeroes > 0) - { -- _pSPIx->transfer(0x00); -+ _spi_write(0x00); - fill_with_zeroes--; - } - if (_cs >= 0) digitalWrite(_cs, HIGH); -- _pSPIx->endTransaction(); -+ _endTransaction(); - } - - void GxEPD2_EPD::_writeDataPGM_sCS(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes) - { -- _pSPIx->beginTransaction(_spi_settings); -+ _beginTransaction(_spi_settings); - for (uint8_t i = 0; i < n; i++) - { - if (_cs >= 0) digitalWrite(_cs, LOW); -- _pSPIx->transfer(pgm_read_byte(&*data++)); -+ _spi_write(pgm_read_byte(&*data++)); - if (_cs >= 0) digitalWrite(_cs, HIGH); - } - while (fill_with_zeroes > 0) - { - if (_cs >= 0) digitalWrite(_cs, LOW); -- _pSPIx->transfer(0x00); -+ _spi_write(0x00); - fill_with_zeroes--; - if (_cs >= 0) digitalWrite(_cs, HIGH); - } -- _pSPIx->endTransaction(); -+ _endTransaction(); - } - - void GxEPD2_EPD::_writeCommandData(const uint8_t* pCommandData, uint8_t datalen) - { -- _pSPIx->beginTransaction(_spi_settings); -+ _beginTransaction(_spi_settings); - if (_dc >= 0) digitalWrite(_dc, LOW); - if (_cs >= 0) digitalWrite(_cs, LOW); -- _pSPIx->transfer(*pCommandData++); -+ _spi_write(*pCommandData++); - if (_dc >= 0) digitalWrite(_dc, HIGH); - for (uint8_t i = 0; i < datalen - 1; i++) // sub the command - { -- _pSPIx->transfer(*pCommandData++); -+ _spi_write(*pCommandData++); - } - if (_cs >= 0) digitalWrite(_cs, HIGH); -- _pSPIx->endTransaction(); -+ _endTransaction(); - } - - void GxEPD2_EPD::_writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen) - { -- _pSPIx->beginTransaction(_spi_settings); -+ _beginTransaction(_spi_settings); - if (_dc >= 0) digitalWrite(_dc, LOW); - if (_cs >= 0) digitalWrite(_cs, LOW); -- _pSPIx->transfer(pgm_read_byte(&*pCommandData++)); -+ _spi_write(pgm_read_byte(&*pCommandData++)); - if (_dc >= 0) digitalWrite(_dc, HIGH); - for (uint8_t i = 0; i < datalen - 1; i++) // sub the command - { -- _pSPIx->transfer(pgm_read_byte(&*pCommandData++)); -+ _spi_write(pgm_read_byte(&*pCommandData++)); - } - if (_cs >= 0) digitalWrite(_cs, HIGH); -- _pSPIx->endTransaction(); -+ _endTransaction(); - } - - void GxEPD2_EPD::_startTransfer() - { -- _pSPIx->beginTransaction(_spi_settings); -+ _beginTransaction(_spi_settings); - if (_cs >= 0) digitalWrite(_cs, LOW); - } - - void GxEPD2_EPD::_transfer(uint8_t value) - { -- _pSPIx->transfer(value); -+ _spi_write(value); - } - - void GxEPD2_EPD::_endTransfer() - { - if (_cs >= 0) digitalWrite(_cs, HIGH); -- _pSPIx->endTransaction(); -+ _endTransaction(); -+} -+ -+void GxEPD2_EPD::_beginTransaction(const SPISettings& settings) -+{ -+ if (_sck < 0) SPI.beginTransaction(settings); -+} -+ -+void GxEPD2_EPD::_spi_write(uint8_t data) -+{ -+ if (_sck < 0) SPI.transfer(data); -+ else -+ { -+#if defined (ESP8266) -+ yield(); -+#endif -+ for (int i = 0; i < 8; i++) -+ { -+ digitalWrite(_mosi, (data & 0x80) ? HIGH : LOW); -+ data <<= 1; -+ digitalWrite(_sck, HIGH); -+ digitalWrite(_sck, LOW); -+ } -+ } -+} -+ -+void GxEPD2_EPD::_endTransaction() -+{ -+ if (_sck < 0) SPI.endTransaction(); -+} -+ -+uint8_t GxEPD2_EPD::_readData() -+{ -+ uint8_t data = 0; -+ _beginTransaction(_spi_settings); -+ if (_cs >= 0) digitalWrite(_cs, LOW); -+ if (_sck < 0) -+ { -+ data = SPI.transfer(0); -+ } -+ else -+ { -+ pinMode(_mosi, INPUT); -+ for (int i = 0; i < 8; i++) -+ { -+ data <<= 1; -+ digitalWrite(_sck, HIGH); -+ data |= digitalRead(_mosi); -+ digitalWrite(_sck, LOW); -+ } -+ pinMode(_mosi, OUTPUT); -+ } -+ if (_cs >= 0) digitalWrite(_cs, HIGH); -+ _endTransaction(); -+ return data; -+} -+ -+void GxEPD2_EPD::_readData(uint8_t* data, uint16_t n) -+{ -+ _beginTransaction(_spi_settings); -+ if (_cs >= 0) digitalWrite(_cs, LOW); -+ if (_sck < 0) -+ { -+ for (uint8_t i = 0; i < n; i++) -+ { -+ *data++ = SPI.transfer(0); -+ } -+ } -+ else -+ { -+ pinMode(_mosi, INPUT); -+ for (uint8_t i = 0; i < n; i++) -+ { -+ *data = 0; -+ for (int i = 0; i < 8; i++) -+ { -+ *data <<= 1; -+ digitalWrite(_sck, HIGH); -+ *data |= digitalRead(_mosi); -+ digitalWrite(_sck, LOW); -+ } -+ data++; -+ } -+ pinMode(_mosi, OUTPUT); -+ } -+ if (_cs >= 0) digitalWrite(_cs, HIGH); -+ _endTransaction(); - } -diff --git a/src/GxEPD2_EPD.h b/src/GxEPD2_EPD.h -index 34c1145..c480b7d 100644 ---- a/src/GxEPD2_EPD.h -+++ b/src/GxEPD2_EPD.h -@@ -8,6 +8,10 @@ - // Version: see library.properties - // - // Library: https://github.com/ZinggJM/GxEPD2 -+// To use SW SPI with GxEPD2: -+// add the special call to the added init method BEFORE the normal init method: -+// display.epd2.init(SW_SCK, SW_MOSI, 115200, true, 20, false); // define or replace SW_SCK, SW_MOSI -+// display.init(115200); // needed to init upper level - - #ifndef _GxEPD2_EPD_H_ - #define _GxEPD2_EPD_H_ -@@ -35,6 +39,7 @@ class GxEPD2_EPD - uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu); - virtual void init(uint32_t serial_diag_bitrate = 0); // serial_diag_bitrate = 0 : disabled - virtual void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 10, bool pulldown_rst_mode = false); -+ virtual void init(int16_t sck, int16_t mosi, uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 20, bool pulldown_rst_mode = false); - virtual void end(); // release SPI and control pins - // Support for Bitmaps (Sprites) to Controller Buffer and to Screen - virtual void clearScreen(uint8_t value) = 0; // init controller memory and screen (default white) -@@ -97,7 +102,6 @@ class GxEPD2_EPD - { - return (a > b ? a : b); - }; -- void selectSPI(SPIClass& spi, SPISettings spi_settings); - protected: - void _reset(); - void _waitWhileBusy(const char* comment = 0, uint16_t busy_time = 5000); -@@ -111,17 +115,22 @@ class GxEPD2_EPD - void _startTransfer(); - void _transfer(uint8_t value); - void _endTransfer(); -+ void _beginTransaction(const SPISettings& settings); -+ void _spi_write(uint8_t data); -+ void _endTransaction(); -+ public: -+ uint8_t _readData(); -+ void _readData(uint8_t* data, uint16_t n); - protected: -- int16_t _cs, _dc, _rst, _busy, _busy_level; -+ int16_t _cs, _dc, _rst, _busy, _busy_level, _sck, _mosi;; - uint32_t _busy_timeout; - bool _diag_enabled, _pulldown_rst_mode; -- SPIClass* _pSPIx; - SPISettings _spi_settings; - bool _initial_write, _initial_refresh; - bool _power_is_on, _using_partial_mode, _hibernating; - bool _init_display_done; - uint16_t _reset_duration; -- void (*_busy_callback)(const void*); -+ void (*_busy_callback)(const void*); - const void* _busy_callback_parameter; - }; - diff --git a/scripts/applyPatches.py b/scripts/applyPatches.py index 83b28c23..1672ab2f 100644 --- a/scripts/applyPatches.py +++ b/scripts/applyPatches.py @@ -28,9 +28,6 @@ def applyPatch(libName, patchFile): # list of patches to apply (relative to /src) applyPatch("ESPAsyncWebServer-esphome", "../patches/AsyncWeb_Prometheus.patch") -#if env['PIOENV'][:13] == "opendtufusion": - #applyPatch("GxEPD2", "../patches/GxEPD2_SW_SPI.patch") -#el if (env['PIOENV'][:5] == "esp32") or (env['PIOENV'][:13] == "opendtufusion"): applyPatch("GxEPD2", "../patches/GxEPD2_HAL.patch") diff --git a/src/CHANGES.md b/src/CHANGES.md index 135fe0fc..c3ffee3b 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,10 @@ # Development Changes +## 0.8.112 - 2024-04-24 +* improved wizard +* converted ePaper and Ethernet to hal-SPI +* improved network connection + ## 0.8.111 - 2024-04-17 * fix MqTT discovery field `ALARM_MES_ID` #1591 * fix Wifi reconnect for ESP32 #1589 #1575 diff --git a/src/defines.h b/src/defines.h index cab8676d..2a4d9306 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 111 +#define VERSION_PATCH 112 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/network/AhoyEthernet.h b/src/network/AhoyEthernet.h index db624a41..90255b26 100644 --- a/src/network/AhoyEthernet.h +++ b/src/network/AhoyEthernet.h @@ -23,30 +23,20 @@ class AhoyEthernet : public AhoyNetwork { mEthSpi.begin(mConfig->sys.eth.pinMiso, mConfig->sys.eth.pinMosi, mConfig->sys.eth.pinSclk, mConfig->sys.eth.pinCs, mConfig->sys.eth.pinIrq, mConfig->sys.eth.pinRst); ETH.setHostname(mConfig->sys.deviceName); - - // static IP - setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { - return ETH.config(ip, gateway, mask, dns1, dns2); - }); } - void tickNetworkLoop() override { - if(mAp.isEnabled()) - mAp.tickLoop(); - - switch(mStatus) { - case NetworkState::DISCONNECTED: - if(mConnected) { - mConnected = false; - mOnNetworkCB(false); - mAp.enable(); + void OnEvent(WiFiEvent_t event) override { + switch(event) { + case ARDUINO_EVENT_ETH_CONNECTED: + if(NetworkState::CONNECTED != mStatus) { + mStatus = NetworkState::CONNECTED; + DPRINTLN(DBG_INFO, F("Network connected")); + setStaticIp(); } break; - case NetworkState::CONNECTED: - break; - - case NetworkState::GOT_IP: + case ARDUINO_EVENT_ETH_GOT_IP: + mStatus = NetworkState::GOT_IP; if(!mConnected) { mAp.disable(); mConnected = true; @@ -55,13 +45,39 @@ class AhoyEthernet : public AhoyNetwork { mOnNetworkCB(true); } break; + + case ARDUINO_EVENT_ETH_STOP: + [[fallthrough]]; + case ARDUINO_EVENT_ETH_DISCONNECTED: + mStatus = NetworkState::DISCONNECTED; + if(mConnected) { + mConnected = false; + mOnNetworkCB(false); + mAp.enable(); + } + break; + + default: + break; } } + void tickNetworkLoop() override { + if(mAp.isEnabled()) + mAp.tickLoop(); + } + String getIp(void) override { return ETH.localIP().toString(); } + private: + void setStaticIp() override { + setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { + return ETH.config(ip, gateway, mask, dns1, dns2); + }); + } + private: AhoyEthernetSpi mEthSpi; }; diff --git a/src/network/AhoyEthernetSpi.h b/src/network/AhoyEthernetSpi.h index 4848de28..ab2eab9e 100644 --- a/src/network/AhoyEthernetSpi.h +++ b/src/network/AhoyEthernetSpi.h @@ -50,7 +50,8 @@ class AhoyEthernetSpi { mHostDevice = (14 == pin_sclk) ? SPI2_HOST : SPI3_HOST; #endif - mSpiPatcher = SpiPatcher::getInstance(mHostDevice); + mSpiPatcher = SpiPatcher::getInstance(mHostDevice, false); + mSpiPatcher->initBus(pin_mosi, pin_miso, pin_sclk, SPI_DMA_CH_AUTO); spi_device_interface_config_t devcfg = { .command_bits = 16, // actually address phase diff --git a/src/network/AhoyNetwork.h b/src/network/AhoyNetwork.h index 889fde8e..7bc0bab2 100644 --- a/src/network/AhoyNetwork.h +++ b/src/network/AhoyNetwork.h @@ -28,7 +28,6 @@ class AhoyNetwork { if('\0' == mConfig->sys.deviceName[0]) snprintf(mConfig->sys.deviceName, DEVNAME_LEN, "%s", DEF_DEVICE_NAME); - WiFi.hostname(mConfig->sys.deviceName); mAp.setup(&mConfig->sys); @@ -117,13 +116,17 @@ class AhoyNetwork { void scan(void) { mScanActive = true; - if(NetworkState::GOT_IP != mStatus) + if(mWifiConnecting) { + mWifiConnecting = false; WiFi.disconnect(); + } WiFi.scanNetworks(true, true); } #endif protected: + virtual void setStaticIp() = 0; + void setupIp(std::function cb) { if(mConfig->sys.ip.ip[0] != 0) { IPAddress ip(mConfig->sys.ip.ip); @@ -136,7 +139,7 @@ class AhoyNetwork { } } - void OnEvent(WiFiEvent_t event) { + virtual void OnEvent(WiFiEvent_t event) { switch(event) { case SYSTEM_EVENT_STA_CONNECTED: [[fallthrough]]; @@ -231,6 +234,9 @@ class AhoyNetwork { uint32_t *mUtcTimestamp = nullptr; bool mConnected = false; bool mScanActive = false; + #if !defined(ETHERNET) + bool mWifiConnecting = false; + #endif OnNetworkCB mOnNetworkCB; OnTimeCB mOnTimeCB; diff --git a/src/network/AhoyWifiEsp32.h b/src/network/AhoyWifiEsp32.h index b3e12b3a..1c31ea78 100644 --- a/src/network/AhoyWifiEsp32.h +++ b/src/network/AhoyWifiEsp32.h @@ -17,16 +17,15 @@ class AhoyWifi : public AhoyNetwork { void begin() override { mAp.enable(); - // static IP - setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { - return WiFi.config(ip, gateway, mask, dns1, dns2); - }); + if(String(FB_WIFI_SSID) == mConfig->sys.stationSsid) + return; // no station wifi defined WiFi.setHostname(mConfig->sys.deviceName); #if !defined(AP_ONLY) WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, WIFI_ALL_CHANNEL_SCAN); + mWifiConnecting = true; DBGPRINT(F("connect to network '")); DBGPRINT(mConfig->sys.stationSsid); @@ -34,24 +33,19 @@ class AhoyWifi : public AhoyNetwork { #endif } - void tickNetworkLoop() override { - if(mAp.isEnabled()) - mAp.tickLoop(); - - switch(mStatus) { - case NetworkState::DISCONNECTED: - if(mConnected) { - mConnected = false; - mOnNetworkCB(false); - MDNS.end(); - begin(); + void OnEvent(WiFiEvent_t event) override { + switch(event) { + case SYSTEM_EVENT_STA_CONNECTED: + if(NetworkState::CONNECTED != mStatus) { + mStatus = NetworkState::CONNECTED; + mWifiConnecting = false; + DPRINTLN(DBG_INFO, F("Network connected")); + setStaticIp(); } break; - case NetworkState::CONNECTED: - break; - - case NetworkState::GOT_IP: + case SYSTEM_EVENT_STA_GOT_IP: + mStatus = NetworkState::GOT_IP; if(mAp.isEnabled()) mAp.disable(); @@ -62,12 +56,42 @@ class AhoyWifi : public AhoyNetwork { mOnNetworkCB(true); } break; + + case ARDUINO_EVENT_WIFI_STA_LOST_IP: + [[fallthrough]]; + case ARDUINO_EVENT_WIFI_STA_STOP: + [[fallthrough]]; + case SYSTEM_EVENT_STA_DISCONNECTED: + mStatus = NetworkState::DISCONNECTED; + if(mConnected) { + mConnected = false; + mOnNetworkCB(false); + MDNS.end(); + WiFi.disconnect(); + begin(); + } + break; + + default: + break; } } + void tickNetworkLoop() override { + if(mAp.isEnabled()) + mAp.tickLoop(); + } + String getIp(void) override { return WiFi.localIP().toString(); } + + private: + void setStaticIp() override { + setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { + return WiFi.config(ip, gateway, mask, dns1, dns2); + }); + } }; #endif /*ESP32 & !ETHERNET*/ diff --git a/src/network/AhoyWifiEsp8266.h b/src/network/AhoyWifiEsp8266.h index 2497448b..2da0a409 100644 --- a/src/network/AhoyWifiEsp8266.h +++ b/src/network/AhoyWifiEsp8266.h @@ -18,11 +18,6 @@ class AhoyWifi : public AhoyNetwork { void begin() override { mAp.enable(); - // static IP - setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { - return WiFi.config(ip, gateway, mask, dns1, dns2); - }); - WiFi.setHostname(mConfig->sys.deviceName); mBSSIDList.clear(); } @@ -37,6 +32,7 @@ class AhoyWifi : public AhoyNetwork { case NetworkState::DISCONNECTED: if(mConnected) { mConnected = false; + mWifiConnecting = false; mOnNetworkCB(false); mAp.enable(); MDNS.end(); @@ -76,16 +72,19 @@ class AhoyWifi : public AhoyNetwork { } DBGPRINTLN(""); WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, 0, &bssid[0]); + mWifiConnecting = true; break; case NetworkState::CONNECTING: if (isTimeout(TIMEOUT)) { WiFi.disconnect(); + mWifiConnecting = false; mStatus = mBSSIDList.empty() ? NetworkState::DISCONNECTED : NetworkState::SCAN_READY; } break; case NetworkState::CONNECTED: + setStaticIp(); break; case NetworkState::GOT_IP: @@ -117,6 +116,12 @@ class AhoyWifi : public AhoyNetwork { } private: + void setStaticIp() override { + setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { + return WiFi.config(ip, gateway, mask, dns1, dns2); + }); + } + bool getBSSIDs() { bool result = false; int n = WiFi.scanComplete(); diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index 37763288..e998a885 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -55,7 +55,6 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { gpio_set_level(mPinClk, 0); gpio_reset_pin(mPinCs); - request_spi(); spi_device_interface_config_t devcfg = { .command_bits = 0, .address_bits = 0, @@ -73,7 +72,6 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { .post_cb = nullptr }; mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi); - release_spi(); if(GPIO_NUM_NC != mPinRst) { gpio_reset_pin(mPinRst); diff --git a/src/utils/spiPatcher.h b/src/utils/spiPatcher.h index 23775a36..bb14a165 100644 --- a/src/utils/spiPatcher.h +++ b/src/utils/spiPatcher.h @@ -22,11 +22,19 @@ class SpiPatcher { // Use binary semaphore instead of mutex for performance reasons mutex = xSemaphoreCreateBinaryStatic(&mutex_buffer); xSemaphoreGive(mutex); + mDev = dev; + mBusState = ESP_FAIL; + } + + public: + SpiPatcher(const SpiPatcher &other) = delete; + void operator=(const SpiPatcher &) = delete; - spi_bus_config_t buscfg = { - .mosi_io_num = -1, - .miso_io_num = -1, - .sclk_io_num = -1, + esp_err_t initBus(int mosi = -1, int miso = -1, int sclk = -1, spi_common_dma_t dmaType = SPI_DMA_DISABLED) { + mBusConfig = spi_bus_config_t { + .mosi_io_num = mosi, + .miso_io_num = miso, + .sclk_io_num = sclk, .quadwp_io_num = -1, .quadhd_io_num = -1, .data4_io_num = -1, @@ -37,21 +45,25 @@ class SpiPatcher { .flags = 0, .intr_flags = 0 }; - ESP_ERROR_CHECK(spi_bus_initialize(dev, &buscfg, SPI_DMA_DISABLED)); - } + ESP_ERROR_CHECK((mBusState = spi_bus_initialize(mDev, &mBusConfig, dmaType))); - public: - SpiPatcher(const SpiPatcher &other) = delete; - void operator=(const SpiPatcher &) = delete; + return mBusState; + } - static SpiPatcher* getInstance(spi_host_device_t dev) { + static SpiPatcher* getInstance(spi_host_device_t dev, bool initialize = true) { if(SPI2_HOST == dev) { - if(nullptr == InstanceHost2) + if(nullptr == InstanceHost2) { InstanceHost2 = new SpiPatcher(dev); + if(initialize) + InstanceHost2->initBus(); + } return InstanceHost2; } else { // SPI3_HOST - if(nullptr == InstanceHost3) + if(nullptr == InstanceHost3) { InstanceHost3 = new SpiPatcher(dev); + if(initialize) + InstanceHost3->initBus(); + } return InstanceHost3; } } @@ -59,6 +71,7 @@ class SpiPatcher { ~SpiPatcher() { vSemaphoreDelete(mutex); } inline void addDevice(spi_host_device_t host_id, const spi_device_interface_config_t *dev_config, spi_device_handle_t *handle) { + assert(mBusState == ESP_OK); if(SPI2_HOST == host_id) mHost2Cnt++; if(SPI3_HOST == host_id) @@ -71,6 +84,7 @@ class SpiPatcher { } inline void request(SpiPatcherHandle* handle) { + assert(mBusState == ESP_OK); xSemaphoreTake(mutex, portMAX_DELAY); if (mCurHandle != handle) { @@ -85,6 +99,7 @@ class SpiPatcher { } inline void release() { + assert(mBusState == ESP_OK); xSemaphoreGive(mutex); } @@ -97,6 +112,9 @@ class SpiPatcher { SemaphoreHandle_t mutex; StaticSemaphore_t mutex_buffer; uint8_t mHost2Cnt = 0, mHost3Cnt = 0; + spi_host_device_t mDev = SPI3_HOST; + esp_err_t mBusState = ESP_FAIL; + spi_bus_config_t mBusConfig; }; #endif /*ESP32*/ diff --git a/src/web/html/wizard.html b/src/web/html/wizard.html index a6a29bbb..98333c12 100644 --- a/src/web/html/wizard.html +++ b/src/web/html/wizard.html @@ -232,7 +232,7 @@ ml("div", {class: "row my-4"}, ml("div", {class: "col a-r"}, ml("input", {type: "button", class:"btn hide", id: "btn", value: "{#BTN_FINISH}", onclick: () => {redirect()}}, null))), ml("div", {class: "row mt-5"}, ml("div", {class: "col a-c"}, ml("a", {onclick: () => {redirect()}}, "{#STOP_WIZARD}"))) ) - v = setInterval(() => {getAjax('/api/setup/getip', printIp)}, 500); + v = setInterval(() => {getAjax('/api/setup/getip', printIp)}, 1000); } function redirect() { @@ -240,7 +240,7 @@ } function printIp(obj) { - if("0.0.0.0" != obj["ip"]) { + if("0.0.0.0" != obj.ip) { clearInterval(v) setHide("btn", false) document.getElementById("state").innerHTML = "{#NETWORK_SUCCESS}" + obj.ip @@ -272,12 +272,12 @@ getAjax("/api/setup", ((o) => c.append(step1(o.eth)))); /*ELSE*/ function nets(obj) { + clearInterval(v) + v = setInterval(() => {getAjax('/api/setup/networks', nets)}, 4000) + if(!obj.success) return; - clearInterval(v) - v = setInterval(() => {getAjax('/api/setup/networks', nets)}, 5000) - var e = document.getElementById("net"); if(obj.networks.length > 0) { var a = [] @@ -289,7 +289,8 @@ e.replaceChildren(...a) } - redirIp = obj.ip + "/index" + if("0.0.0.0" != obj.ip) + redirIp = "http://" + obj.ip + "/index" } c.append(step1()) From d75ba1b5666f517f88b5e43ec9f2d4ad008d92e2 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 25 Apr 2024 22:18:18 +0200 Subject: [PATCH 12/31] code cleanup --- src/app.cpp | 28 ++++++++-------------------- src/app.h | 14 +++++--------- src/config/config.h | 5 +++++ src/config/settings.h | 12 +++--------- src/hms/CmtRadio.h | 24 +++++++++++++++++++++--- 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index d00a95c2..30942fec 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -33,23 +33,16 @@ void app::setup() { resetSystem(); esp_task_wdt_reset(); - mSettings.setup(); - mSettings.getPtr(mConfig); + mSettings.setup(mConfig); ah::Scheduler::setup(mConfig->inst.startWithoutTime); DPRINT(DBG_INFO, F("Settings valid: ")); - DSERIAL.flush(); - if (mSettings.getValid()) - DBGPRINTLN(F("true")); - else - DBGPRINTLN(F("false")); + DBGPRINTLN(mConfig->valid ? F("true") : F("false")); esp_task_wdt_reset(); mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->nrf); #if defined(ESP32) - if(mConfig->cmt.enabled) { - mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, mConfig->cmt.pinSclk, mConfig->cmt.pinSdio, mConfig->cmt.pinCsb, mConfig->cmt.pinFcsb, mConfig->sys.region); - } + mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->cmt, mConfig->sys.region); #endif #ifdef ETHERNET @@ -57,16 +50,16 @@ void app::setup() { mNetwork = static_cast(new AhoyEthernet()); #else mNetwork = static_cast(new AhoyWifi()); - #endif // ETHERNET + #endif mNetwork->setup(mConfig, &mTimestamp, [this](bool gotIp) { this->onNetwork(gotIp); }, [this](bool gotTime) { this->onNtpUpdate(gotTime); }); mNetwork->begin(); esp_task_wdt_reset(); mCommunication.setup(&mTimestamp, &mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace); - mCommunication.addPayloadListener(std::bind(&app::payloadEventListener, this, std::placeholders::_1, std::placeholders::_2)); + mCommunication.addPayloadListener([this] (uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); }); #if defined(ENABLE_MQTT) - mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); }); + mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); }); #endif mSys.setup(&mTimestamp, &mConfig->inst, this); for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { @@ -81,7 +74,6 @@ void app::setup() { esp_task_wdt_reset(); // when WiFi is in client mode, then enable mqtt broker - #if !defined(AP_ONLY) #if defined(ENABLE_MQTT) mMqttEnabled = (mConfig->mqtt.broker[0] > 0); if (mMqttEnabled) { @@ -90,7 +82,6 @@ void app::setup() { mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); }); } #endif - #endif setupLed(); esp_task_wdt_reset(); @@ -126,9 +117,7 @@ void app::setup() { #if defined(ENABLE_SIMULATOR) mSimulator.setup(&mSys, &mTimestamp, 0); - mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { - payloadEventListener(cmd, iv); - }); + mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); }); #endif /*ENABLE_SIMULATOR*/ esp_task_wdt_reset(); @@ -142,8 +131,7 @@ void app::loop(void) { mNrfRadio.loop(); #if defined(ESP32) - if(mConfig->cmt.enabled) - mCmtRadio.loop(); + mCmtRadio.loop(); #endif ah::Scheduler::loop(); diff --git a/src/app.h b/src/app.h index a7be0a55..4f07adc6 100644 --- a/src/app.h +++ b/src/app.h @@ -217,7 +217,7 @@ class app : public IApp, public ah::Scheduler { } bool getSettingsValid() override { - return mSettings.getValid(); + return mConfig->valid; } bool getRebootRequestState() override { @@ -354,15 +354,13 @@ class app : public IApp, public ah::Scheduler { void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true); void payloadEventListener(uint8_t cmd, Inverter<> *iv) { - #if !defined(AP_ONLY) #if defined(ENABLE_MQTT) if (mMqttEnabled) mMqtt.payloadEventListener(cmd, iv); - #endif /*ENABLE_MQTT*/ #endif #if defined(PLUGIN_DISPLAY) - if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type) - mDisplay.payloadEventListener(cmd); + if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type) + mDisplay.payloadEventListener(cmd); #endif updateLed(); } @@ -425,8 +423,7 @@ class app : public IApp, public ah::Scheduler { #ifdef ENABLE_SYSLOG DbgSyslog mDbgSyslog; #endif - //PayloadType mPayload; - //MiPayloadType mMiPayload; + PubSerialType mPubSerial; #if !defined(ETHERNET) //Improv mImprov; @@ -447,10 +444,9 @@ class app : public IApp, public ah::Scheduler { bool mNetworkConnected = false; - // mqtt #if defined(ENABLE_MQTT) PubMqttType mMqtt; - #endif /*ENABLE_MQTT*/ + #endif bool mTickerInstallOnce = false; bool mMqttEnabled = false; diff --git a/src/config/config.h b/src/config/config.h index 3dfac72e..49705eb6 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -28,6 +28,11 @@ // If the next line is uncommented, Ahoy will stay in access point mode all the time //#define AP_ONLY +#if defined(AP_ONLY) + #if defined(ENABLE_MQTT) + #undef ENABLE_MQTT + #endif +#endif // timeout for automatic logoff (20 minutes) #define LOGOUT_TIMEOUT (20 * 60) diff --git a/src/config/settings.h b/src/config/settings.h index e97519a1..4f5ba4c0 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -239,8 +239,9 @@ class settings { std::fill(reinterpret_cast(&mCfg), reinterpret_cast(&mCfg) + sizeof(mCfg), 0); } - void setup() { + void setup(settings_t *&cfg) { DPRINTLN(DBG_INFO, F("Initializing FS ..")); + cfg = &mCfg; mCfg.valid = false; #if !defined(ESP32) @@ -276,14 +277,6 @@ class settings { DPRINTLN(DBG_INFO, F("FS stopped")); } - void getPtr(settings_t *&cfg) { - cfg = &mCfg; - } - - bool getValid(void) { - return mCfg.valid; - } - inline bool getLastSaveSucceed() { return mLastSaveSucceed; } @@ -915,6 +908,7 @@ class settings { } #endif + private: settings_t mCfg; bool mLastSaveSucceed = 0; }; diff --git a/src/hms/CmtRadio.h b/src/hms/CmtRadio.h index 4b043ae2..b6405329 100644 --- a/src/hms/CmtRadio.h +++ b/src/hms/CmtRadio.h @@ -15,16 +15,25 @@ template class CmtRadio : public Radio { typedef Cmt2300a CmtType; public: - void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, uint8_t pinSclk, uint8_t pinSdio, uint8_t pinCsb, uint8_t pinFcsb, uint8_t region = 0, bool genDtuSn = true) { - mCmt.setup(pinSclk, pinSdio, pinCsb, pinFcsb); - reset(genDtuSn, static_cast(region)); + void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, cfgCmt_t *cfg, uint8_t region = 0, bool genDtuSn = true) { + mCfg = cfg; + + if(!cfg->enabled) + return; + mPrivacyMode = privacyMode; mSerialDebug = serialDebug; mPrintWholeTrace = printWholeTrace; mTxBuf.fill(0); + + mCmt.setup(cfg->pinSclk, cfg->pinSdio, cfg->pinCsb, cfg->pinFcsb); + reset(genDtuSn, static_cast(region)); } void loop() override { + if(!mCfg->enabled) + return; + mCmt.loop(); if((!mIrqRcvd) && (!mRqstGetRx)) return; @@ -41,6 +50,9 @@ class CmtRadio : public Radio { } void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit) override { + if(!mCfg->enabled) + return; + DPRINT(DBG_INFO, F("sendControlPacket cmd: ")); DBGHEXLN(cmd); initPacket(iv->radioId.u64, TX_REQ_DEVCONTROL, SINGLE_FRAME); @@ -59,6 +71,9 @@ class CmtRadio : public Radio { } bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) override { + if(!isChipConnected()) + return false; + uint8_t fromCh = mCmt.freq2Chan(fromkHz); uint8_t toCh = mCmt.freq2Chan(tokHz); @@ -68,6 +83,8 @@ class CmtRadio : public Radio { bool switchFrequencyCh(Inverter<> *iv, uint8_t fromCh, uint8_t toCh) override { if((0xff == fromCh) || (0xff == toCh)) return false; + if(!isChipConnected()) + return false; mCmt.switchChannel(fromCh); sendSwitchChCmd(iv, toCh); @@ -188,6 +205,7 @@ class CmtRadio : public Radio { } CmtType mCmt; + cfgCmt_t *mCfg = nullptr; bool mCmtAvail = false; bool mRqstGetRx = false; uint32_t mMillis = 0; From 177420fce2c641b17b359e2a7d07767baed3c0a3 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 25 Apr 2024 23:16:09 +0200 Subject: [PATCH 13/31] 0.8.113 * code cleanup * fix ESP32-C3 compile --- src/CHANGES.md | 4 ++++ src/defines.h | 2 +- src/hm/hmInverter.h | 34 ++++++++++++++++++---------------- src/hm/hmSystem.h | 6 +++--- src/hm/nrfHal.h | 2 +- src/hms/cmtHal.h | 2 +- src/plugins/Display/epdHal.h | 3 ++- src/utils/spiPatcher.h | 10 +++++++++- src/web/RestApi.h | 2 -- 9 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index c3ffee3b..35fbc3cc 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,9 @@ # Development Changes +## 0.8.113 - 2024-04-25 +* code cleanup +* fix ESP32-C3 compile + ## 0.8.112 - 2024-04-24 * improved wizard * converted ePaper and Ethernet to hal-SPI diff --git a/src/defines.h b/src/defines.h index 2a4d9306..36919b21 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 112 +#define VERSION_PATCH 113 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/hm/hmInverter.h b/src/hm/hmInverter.h index 9ac1cacf..9aa75878 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -84,7 +84,7 @@ struct record_t { byteAssign_t* assign = nullptr; // assignment of bytes in payload uint8_t length = 0; // length of the assignment list T *record = nullptr; // data pointer - uint32_t ts = 0; // timestamp of last received payload + uint32_t ts = 0; // Timestamp of last received payload uint8_t pyldLen = 0; // expected payload length for plausibility check MqttSentStatus mqttSentStatus = MqttSentStatus:: NEW_DATA; // indicates the current MqTT sent status }; @@ -146,7 +146,7 @@ class Inverter { statistics_t radioStatistics; // information about transmitted, failed, ... packets HeuristicInv heuristics; // heuristic information / logic uint8_t curCmtFreq = 0; // current used CMT frequency, used to check if freq. was changed during runtime - uint32_t tsMaxAcPower = 0; // holds the timestamp when the MaxAC power was seen + uint32_t tsMaxAcPower = 0; // holds the Timestamp when the MaxAC power was seen bool commEnabled = true; // 'pause night communication' sets this field to false public: @@ -189,7 +189,7 @@ class Inverter { cb(InverterDevInform_Simple, false); // get hardware version } else if((alarmLastId != alarmMesIndex) && (alarmMesIndex != 0)) { cb(AlarmData, false); // get last alarms - } else if((0 == mGridLen) && generalConfig->readGrid) { // read grid profile + } else if((0 == mGridLen) && GeneralConfig->readGrid) { // read grid profile cb(GridOnProFilePara, false); } else if (mGetLossInterval > AHOY_GET_LOSS_INTERVAL) { // get loss rate mGetLossInterval = 1; @@ -213,7 +213,7 @@ class Inverter { if (getChannelFieldValue(CH0, FLD_PART_NUM, rec) == 0) { cb(0x0f, false); // hard- and firmware version for missing HW part nr, delivered by frame 1 mIvRxCnt +=2; - } else if((getChannelFieldValue(CH0, FLD_GRID_PROFILE_CODE, rec) == 0) && generalConfig->readGrid) // read grid profile + } else if((getChannelFieldValue(CH0, FLD_GRID_PROFILE_CODE, rec) == 0) && GeneralConfig->readGrid) // read grid profile cb(0x10, false); // legacy GPF command } } @@ -270,16 +270,18 @@ class Inverter { if(InverterStatus::OFF != status) { mDevControlRequest = true; devControlCmd = cmd; - //assert(App); - //App->triggerTickSend(0); + assert(App); + App->triggerTickSend(id); + return true; } - return (InverterStatus::OFF != status); + return false; } bool setDevCommand(uint8_t cmd) { - if(InverterStatus::OFF != status) + bool retval = (InverterStatus::OFF != status); + if(retval) devControlCmd = cmd; - return (InverterStatus::OFF != status); + return retval; } void addValue(uint8_t pos, const uint8_t buf[], record_t<> *rec) { @@ -409,14 +411,14 @@ class Inverter { if(recordMeas.ts == 0) return false; - if(((*timestamp) - recordMeas.ts) < INVERTER_INACT_THRES_SEC) + if(((*Timestamp) - recordMeas.ts) < INVERTER_INACT_THRES_SEC) avail = true; if(avail) { if(status < InverterStatus::PRODUCING) status = InverterStatus::STARTING; } else { - if(((*timestamp) - recordMeas.ts) > INVERTER_OFF_THRES_SEC) { + if(((*Timestamp) - recordMeas.ts) > INVERTER_OFF_THRES_SEC) { if(status != InverterStatus::OFF) { status = InverterStatus::OFF; actPowerLimit = 0xffff; // power limit will be read once inverter becomes available @@ -817,8 +819,8 @@ class Inverter { } public: - static uint32_t *timestamp; // system timestamp - static cfgInst_t *generalConfig; // general inverter configuration from setup + static uint32_t *Timestamp; // system timestamp + static cfgInst_t *GeneralConfig; // general inverter configuration from setup static IApp *App; uint16_t mDtuRxCnt = 0; @@ -837,9 +839,9 @@ class Inverter { }; template -uint32_t *Inverter::timestamp {0}; +uint32_t *Inverter::Timestamp {0}; template -cfgInst_t *Inverter::generalConfig {0}; +cfgInst_t *Inverter::GeneralConfig {0}; template IApp *Inverter::App {nullptr}; @@ -948,7 +950,7 @@ T calcMaxPowerAcCh0(Inverter<> *iv, uint8_t arg0) { } } if(acPower > acMaxPower) { - iv->tsMaxAcPower = *iv->timestamp; + iv->tsMaxAcPower = *iv->Timestamp; return acPower; } } diff --git a/src/hm/hmSystem.h b/src/hm/hmSystem.h index 28c7905a..0e86881d 100644 --- a/src/hm/hmSystem.h +++ b/src/hm/hmSystem.h @@ -16,8 +16,8 @@ class HmSystem { HmSystem() {} void setup(uint32_t *timestamp, cfgInst_t *config, IApp *app) { - INVERTERTYPE::timestamp = timestamp; - INVERTERTYPE::generalConfig = config; + INVERTERTYPE::Timestamp = timestamp; + INVERTERTYPE::GeneralConfig = config; INVERTERTYPE::App = app; //mInverter[0].app = app; } @@ -26,7 +26,7 @@ class HmSystem { DPRINTLN(DBG_VERBOSE, F("hmSystem.h:addInverter")); INVERTERTYPE *iv = &mInverter[id]; iv->id = id; - iv->config = &mInverter[0].generalConfig->iv[id]; + iv->config = &INVERTERTYPE::GeneralConfig->iv[id]; DPRINT(DBG_VERBOSE, "SERIAL: " + String(iv->config->serial.b[5], HEX)); DPRINTLN(DBG_VERBOSE, " " + String(iv->config->serial.b[4], HEX)); if((iv->config->serial.b[5] == 0x11) || (iv->config->serial.b[5] == 0x10)) { diff --git a/src/hm/nrfHal.h b/src/hm/nrfHal.h index a9228543..a838f8bc 100644 --- a/src/hm/nrfHal.h +++ b/src/hm/nrfHal.h @@ -42,7 +42,7 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { #if defined(CONFIG_IDF_TARGET_ESP32S3) mHostDevice = SPI2_HOST; #else - mHostDevice = (14 == sclk) ? SPI2_HOST : SPI3_HOST; + mHostDevice = (14 == sclk) ? SPI2_HOST : SPI_HOST_OTHER; #endif mSpiPatcher = SpiPatcher::getInstance(mHostDevice); diff --git a/src/hms/cmtHal.h b/src/hms/cmtHal.h index 13a14950..8556a043 100644 --- a/src/hms/cmtHal.h +++ b/src/hms/cmtHal.h @@ -40,7 +40,7 @@ class cmtHal : public SpiPatcherHandle { #if defined(CONFIG_IDF_TARGET_ESP32S3) mHostDevice = SPI2_HOST; #else - mHostDevice = (14 == clk) ? SPI2_HOST : SPI3_HOST; + mHostDevice = (14 == clk) ? SPI2_HOST : SPI_HOST_OTHER; #endif mSpiPatcher = SpiPatcher::getInstance(mHostDevice); diff --git a/src/plugins/Display/epdHal.h b/src/plugins/Display/epdHal.h index e998a885..1718b838 100644 --- a/src/plugins/Display/epdHal.h +++ b/src/plugins/Display/epdHal.h @@ -11,6 +11,7 @@ #include #include + #define EPD_DEFAULT_SPI_SPEED 4000000 // 4 MHz class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { @@ -41,7 +42,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { #if defined(CONFIG_IDF_TARGET_ESP32S3) mHostDevice = SPI3_HOST; #else - mHostDevice = (14 == sclk) ? SPI2_HOST : SPI3_HOST; + mHostDevice = (14 == sclk) ? SPI2_HOST : SPI_HOST_OTHER; #endif mSpiPatcher = SpiPatcher::getInstance(mHostDevice); diff --git a/src/utils/spiPatcher.h b/src/utils/spiPatcher.h index bb14a165..c8f0ba3c 100644 --- a/src/utils/spiPatcher.h +++ b/src/utils/spiPatcher.h @@ -15,6 +15,12 @@ #include #include +#if (SOC_SPI_PERIPH_NUM > 2) + #define SPI_HOST_OTHER SPI3_HOST +#else + #define SPI_HOST_OTHER SPI2_HOST +#endif + class SpiPatcher { protected: explicit SpiPatcher(spi_host_device_t dev) : @@ -74,8 +80,10 @@ class SpiPatcher { assert(mBusState == ESP_OK); if(SPI2_HOST == host_id) mHost2Cnt++; + #if (SOC_SPI_PERIPH_NUM > 2) if(SPI3_HOST == host_id) mHost3Cnt++; + #endif if((mHost2Cnt > 3) || (mHost3Cnt > 3)) DPRINTLN(DBG_ERROR, F("maximum number of SPI devices reached (3)")); @@ -112,7 +120,7 @@ class SpiPatcher { SemaphoreHandle_t mutex; StaticSemaphore_t mutex_buffer; uint8_t mHost2Cnt = 0, mHost3Cnt = 0; - spi_host_device_t mDev = SPI3_HOST; + spi_host_device_t mDev = SPI2_HOST; esp_err_t mBusState = ESP_FAIL; spi_bus_config_t mBusConfig; }; diff --git a/src/web/RestApi.h b/src/web/RestApi.h index 188afe23..ed1aac75 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -1014,8 +1014,6 @@ class RestApi { iv->powerLimit[1] = AbsolutNonPersistent; accepted = iv->setDevControlRequest(ActivePowerContr); - if(accepted) - mApp->triggerTickSend(iv->id); } else if(F("dev") == jsonIn[F("cmd")]) { DPRINTLN(DBG_INFO, F("dev cmd")); iv->setDevCommand(jsonIn[F("val")].as()); From 4fe5c8eef86688fd4a61800f022ad26995d27cbf Mon Sep 17 00:00:00 2001 From: lumapu Date: Tue, 30 Apr 2024 00:05:43 +0200 Subject: [PATCH 14/31] 0.8.114 * fix ESP8266 compile * fix history graph * fix close button color of modal windows in dark mode #1598 * fix only one total field in `/live` #1579 --- src/CHANGES.md | 6 ++++++ src/config/settings.h | 4 ++-- src/defines.h | 2 +- src/web/html/history.html | 30 +++++++++++++++--------------- src/web/html/style.css | 1 + src/web/html/visualization.html | 15 ++++++++------- 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index 35fbc3cc..a023fbe5 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,11 @@ # Development Changes +## 0.8.114 - 2024-04-29 +* fix ESP8266 compile +* fix history graph +* fix close button color of modal windows in dark mode #1598 +* fix only one total field in `/live` #1579 + ## 0.8.113 - 2024-04-25 * code cleanup * fix ESP32-C3 compile diff --git a/src/config/settings.h b/src/config/settings.h index 4f5ba4c0..19579359 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -239,9 +239,9 @@ class settings { std::fill(reinterpret_cast(&mCfg), reinterpret_cast(&mCfg) + sizeof(mCfg), 0); } - void setup(settings_t *&cfg) { + void setup(settings_t *&c) { DPRINTLN(DBG_INFO, F("Initializing FS ..")); - cfg = &mCfg; + c = &mCfg; mCfg.valid = false; #if !defined(ESP32) diff --git a/src/defines.h b/src/defines.h index 36919b21..c0e459a5 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 113 +#define VERSION_PATCH 114 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/web/html/history.html b/src/web/html/history.html index d21e6b96..2a13f572 100644 --- a/src/web/html/history.html +++ b/src/web/html/history.html @@ -45,12 +45,11 @@ s.x_mul = 60 s.ts_start = obj.lastValueTs - (obj.refresh * obj.value.length) s.ts_dur = obj.lastValueTs - s.ts_start - s.ts_pad = (s.ts_dur < 1800) ? s.ts_start % 300 : s.ts_start % 1800 - s.ts_dur -= s.ts_pad + s.ts_pad = (s.ts_dur < 1800) ? 0 : s.ts_start % 1800 + s.ts_dur += s.ts_pad while(s.x_mul * 10 <= s.ts_dur) s.x_mul += (s.x_mul == 60) ? 240 : ((s.x_mul < 1800) ? 300 : 1800) s.x_step = Math.ceil(s.ts_dur / s.x_mul) - s.x_max = s.x_mul * s.x_step s.y_mul = 10 while(s.y_mul * 10 <= obj.max) @@ -79,7 +78,7 @@ ...gridText(n*2, scale), mlNs("g", {transform: "translate(30, 5)"}, [ ...grid(n*2, scale), - ...poly(obj, scale) + ...poly(n*2, obj, scale) ]) ]) } @@ -90,10 +89,10 @@ for(let i = 0; i <= scale.y_max; i += scale.y_mul) { g.push(mlNs("text", {x: 0, y: height-(i*div)+9}, String(i))) } - div = x2 / scale.x_max - for(let i = 0; i < scale.x_max; i++) { - if((i + scale.ts_pad) % scale.x_mul == 0) { - let d = new Date((scale.ts_start + i) * 1000) + div = x2 / scale.ts_dur + for(let i = 0; i < scale.ts_dur; i++) { + if(i % scale.x_mul == 0) { + let d = new Date((scale.ts_start - scale.ts_pad + i) * 1000) g.push(mlNs("text", {x: (i*div)+17, y: height+20}, ("0"+d.getHours()).slice(-2) + ":" + ("0"+d.getMinutes()).slice(-2))) } } @@ -106,28 +105,29 @@ for(let i = 0; i <= scale.y_max; i += scale.y_mul) { g.push(mlNs("line", {x1: 0, x2: x2, y1: height-i*div, y2: height-i*div, "stroke-width": 1, "stroke-dasharray": "1,3", stroke: "#aaa"})) } - div = x2 / scale.x_max - for(let i = 0; i <= scale.x_max; i++) { - if((i + scale.ts_pad) % scale.x_mul == 0) { + div = x2 / scale.ts_dur + for(let i = 0; i <= scale.ts_dur; i++) { + if(i % scale.x_mul == 0) { g.push(mlNs("line", {x1: (i*div), x2: (i*div), y1: 0, y2: height, "stroke-width": 1, "stroke-dasharray": "1,3", stroke: "#aaa"})) } } return g } - function poly(obj, scale) { + function poly(x2, obj, scale) { let pts = "" let i = 0, first = -1, last = -1, lastVal = 0 let div = scale.y_max / height + let xOff = x2 / scale.ts_dur * scale.ts_pad if(div == 0) div = 1 for (val of obj.value) { if(val > 0) { lastVal = val - pts += " " + String(i) + "," + String(height - val / div) + pts += " " + String(i + xOff) + "," + String(height - val / div) if(first < 0) - first = i - last = i + first = i + xOff + last = i + xOff } i += 2 } diff --git a/src/web/html/style.css b/src/web/html/style.css index 2866dda1..2e83078f 100644 --- a/src/web/html/style.css +++ b/src/web/html/style.css @@ -758,6 +758,7 @@ div.hr { font-family: inherit; cursor: pointer; padding: 0; + color: var(--fg); } button.close { diff --git a/src/web/html/visualization.html b/src/web/html/visualization.html index cfc84e72..f0f96112 100644 --- a/src/web/html/visualization.html +++ b/src/web/html/visualization.html @@ -21,6 +21,7 @@ var mNum = 0; var total = Array(6).fill(0); var tPwrAck; + var totalsRendered = false function getErrStr(code) { if("ERR_AUTH") return "{#ERR_AUTH}" @@ -74,6 +75,7 @@ for(var i = 0; i < 6; i++) { total[i] = Math.round(total[i] * 100) / 100; } + totalsRendered = true return ml("div", {class: "row mt-3 mb-5"}, ml("div", {class: "col"}, [ @@ -240,20 +242,18 @@ ]) ); - - var last = true; for(var i = obj.id + 1; i < ivEn.length; i++) { if((i != ivEn.length) && ivEn[i]) { - last = false; getAjax("/api/inverter/id/" + i, parseIv); - break; + return } } - if(last) { - if(mNum > 1) + + if(mNum > 1) { + if(!totalsRendered) mIvHtml.unshift(totals()); - document.getElementById("live").replaceChildren(...mIvHtml); } + document.getElementById("live").replaceChildren(...mIvHtml); } function parseIvAlarm(obj) { @@ -514,6 +514,7 @@ ivEn = Object.values(Object.assign({}, obj["iv"])); mIvHtml = []; mNum = 0; + totalsRendered = false total.fill(0); for(var i = 0; i < obj.iv.length; i++) { if(obj.iv[i]) { From 84ac10531a308b82477b1602a6c76ef9575a346e Mon Sep 17 00:00:00 2001 From: lumapu Date: Fri, 3 May 2024 23:54:06 +0200 Subject: [PATCH 15/31] 0.8.115 * fix inverter communication with manual time sync #1603 * improved queue, only add new object once they not exist * added option to reset values on communication start (sunrise) --- src/CHANGES.md | 5 +++++ src/app.cpp | 5 +++++ src/app.h | 4 +++- src/config/settings.h | 16 ++++++++++------ src/hm/CommQueue.h | 27 +++++++++++++++++++++++---- src/plugins/history.h | 4 ---- src/web/RestApi.h | 1 + src/web/html/setup.html | 8 ++++++-- src/web/lang.json | 7 ++++++- src/web/web.h | 1 + 10 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index a023fbe5..4586fc86 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,10 @@ # Development Changes +## 0.8.115 - 2024-05-03 +* fix inverter communication with manual time sync #1603 +* improved queue, only add new object once they not exist +* added option to reset values on communication start (sunrise) + ## 0.8.114 - 2024-04-29 * fix ESP8266 compile * fix history graph diff --git a/src/app.cpp b/src/app.cpp index 30942fec..b362057a 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -162,6 +162,9 @@ void app::regularTickers(void) { everySec([this]() { mProtection->tickSecond(); }, "prot"); everySec([this]() {mNetwork->tickNetworkLoop(); }, "net"); + if(mConfig->inst.startWithoutTime && !mNetworkConnected) + every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend"); + // Plugins #if defined(PLUGIN_DISPLAY) if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type) @@ -275,6 +278,8 @@ void app::tickIVCommunication(void) { if (mTimestamp >= (mSunset + mConfig->sun.offsetSecEvening)) { // current time is past communication stop, nothing to do. Next update will be done at midnight by tickCalcSunrise nxtTrig = 0; } else { // current time lies within communication start/stop time, set next trigger to communication stop + if((!iv->commEnabled) && mConfig->inst.rstValsCommStart) + zeroValues = true; iv->commEnabled = true; nxtTrig = mSunset + mConfig->sun.offsetSecEvening; } diff --git a/src/app.h b/src/app.h index 4f07adc6..a3692d20 100644 --- a/src/app.h +++ b/src/app.h @@ -303,8 +303,10 @@ class app : public IApp, public ah::Scheduler { DBGPRINTLN(String(newTime)); if(0 == newTime) mNetwork->updateNtpTime(); - else + else { Scheduler::setTimestamp(newTime); + onNtpUpdate(false); + } } uint16_t getHistoryValue(uint8_t type, uint16_t i) override { diff --git a/src/config/settings.h b/src/config/settings.h index 19579359..9459b80b 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -188,6 +188,7 @@ typedef struct { bool rstYieldMidNight; bool rstValsNotAvail; bool rstValsCommStop; + bool rstValsCommStart; bool rstMaxValsMidNight; bool startWithoutTime; bool readGrid; @@ -486,13 +487,14 @@ class settings { mCfg.mqtt.interval = 0; // off mCfg.mqtt.enableRetain = true; - mCfg.inst.sendInterval = SEND_INTERVAL; - mCfg.inst.rstYieldMidNight = false; - mCfg.inst.rstValsNotAvail = false; - mCfg.inst.rstValsCommStop = false; - mCfg.inst.startWithoutTime = false; + mCfg.inst.sendInterval = SEND_INTERVAL; + mCfg.inst.rstYieldMidNight = false; + mCfg.inst.rstValsNotAvail = false; + mCfg.inst.rstValsCommStop = false; + mCfg.inst.rstValsCommStart = false; + mCfg.inst.startWithoutTime = false; mCfg.inst.rstMaxValsMidNight = false; - mCfg.inst.readGrid = true; + mCfg.inst.readGrid = true; for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { mCfg.inst.iv[i].powerLevel = 0xff; // impossible high value @@ -822,6 +824,7 @@ class settings { obj[F("rstMidNight")] = (bool)mCfg.inst.rstYieldMidNight; obj[F("rstNotAvail")] = (bool)mCfg.inst.rstValsNotAvail; obj[F("rstComStop")] = (bool)mCfg.inst.rstValsCommStop; + obj[F("rstComStart")] = (bool)mCfg.inst.rstValsCommStart; obj[F("strtWthtTime")] = (bool)mCfg.inst.startWithoutTime; obj[F("rstMaxMidNight")] = (bool)mCfg.inst.rstMaxValsMidNight; obj[F("rdGrid")] = (bool)mCfg.inst.readGrid; @@ -832,6 +835,7 @@ class settings { getVal(obj, F("rstMidNight"), &mCfg.inst.rstYieldMidNight); getVal(obj, F("rstNotAvail"), &mCfg.inst.rstValsNotAvail); getVal(obj, F("rstComStop"), &mCfg.inst.rstValsCommStop); + getVal(obj, F("rstComStart"), &mCfg.inst.rstValsCommStart); getVal(obj, F("strtWthtTime"), &mCfg.inst.startWithoutTime); getVal(obj, F("rstMaxMidNight"), &mCfg.inst.rstMaxValsMidNight); getVal(obj, F("rdGrid"), &mCfg.inst.readGrid); diff --git a/src/hm/CommQueue.h b/src/hm/CommQueue.h index 328309ac..7164d773 100644 --- a/src/hm/CommQueue.h +++ b/src/hm/CommQueue.h @@ -19,13 +19,19 @@ template class CommQueue { public: void addImportant(Inverter<> *iv, uint8_t cmd) { - dec(&mRdPtr); - mQueue[mRdPtr] = queue_s(iv, cmd, true); + queue_s q(iv, cmd, true); + if(!isIncluded(&q)) { + dec(&mRdPtr); + mQueue[mRdPtr] = q; + } } void add(Inverter<> *iv, uint8_t cmd) { - mQueue[mWrPtr] = queue_s(iv, cmd, false); - inc(&mWrPtr); + queue_s q(iv, cmd, false); + if(!isIncluded(&q)) { + mQueue[mWrPtr] = q; + inc(&mWrPtr); + } } void chgCmd(Inverter<> *iv, uint8_t cmd) { @@ -117,6 +123,19 @@ class CommQueue { --(*ptr); } + private: + bool isIncluded(const queue_s *q) { + uint8_t ptr = mRdPtr; + while (ptr != mWrPtr) { + if(mQueue[ptr].cmd == q->cmd) { + if(mQueue[ptr].iv->id == q->iv->id) + return true; + } + ptr++; + } + return false; + } + protected: std::array mQueue; uint8_t mWrPtr = 0; diff --git a/src/plugins/history.h b/src/plugins/history.h index 7d7be57c..8531b463 100644 --- a/src/plugins/history.h +++ b/src/plugins/history.h @@ -57,7 +57,6 @@ class HistoryData { void tickerSecond() { float curPwr = 0; - //float maxPwr = 0; float yldDay = -0.1; uint32_t ts = 0; @@ -67,7 +66,6 @@ class HistoryData { if (iv == NULL) continue; curPwr += iv->getChannelFieldValue(CH0, FLD_PAC, rec); - //maxPwr += iv->getChannelFieldValue(CH0, FLD_MP, rec); yldDay += iv->getChannelFieldValue(CH0, FLD_YD, rec); if (rec->ts > ts) ts = rec->ts; @@ -81,8 +79,6 @@ class HistoryData { if (curPwr > mMaximumDay) mMaximumDay = roundf(curPwr); } - //if (maxPwr > 0) - // mMaximumDay = roundf(maxPwr); } if ((++mCurPwrDay.loopCnt % mCurPwrDay.refreshCycle) == 0) { diff --git a/src/web/RestApi.h b/src/web/RestApi.h index ed1aac75..b7e33984 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -577,6 +577,7 @@ class RestApi { obj[F("rstMid")] = (bool)mConfig->inst.rstYieldMidNight; obj[F("rstNotAvail")] = (bool)mConfig->inst.rstValsNotAvail; obj[F("rstComStop")] = (bool)mConfig->inst.rstValsCommStop; + obj[F("rstComStart")] = (bool)mConfig->inst.rstValsCommStart; obj[F("strtWthtTm")] = (bool)mConfig->inst.startWithoutTime; obj[F("rdGrid")] = (bool)mConfig->inst.readGrid; obj[F("rstMaxMid")] = (bool)mConfig->inst.rstMaxValsMidNight; diff --git a/src/web/html/setup.html b/src/web/html/setup.html index adb9477a..dbe88663 100644 --- a/src/web/html/setup.html +++ b/src/web/html/setup.html @@ -130,7 +130,11 @@
-
{#INV_PAUSE_SUNSET}
+
{#INV_RESET_SUNRISE}
+
+
+
+
{#INV_RESET_SUNSET}
@@ -670,7 +674,7 @@ function ivGlob(obj) { for(var i of [["invInterval", "interval"]]) document.getElementsByName(i[0])[0].value = obj[i[1]]; - for(var i of ["Mid", "ComStop", "NotAvail", "MaxMid"]) + for(var i of ["Mid", "ComStop", "ComStart", "NotAvail", "MaxMid"]) document.getElementsByName("invRst"+i)[0].checked = obj["rst" + i]; document.getElementsByName("strtWthtTm")[0].checked = obj["strtWthtTm"]; document.getElementsByName("rdGrid")[0].checked = obj["rdGrid"]; diff --git a/src/web/lang.json b/src/web/lang.json index 34b10a25..0093cea9 100644 --- a/src/web/lang.json +++ b/src/web/lang.json @@ -324,10 +324,15 @@ "de": "Werte und Gesamtertrag um Mitternacht zurücksetzen" }, { - "token": "INV_PAUSE_SUNSET", + "token": "INV_RESET_SUNSET", "en": "Reset values at sunset", "de": "Werte bei Sonnenuntergang zurücksetzen" }, + { + "token": "INV_RESET_SUNRISE", + "en": "Reset values at sunrise", + "de": "Werte bei Sonnenaufgang zurücksetzen" + }, { "token": "INV_RESET_NOT_AVAIL", "en": "Reset values when inverter status is 'not available'", diff --git a/src/web/web.h b/src/web/web.h index 7b73fe9f..5d75199b 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -500,6 +500,7 @@ class Web { mConfig->inst.sendInterval = request->arg("invInterval").toInt(); mConfig->inst.rstYieldMidNight = (request->arg("invRstMid") == "on"); mConfig->inst.rstValsCommStop = (request->arg("invRstComStop") == "on"); + mConfig->inst.rstValsCommStart = (request->arg("invRstComStart") == "on"); mConfig->inst.rstValsNotAvail = (request->arg("invRstNotAvail") == "on"); mConfig->inst.startWithoutTime = (request->arg("strtWthtTm") == "on"); mConfig->inst.readGrid = (request->arg("rdGrid") == "on"); From e9ee7c569fa4d24865a6a6d07ead61a7543dd12b Mon Sep 17 00:00:00 2001 From: lumapu Date: Fri, 3 May 2024 23:54:35 +0200 Subject: [PATCH 16/31] 0.8.115 --- src/defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/defines.h b/src/defines.h index c0e459a5..4aad1ee1 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 114 +#define VERSION_PATCH 115 //------------------------------------- typedef struct { uint8_t ch; From abc760e4d235ec2ed9dd5be54be6cb16dd884df4 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 4 May 2024 01:44:31 +0200 Subject: [PATCH 17/31] 0.8.115 * fixed calculation of max AC power (API, MqTT) --- src/CHANGES.md | 1 + src/app.cpp | 1 + src/app.h | 7 +++++ src/appInterface.h | 1 + src/hm/Communication.h | 2 -- src/plugins/MaxPower.h | 56 +++++++++++++++++++++++++++++++++++ src/publisher/pubMqtt.h | 2 +- src/publisher/pubMqttIvData.h | 10 ++++--- src/web/RestApi.h | 3 +- 9 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 src/plugins/MaxPower.h diff --git a/src/CHANGES.md b/src/CHANGES.md index 4586fc86..53485e03 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -4,6 +4,7 @@ * fix inverter communication with manual time sync #1603 * improved queue, only add new object once they not exist * added option to reset values on communication start (sunrise) +* fixed calculation of max AC power (API, MqTT) ## 0.8.114 - 2024-04-29 * fix ESP8266 compile diff --git a/src/app.cpp b/src/app.cpp index b362057a..dd7048e9 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -94,6 +94,7 @@ void app::setup() { mDbgSyslog.setup(mConfig); // be sure to init after mWeb.setup (webSerial uses also debug callback) #endif // Plugins + mMaxPower.setup(&mTimestamp, mConfig->inst.sendInterval); #if defined(PLUGIN_DISPLAY) if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type) #if defined(ESP32) diff --git a/src/app.h b/src/app.h index a3692d20..634696f2 100644 --- a/src/app.h +++ b/src/app.h @@ -31,6 +31,7 @@ #include "utils/syslog.h" #include "web/RestApi.h" #include "web/Protection.h" +#include "plugins/MaxPower.h" #if defined(ENABLE_HISTORY) #include "plugins/history.h" #endif /*ENABLE_HISTORY*/ @@ -309,6 +310,10 @@ class app : public IApp, public ah::Scheduler { } } + float getTotalMaxPower(void) override { + return mMaxPower.getTotalMaxPower(); + } + uint16_t getHistoryValue(uint8_t type, uint16_t i) override { #if defined(ENABLE_HISTORY) return mHistory.valueAt((HistoryStorageType)type, i); @@ -356,6 +361,7 @@ class app : public IApp, public ah::Scheduler { void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true); void payloadEventListener(uint8_t cmd, Inverter<> *iv) { + mMaxPower.payloadEvent(cmd, iv); #if defined(ENABLE_MQTT) if (mMqttEnabled) mMqtt.payloadEventListener(cmd, iv); @@ -457,6 +463,7 @@ class app : public IApp, public ah::Scheduler { uint32_t mSunrise = 0, mSunset = 0; // plugins + MaxPower mMaxPower; #if defined(PLUGIN_DISPLAY) DisplayType mDisplay; DisplayData mDispData; diff --git a/src/appInterface.h b/src/appInterface.h index a5f4d081..f2292ec8 100644 --- a/src/appInterface.h +++ b/src/appInterface.h @@ -64,6 +64,7 @@ class IApp { virtual void resetLockTimeout(void) = 0; virtual bool isProtected(const char *clientIp, const char *token, bool askedFromWeb) const = 0; + virtual float getTotalMaxPower(void) = 0; virtual uint16_t getHistoryValue(uint8_t type, uint16_t i) = 0; virtual uint32_t getHistoryPeriod(uint8_t type) = 0; virtual uint16_t getHistoryMaxDay() = 0; diff --git a/src/hm/Communication.h b/src/hm/Communication.h index 5fd3b487..c4881535 100644 --- a/src/hm/Communication.h +++ b/src/hm/Communication.h @@ -159,8 +159,6 @@ class Communication : public CommQueue<> { mFirstTry = false; mHeu.evalTxChQuality(q->iv, false, 0, 0); mHeu.getTxCh(q->iv); - //q->iv->radioStatistics.rxFailNoAnser++; // should only be one of fail or retransmit. - //q->iv->radioStatistics.txCnt--; q->iv->radioStatistics.retransmits++; q->iv->radio->mRadioWaitTime.stopTimeMonitor(); mState = States::START; diff --git a/src/plugins/MaxPower.h b/src/plugins/MaxPower.h new file mode 100644 index 00000000..8e2d4e37 --- /dev/null +++ b/src/plugins/MaxPower.h @@ -0,0 +1,56 @@ +//----------------------------------------------------------------------------- +// 2024 Ahoy, https://github.com/lumpapu/ahoy +// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/4.0/deed +//----------------------------------------------------------------------------- + +#ifndef __MAX_VALUE__ +#define __MAX_VALUE__ +#pragma once + +#include +#include +#include "../hm/hmDefines.h" + +template +class MaxPower { + public: + MaxPower() { + mTs = nullptr; + mMaxDiff = 60; + mValues.fill(std::make_pair(0, 0.0)); + } + + void setup(uint32_t *ts, uint16_t interval) { + mTs = ts; + mMaxDiff = interval * 4; + } + + void payloadEvent(uint8_t cmd, Inverter<> *iv) { + if(RealTimeRunData_Debug != cmd) + return; + + if(iv->id >= MAX_NUM_INVERTERS) + return; + + record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug); + mValues[iv->id] = std::make_pair(*mTs, iv->getChannelFieldValue(CH0, FLD_PAC, rec)); + } + + T getTotalMaxPower(void) { + T val = 0; + for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { + if((mValues[i].first + mMaxDiff) >= *mTs) + val += mValues[i].second; + else if(mValues[i].first > 0) + return 0; // old data + } + return val; + } + + private: + uint32_t *mTs; + uint32_t mMaxDiff; + std::array, MAX_NUM_INVERTERS> mValues; +}; + +#endif diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index bd34a519..4ce2b76c 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -62,7 +62,7 @@ class PubMqtt { mUptime = uptime; mIntervalTimeout = 1; - SendIvData.setup(sys, utcTs, &mSendList); + SendIvData.setup(app, sys, utcTs, &mSendList); SendIvData.setPublishFunc([this](const char *subTopic, const char *payload, bool retained, uint8_t qos) { publish(subTopic, payload, retained, true, qos); }); diff --git a/src/publisher/pubMqttIvData.h b/src/publisher/pubMqttIvData.h index cd212aa4..aa221be7 100644 --- a/src/publisher/pubMqttIvData.h +++ b/src/publisher/pubMqttIvData.h @@ -24,7 +24,8 @@ class PubMqttIvData { public: PubMqttIvData() : mTotal{}, mSubTopic{}, mVal{} {} - void setup(HMSYSTEM *sys, uint32_t *utcTs, std::queue *sendList) { + void setup(IApp *app, HMSYSTEM *sys, uint32_t *utcTs, std::queue *sendList) { + mApp = app; mSys = sys; mUtcTimestamp = utcTs; mSendList = sendList; @@ -168,9 +169,6 @@ class PubMqttIvData { case FLD_PDC: mTotal[3] += mIv->getValue(mPos, rec); break; - case FLD_MP: - mTotal[4] += mIv->getValue(mPos, rec); - break; } } else mAllTotalFound = false; @@ -261,6 +259,7 @@ class PubMqttIvData { case 4: fieldId = FLD_MP; retained = false; + mTotal[4] = mApp->getTotalMaxPower(); break; } snprintf(mSubTopic.data(), mSubTopic.size(), "total/%s", fields[fieldId]); @@ -274,6 +273,9 @@ class PubMqttIvData { } } + private: + IApp *mApp = nullptr; + HMSYSTEM *mSys = nullptr; uint32_t *mUtcTimestamp = nullptr; pubMqttPublisherType mPublish; diff --git a/src/web/RestApi.h b/src/web/RestApi.h index b7e33984..375c0e52 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -105,7 +105,6 @@ class RestApi { else if(path == "live") getLive(request,root); else if (path == "powerHistory") getPowerHistory(request, root); else if (path == "powerHistoryDay") getPowerHistoryDay(request, root); - else if (path == "yieldDayHistory") getYieldDayHistory(request, root); else { if(path.substring(0, 12) == "inverter/id/") getInverter(root, request->url().substring(17).toInt()); @@ -298,7 +297,6 @@ class RestApi { #if defined(ENABLE_HISTORY) ep[F("powerHistory")] = url + F("powerHistory"); ep[F("powerHistoryDay")] = url + F("powerHistoryDay"); - ep[F("yieldDayHistory")] = url + F("yieldDayHistory"); #endif } @@ -916,6 +914,7 @@ class RestApi { obj[F("fld_units")][fld] = String(units[fieldUnits[dcList[fld]]]); obj[F("fld_names")][fld] = String(fields[dcList[fld]]); } + obj[F("max_total_pwr")] = mApp->getTotalMaxPower(); Inverter<> *iv; for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { From e4a467cd9bb49dd04565f4642551eaa01364a84f Mon Sep 17 00:00:00 2001 From: lumapu Date: Sun, 5 May 2024 09:30:41 +0200 Subject: [PATCH 18/31] 0.8.116 * calculation of max AC power * fix counter overflow communication queue * added max inverter temperature --- src/CHANGES.md | 7 ++++- src/app.cpp | 7 ++++- src/defines.h | 2 +- src/hm/CommQueue.h | 2 +- src/hm/hmDefines.h | 15 ++++++----- src/hm/hmInverter.h | 46 ++++++++++++++++++++++++++------- src/hms/hmsDefines.h | 12 ++++++--- src/platformio.ini | 1 - src/plugins/MaxPower.h | 14 +++++++--- src/web/RestApi.h | 7 ++--- src/web/html/about.html | 2 +- src/web/html/visualization.html | 9 ++++--- 12 files changed, 89 insertions(+), 35 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index 53485e03..87e18bb3 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,8 +1,13 @@ # Development Changes +## 0.8.116 - 2024-05-05 +* calculation of max AC power +* fix counter overflow communication queue +* added max inverter temperature + ## 0.8.115 - 2024-05-03 * fix inverter communication with manual time sync #1603 -* improved queue, only add new object once they not exist +* improved queue, only add new object once they not exist in queue * added option to reset values on communication start (sunrise) * fixed calculation of max AC power (API, MqTT) diff --git a/src/app.cpp b/src/app.cpp index dd7048e9..8ee943da 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -361,6 +361,8 @@ void app::tickMidnight(void) { uint8_t pos = iv->getPosByChFld(i, FLD_MP, rec); iv->setValue(pos, rec, 0.0f); } + if(InverterStatus::OFF == iv->getStatus()) + iv->resetAlarms(true); } } @@ -435,6 +437,9 @@ bool app::sendIv(Inverter<> *iv) { void app:: zeroIvValues(bool checkAvail, bool skipYieldDay) { Inverter<> *iv; bool changed = false; + + mMaxPower.reset(); + // set values to zero, except yields for (uint8_t id = 0; id < mSys.getNumInverters(); id++) { iv = mSys.getInverterByPos(id); @@ -470,7 +475,7 @@ void app:: zeroIvValues(bool checkAvail, bool skipYieldDay) { pos = iv->getPosByChFld(ch, FLD_MP, rec); iv->setValue(pos, rec, 0.0f); } - iv->resetAlarms(); + iv->resetAlarms(true); iv->doCalculations(); } } diff --git a/src/defines.h b/src/defines.h index 4aad1ee1..16b21456 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 115 +#define VERSION_PATCH 116 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/hm/CommQueue.h b/src/hm/CommQueue.h index 7164d773..bf6f6861 100644 --- a/src/hm/CommQueue.h +++ b/src/hm/CommQueue.h @@ -131,7 +131,7 @@ class CommQueue { if(mQueue[ptr].iv->id == q->iv->id) return true; } - ptr++; + inc(&ptr); } return false; } diff --git a/src/hm/hmDefines.h b/src/hm/hmDefines.h index 1dc3148d..98281805 100644 --- a/src/hm/hmDefines.h +++ b/src/hm/hmDefines.h @@ -59,14 +59,14 @@ enum {FLD_UDC = 0, FLD_IDC, FLD_PDC, FLD_YD, FLD_YW, FLD_YT, FLD_IRR, FLD_Q, FLD_EVT, FLD_FW_VERSION, FLD_FW_BUILD_YEAR, FLD_FW_BUILD_MONTH_DAY, FLD_FW_BUILD_HOUR_MINUTE, FLD_BOOTLOADER_VER, FLD_ACT_ACTIVE_PWR_LIMIT, FLD_PART_NUM, FLD_HW_VERSION, FLD_GRID_PROFILE_CODE, - FLD_GRID_PROFILE_VERSION, /*FLD_ACT_REACTIVE_PWR_LIMIT, FLD_ACT_PF,*/ FLD_LAST_ALARM_CODE, FLD_MP}; + FLD_GRID_PROFILE_VERSION, /*FLD_ACT_REACTIVE_PWR_LIMIT, FLD_ACT_PF,*/ FLD_LAST_ALARM_CODE, FLD_MP, FLD_MT}; const char* const fields[] = {"U_DC", "I_DC", "P_DC", "YieldDay", "YieldWeek", "YieldTotal", "U_AC", "U_AC_1N", "U_AC_2N", "U_AC_3N", "UAC_12", "UAC_23", "UAC_31", "I_AC", "IAC_1", "I_AC_2", "I_AC_3", "P_AC", "F_AC", "Temp", "PF_AC", "Efficiency", "Irradiation","Q_AC", "ALARM_MES_ID","FWVersion","FWBuildYear","FWBuildMonthDay","FWBuildHourMinute","BootloaderVersion", "active_PowerLimit", "HWPartNumber", "HWVersion", "GridProfileCode", - "GridProfileVersion", /*"reactivePowerLimit","Powerfactor",*/ "LastAlarmCode", "MaxPower"}; + "GridProfileVersion", /*"reactivePowerLimit","Powerfactor",*/ "LastAlarmCode", "MaxPower", "MaxTemp"}; const char* const notAvail = "n/a"; const uint8_t fieldUnits[] = {UNIT_V, UNIT_A, UNIT_W, UNIT_WH, UNIT_KWH, UNIT_KWH, @@ -103,7 +103,7 @@ const byteAssign_fieldDeviceClass deviceFieldAssignment[] = { #define DEVICE_CLS_ASSIGN_LIST_LEN (sizeof(deviceFieldAssignment) / sizeof(byteAssign_fieldDeviceClass)) // indices to calculation functions, defined in hmInverter.h -enum {CALC_YT_CH0 = 0, CALC_YD_CH0, CALC_UDC_CH, CALC_PDC_CH0, CALC_EFF_CH0, CALC_IRR_CH, CALC_MPAC_CH0, CALC_MPDC_CH}; +enum {CALC_YT_CH0 = 0, CALC_YD_CH0, CALC_UDC_CH, CALC_PDC_CH0, CALC_EFF_CH0, CALC_IRR_CH, CALC_MPAC_CH0, CALC_MPDC_CH, CALC_MT_CH0}; enum {CMD_CALC = 0xffff}; @@ -208,7 +208,8 @@ const byteAssign_t hm1chAssignment[] = { { FLD_YT, UNIT_KWH, CH0, CALC_YT_CH0, 0, CMD_CALC }, { FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC }, { FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC }, - { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC } + { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC }, + { FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC } }; #define HM1CH_LIST_LEN (sizeof(hm1chAssignment) / sizeof(byteAssign_t)) #define HM1CH_PAYLOAD_LEN 30 @@ -246,7 +247,8 @@ const byteAssign_t hm2chAssignment[] = { { FLD_YT, UNIT_KWH, CH0, CALC_YT_CH0, 0, CMD_CALC }, { FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC }, { FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC }, - { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC } + { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC }, + { FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC } }; #define HM2CH_LIST_LEN (sizeof(hm2chAssignment) / sizeof(byteAssign_t)) @@ -301,7 +303,8 @@ const byteAssign_t hm4chAssignment[] = { { FLD_YT, UNIT_KWH, CH0, CALC_YT_CH0, 0, CMD_CALC }, { FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC }, { FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC }, - { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC } + { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC }, + { FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC } }; #define HM4CH_LIST_LEN (sizeof(hm4chAssignment) / sizeof(byteAssign_t)) #define HM4CH_PAYLOAD_LEN 62 diff --git a/src/hm/hmInverter.h b/src/hm/hmInverter.h index 9aa75878..2263188c 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -56,6 +56,9 @@ T calcMaxPowerAcCh0(Inverter<> *iv, uint8_t arg0); template T calcMaxPowerDc(Inverter<> *iv, uint8_t arg0); +template +T calcMaxTemperature(Inverter<> *iv, uint8_t arg0); + template using func_t = T (Inverter<> *, uint8_t); @@ -100,14 +103,15 @@ struct alarm_t { // list of all available functions, mapped in hmDefines.h template const calcFunc_t calcFunctions[] = { - { CALC_YT_CH0, &calcYieldTotalCh0 }, - { CALC_YD_CH0, &calcYieldDayCh0 }, - { CALC_UDC_CH, &calcUdcCh }, - { CALC_PDC_CH0, &calcPowerDcCh0 }, - { CALC_EFF_CH0, &calcEffiencyCh0 }, - { CALC_IRR_CH, &calcIrradiation }, - { CALC_MPAC_CH0, &calcMaxPowerAcCh0 }, - { CALC_MPDC_CH, &calcMaxPowerDc } + { CALC_YT_CH0, &calcYieldTotalCh0 }, + { CALC_YD_CH0, &calcYieldDayCh0 }, + { CALC_UDC_CH, &calcUdcCh }, + { CALC_PDC_CH0, &calcPowerDcCh0 }, + { CALC_EFF_CH0, &calcEffiencyCh0 }, + { CALC_IRR_CH, &calcIrradiation }, + { CALC_MPAC_CH0, &calcMaxPowerAcCh0 }, + { CALC_MPDC_CH, &calcMaxPowerDc }, + { CALC_MT_CH0, &calcMaxTemperature } }; template @@ -147,6 +151,7 @@ class Inverter { HeuristicInv heuristics; // heuristic information / logic uint8_t curCmtFreq = 0; // current used CMT frequency, used to check if freq. was changed during runtime uint32_t tsMaxAcPower = 0; // holds the Timestamp when the MaxAC power was seen + uint32_t tsMaxTemperature = 0; // holds the Timestamp when the max temperature was seen bool commEnabled = true; // 'pause night communication' sets this field to false public: @@ -579,7 +584,7 @@ class Inverter { } } - void resetAlarms() { + void resetAlarms(bool clear = false) { lastAlarm.fill({0, 0, 0}); mAlarmNxtWrPos = 0; alarmCnt = 0; @@ -587,6 +592,11 @@ class Inverter { memset(mOffYD, 0, sizeof(float) * 6); memset(mLastYD, 0, sizeof(float) * 6); + + if(clear) { + tsMaxAcPower = 0; + tsMaxTemperature = 0; + } } bool parseGetLossRate(const uint8_t pyld[], uint8_t len) { @@ -977,4 +987,22 @@ T calcMaxPowerDc(Inverter<> *iv, uint8_t arg0) { return dcMaxPower; } +template +T calcMaxTemperature(Inverter<> *iv, uint8_t arg0) { + DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcMaxTemperature")); + // arg0 = channel + if(NULL != iv) { + record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug); + T temp = iv->getChannelFieldValue(arg0, FLD_T, rec); + T maxTemp = iv->getChannelFieldValue(arg0, FLD_MT, rec); + + if(temp > maxTemp) { + iv->tsMaxTemperature = *iv->Timestamp; + return temp; + } + return maxTemp; + } + return 0; +} + #endif /*__HM_INVERTER_H__*/ diff --git a/src/hms/hmsDefines.h b/src/hms/hmsDefines.h index 61275dc1..c71aa796 100644 --- a/src/hms/hmsDefines.h +++ b/src/hms/hmsDefines.h @@ -33,7 +33,8 @@ const byteAssign_t hms1chAssignment[] = { { FLD_YT, UNIT_KWH, CH0, CALC_YT_CH0, 0, CMD_CALC }, { FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC }, { FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC }, - { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC } + { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC }, + { FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC } }; #define HMS1CH_LIST_LEN (sizeof(hms1chAssignment) / sizeof(byteAssign_t)) #define HMS1CH_PAYLOAD_LEN 30 @@ -70,7 +71,8 @@ const byteAssign_t hms2chAssignment[] = { { FLD_YT, UNIT_KWH, CH0, CALC_YT_CH0, 0, CMD_CALC }, { FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC }, { FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC }, - { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC } + { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC }, + { FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC } }; #define HMS2CH_LIST_LEN (sizeof(hms2chAssignment) / sizeof(byteAssign_t)) #define HMS2CH_PAYLOAD_LEN 42 @@ -123,7 +125,8 @@ const byteAssign_t hms4chAssignment[] = { { FLD_YT, UNIT_KWH, CH0, CALC_YT_CH0, 0, CMD_CALC }, { FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC }, { FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC }, - { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC } + { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC }, + { FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC } }; #define HMS4CH_LIST_LEN (sizeof(hms4chAssignment) / sizeof(byteAssign_t)) #define HMS4CH_PAYLOAD_LEN 66 @@ -199,7 +202,8 @@ const byteAssign_t hmt6chAssignment[] = { { FLD_YT, UNIT_KWH, CH0, CALC_YT_CH0, 0, CMD_CALC }, { FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC }, { FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC }, - { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC } + { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC }, + { FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC } }; #define HMT6CH_LIST_LEN (sizeof(hmt6chAssignment) / sizeof(byteAssign_t)) #define HMT6CH_PAYLOAD_LEN 98 diff --git a/src/platformio.ini b/src/platformio.ini index 53a56dd1..49eff335 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -27,7 +27,6 @@ extra_scripts = lib_deps = https://github.com/esphome/ESPAsyncWebServer @ ^3.1.0 https://github.com/nRF24/RF24.git#v1.4.8 - paulstoffregen/Time @ ^1.6.1 https://github.com/bertmelis/espMqttClient#v1.6.0 bblanchon/ArduinoJson @ ^6.21.3 diff --git a/src/plugins/MaxPower.h b/src/plugins/MaxPower.h index 8e2d4e37..34eeaf9f 100644 --- a/src/plugins/MaxPower.h +++ b/src/plugins/MaxPower.h @@ -17,7 +17,7 @@ class MaxPower { MaxPower() { mTs = nullptr; mMaxDiff = 60; - mValues.fill(std::make_pair(0, 0.0)); + reset(); } void setup(uint32_t *ts, uint16_t interval) { @@ -25,6 +25,11 @@ class MaxPower { mMaxDiff = interval * 4; } + void reset(void) { + mValues.fill(std::make_pair(0, 0.0)); + mLast = 0.0; + } + void payloadEvent(uint8_t cmd, Inverter<> *iv) { if(RealTimeRunData_Debug != cmd) return; @@ -42,14 +47,17 @@ class MaxPower { if((mValues[i].first + mMaxDiff) >= *mTs) val += mValues[i].second; else if(mValues[i].first > 0) - return 0; // old data + return mLast; // old data } - return val; + if(val > mLast) + mLast = val; + return mLast; } private: uint32_t *mTs; uint32_t mMaxDiff; + float mLast; std::array, MAX_NUM_INVERTERS> mValues; }; diff --git a/src/web/RestApi.h b/src/web/RestApi.h index 375c0e52..dcfd8044 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -603,6 +603,7 @@ class RestApi { obj[F("alarm_cnt")] = iv->alarmCnt; obj[F("rssi")] = iv->rssi; obj[F("ts_max_ac_pwr")] = iv->tsMaxAcPower; + obj[F("ts_max_temp")] = iv->tsMaxTemperature; JsonArray ch = obj.createNestedArray("ch"); @@ -905,6 +906,7 @@ class RestApi { void getLive(AsyncWebServerRequest *request, JsonObject obj) { getGeneric(request, obj.createNestedObject(F("generic"))); obj[F("refresh")] = mConfig->inst.sendInterval; + obj[F("max_total_pwr")] = ah::round3(mApp->getTotalMaxPower()); for (uint8_t fld = 0; fld < sizeof(acList); fld++) { obj[F("ch0_fld_units")][fld] = String(units[fieldUnits[acList[fld]]]); @@ -914,7 +916,6 @@ class RestApi { obj[F("fld_units")][fld] = String(units[fieldUnits[dcList[fld]]]); obj[F("fld_names")][fld] = String(fields[dcList[fld]]); } - obj[F("max_total_pwr")] = mApp->getTotalMaxPower(); Inverter<> *iv; for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { @@ -1106,9 +1107,9 @@ class RestApi { private: constexpr static uint8_t acList[] = {FLD_UAC, FLD_IAC, FLD_PAC, FLD_F, FLD_PF, FLD_T, FLD_YT, - FLD_YD, FLD_PDC, FLD_EFF, FLD_Q, FLD_MP}; + FLD_YD, FLD_PDC, FLD_EFF, FLD_Q, FLD_MP, FLD_MT}; constexpr static uint8_t acListHmt[] = {FLD_UAC_1N, FLD_IAC_1, FLD_PAC, FLD_F, FLD_PF, FLD_T, - FLD_YT, FLD_YD, FLD_PDC, FLD_EFF, FLD_Q, FLD_MP}; + FLD_YT, FLD_YD, FLD_PDC, FLD_EFF, FLD_Q, FLD_MP, FLD_MT}; constexpr static uint8_t dcList[] = {FLD_UDC, FLD_IDC, FLD_PDC, FLD_YD, FLD_YT, FLD_IRR, FLD_MP}; private: diff --git a/src/web/html/about.html b/src/web/html/about.html index c0eb8c5e..1b27ac9d 100644 --- a/src/web/html/about.html +++ b/src/web/html/about.html @@ -14,7 +14,7 @@
Used Libraries
- + diff --git a/src/web/html/visualization.html b/src/web/html/visualization.html index f0f96112..ab909ba1 100644 --- a/src/web/html/visualization.html +++ b/src/web/html/visualization.html @@ -106,7 +106,6 @@ total[4] += obj.ch[0][8]; // P_DC total[5] += obj.ch[0][10]; // Q_AC } - total[3] += obj.ch[0][11]; // MAX P_AC total[1] += obj.ch[0][7]; // YieldDay total[2] += obj.ch[0][6]; // YieldTotal @@ -121,7 +120,8 @@ pwrLimit += ", " + (obj.max_pwr * obj.power_limit_read / 100).toFixed(1) + " W"; } - var maxAcPwr = toIsoDateStr(new Date(obj.ts_max_ac_pwr * 1000)); + var maxAcPwrDate = toIsoDateStr(new Date(obj.ts_max_ac_pwr * 1000)) + var maxTempDate = toIsoDateStr(new Date(obj.ts_max_temp * 1000)) return ml("div", {class: "row mt-2"}, ml("div", {class: "col"}, [ ml("div", {class: "p-2 " + clh}, @@ -136,7 +136,7 @@ ml("div", {class: "col a-c"}, ml("span", { class: "pointer", onclick: function() { getAjax("/api/inverter/alarm/" + obj.id, parseIvAlarm); }}, ("{#ALARMS}: " + obj.alarm_cnt))), - ml("div", {class: "col a-r mx-2 mx-md-1"}, String(obj.ch[0][5].toFixed(1)) + t.innerText) + ml("div", {class: "col a-r mx-2 mx-md-1 tooltip", data: (obj.ch[0][12] + t.innerText + "\n" + maxTempDate)}, String(obj.ch[0][5].toFixed(1)) + t.innerText) ]) ), ml("div", {class: "p-2 " + clbg}, [ @@ -147,7 +147,7 @@ ]), ml("div", {class: "hr"}), ml("div", {class: "row mt-2"},[ - numMid(obj.ch[0][11], "W", "{#MAX_AC_POWER}", {class: "fs-6 tooltip", data: maxAcPwr}), + numMid(obj.ch[0][11], "W", "{#MAX_AC_POWER}", {class: "fs-6 tooltip", data: maxAcPwrDate}), numMid(obj.ch[0][8], "W", "{#DC_POWER}"), numMid(obj.ch[0][0], "V", "{#AC_VOLTAGE}"), numMid(obj.ch[0][1], "A", "{#AC_CURRENT}"), @@ -516,6 +516,7 @@ mNum = 0; totalsRendered = false total.fill(0); + total[3] = obj.max_total_pwr for(var i = 0; i < obj.iv.length; i++) { if(obj.iv[i]) { getAjax("/api/inverter/id/" + i, parseIv); From 4191aafeebe06bc5aedb6321d532b9d6eecc8742 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 9 May 2024 00:19:44 +0200 Subject: [PATCH 19/31] 0.8.117 * fix reboot issue #1607 --- src/CHANGES.md | 3 +++ src/app.cpp | 2 +- src/defines.h | 2 +- src/plugins/MaxPower.h | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index 87e18bb3..d2170bf1 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,8 @@ # Development Changes +## 0.8.117 - 2024-05-09 +* fix reboot issue #1607 + ## 0.8.116 - 2024-05-05 * calculation of max AC power * fix counter overflow communication queue diff --git a/src/app.cpp b/src/app.cpp index 8ee943da..a92a5f3c 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -481,7 +481,7 @@ void app:: zeroIvValues(bool checkAvail, bool skipYieldDay) { } if(changed) - payloadEventListener(RealTimeRunData_Debug, NULL); + payloadEventListener(RealTimeRunData_Debug, nullptr); } //----------------------------------------------------------------------------- diff --git a/src/defines.h b/src/defines.h index 16b21456..2d3a5ad4 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 116 +#define VERSION_PATCH 117 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/plugins/MaxPower.h b/src/plugins/MaxPower.h index 34eeaf9f..a8db6909 100644 --- a/src/plugins/MaxPower.h +++ b/src/plugins/MaxPower.h @@ -34,6 +34,9 @@ class MaxPower { if(RealTimeRunData_Debug != cmd) return; + if(nullptr == iv) + return; + if(iv->id >= MAX_NUM_INVERTERS) return; From fe80d3997d8f50cdb4be62ed5f4d1bb625e6c77d Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 9 May 2024 01:01:23 +0200 Subject: [PATCH 20/31] 0.8.117 * fix max temperature tooltip if only one inverter is configured #1605 --- src/CHANGES.md | 3 ++- src/hm/hmInverter.h | 5 +++-- src/web/html/style.css | 2 +- src/web/html/visualization.html | 8 ++++---- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index d2170bf1..f248814e 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,7 +1,8 @@ # Development Changes ## 0.8.117 - 2024-05-09 -* fix reboot issue #1607 +* fix reboot issue #1607 #1606 +* fix max temperature tooltip if only one inverter is configured #1605 ## 0.8.116 - 2024-05-05 * calculation of max AC power diff --git a/src/hm/hmInverter.h b/src/hm/hmInverter.h index 2263188c..445a7bab 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -234,6 +234,7 @@ class Inverter { initAssignment(&recordAlarm, AlarmData); toRadioId(); curCmtFreq = this->config->frequency; // update to frequency read from settings + resetAlarms(true); } uint8_t getPosByChFld(uint8_t channel, uint8_t fieldId, record_t<> *rec) { @@ -594,8 +595,8 @@ class Inverter { memset(mLastYD, 0, sizeof(float) * 6); if(clear) { - tsMaxAcPower = 0; - tsMaxTemperature = 0; + tsMaxAcPower = *Timestamp; + tsMaxTemperature = *Timestamp; } } diff --git a/src/web/html/style.css b/src/web/html/style.css index 2e83078f..cda2df23 100644 --- a/src/web/html/style.css +++ b/src/web/html/style.css @@ -687,7 +687,7 @@ div.hr { border-radius: 3px; display: inline-block; position: absolute; - transform: translate(-50%,-100%); + transform: translate(-50%,-50%); margin:0 auto; color: var(--fg2); min-width: 100px; diff --git a/src/web/html/visualization.html b/src/web/html/visualization.html index ab909ba1..d1f6ded6 100644 --- a/src/web/html/visualization.html +++ b/src/web/html/visualization.html @@ -55,11 +55,11 @@ ]); } - function numMid(val, unit, des, opt={class: "fs-6"}) { + function numMid(val, unit, des, opt={class: "row"}) { return ml("div", {class: "col-6 col-sm-4 col-md-3 mb-2"}, [ - ml("div", {class: "row"}, + ml("div", opt, ml("div", {class: "col"}, [ - ml("span", opt, String(Math.round(val * 100) / 100)), + ml("span", {class: "fs-6"}, String(Math.round(val * 100) / 100)), ml("span", {class: "fs-8 mx-1"}, unit) ]) ), @@ -147,7 +147,7 @@ ]), ml("div", {class: "hr"}), ml("div", {class: "row mt-2"},[ - numMid(obj.ch[0][11], "W", "{#MAX_AC_POWER}", {class: "fs-6 tooltip", data: maxAcPwrDate}), + numMid(obj.ch[0][11], "W", "{#MAX_AC_POWER}", {class: "row tooltip", data: maxAcPwrDate}), numMid(obj.ch[0][8], "W", "{#DC_POWER}"), numMid(obj.ch[0][0], "V", "{#AC_VOLTAGE}"), numMid(obj.ch[0][1], "A", "{#AC_CURRENT}"), From bf78c6ab0d029d602ebcb112d5e0622187202d72 Mon Sep 17 00:00:00 2001 From: lumapu Date: Fri, 10 May 2024 09:54:45 +0200 Subject: [PATCH 21/31] possible reconnect strongest wifi fix --- src/network/AhoyWifiEsp32.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/network/AhoyWifiEsp32.h b/src/network/AhoyWifiEsp32.h index 1c31ea78..52ad6292 100644 --- a/src/network/AhoyWifiEsp32.h +++ b/src/network/AhoyWifiEsp32.h @@ -20,6 +20,7 @@ class AhoyWifi : public AhoyNetwork { if(String(FB_WIFI_SSID) == mConfig->sys.stationSsid) return; // no station wifi defined + WiFi.disconnect(); // clean up WiFi.setHostname(mConfig->sys.deviceName); #if !defined(AP_ONLY) WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); @@ -67,7 +68,6 @@ class AhoyWifi : public AhoyNetwork { mConnected = false; mOnNetworkCB(false); MDNS.end(); - WiFi.disconnect(); begin(); } break; From d43ebc6c5a1bdbaaad91406ef1a6dd23bc670e36 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 11 May 2024 00:41:25 +0200 Subject: [PATCH 22/31] 0.8.118 * possible fix reset max values #1609 * slightly improved WiFi reconnect --- src/CHANGES.md | 4 ++++ src/app.cpp | 24 ++++++++---------------- src/config/settings.h | 16 ++++++++-------- src/defines.h | 2 +- src/hm/hmInverter.h | 22 +++++++++++----------- src/web/html/setup.html | 2 +- src/web/lang.json | 6 +++--- 7 files changed, 36 insertions(+), 40 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index f248814e..8fb3e579 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,9 @@ # Development Changes +## 0.8.118 - 2024-05-10 +* possible fix reset max values #1609 +* slightly improved WiFi reconnect + ## 0.8.117 - 2024-05-09 * fix reboot issue #1607 #1606 * fix max temperature tooltip if only one inverter is configured #1605 diff --git a/src/app.cpp b/src/app.cpp index a92a5f3c..b2f3c615 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -353,20 +353,9 @@ void app::tickMidnight(void) { // reset alarms if(InverterStatus::OFF == iv->getStatus()) iv->resetAlarms(); - - // clear max values - if(mConfig->inst.rstMaxValsMidNight) { - record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug); - for(uint8_t i = 0; i <= iv->channels; i++) { - uint8_t pos = iv->getPosByChFld(i, FLD_MP, rec); - iv->setValue(pos, rec, 0.0f); - } - if(InverterStatus::OFF == iv->getStatus()) - iv->resetAlarms(true); - } } - if (mConfig->inst.rstYieldMidNight) { + if (mConfig->inst.rstValsAtMidNight) { zeroIvValues(!CHECK_AVAIL, !SKIP_YIELD_DAY); #if defined(ENABLE_MQTT) @@ -470,12 +459,15 @@ void app:: zeroIvValues(bool checkAvail, bool skipYieldDay) { pos = iv->getPosByChFld(ch, fld, rec); iv->setValue(pos, rec, 0.0f); } - // zero max power - if(!skipYieldDay) { + // zero max power and max temperature + if(mConfig->inst.rstIncludeMaxVals) { pos = iv->getPosByChFld(ch, FLD_MP, rec); iv->setValue(pos, rec, 0.0f); - } - iv->resetAlarms(true); + pos = iv->getPosByChFld(ch, FLD_MT, rec); + iv->setValue(pos, rec, 0.0f); + iv->resetAlarms(true); + } else + iv->resetAlarms(); iv->doCalculations(); } } diff --git a/src/config/settings.h b/src/config/settings.h index 9459b80b..25243fe7 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -185,11 +185,11 @@ typedef struct { cfgIv_t iv[MAX_NUM_INVERTERS]; uint16_t sendInterval; - bool rstYieldMidNight; + bool rstValsAtMidNight; bool rstValsNotAvail; bool rstValsCommStop; bool rstValsCommStart; - bool rstMaxValsMidNight; + bool rstIncludeMaxVals; bool startWithoutTime; bool readGrid; } cfgInst_t; @@ -488,12 +488,12 @@ class settings { mCfg.mqtt.enableRetain = true; mCfg.inst.sendInterval = SEND_INTERVAL; - mCfg.inst.rstYieldMidNight = false; + mCfg.inst.rstValsAtMidNight = false; mCfg.inst.rstValsNotAvail = false; mCfg.inst.rstValsCommStop = false; mCfg.inst.rstValsCommStart = false; mCfg.inst.startWithoutTime = false; - mCfg.inst.rstMaxValsMidNight = false; + mCfg.inst.rstIncludeMaxVals = false; mCfg.inst.readGrid = true; for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { @@ -821,23 +821,23 @@ class settings { if(set) { obj[F("intvl")] = mCfg.inst.sendInterval; // obj[F("en")] = (bool)mCfg.inst.enabled; - obj[F("rstMidNight")] = (bool)mCfg.inst.rstYieldMidNight; + obj[F("rstMidNight")] = (bool)mCfg.inst.rstValsAtMidNight; obj[F("rstNotAvail")] = (bool)mCfg.inst.rstValsNotAvail; obj[F("rstComStop")] = (bool)mCfg.inst.rstValsCommStop; obj[F("rstComStart")] = (bool)mCfg.inst.rstValsCommStart; obj[F("strtWthtTime")] = (bool)mCfg.inst.startWithoutTime; - obj[F("rstMaxMidNight")] = (bool)mCfg.inst.rstMaxValsMidNight; + obj[F("rstMaxMidNight")] = (bool)mCfg.inst.rstIncludeMaxVals; obj[F("rdGrid")] = (bool)mCfg.inst.readGrid; } else { getVal(obj, F("intvl"), &mCfg.inst.sendInterval); // getVal(obj, F("en"), &mCfg.inst.enabled); - getVal(obj, F("rstMidNight"), &mCfg.inst.rstYieldMidNight); + getVal(obj, F("rstMidNight"), &mCfg.inst.rstValsAtMidNight); getVal(obj, F("rstNotAvail"), &mCfg.inst.rstValsNotAvail); getVal(obj, F("rstComStop"), &mCfg.inst.rstValsCommStop); getVal(obj, F("rstComStart"), &mCfg.inst.rstValsCommStart); getVal(obj, F("strtWthtTime"), &mCfg.inst.startWithoutTime); - getVal(obj, F("rstMaxMidNight"), &mCfg.inst.rstMaxValsMidNight); + getVal(obj, F("rstMaxMidNight"), &mCfg.inst.rstIncludeMaxVals); getVal(obj, F("rdGrid"), &mCfg.inst.readGrid); } diff --git a/src/defines.h b/src/defines.h index 2d3a5ad4..b68eabfd 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 117 +#define VERSION_PATCH 118 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/hm/hmInverter.h b/src/hm/hmInverter.h index 445a7bab..5a9f2130 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -239,15 +239,15 @@ class Inverter { uint8_t getPosByChFld(uint8_t channel, uint8_t fieldId, record_t<> *rec) { DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getPosByChFld")); - if(NULL != rec) { - uint8_t pos = 0; - for(; pos < rec->length; pos++) { - if((rec->assign[pos].ch == channel) && (rec->assign[pos].fieldId == fieldId)) - break; - } - return (pos >= rec->length) ? 0xff : pos; - } else + if(nullptr == rec) return 0xff; + + uint8_t pos = 0; + for(; pos < rec->length; pos++) { + if((rec->assign[pos].ch == channel) && (rec->assign[pos].fieldId == fieldId)) + break; + } + return (pos >= rec->length) ? 0xff : pos; } byteAssign_t *getByteAssign(uint8_t pos, record_t<> *rec) { @@ -363,7 +363,7 @@ class Inverter { bool setValue(uint8_t pos, record_t<> *rec, REC_TYP val) { DPRINTLN(DBG_VERBOSE, F("hmInverter.h:setValue")); - if(NULL == rec) + if(nullptr == rec) return false; if(pos > rec->length) return false; @@ -585,7 +585,7 @@ class Inverter { } } - void resetAlarms(bool clear = false) { + void resetAlarms(bool clearTs = false) { lastAlarm.fill({0, 0, 0}); mAlarmNxtWrPos = 0; alarmCnt = 0; @@ -594,7 +594,7 @@ class Inverter { memset(mOffYD, 0, sizeof(float) * 6); memset(mLastYD, 0, sizeof(float) * 6); - if(clear) { + if(clearTs) { tsMaxAcPower = *Timestamp; tsMaxTemperature = *Timestamp; } diff --git a/src/web/html/setup.html b/src/web/html/setup.html index dbe88663..37193be1 100644 --- a/src/web/html/setup.html +++ b/src/web/html/setup.html @@ -142,7 +142,7 @@
-
{#INV_RESET_MAX_MIDNIGHT}
+
{#INV_RESET_MAX_VALUES}
diff --git a/src/web/lang.json b/src/web/lang.json index 0093cea9..535dfba1 100644 --- a/src/web/lang.json +++ b/src/web/lang.json @@ -339,9 +339,9 @@ "de": "Werte zurücksetzen, sobald der Wechselrichter nicht erreichbar ist" }, { - "token": "INV_RESET_MAX_MIDNIGHT", - "en": "Reset 'max' values at midnight", - "de": "Maximalwerte mitternachts zurücksetzen" + "token": "INV_RESET_MAX_VALUES", + "en": "Inlude reset 'max' values", + "de": "Maximalwerte auch zurücksetzen" }, { "token": "INV_START_WITHOUT_TIME", From 85e3dea2818c5a9b517e490abc492562b44e88b6 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 11 May 2024 01:40:00 +0200 Subject: [PATCH 23/31] 0.8.118 * update AsyncWebserver to `3.2.0` --- src/CHANGES.md | 1 + src/platformio.ini | 2 +- src/web/RestApi.h | 4 ++-- src/web/web.h | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index 8fb3e579..0e90a4f5 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -3,6 +3,7 @@ ## 0.8.118 - 2024-05-10 * possible fix reset max values #1609 * slightly improved WiFi reconnect +* update AsyncWebserver to `3.2.0` ## 0.8.117 - 2024-05-09 * fix reboot issue #1607 #1606 diff --git a/src/platformio.ini b/src/platformio.ini index 49eff335..efcc6e5f 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -25,7 +25,7 @@ extra_scripts = pre:../scripts/reduceGxEPD2.py lib_deps = - https://github.com/esphome/ESPAsyncWebServer @ ^3.1.0 + https://github.com/esphome/ESPAsyncWebServer @ ^3.2.0 https://github.com/nRF24/RF24.git#v1.4.8 paulstoffregen/Time @ ^1.6.1 https://github.com/bertmelis/espMqttClient#v1.6.0 diff --git a/src/web/RestApi.h b/src/web/RestApi.h index dcfd8044..f3477c92 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -572,13 +572,13 @@ class RestApi { } obj[F("interval")] = String(mConfig->inst.sendInterval); obj[F("max_num_inverters")] = MAX_NUM_INVERTERS; - obj[F("rstMid")] = (bool)mConfig->inst.rstYieldMidNight; + obj[F("rstMid")] = (bool)mConfig->inst.rstValsAtMidNight; obj[F("rstNotAvail")] = (bool)mConfig->inst.rstValsNotAvail; obj[F("rstComStop")] = (bool)mConfig->inst.rstValsCommStop; obj[F("rstComStart")] = (bool)mConfig->inst.rstValsCommStart; obj[F("strtWthtTm")] = (bool)mConfig->inst.startWithoutTime; obj[F("rdGrid")] = (bool)mConfig->inst.readGrid; - obj[F("rstMaxMid")] = (bool)mConfig->inst.rstMaxValsMidNight; + obj[F("rstMaxMid")] = (bool)mConfig->inst.rstIncludeMaxVals; } void getInverter(JsonObject obj, uint8_t id) { diff --git a/src/web/web.h b/src/web/web.h index 5d75199b..9c8750fc 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -498,13 +498,13 @@ class Web { if (request->arg("invInterval") != "") mConfig->inst.sendInterval = request->arg("invInterval").toInt(); - mConfig->inst.rstYieldMidNight = (request->arg("invRstMid") == "on"); + mConfig->inst.rstValsAtMidNight = (request->arg("invRstMid") == "on"); mConfig->inst.rstValsCommStop = (request->arg("invRstComStop") == "on"); mConfig->inst.rstValsCommStart = (request->arg("invRstComStart") == "on"); mConfig->inst.rstValsNotAvail = (request->arg("invRstNotAvail") == "on"); mConfig->inst.startWithoutTime = (request->arg("strtWthtTm") == "on"); mConfig->inst.readGrid = (request->arg("rdGrid") == "on"); - mConfig->inst.rstMaxValsMidNight = (request->arg("invRstMaxMid") == "on"); + mConfig->inst.rstIncludeMaxVals = (request->arg("invRstMaxMid") == "on"); // pinout From c29306e0741b586b1effd60aea6461c3b174940d Mon Sep 17 00:00:00 2001 From: rejoe2 Date: Thu, 16 May 2024 08:31:17 +0200 Subject: [PATCH 24/31] Fix MI overnight behaviour will now calculate AC values after midnight reset of state array --- src/hm/Communication.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hm/Communication.h b/src/hm/Communication.h index c4881535..3192c513 100644 --- a/src/hm/Communication.h +++ b/src/hm/Communication.h @@ -895,7 +895,7 @@ class Communication : public CommQueue<> { uint8_t oldState = rec->record[q->iv->getPosByChFld(0, FLD_EVT, rec)]; if ( prntsts != oldState ) { // sth.'s changed? stsok = false; - if(!oldState) { // initial zero value? => just write this channel to main state and raise changed flags + if( (!oldState) || (!q->iv->alarmCnt) ) { // initial zero value? => just write this channel to main state and raise changed flags changedStatus = true; q->iv->alarmCnt = 1; // minimum... } else { From 61ee8aa91f9dbff0059f98614b20c750853f200f Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 16 May 2024 21:42:56 +0200 Subject: [PATCH 25/31] 0.8.119 * fix reset values at midnight if WiFi isn't available #1620 * fix typo in English versions --- src/CHANGES.md | 4 ++++ src/app.cpp | 14 ++++++++------ src/defines.h | 2 +- src/web/lang.json | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index 0e90a4f5..d5bc62a9 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,9 @@ # Development Changes +## 0.8.119 - 2024-05-16 +* fix reset values at midnight if WiFi isn't available #1620 +* fix typo in English versions + ## 0.8.118 - 2024-05-10 * possible fix reset max values #1609 * slightly improved WiFi reconnect diff --git a/src/app.cpp b/src/app.cpp index b2f3c615..accd0c87 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -148,12 +148,14 @@ void app::loop(void) { //----------------------------------------------------------------------------- void app::onNetwork(bool gotIp) { mNetworkConnected = gotIp; - ah::Scheduler::resetTicker(); - regularTickers(); //reinstall regular tickers - every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend"); - mTickerInstallOnce = true; - mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers! - once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2"); + if(gotIp) { + ah::Scheduler::resetTicker(); + regularTickers(); //reinstall regular tickers + every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend"); + mTickerInstallOnce = true; + mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers! + once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2"); + } } //----------------------------------------------------------------------------- diff --git a/src/defines.h b/src/defines.h index b68eabfd..3e7adc01 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 118 +#define VERSION_PATCH 119 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/web/lang.json b/src/web/lang.json index 535dfba1..901b53ed 100644 --- a/src/web/lang.json +++ b/src/web/lang.json @@ -340,7 +340,7 @@ }, { "token": "INV_RESET_MAX_VALUES", - "en": "Inlude reset 'max' values", + "en": "Include reset 'max' values", "de": "Maximalwerte auch zurücksetzen" }, { From e0924303f70980a503837ee1d335e87806f36233 Mon Sep 17 00:00:00 2001 From: lumapu Date: Fri, 17 May 2024 23:25:12 +0200 Subject: [PATCH 26/31] 0.8.119 * add yield day to history graph #1614 --- src/CHANGES.md | 3 ++- src/platformio.ini | 1 + src/plugins/history.h | 2 +- src/web/RestApi.h | 45 ++++++++++++++++++--------------------- src/web/html/history.html | 7 +++++- src/web/lang.json | 5 +++++ 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index d5bc62a9..8e6855b0 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,8 +1,9 @@ # Development Changes -## 0.8.119 - 2024-05-16 +## 0.8.119 - 2024-05-17 * fix reset values at midnight if WiFi isn't available #1620 * fix typo in English versions +* add yield day to history graph #1614 ## 0.8.118 - 2024-05-10 * possible fix reset max values #1609 diff --git a/src/platformio.ini b/src/platformio.ini index efcc6e5f..ebf7ad41 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -163,6 +163,7 @@ build_flags = ${env:esp32-wroom32-minimal.build_flags} -DENABLE_MQTT -DPLUGIN_DISPLAY -DENABLE_HISTORY + -DENABLE_SIMULATOR monitor_filters = esp32_exception_decoder diff --git a/src/plugins/history.h b/src/plugins/history.h index 8531b463..bffbc6d5 100644 --- a/src/plugins/history.h +++ b/src/plugins/history.h @@ -62,9 +62,9 @@ class HistoryData { for (uint8_t i = 0; i < mSys->getNumInverters(); i++) { Inverter<> *iv = mSys->getInverterByPos(i); - record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug); if (iv == NULL) continue; + record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug); curPwr += iv->getChannelFieldValue(CH0, FLD_PAC, rec); yldDay += iv->getChannelFieldValue(CH0, FLD_YD, rec); if (rec->ts > ts) diff --git a/src/web/RestApi.h b/src/web/RestApi.h index f3477c92..f3ca6bc9 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -103,8 +103,8 @@ class RestApi { else if(path == "setup/getip") getIp(root); #endif /* !defined(ETHERNET) */ else if(path == "live") getLive(request,root); - else if (path == "powerHistory") getPowerHistory(request, root); - else if (path == "powerHistoryDay") getPowerHistoryDay(request, root); + else if (path == "powerHistory") getPowerHistory(request, root, HistoryStorageType::POWER); + else if (path == "powerHistoryDay") getPowerHistory(request, root, HistoryStorageType::POWER_DAY); else { if(path.substring(0, 12) == "inverter/id/") getInverter(root, request->url().substring(17).toInt()); @@ -927,40 +927,37 @@ class RestApi { } } - void getPowerHistory(AsyncWebServerRequest *request, JsonObject obj) { + void getPowerHistory(AsyncWebServerRequest *request, JsonObject obj, HistoryStorageType type) { getGeneric(request, obj.createNestedObject(F("generic"))); #if defined(ENABLE_HISTORY) - obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::POWER); + obj[F("refresh")] = mApp->getHistoryPeriod(static_cast(type)); + uint16_t max = 0; for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { - uint16_t value = mApp->getHistoryValue((uint8_t)HistoryStorageType::POWER, fld); + uint16_t value = mApp->getHistoryValue(static_cast(type), fld); obj[F("value")][fld] = value; if (value > max) max = value; } obj[F("max")] = max; - obj[F("lastValueTs")] = mApp->getHistoryLastValueTs((uint8_t)HistoryStorageType::POWER); - #endif /*ENABLE_HISTORY*/ - } - void getPowerHistoryDay(AsyncWebServerRequest *request, JsonObject obj){ - //getGeneric(request, obj.createNestedObject(F("generic"))); - #if defined(ENABLE_HISTORY) - obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::POWER_DAY); - uint16_t max = 0; - for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { - uint16_t value = mApp->getHistoryValue((uint8_t)HistoryStorageType::POWER_DAY, fld); - obj[F("value")][fld] = value; - if (value > max) - max = value; + if(HistoryStorageType::POWER_DAY == type) { + float yldDay = 0; + for (uint8_t i = 0; i < mSys->getNumInverters(); i++) { + Inverter<> *iv = mSys->getInverterByPos(i); + if (iv == NULL) + continue; + record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug); + yldDay += iv->getChannelFieldValue(CH0, FLD_YD, rec); + } + obj[F("yld")] = ah::round3(yldDay / 1000.0); } - obj[F("max")] = max; - obj[F("lastValueTs")] = mApp->getHistoryLastValueTs((uint8_t)HistoryStorageType::POWER_DAY); + + obj[F("lastValueTs")] = mApp->getHistoryLastValueTs(static_cast(type)); #endif /*ENABLE_HISTORY*/ } - - void getYieldDayHistory(AsyncWebServerRequest *request, JsonObject obj) { + /*void getYieldDayHistory(AsyncWebServerRequest *request, JsonObject obj) { //getGeneric(request, obj.createNestedObject(F("generic"))); #if defined(ENABLE_HISTORY) && defined(ENABLE_HISTORY_YIELD_PER_DAY) obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::YIELD); @@ -972,8 +969,8 @@ class RestApi { max = value; } obj[F("max")] = max; - #endif /*ENABLE_HISTORY*/ - } + #endif + }*/ bool setCtrl(JsonObject jsonIn, JsonObject jsonOut, const char *clientIP) { if(jsonIn.containsKey(F("auth"))) { diff --git a/src/web/html/history.html b/src/web/html/history.html index 2a13f572..08227b1f 100644 --- a/src/web/html/history.html +++ b/src/web/html/history.html @@ -133,12 +133,17 @@ } let pts2 = pts + " " + String(last) + "," + String(height) pts2 += " " + String(first) + "," + String(height) - return [ + elm = [ mlNs("polyline", {stroke: "url(#gLine)", fill: "none", points: pts}), mlNs("polyline", {stroke: "none", fill: "url(#gFill)", points: pts2}), mlNs("text", {x: i*.8, y: 10}, "{#MAXIMUM}: " + String(obj.max) + "W"), mlNs("text", {x: i*.8, y: 25}, "{#LAST_VALUE}: " + String(lastVal) + "W") ] + + if(undefined !== obj.yld) + elm.push(mlNs("text", {x: i*.8, y: 40}, "{#YIELD_DAY}: " + String(obj.yld) + "kWh")) + + return elm; } diff --git a/src/web/lang.json b/src/web/lang.json index 901b53ed..0b0754b2 100644 --- a/src/web/lang.json +++ b/src/web/lang.json @@ -1587,6 +1587,11 @@ "token": "LAST_VALUE", "en": "Last value", "de": "Letzter Wert" + }, + { + "token": "YIELD_DAY", + "en": "Yield day", + "de": "Tagesertrag" } ] } From 290bc91a6277808f70cfcf661dc283069e1d607b Mon Sep 17 00:00:00 2001 From: lumapu Date: Fri, 17 May 2024 23:25:55 +0200 Subject: [PATCH 27/31] 0.8.119 * disable simulator for ESP32 --- src/platformio.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/src/platformio.ini b/src/platformio.ini index ebf7ad41..efcc6e5f 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -163,7 +163,6 @@ build_flags = ${env:esp32-wroom32-minimal.build_flags} -DENABLE_MQTT -DPLUGIN_DISPLAY -DENABLE_HISTORY - -DENABLE_SIMULATOR monitor_filters = esp32_exception_decoder From 16b57416df99a7cdc7fdbfa2d03c84110702ccf7 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 18 May 2024 01:56:29 +0200 Subject: [PATCH 28/31] 0.8.119 * added script and [instructions](../manual/factory_firmware.md) how to generate factory firmware which includes predefined settings --- .gitignore | 1 + manual/factory_firmware.md | 59 ++++++++++++++++++++++++++++++++++ scripts/add_littlefs_binary.py | 55 +++++++++++++++++++++++++++++++ src/CHANGES.md | 1 + src/platformio.ini | 1 + 5 files changed, 117 insertions(+) create mode 100644 manual/factory_firmware.md create mode 100644 scripts/add_littlefs_binary.py diff --git a/.gitignore b/.gitignore index b5c699cc..bb6620a2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ src/config/config_override.h src/web/html/h/* src/web/html/tmp/* +src/data/* /**/Debug /**/v16/* *.db diff --git a/manual/factory_firmware.md b/manual/factory_firmware.md new file mode 100644 index 00000000..e96cf575 --- /dev/null +++ b/manual/factory_firmware.md @@ -0,0 +1,59 @@ +# Generate factory firmware + +If the firmware should already contain predefined settings this guide will help you to compile these into a single binary file. + +## Generate default settings + +First install on the requested platform the standard firmware and configure everything to your needs. Once you did all changes store them and export them to a `json` file. + +## Further prepare default settings + +First create a directory `data` inside the following project path: `src/`. + +As the export removes all your password you need to add them again to the `json` file. Open the `json` file with a text editor and search for all the `"pwd": ""`. Between the second bunch of quotation marks you have to place the password. + +*Note: It's recommended to keep all information in one line to save space on the ESP littlefs partition* + +Next rename your export file to `settings.json` and move it to the new created directory. It should be look similar to this: + +``` +ahoy + |-- src + |-- data + |-- settings.json + |-- config + |-- network + ... +``` + +## modify platform.ini to build factory binary + +Open the file `src/platformio.ini` and uncomment the following line `#post:../scripts/add_littlefs_binary.py` (remove the `#`) + +## build firmware + +Choose your prefered environment and build firmware as usual. Once the process is finished you should find along with the standard `firmware.bin` an additional file called `firmware.factory.bin`. Both files are located here: `src/.pio/build/[ENVIRONMENT]/` + +## Upload to device + +Python: +`esptool.py -b 921600 write_flash --flash_mode dio --flash_size detect 0x10000 firmware.factory.bin` + +Windows: +`esptool.exe -b 921600 write_flash --flash_mode dio --flash_size detect 0x10000 firmware.factory.bin` + +For a 4MB flash size the upload should be finished within 22 seconds. + +## Testing + +Reboot your ESP an check if all your settings are present. + +## Keep updated with 'Mainline' + +From time to time a new version of AhoyDTU will be published. To get this changes into your alread prepared factory binary generation environment you have to do only a few steps: + +1. revert the changes of `platformio.ini` by executing from repository root: `git checkout src/platformio.ini` +2. pull new changes from remote: `git pull` +3. modify the `platformio.ini` again as you can read above (remove comment) +4. build and upload +5. enjoy diff --git a/scripts/add_littlefs_binary.py b/scripts/add_littlefs_binary.py new file mode 100644 index 00000000..7afc3391 --- /dev/null +++ b/scripts/add_littlefs_binary.py @@ -0,0 +1,55 @@ +import os +import subprocess +import shutil +Import("env") + + +def build_littlefs(): + result = subprocess.run(["pio", "run", "--target", "buildfs", "--environment", env['PIOENV']]) + if result.returncode != 0: + print("Error building LittleFS:") + exit(1) + else: + print("LittleFS build successful") + +def merge_bins(): + flash_size = int(env.get("BOARD_FLASH_SIZE", "4MB").replace("MB", "")) + app0_offset = 0x10000 + if env['PIOENV'][:7] == "esp8266": + app0_offset = 0 + elif env['PIOENV'][:7] == "esp8285": + app0_offset = 0 + + littlefs_offset = 0x290000 + if flash_size == 8: + littlefs_offset = 0x670000 + elif flash_size == 16: + littlefs_offset = 0xc90000 + + # save current wd + start = os.getcwd() + os.chdir('.pio/build/' + env['PIOENV'] + '/') + + with open("firmware.bin", "rb") as firmware_file: + firmware_data = firmware_file.read() + + with open("littlefs.bin", "rb") as littlefs_file: + littlefs_data = littlefs_file.read() + + with open("firmware.factory.bin", "wb") as merged_file: + # fill gap with 0xff + merged_file.write(firmware_data) + if len(firmware_data) < (littlefs_offset - app0_offset): + merged_file.write(b'\xFF' * ((littlefs_offset - app0_offset) - len(firmware_data))) + + merged_file.write(littlefs_data) + + os.chdir(start) + +def main(target, source, env): + build_littlefs() + merge_bins() + + +# ensure that script is called once firmeware was compiled +env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", main) diff --git a/src/CHANGES.md b/src/CHANGES.md index 8e6855b0..d3721677 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -4,6 +4,7 @@ * fix reset values at midnight if WiFi isn't available #1620 * fix typo in English versions * add yield day to history graph #1614 +* added script and [instructions](../manual/factory_firmware.md) how to generate factory firmware which includes predefined settings ## 0.8.118 - 2024-05-10 * possible fix reset max values #1609 diff --git a/src/platformio.ini b/src/platformio.ini index efcc6e5f..9f226af0 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -23,6 +23,7 @@ extra_scripts = pre:../scripts/convertHtml.py pre:../scripts/applyPatches.py pre:../scripts/reduceGxEPD2.py + #post:../scripts/add_littlefs_binary.py lib_deps = https://github.com/esphome/ESPAsyncWebServer @ ^3.2.0 From 2f84bc488ec662dc3e04d3e73c34eeae9909cf84 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 18 May 2024 02:05:06 +0200 Subject: [PATCH 29/31] 0.8.119 * update offsets in python script --- manual/factory_firmware.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/manual/factory_firmware.md b/manual/factory_firmware.md index e96cf575..f5cd3d91 100644 --- a/manual/factory_firmware.md +++ b/manual/factory_firmware.md @@ -1,4 +1,4 @@ -# Generate factory firmware +# Generate factory firmware (ESP32) If the firmware should already contain predefined settings this guide will help you to compile these into a single binary file. @@ -36,11 +36,23 @@ Choose your prefered environment and build firmware as usual. Once the process i ## Upload to device +Navigate to the firmware output directory `src/.pio/build/[ENVIRONMENT]/` and open a terminal. + +### ESP32 + +Python: +`esptool.py -b 921600 write_flash --flash_mode dio --flash_size detect 0x1000 bootloader.bin 0x8000 partitions.bin 0x10000 firmware.factory.bin` + +Windows: +`esptool.exe -b 921600 write_flash --flash_mode dio --flash_size detect 0x1000 bootloader.bin 0x8000 partitions.bin 0x10000 firmware.factory.bin` + +### ESP32-S3 (OpenDTU Fusion Board) + Python: -`esptool.py -b 921600 write_flash --flash_mode dio --flash_size detect 0x10000 firmware.factory.bin` +`esptool.py -b 921600 write_flash --flash_mode dio --flash_size detect 0x0 firmware.factory.bin` Windows: -`esptool.exe -b 921600 write_flash --flash_mode dio --flash_size detect 0x10000 firmware.factory.bin` +`esptool.exe -b 921600 write_flash --flash_mode dio --flash_size detect 0x0 firmware.factory.bin` For a 4MB flash size the upload should be finished within 22 seconds. From 43c3a965c9be67f3ec18892ce6a2488cdb9a0c20 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 18 May 2024 02:43:21 +0200 Subject: [PATCH 30/31] 0.8.119 * fix compile for versions without history --- src/web/RestApi.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/web/RestApi.h b/src/web/RestApi.h index f3ca6bc9..eaa18a0f 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -103,8 +103,10 @@ class RestApi { else if(path == "setup/getip") getIp(root); #endif /* !defined(ETHERNET) */ else if(path == "live") getLive(request,root); + #if defined(ENABLE_HISTORY) else if (path == "powerHistory") getPowerHistory(request, root, HistoryStorageType::POWER); else if (path == "powerHistoryDay") getPowerHistory(request, root, HistoryStorageType::POWER_DAY); + #endif /*ENABLE_HISTORY*/ else { if(path.substring(0, 12) == "inverter/id/") getInverter(root, request->url().substring(17).toInt()); @@ -927,9 +929,9 @@ class RestApi { } } + #if defined(ENABLE_HISTORY) void getPowerHistory(AsyncWebServerRequest *request, JsonObject obj, HistoryStorageType type) { getGeneric(request, obj.createNestedObject(F("generic"))); - #if defined(ENABLE_HISTORY) obj[F("refresh")] = mApp->getHistoryPeriod(static_cast(type)); uint16_t max = 0; @@ -954,12 +956,12 @@ class RestApi { } obj[F("lastValueTs")] = mApp->getHistoryLastValueTs(static_cast(type)); - #endif /*ENABLE_HISTORY*/ } + #endif /*ENABLE_HISTORY*/ - /*void getYieldDayHistory(AsyncWebServerRequest *request, JsonObject obj) { - //getGeneric(request, obj.createNestedObject(F("generic"))); - #if defined(ENABLE_HISTORY) && defined(ENABLE_HISTORY_YIELD_PER_DAY) + + #if defined(ENABLE_HISTORY_YIELD_PER_DAY) + void getYieldDayHistory(AsyncWebServerRequest *request, JsonObject obj) { obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::YIELD); uint16_t max = 0; for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { @@ -969,8 +971,8 @@ class RestApi { max = value; } obj[F("max")] = max; - #endif - }*/ + } + #endif /*ENABLE_HISTORY_YIELD_PER_DAY*/ bool setCtrl(JsonObject jsonIn, JsonObject jsonOut, const char *clientIP) { if(jsonIn.containsKey(F("auth"))) { From 810d36c341b054581ada79d8d06f3ab2050b72c9 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 18 May 2024 02:44:29 +0200 Subject: [PATCH 31/31] 0.8.119 * merge PR: Fix MI overnight behaviour #1626 --- src/CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CHANGES.md b/src/CHANGES.md index d3721677..1cef0a8a 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -5,6 +5,7 @@ * fix typo in English versions * add yield day to history graph #1614 * added script and [instructions](../manual/factory_firmware.md) how to generate factory firmware which includes predefined settings +* merge PR: Fix MI overnight behaviour #1626 ## 0.8.118 - 2024-05-10 * possible fix reset max values #1609