Refactor DS1624 driver

This commit is contained in:
Theo Arends 2019-12-28 11:35:44 +01:00
parent 7295b66310
commit 5f62b59f05
2 changed files with 72 additions and 63 deletions

View File

@ -419,6 +419,7 @@
// #define USE_PCF8574 // [I2cDriver2] Enable PCF8574 I/O Expander (I2C addresses 0x20 - 0x26 and 0x39 - 0x3F) (+1k9 code)
// #define USE_HIH6 // [I2cDriver36] Enable Honeywell HIH Humidity and Temperature sensor (I2C address 0x27) (+0k6)
// #define USE_DHT12 // [I2cDriver41] Enable DHT12 humidity and temperature sensor (I2C address 0x5C) (+0k7 code)
// #define USE_DS1624 // [I2cDriver42] Enable DS1624, DS1621 temperature sensor (I2C addresses 0x48 - 0x4F) (+1k2 code)
// #define USE_DISPLAY // Add I2C Display Support (+2k code)
#define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0

View File

@ -44,33 +44,36 @@ enum {
};
#define DS1624_MAX_SENSORS 8
bool ds1624_init = false;
struct {
bool valid;
float value;
int type;
uint8_t type;
bool valid;
char name[9];
} ds1624_sns[DS1624_MAX_SENSORS];
uint32_t DS1624_Idx2Addr(int idx) {
uint32_t DS1624_Idx2Addr(uint32_t idx) {
return 0x48 + idx;
}
void DS1624_HotPlugUp(int idx) {
uint32_t addr;
uint8_t config,tmp;
void DS1624_HotPlugUp(uint32_t idx)
{
uint32_t addr = DS1624_Idx2Addr(idx);
addr = DS1624_Idx2Addr(idx);
if (I2cActive(addr)) return;
if (!I2cSetDevice(addr)) return;
if (I2cActive(addr)) { return; }
if (!I2cSetDevice(addr)) { return; }
uint8_t config;
if (I2cValidRead8(&config, addr, DS1624_CONF_REGISTER)) {
if (I2cValidRead8(&tmp, addr, DS1624_MEM_REGISTER))
ds1624_sns[idx].type=DS1624_TYPE_DS1624;
else ds1624_sns[idx].type=DS1624_TYPE_DS1621;
if (ds1624_sns[idx].type==DS1624_TYPE_DS1621)
snprintf_P(ds1624_sns[idx].name, sizeof(ds1624_sns[idx].name), PSTR("DS1621_%d"), idx);
else snprintf_P(ds1624_sns[idx].name, sizeof(ds1624_sns[idx].name), PSTR("DS1624_%d"), idx);
uint8_t tmp;
ds1624_sns[idx].type = (I2cValidRead8(&tmp, addr, DS1624_MEM_REGISTER)) ? DS1624_TYPE_DS1624 : DS1624_TYPE_DS1621;
snprintf_P(ds1624_sns[idx].name, sizeof(ds1624_sns[idx].name), PSTR("DS162%c%c%d"),
(ds1624_sns[idx].type == DS1624_TYPE_DS1621) ? '1' : '4', IndexSeparator(), idx);
I2cSetActiveFound(addr, ds1624_sns[idx].name);
ds1624_sns[idx].valid = true;
if ((config & 1) == 1) {
config &= 0xfe;
@ -82,9 +85,10 @@ void DS1624_HotPlugUp(int idx) {
}
}
void DS1624_HotPlugDown(int idx) {
void DS1624_HotPlugDown(int idx)
{
uint32_t addr = DS1624_Idx2Addr(idx);
if (I2cActive(addr)==false) return;
if (!I2cActive(addr)) { return; }
I2cResetActive(addr);
ds1624_sns[idx].valid = false;
AddLog_P2(LOG_LEVEL_INFO, "Hot UnPlug %s", ds1624_sns[idx].name);
@ -92,16 +96,16 @@ void DS1624_HotPlugDown(int idx) {
bool DS1624GetTemp(float *value, int idx)
{
uint16_t t;
bool result;
uint32_t addr = DS1624_Idx2Addr(idx);
if (!I2cValidRead16(&t, DS1624_Idx2Addr(idx), DS1624_TEMP_REGISTER)) return false;
*value = ConvertTemp(((float)(int8_t)(t>>8)) + ((t>>4)&0xf)*0.0625);
uint16_t t;
if (!I2cValidRead16(&t, DS1624_Idx2Addr(idx), DS1624_TEMP_REGISTER)) { return false; }
*value = ((float)(int8_t)(t>>8)) + ((t>>4)&0xf)*0.0625;
if (ds1624_sns[idx].type == DS1624_TYPE_DS1621) { // Higher resolution
uint8_t perc,remain;
if (I2cValidRead8(&remain, addr, DS1621_COUNTER_REGISTER)==false) return true;
if (I2cValidRead8(&perc, addr, DS1621_SLOPE_REGISTER)==false) return true;
uint8_t remain;
if (!I2cValidRead8(&remain, addr, DS1621_COUNTER_REGISTER)) { return true; }
uint8_t perc;
if (!I2cValidRead8(&perc, addr, DS1621_SLOPE_REGISTER)) { return true; }
*value += ((float)perc - (float)remain)/((float)perc) - 0.25;
}
return true;
@ -111,13 +115,15 @@ void DS1624HotPlugScan(void)
{
uint16_t t;
for (int idx = 0; idx < DS1624_MAX_SENSORS; idx++) {
for (uint32_t idx = 0; idx < DS1624_MAX_SENSORS; idx++) {
uint32_t addr = DS1624_Idx2Addr(idx);
if ((I2cActive(addr)==true) && (ds1624_sns[idx].valid==false))
if (I2cActive(addr) && !ds1624_sns[idx].valid) {
continue; // is busy by another driver
}
if (!I2cValidRead16(&t, DS1624_Idx2Addr(idx), DS1624_TEMP_REGISTER)) {
DS1624_HotPlugDown(idx);
continue; }
continue;
}
DS1624_HotPlugUp(idx);
}
}
@ -125,31 +131,32 @@ void DS1624HotPlugScan(void)
void DS1624EverySecond(void)
{
float t;
for (int i = 0; i < DS1624_MAX_SENSORS; i++) {
if (ds1624_sns[i].valid==false) continue;
if (!DS1624GetTemp(&t, i)) continue;
ds1624_sns[i].value=t;
for (uint32_t i = 0; i < DS1624_MAX_SENSORS; i++) {
if (!ds1624_sns[i].valid) { continue; }
if (!DS1624GetTemp(&t, i)) { continue; }
ds1624_sns[i].value = ConvertTemp(t);
}
}
void DS1624Show(bool json)
{
int i;
char temperature[33];
bool once = true;
for (i = 0; i < DS1624_MAX_SENSORS; i++) {
if (ds1624_sns[i].valid==false) continue;
for (uint32_t i = 0; i < DS1624_MAX_SENSORS; i++) {
if (!ds1624_sns[i].valid) { continue; }
dtostrfd(ds1624_sns[i].value, Settings.flag2.temperature_resolution, temperature);
if (json) {
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s}"), ds1624_sns[i].name, temperature);
if (0 == tele_period) { //FIXME 0==i WTF?
if ((0 == tele_period) && once) {
#ifdef USE_DOMOTICZ
DomoticzSensor(DZ_TEMP, temperature);
#endif // USE_DOMOTICZ
#ifdef USE_KNX
KnxSensor(KNX_TEMPERATURE, temperature);
#endif // USE_KNX
once = false;
}
#ifdef USE_WEBSERVER
} else {
@ -163,15 +170,16 @@ void DS1624Show(bool json)
* Interface
\*********************************************************************************************/
#define FUNC_HOTPLUG_SCAN 999 // FIXME remove it after HOTPLUG supported
#define FUNC_HOTPLUG_SCAN 255 // FIXME remove it after HOTPLUG supported
bool Xsns59(uint8_t function)
{
if (!I2cEnabled(XI2C_20)) { return false; }
if (!I2cEnabled(XI2C_42)) { return false; }
bool result = false;
if (FUNC_INIT == function) {
if (ds1624_init==false) {
if (!ds1624_init) {
memset(ds1624_sns, 0, sizeof(ds1624_sns));
ds1624_init = true;
DS1624HotPlugScan(); // If HotPlug is off