Skip to content

Commit

Permalink
Migrate all frequency calculations to Hz
Browse files Browse the repository at this point in the history
Previously the code contains calculations  using a mixture  of kHz and Hz.

Thanks to @Fribur
  • Loading branch information
tbnobody committed Jan 13, 2024
1 parent ce2109a commit ee78698
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <cstdint>

#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_PASSWORD_STRLEN 64
Expand Down
2 changes: 1 addition & 1 deletion include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
#define DTU_POLL_INTERVAL 5U
#define DTU_NRF_PA_LEVEL 0U
#define DTU_CMT_PA_LEVEL 0
#define DTU_CMT_FREQUENCY 865000U
#define DTU_CMT_FREQUENCY 865000000U

#define MQTT_HASS_ENABLED false
#define MQTT_HASS_EXPIRE true
Expand Down
42 changes: 21 additions & 21 deletions lib/Hoymiles/src/HoymilesRadio_CMT.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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 "Hoymiles.h"
Expand All @@ -12,38 +12,38 @@
// 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)

// frequency can not be lower than actual initailized base freq
#define MIN_FREQ_KHZ ((HOY_BASE_FREQ + (CMT_BASE_CH_OFFSET >= 1 ? CMT_BASE_CH_OFFSET : 1) * CMT2300A_ONE_STEP_SIZE * FH_OFFSET) / 1000)
// frequency can not be lower than actual initailized base freq + 250000
#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
#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) {
Hoymiles.getMessageOutput()->printf("%.3f MHz is not divisible by 250 kHz!\r\n", freq_kHz / 1000.0);
if ((frequency % 250000) != 0) {
Hoymiles.getMessageOutput()->printf("%.3f MHz is not divisible by 250 kHz!\r\n", frequency / 1000000.0);
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",
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
}
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",
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) {
return false;
}
Expand Down Expand Up @@ -134,7 +134,7 @@ void HoymilesRadio_CMT::loop()

if (nullptr != inv) {
// 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);
Hoymiles.getMessageOutput()->printf("| %d dBm\r\n", f.rssi);

Expand Down Expand Up @@ -193,12 +193,12 @@ bool HoymilesRadio_CMT::isConnected() const

uint32_t HoymilesRadio_CMT::getMinFrequency()
{
return MIN_FREQ_KHZ;
return MIN_FREQ;
}

uint32_t HoymilesRadio_CMT::getMaxFrequency()
{
return MAX_FREQ_KHZ;
return MAX_FREQ;
}

void ARDUINO_ISR_ATTR HoymilesRadio_CMT::handleInt1()
Expand All @@ -220,11 +220,11 @@ void HoymilesRadio_CMT::sendEsbPacket(CommandAbstract& cmd)
_radio->stopListening();

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 --> ",
cmd.getCommandName().c_str(), getFrequencyFromChannel(_radio->getChannel()));
cmd.getCommandName().c_str(), getFrequencyFromChannel(_radio->getChannel()) / 1000000.0);
cmd.dumpDataPayload(Hoymiles.getMessageOutput());

if (!_radio->write(cmd.getDataPayload(), cmd.getDataSize())) {
Expand Down
10 changes: 5 additions & 5 deletions lib/Hoymiles/src/HoymilesRadio_CMT.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define FRAGMENT_BUFFER_SIZE 30

#ifndef HOYMILES_CMT_WORK_FREQ
#define HOYMILES_CMT_WORK_FREQ 865000
#define HOYMILES_CMT_WORK_FREQ 865000000
#endif

class HoymilesRadio_CMT : public HoymilesRadio {
Expand All @@ -29,8 +29,8 @@ class HoymilesRadio_CMT : public HoymilesRadio {
static uint32_t getMinFrequency();
static uint32_t getMaxFrequency();

static float getFrequencyFromChannel(const uint8_t channel);
static uint8_t getChannelFromFrequency(const uint32_t freq_kHz);
static uint32_t getFrequencyFromChannel(const uint8_t channel);
static uint8_t getChannelFromFrequency(const uint32_t frequency);

private:
void ARDUINO_ISR_ATTR handleInt1();
Expand All @@ -51,5 +51,5 @@ class HoymilesRadio_CMT : public HoymilesRadio {

uint32_t _inverterTargetFrequency = HOYMILES_CMT_WORK_FREQ;

bool cmtSwitchDtuFreq(const uint32_t to_freq_kHz);
};
bool cmtSwitchDtuFreq(const uint32_t to_frequency);
};
5 changes: 5 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ void ConfigurationClass::migrate()
nvs_flash_init();
}

if (config.Cfg.Version < 0x00011b00) {
// Convert from kHz to Hz
config.Dtu.Cmt.Frequency *= 1000;
}

f.close();

config.Cfg.Version = CONFIG_VERSION;
Expand Down
8 changes: 4 additions & 4 deletions webapp/src/views/DtuAdminView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<div class="input-group mb-3">
<input type="range" class="form-control form-range"
v-model="dtuConfigList.cmt_frequency"
min="860250" max="923500" step="250"
min="860250000" max="923500000" step="250000"
id="cmtFrequency" aria-describedby="basic-addon2"
style="height: unset;" />
<span class="input-group-text" id="basic-addon2">{{ cmtFrequencyText }}</span>
Expand Down Expand Up @@ -111,13 +111,13 @@ export default defineComponent({
},
computed: {
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() {
return this.$t("dtuadmin.dBm", { dbm: this.$n(this.dtuConfigList.cmt_palevel * 1) });
},
cmtIsOutOfEu() {
return this.dtuConfigList.cmt_frequency < 863000 || this.dtuConfigList.cmt_frequency > 870000;
return this.dtuConfigList.cmt_frequency < 863000000 || this.dtuConfigList.cmt_frequency > 870000000;
}
},
methods: {
Expand Down Expand Up @@ -154,4 +154,4 @@ export default defineComponent({
},
},
});
</script>
</script>

0 comments on commit ee78698

Please sign in to comment.