mirror of
https://github.com/lumapu/ahoy.git
synced 2025-07-23 11:17:11 +02:00
further improvements to generic functions
This commit is contained in:
parent
01f113cd2b
commit
86822eb7b8
4 changed files with 60 additions and 91 deletions
|
@ -191,10 +191,6 @@ void app::onNetwork(bool gotIp) {
|
||||||
ah::Scheduler::resetTicker();
|
ah::Scheduler::resetTicker();
|
||||||
regularTickers(); //reinstall regular tickers
|
regularTickers(); //reinstall regular tickers
|
||||||
every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend");
|
every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend");
|
||||||
#if defined(ESP32)
|
|
||||||
if(mConfig->cmt.enabled)
|
|
||||||
everySec(std::bind(&CmtRadioType::tickSecond, &mCmtRadio), "tsCmt");
|
|
||||||
#endif
|
|
||||||
mMqttReconnect = true;
|
mMqttReconnect = true;
|
||||||
mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers!
|
mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers!
|
||||||
//once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2");
|
//once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2");
|
||||||
|
|
|
@ -6,10 +6,7 @@
|
||||||
#ifndef __HM_RADIO_H__
|
#ifndef __HM_RADIO_H__
|
||||||
#define __HM_RADIO_H__
|
#define __HM_RADIO_H__
|
||||||
|
|
||||||
#include "../utils/dbg.h"
|
|
||||||
#include <RF24.h>
|
#include <RF24.h>
|
||||||
#include "../utils/crc.h"
|
|
||||||
#include "../config/config.h"
|
|
||||||
#include "SPI.h"
|
#include "SPI.h"
|
||||||
#include "radio.h"
|
#include "radio.h"
|
||||||
|
|
||||||
|
@ -137,17 +134,10 @@ class HmRadio : public Radio {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleIntr(void) {
|
|
||||||
mIrqRcvd = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isChipConnected(void) {
|
bool isChipConnected(void) {
|
||||||
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:isChipConnected"));
|
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:isChipConnected"));
|
||||||
return mNrf24.isChipConnected();
|
return mNrf24.isChipConnected();
|
||||||
}
|
}
|
||||||
void enableDebug() {
|
|
||||||
mSerialDebug = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit, bool isNoMI = true, bool is4chMI = false) {
|
void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit, bool isNoMI = true, bool is4chMI = false) {
|
||||||
DPRINT(DBG_INFO, F("sendControlPacket cmd: 0x"));
|
DPRINT(DBG_INFO, F("sendControlPacket cmd: 0x"));
|
||||||
|
@ -199,27 +189,6 @@ class HmRadio : public Radio {
|
||||||
sendPacket(iv, cnt, isRetransmit, isNoMI);
|
sendPacket(iv, cnt, isRetransmit, isNoMI);
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareDevInformCmd(Inverter<> *iv, uint8_t cmd, uint32_t ts, uint16_t alarmMesId, bool isRetransmit, uint8_t reqfld=TX_REQ_INFO) { // might not be necessary to add additional arg.
|
|
||||||
if(mSerialDebug) {
|
|
||||||
DPRINT(DBG_DEBUG, F("prepareDevInformCmd 0x"));
|
|
||||||
DPRINTLN(DBG_DEBUG,String(cmd, HEX));
|
|
||||||
}
|
|
||||||
initPacket(iv->radioId.u64, reqfld, ALL_FRAMES);
|
|
||||||
mTxBuf[10] = cmd; // cid
|
|
||||||
mTxBuf[11] = 0x00;
|
|
||||||
CP_U32_LittleEndian(&mTxBuf[12], ts);
|
|
||||||
if (cmd == AlarmData ) { //cmd == RealTimeRunData_Debug ||
|
|
||||||
mTxBuf[18] = (alarmMesId >> 8) & 0xff;
|
|
||||||
mTxBuf[19] = (alarmMesId ) & 0xff;
|
|
||||||
}
|
|
||||||
sendPacket(iv, 24, isRetransmit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendCmdPacket(Inverter<> *iv, uint8_t mid, uint8_t pid, bool isRetransmit, bool appendCrc16=true) {
|
|
||||||
initPacket(iv->radioId.u64, mid, pid);
|
|
||||||
sendPacket(iv, 10, isRetransmit, appendCrc16);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t getDataRate(void) {
|
uint8_t getDataRate(void) {
|
||||||
if(!mNrf24.isChipConnected())
|
if(!mNrf24.isChipConnected())
|
||||||
return 3; // unknown
|
return 3; // unknown
|
||||||
|
@ -231,7 +200,6 @@ class HmRadio : public Radio {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::queue<packet_t> mBufCtrl;
|
std::queue<packet_t> mBufCtrl;
|
||||||
bool mSerialDebug;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool getReceived(void) {
|
bool getReceived(void) {
|
||||||
|
@ -264,19 +232,7 @@ class HmRadio : public Radio {
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendPacket(Inverter<> *iv, uint8_t len, bool isRetransmit, bool appendCrc16=true) {
|
void sendPacket(Inverter<> *iv, uint8_t len, bool isRetransmit, bool appendCrc16=true) {
|
||||||
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendPacket"));
|
updateCrcs(len, appendCrc16);
|
||||||
//DPRINTLN(DBG_VERBOSE, "sent packet: #" + String(iv->radioStatistics.txCnt));
|
|
||||||
|
|
||||||
// append crc's
|
|
||||||
if (appendCrc16 && (len > 10)) {
|
|
||||||
// crc control data
|
|
||||||
uint16_t crc = ah::crc16(&mTxBuf[10], len - 10);
|
|
||||||
mTxBuf[len++] = (crc >> 8) & 0xff;
|
|
||||||
mTxBuf[len++] = (crc ) & 0xff;
|
|
||||||
}
|
|
||||||
// crc over all
|
|
||||||
mTxBuf[len] = ah::crc8(mTxBuf, len);
|
|
||||||
len++;
|
|
||||||
|
|
||||||
// set TX and RX channels
|
// set TX and RX channels
|
||||||
mTxChIdx = (mTxChIdx + 1) % RF_CHANNELS;
|
mTxChIdx = (mTxChIdx + 1) % RF_CHANNELS;
|
||||||
|
@ -302,7 +258,9 @@ class HmRadio : public Radio {
|
||||||
iv->radioStatistics.txCnt++;
|
iv->radioStatistics.txCnt++;
|
||||||
}
|
}
|
||||||
|
|
||||||
volatile bool mIrqRcvd;
|
uint64_t getIvId(Inverter<> *iv) {
|
||||||
|
return iv->radioId.u64;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t mRfChLst[RF_CHANNELS];
|
uint8_t mRfChLst[RF_CHANNELS];
|
||||||
uint8_t mTxChIdx;
|
uint8_t mTxChIdx;
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
#define ALL_FRAMES 0x80
|
#define ALL_FRAMES 0x80
|
||||||
#define SINGLE_FRAME 0x81
|
#define SINGLE_FRAME 0x81
|
||||||
|
|
||||||
|
#include "../utils/dbg.h"
|
||||||
|
#include "../utils/crc.h"
|
||||||
|
|
||||||
// forward declaration of class
|
// forward declaration of class
|
||||||
template <class REC_TYP=float>
|
template <class REC_TYP=float>
|
||||||
class Inverter;
|
class Inverter;
|
||||||
|
@ -19,11 +22,40 @@ class Inverter;
|
||||||
class Radio {
|
class Radio {
|
||||||
public:
|
public:
|
||||||
virtual void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit, bool isNoMI = true, bool is4chMI = false) = 0;
|
virtual void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit, bool isNoMI = true, bool is4chMI = false) = 0;
|
||||||
virtual void prepareDevInformCmd(Inverter<> *iv, uint8_t cmd, uint32_t ts, uint16_t alarmMesId, bool isRetransmit, uint8_t reqfld=TX_REQ_INFO) = 0;
|
|
||||||
virtual void sendCmdPacket(Inverter<> *iv, uint8_t mid, uint8_t pid, bool isRetransmit, bool appendCrc16=true) = 0;
|
|
||||||
virtual bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) { return true; }
|
virtual bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) { return true; }
|
||||||
|
|
||||||
|
void handleIntr(void) {
|
||||||
|
mIrqRcvd = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void enableDebug() {
|
||||||
|
mSerialDebug = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendCmdPacket(Inverter<> *iv, uint8_t mid, uint8_t pid, bool isRetransmit, bool appendCrc16=true) {
|
||||||
|
initPacket(getIvId(iv), mid, pid);
|
||||||
|
sendPacket(iv, 10, isRetransmit, appendCrc16);
|
||||||
|
}
|
||||||
|
|
||||||
|
void prepareDevInformCmd(Inverter<> *iv, uint8_t cmd, uint32_t ts, uint16_t alarmMesId, bool isRetransmit, uint8_t reqfld=TX_REQ_INFO) { // might not be necessary to add additional arg.
|
||||||
|
if(mSerialDebug) {
|
||||||
|
DPRINT(DBG_DEBUG, F("prepareDevInformCmd 0x"));
|
||||||
|
DPRINTLN(DBG_DEBUG,String(cmd, HEX));
|
||||||
|
}
|
||||||
|
initPacket(getIvId(iv), reqfld, ALL_FRAMES);
|
||||||
|
mTxBuf[10] = cmd;
|
||||||
|
CP_U32_LittleEndian(&mTxBuf[12], ts);
|
||||||
|
if (cmd == AlarmData ) { //cmd == RealTimeRunData_Debug ||
|
||||||
|
mTxBuf[18] = (alarmMesId >> 8) & 0xff;
|
||||||
|
mTxBuf[19] = (alarmMesId ) & 0xff;
|
||||||
|
}
|
||||||
|
sendPacket(iv, 24, isRetransmit);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
virtual void sendPacket(Inverter<> *iv, uint8_t len, bool isRetransmit, bool appendCrc16=true) = 0;
|
||||||
|
virtual uint64_t getIvId(Inverter<> *iv) = 0;
|
||||||
|
|
||||||
void initPacket(uint64_t ivId, uint8_t mid, uint8_t pid) {
|
void initPacket(uint64_t ivId, uint8_t mid, uint8_t pid) {
|
||||||
mTxBuf[0] = mid;
|
mTxBuf[0] = mid;
|
||||||
CP_U32_BigEndian(&mTxBuf[1], ivId >> 8);
|
CP_U32_BigEndian(&mTxBuf[1], ivId >> 8);
|
||||||
|
@ -32,6 +64,19 @@ class Radio {
|
||||||
memset(&mTxBuf[10], 0x00, (MAX_RF_PAYLOAD_SIZE-10));
|
memset(&mTxBuf[10], 0x00, (MAX_RF_PAYLOAD_SIZE-10));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void updateCrcs(uint8_t len, bool appendCrc16=true) {
|
||||||
|
// append crc's
|
||||||
|
if (appendCrc16 && (len > 10)) {
|
||||||
|
// crc control data
|
||||||
|
uint16_t crc = ah::crc16(&mTxBuf[10], len - 10);
|
||||||
|
mTxBuf[len++] = (crc >> 8) & 0xff;
|
||||||
|
mTxBuf[len++] = (crc ) & 0xff;
|
||||||
|
}
|
||||||
|
// crc over all
|
||||||
|
mTxBuf[len] = ah::crc8(mTxBuf, len);
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
void generateDtuSn(void) {
|
void generateDtuSn(void) {
|
||||||
uint32_t chipID = 0;
|
uint32_t chipID = 0;
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
|
@ -48,6 +93,8 @@ class Radio {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t mDtuSn;
|
uint32_t mDtuSn;
|
||||||
|
volatile bool mIrqRcvd;
|
||||||
|
bool mSerialDebug;
|
||||||
uint8_t mTxBuf[MAX_RF_PAYLOAD_SIZE];
|
uint8_t mTxBuf[MAX_RF_PAYLOAD_SIZE];
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
#ifndef __HMS_RADIO_H__
|
#ifndef __HMS_RADIO_H__
|
||||||
#define __HMS_RADIO_H__
|
#define __HMS_RADIO_H__
|
||||||
|
|
||||||
#include "../utils/dbg.h"
|
|
||||||
#include "cmt2300a.h"
|
#include "cmt2300a.h"
|
||||||
#include "../hm/radio.h"
|
#include "../hm/radio.h"
|
||||||
|
|
||||||
|
@ -44,17 +43,6 @@ class CmtRadio : public Radio {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tickSecond() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleIntr(void) {
|
|
||||||
mIrqRcvd = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void enableDebug() {
|
|
||||||
mSerialDebug = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isConnected() {
|
bool isConnected() {
|
||||||
return mCmtAvail;
|
return mCmtAvail;
|
||||||
}
|
}
|
||||||
|
@ -90,30 +78,11 @@ class CmtRadio : public Radio {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareDevInformCmd(Inverter<> *iv, uint8_t cmd, uint32_t ts, uint16_t alarmMesId, bool isRetransmit, uint8_t reqfld=TX_REQ_INFO) { // might not be necessary to add additional arg.
|
std::queue<packet_t> mBufCtrl;
|
||||||
initPacket(iv->radioId.u64, reqfld, ALL_FRAMES);
|
|
||||||
mTxBuf[10] = cmd;
|
|
||||||
CP_U32_LittleEndian(&mTxBuf[12], ts);
|
|
||||||
if (cmd == AlarmData ) { //cmd == RealTimeRunData_Debug ||
|
|
||||||
mTxBuf[18] = (alarmMesId >> 8) & 0xff;
|
|
||||||
mTxBuf[19] = (alarmMesId ) & 0xff;
|
|
||||||
}
|
|
||||||
sendPacket(iv, 24, isRetransmit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendCmdPacket(Inverter<> *iv, uint8_t mid, uint8_t pid, bool isRetransmit, bool appendCrc16=true) {
|
private:
|
||||||
initPacket(iv->radioId.u64, mid, pid);
|
void sendPacket(Inverter<> *iv, uint8_t len, bool isRetransmit, bool appendCrc16=true) {
|
||||||
sendPacket(iv, 10, isRetransmit);
|
updateCrcs(len, appendCrc16);
|
||||||
}
|
|
||||||
|
|
||||||
void sendPacket(Inverter<> *iv, uint8_t len, bool isRetransmit) {
|
|
||||||
if (len > 14) {
|
|
||||||
uint16_t crc = ah::crc16(&mTxBuf[10], len - 10);
|
|
||||||
mTxBuf[len++] = (crc >> 8) & 0xff;
|
|
||||||
mTxBuf[len++] = (crc ) & 0xff;
|
|
||||||
}
|
|
||||||
mTxBuf[len] = ah::crc8(mTxBuf, len);
|
|
||||||
len++;
|
|
||||||
|
|
||||||
if(mSerialDebug) {
|
if(mSerialDebug) {
|
||||||
DPRINT(DBG_INFO, F("TX "));
|
DPRINT(DBG_INFO, F("TX "));
|
||||||
|
@ -136,9 +105,10 @@ class CmtRadio : public Radio {
|
||||||
iv->radioStatistics.txCnt++;
|
iv->radioStatistics.txCnt++;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::queue<packet_t> mBufCtrl;
|
uint64_t getIvId(Inverter<> *iv) {
|
||||||
|
return iv->radioId.u64;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
inline void reset(bool genDtuSn) {
|
inline void reset(bool genDtuSn) {
|
||||||
if(genDtuSn)
|
if(genDtuSn)
|
||||||
generateDtuSn();
|
generateDtuSn();
|
||||||
|
@ -182,8 +152,6 @@ class CmtRadio : public Radio {
|
||||||
}
|
}
|
||||||
|
|
||||||
CmtType mCmt;
|
CmtType mCmt;
|
||||||
bool mSerialDebug;
|
|
||||||
bool mIrqRcvd;
|
|
||||||
bool mRqstGetRx;
|
bool mRqstGetRx;
|
||||||
bool mCmtAvail;
|
bool mCmtAvail;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue