Skip to content

Commit

Permalink
Refactor Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
shining-man committed Nov 5, 2024
1 parent a46894f commit 5f1dd5a
Show file tree
Hide file tree
Showing 18 changed files with 787 additions and 588 deletions.
5 changes: 4 additions & 1 deletion include/BscSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
#include "Arduino.h"
#include "defines.h"
#include <SoftwareSerial.h>
#include "extension/ExtManager.h"



class BscSerial {
public:
BscSerial();

void initSerial();
void initSerial(ExtManager &extManager);
void stopCyclicRun(bool state);
void setHwSerial(uint8_t u8_devNr, uint32_t baudrate);
void setSoftSerial(uint8_t u8_devNr, uint32_t baudrate);
Expand Down
12 changes: 9 additions & 3 deletions include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ enum serialRxTxEn_e {serialRxTx_RxTxDisable, serialRxTx_TxEn, serialRxTx_RxEn};
#define I2C_DEV_ADDR_SLAVE1 16
#define I2C_DEV_ADDR_SLAVE2 17
#define I2C_DEV_ADDR_SERIAL_EXTENSION 32
#define I2C_DEV_ADDR_BT_EXTENSION_1 0x60
#define I2C_FREQUENCY 1000000U
#define I2C_CNT_SLAVES 2

Expand Down Expand Up @@ -496,7 +497,9 @@ enum serialDataRwTyp_e {BPN_NO_DATA, BPN_READ_SETTINGS, BPN_WRITE_READ_SETTINGS,
#define BMS_DATA 0x01 //BMS-Daten
#define INVERTER_DATA 0x02 //Inverter-Daten
#define BSC_DATA 0x03 //
#define BSC_GET_SLAVE_DATA 0x0A //
#define BSC_GET_BT_EXTENSION_DATA 0x0B // Hole Daten von BT Extension
#define BSC_GET_BT_EXTENSION_BMSDATA 0x0C // Hole Daten von BT Extension
#define BSC_SET_BT_EXTENSION_DATA 0x0D // Sende Daten zur BT Extension

//BMS_DATA 0x01
#define BMS_CELL_VOLTAGE 0x01
Expand Down Expand Up @@ -528,8 +531,11 @@ enum serialDataRwTyp_e {BPN_NO_DATA, BPN_READ_SETTINGS, BPN_WRITE_READ_SETTINGS,
#define BSC_RELAIS 0x03
#define BSC_DISPLAY_TIMEOUT 0x04

//BSC_GET_SLAVE_DATA 0x0A
//#define BMS_GET_ALL_DATA 0x01
// BSC_GET_BT_EXTENSION_DATA
#define BSC_DISP_GET_FOUND_MAC 0x01

// BSC_SET_BT_EXTENSION_DATA
#define BSC_BT_CONNECT_MACS 0x01 // Zu verbindende MACs


/*********************************************
Expand Down
25 changes: 25 additions & 0 deletions include/extension/ExtBluetooth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 tobias
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT


#ifndef EXTBLUETOOTH_H
#define EXTBLUETOOTH_H

#include "extension/ExtDeviceI2C.h"
#include "defines.h"


class ExtBluetooth : public ExtDeviceI2C {
public:
ExtBluetooth(uint8_t address);
~ExtBluetooth();

void initialize() override;

void readBtData();

};

#endif
55 changes: 55 additions & 0 deletions include/extension/ExtDeviceI2C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2024 Tobias Himmler
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT


#ifndef EXTDEVICE_I2C_H
#define EXTDEVICE_I2C_H

#include "extension/ExtPeripheral.h"
#include <stdint.h>

#include "inverter/Inverter.hpp"

class ExtDeviceI2C : public ExtPeripheral {
private:
static ExtDeviceI2C* instance;

SemaphoreHandle_t mutexI2cRx = NULL;
uint8_t mI2cRxBuf[256];

static void onReceiveWrapper(int len);
static void onRequestWrapper();
void onReceiveCb(int len);
void onRequestCb();

protected:
uint8_t deviceAddress;

void i2cCyclicRun(Inverter &inverter);

void I2cRxSemaphoreTake();
void I2cRxSemaphoreGive();

void i2cWriteRegister(uint8_t u8_i2cDevAdr, uint8_t u8_reg, uint8_t u8_data);
void i2cWriteBytes(uint8_t devAdr, uint8_t *data, uint8_t dataLen);
void i2cReadBytes(uint8_t *data, uint8_t dataLen);
uint8_t i2cRequest(uint8_t devAdr, uint8_t dataLen);

void i2cSendData(Inverter &inverter, uint8_t i2cAdr, uint8_t data1, uint8_t data2, uint8_t data3, const void *dataAdr, uint8_t dataLen);
void i2cSendData(Inverter &inverter, uint8_t i2cAdr, uint8_t data1, uint8_t data2, uint8_t data3, std::string data, uint8_t dataLen);
void i2cSendData(Inverter &inverter, uint8_t i2cAdr, uint8_t data1, uint8_t data2, uint8_t data3, int16_t data);

uint8_t isDeviceAvailable(uint8_t devAdresse);

public:
ExtDeviceI2C(uint8_t address);


protected:
//void writeByte(uint8_t reg, uint8_t data);
//uint8_t readByte(uint8_t reg);
};

#endif
25 changes: 25 additions & 0 deletions include/extension/ExtDisplay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 tobias
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT


#ifndef EXTDISPLAY_H
#define EXTDISPLAY_H

#include "extension/ExtDeviceI2C.h"
#include "defines.h"


class ExtDisplay : public ExtDeviceI2C {
public:
ExtDisplay(uint8_t address);
~ExtDisplay();

void initialize() override;

void sendDataStr(Inverter &inverter, uint8_t data1, uint8_t data2, std::string data, uint8_t dataLen);
void sendData(Inverter &inverter);
};

#endif
38 changes: 38 additions & 0 deletions include/extension/ExtManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2024 tobias
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT


#ifndef EXTMANAGER_H
#define EXTMANAGER_H

#include <vector>
#include <memory>
#include "extension/ExtPeripheral.h"
#include "extension/ExtSerial.h"
#include "extension/ExtDisplay.h"
#include "extension/ExtBluetooth.h"


class ExtManager {
public:
ExtManager();

void initialize();
void cyclicRun(Inverter &inverter);

ExtSerial &getSerial(size_t index);
ExtDisplay &getDisplay();
ExtBluetooth &getBt();

private:
const std::vector<uint8_t> serialAdresses = {I2C_DEV_ADDR_SERIAL_EXTENSION};

std::vector<std::unique_ptr<ExtSerial>> extSerials;
ExtDisplay extDisplay;
ExtBluetooth extBluetooth;

};

#endif
24 changes: 24 additions & 0 deletions include/extension/ExtPeripheral.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2024 Tobias Himmler
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT


#ifndef EXTPERIPHERAL_H
#define EXTPERIPHERAL_H

class ExtPeripheral {
protected:
bool enabled = false;

virtual void initialize() = 0; // Initialisiert das Gerät

void setEnabled(bool enable) { enabled = enable; }

public:
virtual ~ExtPeripheral() = default; // Virtueller Destruktor für saubere Speicherfreigabe

bool isEnabled() const { return enabled; }
};

#endif
25 changes: 25 additions & 0 deletions include/extension/ExtSerial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 tobias
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT


#ifndef EXTSERIAL_H
#define EXTSERIAL_H

#include "extension/ExtDeviceI2C.h"
#include "defines.h"


class ExtSerial : public ExtDeviceI2C {
public:
ExtSerial(uint8_t address);
~ExtSerial();

void initialize() override;

void extSerialSetEnable(uint8_t u8_serialDevNr, serialRxTxEn_e serialRxTxEn);

};

#endif
21 changes: 0 additions & 21 deletions include/i2c.h

This file was deleted.

7 changes: 7 additions & 0 deletions src/BleHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ BleHandler::~BleHandler() {

}


/*void BleHandler::startScan()
{
}*/


void BleHandler::setBalancerState(uint8_t devNr, boolean Alarm)
{

Expand Down
15 changes: 9 additions & 6 deletions src/BscSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "BmsData.h"
#include "log.h"
#include "dio.h"
#include "i2c.h"
#include "crc.h"

//include Devices
Expand All @@ -33,6 +32,8 @@

static const char *TAG = "BSC_SERIAL";

ExtManager *mExtManager;

uint32_t serialMqttSendeTimer;

struct serialDeviceData_s
Expand All @@ -55,9 +56,10 @@ BscSerial::BscSerial()

}

void BscSerial::initSerial()
void BscSerial::initSerial(ExtManager &extManager)
{
mSerialMutex = xSemaphoreCreateMutex();
mExtManager = &extManager;

#ifdef LILYGO_TCAN485
pinMode(TCAN485_RS485_EN_PIN, OUTPUT);
Expand Down Expand Up @@ -140,7 +142,7 @@ void BscSerial::setHwSerial(uint8_t u8_devNr, uint32_t baudrate)
Serial2.begin(baudrate,SERIAL_8N1,SERIAL3_PIN_RX,SERIAL3_PIN_TX);
serialDeviceData[u8_devNr].stream_mPort=&Serial2;
}
else if(u8_devNr>2 && isSerialExtEnabled()) // Hw Serial 0
else if(u8_devNr>2 && mExtManager->getSerial(0).isEnabled()) // Hw Serial 0
{
Serial2.end();
//Serial2.setRxBufferSize(300);
Expand Down Expand Up @@ -364,7 +366,8 @@ void BscSerial::setRxTxEnable(uint8_t u8_devNr, serialRxTxEn_e e_rw)
}
else if(u8_devNr>2 && u8_devNr<=10 && getHwVersion()>=2)
{
i2cExtSerialSetEnable(u8_devNr-3, e_rw);
if(mExtManager == nullptr) BSC_LOGE(TAG,"Fehler beim Zugriff auf die Serial Extension");
else mExtManager->getSerial(0).extSerialSetEnable(u8_devNr-3, e_rw);
}
#endif
}
Expand Down Expand Up @@ -415,14 +418,14 @@ void BscSerial::cyclicRun()

//Workaround: Notwendig damit der Transceiver nicht in einen komischen Zustand geht, indem er den RX "flattern" lässt.
//Unklar wo das Verhalten herkommt.
if(i>2 && isSerialExtEnabled())
if(i>2 && mExtManager->getSerial(0).isEnabled())
{
setRxTxEnable(2,serialRxTx_RxEn);
usleep(50);
setRxTxEnable(2,serialRxTx_RxTxDisable);
}

if(i>=2 && isSerialExtEnabled()) setSerialBaudrate(i); //Baudrate wechslen
if(i>=2 && mExtManager->getSerial(0).isEnabled()) setSerialBaudrate(i); //Baudrate wechslen

uint8_t *u8_pBmsFilterErrorCounter = getBmsFilterErrorCounter(BT_DEVICES_COUNT+i);

Expand Down
Loading

0 comments on commit 5f1dd5a

Please sign in to comment.