PlantCtrl/esp32/include/DS2438.h

111 lines
3.3 KiB
C
Raw Permalink Normal View History

/**
* @file DS2438.h
2021-02-16 22:25:12 +01:00
*
*
*/
#ifndef DS2438_h
#define DS2438_h
#include <Arduino.h>
#include <OneWire.h>
2021-10-22 15:52:19 +02:00
#include "RunningMedian.h"
2021-02-16 22:25:12 +01:00
#define DS2438_TEMPERATURE_CONVERSION_COMMAND 0x44
#define DS2438_VOLTAGE_CONVERSION_COMMAND 0xb4
#define DS2438_WRITE_SCRATCHPAD_COMMAND 0x4e
#define DS2438_COPY_SCRATCHPAD_COMMAND 0x48
#define DS2438_READ_SCRATCHPAD_COMMAND 0xbe
#define DS2438_RECALL_MEMORY_COMMAND 0xb8
#define PAGE_MIN 0
#define PAGE_MAX 7
#define DS2438_CHA 0
#define DS2438_CHB 1
#define DS2438_MODE_CHA 0x01
#define DS2438_MODE_CHB 0x02
#define DS2438_MODE_TEMPERATURE 0x04
#define DS2438_TEMPERATURE_DELAY 10
#define DS2438_VOLTAGE_CONVERSION_DELAY 8
2021-10-22 15:52:19 +02:00
#define DS2438_MEDIAN_COUNT 5
#define DS2438_MEDIAN_DELAY 50
2021-02-16 22:25:12 +01:00
#define DEFAULT_PAGE0(var) uint8_t var[8] { \
0b00001011 /* X, ADB=0, NVB=0, TB=0, AD=1, EE=0, CA=1, IAD=1 */, \
0, /* Temperatur */ \
0, /* Temperatur */ \
0, /* Voltage */ \
0, /* Voltage */ \
0, /* Current */ \
0, /* Current */ \
0b10000000 /* Threshold to 4LSB */ \
2021-02-16 22:25:12 +01:00
}
typedef struct PageOne {
uint8_t eleapsedTimerByte0; /**< LSB of timestamp */
uint8_t eleapsedTimerByte1;
uint8_t eleapsedTimerByte2;
uint8_t eleapsedTimerByte3; /**< MSB of timestamp */
uint8_t ICA; /**< Integrated Current Accumulator (current flowing into and out of the battery) */
uint8_t offsetRegisterByte0; /**< Offset for ADC calibdation */
uint8_t offsetRegisterByte1; /**< Offset for ADC calibdation */
uint8_t reserved;
} PageOne_t;
typedef struct PageSeven {
uint8_t userByte0;
uint8_t userByte1;
uint8_t userByte2;
uint8_t userByte3;
uint8_t CCA0; /**< Charging Current Accumulator (CCA) */
uint8_t CCA1; /**< Charging Current Accumulator (CCA) */
uint8_t DCA0; /**< Discharge Current Accumulator (DCA) */
uint8_t DCA1; /**< Discharge Current Accumulator (DCA) */
} PageSeven_t;
typedef uint8_t DeviceAddress[8];
class DS2438 {
public:
2021-05-27 21:43:15 +02:00
DS2438(OneWire *ow, float currentShunt, int retryOnCRCError);
2021-02-16 22:25:12 +01:00
void begin();
2021-10-22 15:52:19 +02:00
void updateMultiple();
2021-02-16 22:25:12 +01:00
double getTemperature();
float getVoltage(int channel=DS2438_CHA);
float getCurrent();
2021-02-16 23:40:05 +01:00
long getICA();
long getCCA();
long getDCA();
float getAh();
2021-02-16 22:25:12 +01:00
boolean isError();
boolean isFound();
private:
bool validAddress(const uint8_t*);
bool validFamily(const uint8_t* deviceAddress);
2021-10-22 15:52:19 +02:00
void update(bool firstIteration);
2021-02-16 22:25:12 +01:00
bool deviceFound = false;
OneWire *_ow;
DeviceAddress _address;
uint8_t _mode;
2021-10-22 15:52:19 +02:00
RunningMedian _temperature = RunningMedian(DS2438_MEDIAN_COUNT*2);
RunningMedian _voltageA = RunningMedian(DS2438_MEDIAN_COUNT);
RunningMedian _voltageB = RunningMedian(DS2438_MEDIAN_COUNT);
RunningMedian _current = RunningMedian(DS2438_MEDIAN_COUNT);
2021-02-16 22:25:12 +01:00
float _currentShunt;
2021-05-27 21:43:15 +02:00
int _retryOnCRCError;
2021-02-16 23:40:05 +01:00
long _CCA;
long _DCA;
long _ICA;
2021-02-16 22:25:12 +01:00
boolean _error;
boolean startConversion(int channel, boolean doTemperature);
boolean selectChannel(int channel);
void writePage(int page, uint8_t *data);
boolean readPage(int page, uint8_t *data);
};
#endif