diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..9fcb3325 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +patches/GxEPD2_HAL.patch eol=lf \ No newline at end of file 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..f5cd3d91 --- /dev/null +++ b/manual/factory_firmware.md @@ -0,0 +1,71 @@ +# 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. + +## 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 + +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 0x0 firmware.factory.bin` + +Windows: +`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. + +## 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/patches/GxEPD2_HAL.patch b/patches/GxEPD2_HAL.patch new file mode 100644 index 00000000..d7b394eb --- /dev/null +++ b/patches/GxEPD2_HAL.patch @@ -0,0 +1,392 @@ +diff --git a/src/GxEPD2_EPD.cpp b/src/GxEPD2_EPD.cpp +index 8df8bef..e9dfb19 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->writeCmd(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..13424b6 +--- /dev/null ++++ b/src/GxEPD2_Hal.h +@@ -0,0 +1,19 @@ ++#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 val) = 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/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/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/scripts/applyPatches.py b/scripts/applyPatches.py index 91b3498c..1672ab2f 100644 --- a/scripts/applyPatches.py +++ b/scripts/applyPatches.py @@ -28,8 +28,8 @@ 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") +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"): applyPatch("RF24", "../patches/RF24_Hal.patch") diff --git a/src/CHANGES.md b/src/CHANGES.md index 135fe0fc..1cef0a8a 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,47 @@ # Development Changes +## 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 +* 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 +* slightly improved WiFi reconnect +* update AsyncWebserver to `3.2.0` + +## 0.8.117 - 2024-05-09 +* 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 +* 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 in queue +* 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 +* 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 + +## 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/app.cpp b/src/app.cpp index d00a95c2..accd0c87 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(); @@ -103,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) @@ -126,9 +118,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 +132,7 @@ void app::loop(void) { mNrfRadio.loop(); #if defined(ESP32) - if(mConfig->cmt.enabled) - mCmtRadio.loop(); + mCmtRadio.loop(); #endif ah::Scheduler::loop(); @@ -159,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"); + } } //----------------------------------------------------------------------------- @@ -174,6 +165,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) @@ -287,6 +281,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; } @@ -359,18 +355,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 (mConfig->inst.rstYieldMidNight) { + if (mConfig->inst.rstValsAtMidNight) { zeroIvValues(!CHECK_AVAIL, !SKIP_YIELD_DAY); #if defined(ENABLE_MQTT) @@ -441,6 +428,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); @@ -471,18 +461,21 @@ 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(); + pos = iv->getPosByChFld(ch, FLD_MT, rec); + iv->setValue(pos, rec, 0.0f); + iv->resetAlarms(true); + } else + iv->resetAlarms(); iv->doCalculations(); } } if(changed) - payloadEventListener(RealTimeRunData_Debug, NULL); + payloadEventListener(RealTimeRunData_Debug, nullptr); } //----------------------------------------------------------------------------- diff --git a/src/app.h b/src/app.h index a7be0a55..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*/ @@ -217,7 +218,7 @@ class app : public IApp, public ah::Scheduler { } bool getSettingsValid() override { - return mSettings.getValid(); + return mConfig->valid; } bool getRebootRequestState() override { @@ -303,8 +304,14 @@ class app : public IApp, public ah::Scheduler { DBGPRINTLN(String(newTime)); if(0 == newTime) mNetwork->updateNtpTime(); - else + else { Scheduler::setTimestamp(newTime); + onNtpUpdate(false); + } + } + + float getTotalMaxPower(void) override { + return mMaxPower.getTotalMaxPower(); } uint16_t getHistoryValue(uint8_t type, uint16_t i) override { @@ -354,15 +361,14 @@ 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) + mMaxPower.payloadEvent(cmd, iv); #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 +431,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 +452,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; @@ -459,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/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 c608dabb..d10f641b 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -186,10 +186,11 @@ typedef struct { cfgIv_t iv[MAX_NUM_INVERTERS]; uint16_t sendInterval; - bool rstYieldMidNight; + bool rstValsAtMidNight; bool rstValsNotAvail; bool rstValsCommStop; - bool rstMaxValsMidNight; + bool rstValsCommStart; + bool rstIncludeMaxVals; bool startWithoutTime; bool readGrid; } cfgInst_t; @@ -240,8 +241,9 @@ class settings { std::fill(reinterpret_cast(&mCfg), reinterpret_cast(&mCfg) + sizeof(mCfg), 0); } - void setup() { + void setup(settings_t *&c) { DPRINTLN(DBG_INFO, F("Initializing FS ..")); + c = &mCfg; mCfg.valid = false; #if !defined(ESP32) @@ -277,14 +279,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; } @@ -495,13 +489,14 @@ class settings { mCfg.mqtt.json = 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.rstMaxValsMidNight = false; - mCfg.inst.readGrid = true; + mCfg.inst.sendInterval = SEND_INTERVAL; + mCfg.inst.rstValsAtMidNight = false; + mCfg.inst.rstValsNotAvail = false; + mCfg.inst.rstValsCommStop = false; + mCfg.inst.rstValsCommStart = false; + mCfg.inst.startWithoutTime = false; + mCfg.inst.rstIncludeMaxVals = false; + mCfg.inst.readGrid = true; for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { mCfg.inst.iv[i].powerLevel = 0xff; // impossible high value @@ -830,21 +825,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); } @@ -919,6 +916,7 @@ class settings { } #endif + private: settings_t mCfg; bool mLastSaveSucceed = 0; }; diff --git a/src/defines.h b/src/defines.h index cab8676d..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 111 +#define VERSION_PATCH 119 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/hm/CommQueue.h b/src/hm/CommQueue.h index 328309ac..bf6f6861 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; + } + inc(&ptr); + } + return false; + } + protected: std::array mQueue; uint8_t mWrPtr = 0; diff --git a/src/hm/Communication.h b/src/hm/Communication.h index 5fd3b487..3192c513 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; @@ -897,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 { diff --git a/src/hm/hmDefines.h b/src/hm/hmDefines.h index d3d8d87d..06d0aad0 100644 --- a/src/hm/hmDefines.h +++ b/src/hm/hmDefines.h @@ -304,7 +304,7 @@ const byteAssign_t hm4chAssignment[] = { { 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_MT, UNIT_C, CH0, CALC_MT_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 72226154..34d04491 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -59,6 +59,9 @@ T calcMaxTempCh0(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); @@ -87,7 +90,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 }; @@ -103,15 +106,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_MT_CH0, &calcMaxTempCh0 } + { 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 @@ -150,8 +153,8 @@ 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 tsMaxTemp = 0; // holds the timestamp when the max temp was seen + 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: @@ -194,7 +197,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; @@ -218,7 +221,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 } } @@ -234,19 +237,20 @@ 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) { 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) { @@ -275,16 +279,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) { @@ -360,7 +366,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; @@ -414,14 +420,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 @@ -582,7 +588,7 @@ class Inverter { } } - void resetAlarms() { + void resetAlarms(bool clearTs = false) { lastAlarm.fill({0, 0, 0}); mAlarmNxtWrPos = 0; alarmCnt = 0; @@ -590,6 +596,11 @@ class Inverter { memset(mOffYD, 0, sizeof(float) * 6); memset(mLastYD, 0, sizeof(float) * 6); + + if(clearTs) { + tsMaxAcPower = *Timestamp; + tsMaxTemperature = *Timestamp; + } } bool parseGetLossRate(const uint8_t pyld[], uint8_t len) { @@ -822,8 +833,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; @@ -842,9 +853,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}; @@ -953,7 +964,7 @@ T calcMaxPowerAcCh0(Inverter<> *iv, uint8_t arg0) { } } if(acPower > acMaxPower) { - iv->tsMaxAcPower = *iv->timestamp; + iv->tsMaxAcPower = *iv->Timestamp; return acPower; } } @@ -1002,4 +1013,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/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 89fe0885..a838f8bc 100644 --- a/src/hm/nrfHal.h +++ b/src/hm/nrfHal.h @@ -9,7 +9,6 @@ #pragma once #include "../utils/spiPatcher.h" - #include #include @@ -18,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); @@ -42,7 +39,13 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle { mPinEn = static_cast(en); mSpiSpeed = speed; - mHostDevice = mSpiPatcher->getDevice(); + #if defined(CONFIG_IDF_TARGET_ESP32S3) + mHostDevice = SPI2_HOST; + #else + mHostDevice = (14 == sclk) ? SPI2_HOST : SPI_HOST_OTHER; + #endif + + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); gpio_reset_pin(mPinMosi); gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT); @@ -56,6 +59,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,14 +76,14 @@ 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); gpio_set_direction(mPinEn, GPIO_MODE_OUTPUT); gpio_set_level(mPinEn, 0); } - bool begin() override { return true; } 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; diff --git a/src/hms/cmtHal.h b/src/hms/cmtHal.h index a4bec587..8556a043 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,13 @@ class cmtHal : public SpiPatcherHandle { mPinFcs = static_cast(fcs); mSpiSpeed = speed; - mHostDevice = mSpiPatcher->getDevice(); + #if defined(CONFIG_IDF_TARGET_ESP32S3) + mHostDevice = SPI2_HOST; + #else + mHostDevice = (14 == clk) ? SPI2_HOST : SPI_HOST_OTHER; + #endif + + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); gpio_reset_pin(mPinSdio); gpio_set_direction(mPinSdio, GPIO_MODE_INPUT_OUTPUT); @@ -50,6 +54,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, @@ -66,7 +71,8 @@ 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); spi_device_interface_config_t devcfg_fifo = { 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 b41431b4..ab2eab9e 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,14 @@ 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 - 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)); + 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 @@ -79,8 +70,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 +127,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/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..52ad6292 100644 --- a/src/network/AhoyWifiEsp32.h +++ b/src/network/AhoyWifiEsp32.h @@ -17,16 +17,16 @@ 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.disconnect(); // clean up 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 +34,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 +57,41 @@ 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(); + 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/platformio.ini b/src/platformio.ini index 53a56dd1..9f226af0 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -23,11 +23,11 @@ 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.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 bblanchon/ArduinoJson @ ^6.21.3 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..1718b838 --- /dev/null +++ b/src/plugins/Display/epdHal.h @@ -0,0 +1,304 @@ +//----------------------------------------------------------------------------- +// 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() {} + + void patch() override { + esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, 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(mPinBusy, 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; + + #if defined(CONFIG_IDF_TARGET_ESP32S3) + mHostDevice = SPI3_HOST; + #else + mHostDevice = (14 == sclk) ? SPI2_HOST : SPI_HOST_OTHER; + #endif + + mSpiPatcher = SpiPatcher::getInstance(mHostDevice); + + 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 + }; + mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi); + + if(GPIO_NUM_NC != mPinRst) { + gpio_reset_pin(mPinRst); + gpio_set_direction(mPinRst, GPIO_MODE_OUTPUT); + gpio_set_level(mPinRst, HIGH); + } + + 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(); + + size_t spiLen = static_cast(1u) << 3; + spi_transaction_t t = { + .flags = 0, + .cmd = 0, + .addr = 0, + .length = spiLen, + .rxlength = spiLen, + .user = NULL, + .tx_buffer = data, + .rx_buffer = data + }; + 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(); + + size_t spiLen = static_cast(n) << 3; + spi_transaction_t t = { + .flags = 0, + .cmd = 0, + .addr = 0, + .length = spiLen, + .rxlength = spiLen, + .user = NULL, + .tx_buffer = data, + .rx_buffer = data + }; + 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 = SPI_TRANS_CS_KEEP_ACTIVE, + .cmd = 0, + .addr = 0, + .length = 1u, + .rxlength = 1u, + .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 = spiLen, + .rxlength = spiLen, + .user = NULL, + .tx_buffer = data, + .rx_buffer = data + }; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t)); + gpio_set_level(mPinDc, HIGH); + + 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_device_acquire_bus(spi, portMAX_DELAY); + + size_t spiLen = static_cast(1u) << 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 + }; + 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]); + + spiLen = static_cast(n-1) << 3; + spi_transaction_t t1 = { + .flags = SPI_TRANS_CS_KEEP_ACTIVE, + .cmd = 0, + .addr = 0, + .length = spiLen, + .rxlength = spiLen, + .user = NULL, + .tx_buffer = data, + .rx_buffer = data + }; + ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t1)); + spi_device_release_bus(spi); + + release_spi(); + } + + void startTransfer(void) override { + request_spi(); + } + + void endTransfer(void) override { + release_spi(); + } + + void transfer(const uint8_t val) override { + 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 = spiLen, + .rxlength = spiLen, + .user = NULL, + .tx_buffer = data, + .rx_buffer = data + }; + 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__*/ diff --git a/src/plugins/MaxPower.h b/src/plugins/MaxPower.h new file mode 100644 index 00000000..a8db6909 --- /dev/null +++ b/src/plugins/MaxPower.h @@ -0,0 +1,67 @@ +//----------------------------------------------------------------------------- +// 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; + reset(); + } + + void setup(uint32_t *ts, uint16_t interval) { + mTs = ts; + 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; + + if(nullptr == iv) + 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 mLast; // old data + } + if(val > mLast) + mLast = val; + return mLast; + } + + private: + uint32_t *mTs; + uint32_t mMaxDiff; + float mLast; + std::array, MAX_NUM_INVERTERS> mValues; +}; + +#endif diff --git a/src/plugins/history.h b/src/plugins/history.h index 7d7be57c..bffbc6d5 100644 --- a/src/plugins/history.h +++ b/src/plugins/history.h @@ -57,17 +57,15 @@ class HistoryData { void tickerSecond() { float curPwr = 0; - //float maxPwr = 0; float yldDay = -0.1; uint32_t ts = 0; 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); - //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/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 1650560e..71f53f7b 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -62,7 +62,7 @@ class PubMqtt { mUptime = uptime; mIntervalTimeout = 1; - SendIvData.setup(sys, cfg_mqtt->json, 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 be6ac2c0..984578ff 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, bool json, uint32_t *utcTs, std::queue *sendList) { + void setup(IApp *app, HMSYSTEM *sys, uint32_t *utcTs, std::queue *sendList) { + mApp = app; mSys = sys; mJson = json; mUtcTimestamp = utcTs; @@ -169,9 +170,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; @@ -299,6 +297,7 @@ class PubMqttIvData { case 4: fieldId = FLD_MP; retained = false; + mTotal[4] = mApp->getTotalMaxPower(); break; } if (!mJson) { @@ -328,6 +327,9 @@ class PubMqttIvData { } } + private: + IApp *mApp = nullptr; + HMSYSTEM *mSys = nullptr; uint32_t *mUtcTimestamp = nullptr; pubMqttPublisherType mPublish; 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..c8f0ba3c 100644 --- a/src/utils/spiPatcher.h +++ b/src/utils/spiPatcher.h @@ -9,23 +9,38 @@ #if defined(ESP32) +#include "dbg.h" #include "spiPatcherHandle.h" #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) : - mHostDevice(dev), mCurHandle(nullptr) { + mCurHandle(nullptr) { // Use binary semaphore instead of mutex for performance reasons mutex = xSemaphoreCreateBinaryStatic(&mutex_buffer); xSemaphoreGive(mutex); + mDev = dev; + mBusState = ESP_FAIL; + } - spi_bus_config_t buscfg = { - .mosi_io_num = -1, - .miso_io_num = -1, - .sclk_io_num = -1, + public: + SpiPatcher(const SpiPatcher &other) = delete; + void operator=(const SpiPatcher &) = delete; + + 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, @@ -36,26 +51,48 @@ class SpiPatcher { .flags = 0, .intr_flags = 0 }; - ESP_ERROR_CHECK(spi_bus_initialize(mHostDevice, &buscfg, SPI_DMA_DISABLED)); + ESP_ERROR_CHECK((mBusState = spi_bus_initialize(mDev, &mBusConfig, dmaType))); + + return mBusState; } - public: - SpiPatcher(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; + static SpiPatcher* getInstance(spi_host_device_t dev, bool initialize = true) { + if(SPI2_HOST == dev) { + if(nullptr == InstanceHost2) { + InstanceHost2 = new SpiPatcher(dev); + if(initialize) + InstanceHost2->initBus(); + } + return InstanceHost2; + } else { // SPI3_HOST + if(nullptr == InstanceHost3) { + InstanceHost3 = new SpiPatcher(dev); + if(initialize) + InstanceHost3->initBus(); + } + 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) { + 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)")); + + ESP_ERROR_CHECK(spi_bus_add_device(host_id, dev_config, handle)); } inline void request(SpiPatcherHandle* handle) { + assert(mBusState == ESP_OK); xSemaphoreTake(mutex, portMAX_DELAY); if (mCurHandle != handle) { @@ -70,17 +107,22 @@ class SpiPatcher { } inline void release() { + assert(mBusState == ESP_OK); xSemaphoreGive(mutex); } 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; + spi_host_device_t mDev = SPI2_HOST; + esp_err_t mBusState = ESP_FAIL; + spi_bus_config_t mBusConfig; }; #endif /*ESP32*/ diff --git a/src/web/RestApi.h b/src/web/RestApi.h index 9a633e91..1070f406 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -103,9 +103,10 @@ 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 == "yieldDayHistory") getYieldDayHistory(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()); @@ -298,7 +299,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 } @@ -574,12 +574,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) { @@ -604,7 +605,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->tsMaxTemp; + obj[F("ts_max_temp")] = iv->tsMaxTemperature; JsonArray ch = obj.createNestedArray("ch"); @@ -908,6 +909,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]]]); @@ -928,42 +930,39 @@ class RestApi { } } - void getPowerHistory(AsyncWebServerRequest *request, JsonObject obj) { + #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((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); - #endif /*ENABLE_HISTORY*/ + + obj[F("lastValueTs")] = mApp->getHistoryLastValueTs(static_cast(type)); } + #endif /*ENABLE_HISTORY*/ + #if defined(ENABLE_HISTORY_YIELD_PER_DAY) 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); uint16_t max = 0; for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { @@ -973,8 +972,8 @@ class RestApi { max = value; } obj[F("max")] = max; - #endif /*ENABLE_HISTORY*/ } + #endif /*ENABLE_HISTORY_YIELD_PER_DAY*/ bool setCtrl(JsonObject jsonIn, JsonObject jsonOut, const char *clientIP) { if(jsonIn.containsKey(F("auth"))) { @@ -1016,8 +1015,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()); 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/history.html b/src/web/html/history.html index d21e6b96..08227b1f 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,39 +105,45 @@ 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 } 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/html/setup.html b/src/web/html/setup.html index d3976c4e..b5f5a6f2 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}
@@ -138,7 +142,7 @@
-
{#INV_RESET_MAX_MIDNIGHT}
+
{#INV_RESET_MAX_VALUES}
@@ -674,7 +678,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/html/style.css b/src/web/html/style.css index 2866dda1..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; @@ -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 6c3bb67f..d1f6ded6 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}" @@ -54,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) ]) ), @@ -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"}, [ @@ -104,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 @@ -119,8 +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 maxTemp = toIsoDateStr(new Date(obj.ts_max_temp * 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}, @@ -135,8 +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 tooltip", data: maxTemp}, String(obj.ch[0][5].toFixed(1)) + t.innerText + "
" + - "Max:" + String(obj.ch[0][12].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: "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}"), @@ -242,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) { @@ -516,7 +514,9 @@ ivEn = Object.values(Object.assign({}, obj["iv"])); mIvHtml = []; 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); 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()) diff --git a/src/web/lang.json b/src/web/lang.json index ce696152..6f1b7c03 100644 --- a/src/web/lang.json +++ b/src/web/lang.json @@ -324,19 +324,24 @@ "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'", "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": "Include reset 'max' values", + "de": "Maximalwerte auch zurücksetzen" }, { "token": "INV_START_WITHOUT_TIME", @@ -1587,6 +1592,11 @@ "token": "LAST_VALUE", "en": "Last value", "de": "Letzter Wert" + }, + { + "token": "YIELD_DAY", + "en": "Yield day", + "de": "Tagesertrag" } ] } diff --git a/src/web/web.h b/src/web/web.h index b75aae5b..2d017969 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -498,12 +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