Migrate all frequency calculations to Hz
Previously the code contains calculations using a mixture of kHz and Hz. Thanks to @Fribur
This commit is contained in:
parent
ce2109ab1b
commit
ee78698e37
@ -5,7 +5,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#define CONFIG_FILENAME "/config.json"
|
#define CONFIG_FILENAME "/config.json"
|
||||||
#define CONFIG_VERSION 0x00011a00 // 0.1.26 // make sure to clean all after change
|
#define CONFIG_VERSION 0x00011b00 // 0.1.27 // make sure to clean all after change
|
||||||
|
|
||||||
#define WIFI_MAX_SSID_STRLEN 32
|
#define WIFI_MAX_SSID_STRLEN 32
|
||||||
#define WIFI_MAX_PASSWORD_STRLEN 64
|
#define WIFI_MAX_PASSWORD_STRLEN 64
|
||||||
|
|||||||
@ -83,7 +83,7 @@
|
|||||||
#define DTU_POLL_INTERVAL 5U
|
#define DTU_POLL_INTERVAL 5U
|
||||||
#define DTU_NRF_PA_LEVEL 0U
|
#define DTU_NRF_PA_LEVEL 0U
|
||||||
#define DTU_CMT_PA_LEVEL 0
|
#define DTU_CMT_PA_LEVEL 0
|
||||||
#define DTU_CMT_FREQUENCY 865000U
|
#define DTU_CMT_FREQUENCY 865000000U
|
||||||
|
|
||||||
#define MQTT_HASS_ENABLED false
|
#define MQTT_HASS_ENABLED false
|
||||||
#define MQTT_HASS_EXPIRE true
|
#define MQTT_HASS_EXPIRE true
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2023 Thomas Basler and others
|
* Copyright (C) 2024 Thomas Basler and others
|
||||||
*/
|
*/
|
||||||
#include "HoymilesRadio_CMT.h"
|
#include "HoymilesRadio_CMT.h"
|
||||||
#include "Hoymiles.h"
|
#include "Hoymiles.h"
|
||||||
@ -12,38 +12,38 @@
|
|||||||
// offset from initalized CMT base frequency to Hoy base frequency in channels
|
// offset from initalized CMT base frequency to Hoy base frequency in channels
|
||||||
#define CMT_BASE_CH_OFFSET ((CMT_BASE_FREQ - HOY_BASE_FREQ) / CMT2300A_ONE_STEP_SIZE / FH_OFFSET)
|
#define CMT_BASE_CH_OFFSET ((CMT_BASE_FREQ - HOY_BASE_FREQ) / CMT2300A_ONE_STEP_SIZE / FH_OFFSET)
|
||||||
|
|
||||||
// frequency can not be lower than actual initailized base freq
|
// frequency can not be lower than actual initailized base freq + 250000
|
||||||
#define MIN_FREQ_KHZ ((HOY_BASE_FREQ + (CMT_BASE_CH_OFFSET >= 1 ? CMT_BASE_CH_OFFSET : 1) * CMT2300A_ONE_STEP_SIZE * FH_OFFSET) / 1000)
|
#define MIN_FREQ ((HOY_BASE_FREQ + (CMT_BASE_CH_OFFSET > 1 ? CMT_BASE_CH_OFFSET : 1) * CMT2300A_ONE_STEP_SIZE * FH_OFFSET))
|
||||||
|
|
||||||
// =923500, 0xFF does not work
|
// =923500, 0xFF does not work
|
||||||
#define MAX_FREQ_KHZ ((HOY_BASE_FREQ + 0xFE * CMT2300A_ONE_STEP_SIZE * FH_OFFSET) / 1000)
|
#define MAX_FREQ ((HOY_BASE_FREQ + 0xFE * CMT2300A_ONE_STEP_SIZE * FH_OFFSET))
|
||||||
|
|
||||||
float HoymilesRadio_CMT::getFrequencyFromChannel(const uint8_t channel)
|
uint32_t HoymilesRadio_CMT::getFrequencyFromChannel(const uint8_t channel)
|
||||||
{
|
{
|
||||||
return (CMT_BASE_FREQ + (CMT_BASE_CH_OFFSET + channel) * FH_OFFSET * CMT2300A_ONE_STEP_SIZE) / 1000000.0;
|
return (CMT_BASE_FREQ + (CMT_BASE_CH_OFFSET + channel) * FH_OFFSET * CMT2300A_ONE_STEP_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t HoymilesRadio_CMT::getChannelFromFrequency(const uint32_t freq_kHz)
|
uint8_t HoymilesRadio_CMT::getChannelFromFrequency(const uint32_t frequency)
|
||||||
{
|
{
|
||||||
if ((freq_kHz % 250) != 0) {
|
if ((frequency % 250000) != 0) {
|
||||||
Hoymiles.getMessageOutput()->printf("%.3f MHz is not divisible by 250 kHz!\r\n", freq_kHz / 1000.0);
|
Hoymiles.getMessageOutput()->printf("%.3f MHz is not divisible by 250 kHz!\r\n", frequency / 1000000.0);
|
||||||
return 0xFF; // ERROR
|
return 0xFF; // ERROR
|
||||||
}
|
}
|
||||||
if (freq_kHz < MIN_FREQ_KHZ || freq_kHz > MAX_FREQ_KHZ) {
|
if (frequency < MIN_FREQ || frequency > MAX_FREQ) {
|
||||||
Hoymiles.getMessageOutput()->printf("%.2f MHz is out of Hoymiles/CMT range! (%.2f MHz - %.2f MHz)\r\n",
|
Hoymiles.getMessageOutput()->printf("%.2f MHz is out of Hoymiles/CMT range! (%.2f MHz - %.2f MHz)\r\n",
|
||||||
freq_kHz / 1000.0, MIN_FREQ_KHZ / 1000.0, MAX_FREQ_KHZ / 1000.0);
|
frequency / 1000000.0, MIN_FREQ / 1000000.0, MAX_FREQ / 1000000.0);
|
||||||
return 0xFF; // ERROR
|
return 0xFF; // ERROR
|
||||||
}
|
}
|
||||||
if (freq_kHz < 863000 || freq_kHz > 870000) {
|
if (frequency < 863000000 || frequency > 870000000) {
|
||||||
Hoymiles.getMessageOutput()->printf("!!! caution: %.2f MHz is out of EU legal range! (863 - 870 MHz)\r\n",
|
Hoymiles.getMessageOutput()->printf("!!! caution: %.2f MHz is out of EU legal range! (863 - 870 MHz)\r\n",
|
||||||
freq_kHz / 1000.0);
|
frequency / 1000000.0);
|
||||||
}
|
}
|
||||||
return (freq_kHz * 1000 - CMT_BASE_FREQ) / CMT2300A_ONE_STEP_SIZE / FH_OFFSET - CMT_BASE_CH_OFFSET; // frequency to channel
|
return (frequency - CMT_BASE_FREQ) / CMT2300A_ONE_STEP_SIZE / FH_OFFSET - CMT_BASE_CH_OFFSET; // frequency to channel
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HoymilesRadio_CMT::cmtSwitchDtuFreq(const uint32_t to_freq_kHz)
|
bool HoymilesRadio_CMT::cmtSwitchDtuFreq(const uint32_t to_frequency)
|
||||||
{
|
{
|
||||||
const uint8_t toChannel = getChannelFromFrequency(to_freq_kHz);
|
const uint8_t toChannel = getChannelFromFrequency(to_frequency);
|
||||||
if (toChannel == 0xFF) {
|
if (toChannel == 0xFF) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -134,7 +134,7 @@ void HoymilesRadio_CMT::loop()
|
|||||||
|
|
||||||
if (nullptr != inv) {
|
if (nullptr != inv) {
|
||||||
// Save packet in inverter rx buffer
|
// Save packet in inverter rx buffer
|
||||||
Hoymiles.getMessageOutput()->printf("RX %.2f MHz --> ", getFrequencyFromChannel(f.channel));
|
Hoymiles.getMessageOutput()->printf("RX %.2f MHz --> ", getFrequencyFromChannel(f.channel) / 1000000.0);
|
||||||
dumpBuf(f.fragment, f.len, false);
|
dumpBuf(f.fragment, f.len, false);
|
||||||
Hoymiles.getMessageOutput()->printf("| %d dBm\r\n", f.rssi);
|
Hoymiles.getMessageOutput()->printf("| %d dBm\r\n", f.rssi);
|
||||||
|
|
||||||
@ -193,12 +193,12 @@ bool HoymilesRadio_CMT::isConnected() const
|
|||||||
|
|
||||||
uint32_t HoymilesRadio_CMT::getMinFrequency()
|
uint32_t HoymilesRadio_CMT::getMinFrequency()
|
||||||
{
|
{
|
||||||
return MIN_FREQ_KHZ;
|
return MIN_FREQ;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t HoymilesRadio_CMT::getMaxFrequency()
|
uint32_t HoymilesRadio_CMT::getMaxFrequency()
|
||||||
{
|
{
|
||||||
return MAX_FREQ_KHZ;
|
return MAX_FREQ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ARDUINO_ISR_ATTR HoymilesRadio_CMT::handleInt1()
|
void ARDUINO_ISR_ATTR HoymilesRadio_CMT::handleInt1()
|
||||||
@ -220,11 +220,11 @@ void HoymilesRadio_CMT::sendEsbPacket(CommandAbstract& cmd)
|
|||||||
_radio->stopListening();
|
_radio->stopListening();
|
||||||
|
|
||||||
if (cmd.getDataPayload()[0] == 0x56) { // @todo(tbnobody) Bad hack to identify ChannelChange Command
|
if (cmd.getDataPayload()[0] == 0x56) { // @todo(tbnobody) Bad hack to identify ChannelChange Command
|
||||||
cmtSwitchDtuFreq(HOY_BOOT_FREQ / 1000);
|
cmtSwitchDtuFreq(HOY_BOOT_FREQ);
|
||||||
}
|
}
|
||||||
|
|
||||||
Hoymiles.getMessageOutput()->printf("TX %s %.2f MHz --> ",
|
Hoymiles.getMessageOutput()->printf("TX %s %.2f MHz --> ",
|
||||||
cmd.getCommandName().c_str(), getFrequencyFromChannel(_radio->getChannel()));
|
cmd.getCommandName().c_str(), getFrequencyFromChannel(_radio->getChannel()) / 1000000.0);
|
||||||
cmd.dumpDataPayload(Hoymiles.getMessageOutput());
|
cmd.dumpDataPayload(Hoymiles.getMessageOutput());
|
||||||
|
|
||||||
if (!_radio->write(cmd.getDataPayload(), cmd.getDataSize())) {
|
if (!_radio->write(cmd.getDataPayload(), cmd.getDataSize())) {
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
#define FRAGMENT_BUFFER_SIZE 30
|
#define FRAGMENT_BUFFER_SIZE 30
|
||||||
|
|
||||||
#ifndef HOYMILES_CMT_WORK_FREQ
|
#ifndef HOYMILES_CMT_WORK_FREQ
|
||||||
#define HOYMILES_CMT_WORK_FREQ 865000
|
#define HOYMILES_CMT_WORK_FREQ 865000000
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class HoymilesRadio_CMT : public HoymilesRadio {
|
class HoymilesRadio_CMT : public HoymilesRadio {
|
||||||
@ -29,8 +29,8 @@ public:
|
|||||||
static uint32_t getMinFrequency();
|
static uint32_t getMinFrequency();
|
||||||
static uint32_t getMaxFrequency();
|
static uint32_t getMaxFrequency();
|
||||||
|
|
||||||
static float getFrequencyFromChannel(const uint8_t channel);
|
static uint32_t getFrequencyFromChannel(const uint8_t channel);
|
||||||
static uint8_t getChannelFromFrequency(const uint32_t freq_kHz);
|
static uint8_t getChannelFromFrequency(const uint32_t frequency);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ARDUINO_ISR_ATTR handleInt1();
|
void ARDUINO_ISR_ATTR handleInt1();
|
||||||
@ -51,5 +51,5 @@ private:
|
|||||||
|
|
||||||
uint32_t _inverterTargetFrequency = HOYMILES_CMT_WORK_FREQ;
|
uint32_t _inverterTargetFrequency = HOYMILES_CMT_WORK_FREQ;
|
||||||
|
|
||||||
bool cmtSwitchDtuFreq(const uint32_t to_freq_kHz);
|
bool cmtSwitchDtuFreq(const uint32_t to_frequency);
|
||||||
};
|
};
|
||||||
@ -365,6 +365,11 @@ void ConfigurationClass::migrate()
|
|||||||
nvs_flash_init();
|
nvs_flash_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config.Cfg.Version < 0x00011b00) {
|
||||||
|
// Convert from kHz to Hz
|
||||||
|
config.Dtu.Cmt.Frequency *= 1000;
|
||||||
|
}
|
||||||
|
|
||||||
f.close();
|
f.close();
|
||||||
|
|
||||||
config.Cfg.Version = CONFIG_VERSION;
|
config.Cfg.Version = CONFIG_VERSION;
|
||||||
|
|||||||
@ -56,7 +56,7 @@
|
|||||||
<div class="input-group mb-3">
|
<div class="input-group mb-3">
|
||||||
<input type="range" class="form-control form-range"
|
<input type="range" class="form-control form-range"
|
||||||
v-model="dtuConfigList.cmt_frequency"
|
v-model="dtuConfigList.cmt_frequency"
|
||||||
min="860250" max="923500" step="250"
|
min="860250000" max="923500000" step="250000"
|
||||||
id="cmtFrequency" aria-describedby="basic-addon2"
|
id="cmtFrequency" aria-describedby="basic-addon2"
|
||||||
style="height: unset;" />
|
style="height: unset;" />
|
||||||
<span class="input-group-text" id="basic-addon2">{{ cmtFrequencyText }}</span>
|
<span class="input-group-text" id="basic-addon2">{{ cmtFrequencyText }}</span>
|
||||||
@ -111,13 +111,13 @@ export default defineComponent({
|
|||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
cmtFrequencyText() {
|
cmtFrequencyText() {
|
||||||
return this.$t("dtuadmin.MHz", { mhz: this.$n(this.dtuConfigList.cmt_frequency / 1000, "decimalTwoDigits") });
|
return this.$t("dtuadmin.MHz", { mhz: this.$n(this.dtuConfigList.cmt_frequency / 1000000, "decimalTwoDigits") });
|
||||||
},
|
},
|
||||||
cmtPaLevelText() {
|
cmtPaLevelText() {
|
||||||
return this.$t("dtuadmin.dBm", { dbm: this.$n(this.dtuConfigList.cmt_palevel * 1) });
|
return this.$t("dtuadmin.dBm", { dbm: this.$n(this.dtuConfigList.cmt_palevel * 1) });
|
||||||
},
|
},
|
||||||
cmtIsOutOfEu() {
|
cmtIsOutOfEu() {
|
||||||
return this.dtuConfigList.cmt_frequency < 863000 || this.dtuConfigList.cmt_frequency > 870000;
|
return this.dtuConfigList.cmt_frequency < 863000000 || this.dtuConfigList.cmt_frequency > 870000000;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user