Dallas sensor detection.

Minor clenaup & fixes.
This commit is contained in:
Blaz Kristan 2021-04-29 17:44:31 +02:00
parent 0d8e763a5f
commit 1a279d14c4
4 changed files with 57 additions and 58 deletions

View File

@ -57,23 +57,24 @@ class UsermodTemperature : public Usermod {
static const char _enabled[]; static const char _enabled[];
static const char _readInterval[]; static const char _readInterval[];
//Dallas sensor quick reading. Credit to - Author: Peter Scargill, August 17th, 2013 //Dallas sensor quick (& dirty) reading. Credit to - Author: Peter Scargill, August 17th, 2013
int16_t readDallas() { int16_t readDallas() {
byte i; byte i;
byte data[2]; byte data[2];
int16_t result; int16_t result; // raw data from sensor
oneWire->reset(); oneWire->reset();
oneWire->write(0xCC); oneWire->write(0xCC); // skip ROM
oneWire->write(0xBE); oneWire->write(0xBE); // read (temperature) from EEPROM
for (i=0; i < 2; i++) data[i] = oneWire->read(); for (i=0; i < 2; i++) data[i] = oneWire->read(); // first 2 bytes contain temperature
for (i=2; i < 8; i++) oneWire->read(); // read unused bytes
result = (data[1]<<8) | data[0]; result = (data[1]<<8) | data[0];
result >>= 4; result >>= 4; // 9-bit precision accurate to 1°C (/16)
if (data[1]&0x80) result |= 61440; if (data[1]&0x80) result |= 0xF000; // fix negative value
if (data[0]&0x08) ++result; //if (data[0]&0x08) ++result;
oneWire->reset(); oneWire->reset();
oneWire->write(0xCC); oneWire->write(0xCC); // skip ROM
oneWire->write(0x44,1); oneWire->write(0x44,0); // request new temperature reading (without parasite power)
return result*10; return result;
} }
void requestTemperatures() { void requestTemperatures() {
@ -84,7 +85,7 @@ class UsermodTemperature : public Usermod {
} }
void getTemperature() { void getTemperature() {
temperature = readDallas()/10.0f; temperature = readDallas();
if (!degC) temperature = temperature * 1.8f + 32; if (!degC) temperature = temperature * 1.8f + 32;
lastMeasurement = millis(); lastMeasurement = millis();
waitingForConversion = false; waitingForConversion = false;
@ -92,44 +93,45 @@ class UsermodTemperature : public Usermod {
DEBUG_PRINTF("Read temperature %2.1f.\n", temperature); DEBUG_PRINTF("Read temperature %2.1f.\n", temperature);
} }
bool findSensor() {
DEBUG_PRINTLN(F("Searching for sensor..."));
uint8_t deviceAddress[8] = {0,0,0,0,0,0,0,0};
// find out if we have DS18xxx sensor attached
oneWire->reset_search();
while (oneWire->search(deviceAddress)) {
if (oneWire->crc8(deviceAddress, 7) == deviceAddress[7]) {
switch (deviceAddress[0]) {
case 0x10: // DS18S20
case 0x22: // DS18B20
case 0x28: // DS1822
case 0x3B: // DS1825
case 0x42: // DS28EA00
DEBUG_PRINTLN(F("Sensor found."));
return true;
}
}
}
return false;
}
public: public:
void setup() { void setup() {
//bool sensorFound = false; int retries = 10;
// pin retrieved from cfg.json (readFromConfig()) prior to running setup() // pin retrieved from cfg.json (readFromConfig()) prior to running setup()
if (!pinManager.allocatePin(temperaturePin,false)) { if (!pinManager.allocatePin(temperaturePin,false)) {
temperaturePin = -1; // allocation failed temperaturePin = -1; // allocation failed
disabled = true;
DEBUG_PRINTLN(F("Temperature pin allocation failed.")); DEBUG_PRINTLN(F("Temperature pin allocation failed."));
} else { } else {
//DeviceAddress deviceAddress; if (!disabled) {
oneWire = new OneWire(temperaturePin); // config says we are enabled
oneWire->reset(); oneWire = new OneWire(temperaturePin);
/* if (!oneWire->reset())
// find out if we have DS18xxx sensor attached disabled = true; // resetting 1-Wire bus yielded an error
oneWire->reset_search(); else
while (oneWire->search(deviceAddress)) { while ((disabled=!findSensor()) && retries--) delay(25); // try to find sensor
if (oneWire->crc8(deviceAddress, 7) == deviceAddress[7]) {
switch (deviceAddress[0]) {
case 0x10: // DS18S20
case 0x22: // DS18B20
case 0x28: // DS1822
case 0x3B: // DS1825
case 0x42: // DS28EA00
sensorFound = true; // sensor found;
DEBUG_PRINTLN(F("Sensor found."));
break;
}
}
} }
*/
}
disabled = disabled || (temperaturePin==-1);
if (!disabled) {
DEBUG_PRINTLN(F("Dallas Temperature found"));
} else {
DEBUG_PRINTLN(F("Dallas Temperature not found"));
} }
initDone = true; initDone = true;
} }
@ -152,7 +154,7 @@ class UsermodTemperature : public Usermod {
} }
// we were waiting for a conversion to complete, have we waited log enough? // we were waiting for a conversion to complete, have we waited log enough?
if (now - lastTemperaturesRequest >= 800 /* 93.75ms per the datasheet but can be up to 750ms*/) { if (now - lastTemperaturesRequest >= 800 /* 93.75ms per the datasheet but can be up to 750ms */) {
getTemperature(); getTemperature();
if (WLED_MQTT_CONNECTED) { if (WLED_MQTT_CONNECTED) {

View File

@ -86,6 +86,8 @@ void deserializeConfig() {
noWifiSleep = !noWifiSleep; noWifiSleep = !noWifiSleep;
//int wifi_phy = doc[F("wifi")][F("phy")]; //force phy mode n? //int wifi_phy = doc[F("wifi")][F("phy")]; //force phy mode n?
DEBUG_PRINTLN(F(" Done network."));
JsonObject hw = doc[F("hw")]; JsonObject hw = doc[F("hw")];
// initialize LED pins and lengths prior to other HW // initialize LED pins and lengths prior to other HW
@ -120,8 +122,6 @@ void deserializeConfig() {
if (length==0 || length+lC > MAX_LEDS) continue; // zero length or we reached max. number of LEDs, just stop if (length==0 || length+lC > MAX_LEDS) continue; // zero length or we reached max. number of LEDs, just stop
uint16_t start = elm[F("start")] | 0; uint16_t start = elm[F("start")] | 0;
if (start >= lC+length) continue; // something is very wrong :) if (start >= lC+length) continue; // something is very wrong :)
//limit length of strip if it would exceed total configured LEDs
//if (start + length > ledCount) length = ledCount - start;
uint8_t colorOrder = elm[F("order")]; uint8_t colorOrder = elm[F("order")];
uint8_t skipFirst = elm[F("skip")]; uint8_t skipFirst = elm[F("skip")];
uint8_t ledType = elm["type"] | TYPE_WS2812_RGB; uint8_t ledType = elm["type"] | TYPE_WS2812_RGB;
@ -134,11 +134,12 @@ void deserializeConfig() {
lC += length; lC += length;
BusConfig bc = BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst); BusConfig bc = BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst);
mem += BusManager::memUsage(bc); mem += BusManager::memUsage(bc);
if (mem <= MAX_LED_MEMORY) busses.add(bc); DEBUG_PRINT(F(" Adding bus no. "));
DEBUG_PRINTLN(busses.getNumBusses());
if (mem <= MAX_LED_MEMORY && busses.getNumBusses() <= WLED_MAX_BUSSES) busses.add(bc); // finalization will be done in WLED::beginStrip()
} }
if (lC > ledCount) ledCount = lC; // fix incorrect total length (honour analog setup) if (lC > ledCount) ledCount = lC; // fix incorrect total length (honour analog setup)
//strip.finalizeInit(); // will be done in WLED::beginStrip() DEBUG_PRINTLN(F(" Done LEDs."));
if (hw_led["rev"]) busses.getBus(0)->reversed = true; //set 0.11 global reversed setting for first bus
JsonObject hw_btn_ins_0 = hw[F("btn")][F("ins")][0]; JsonObject hw_btn_ins_0 = hw[F("btn")][F("ins")][0];
CJSON(buttonType, hw_btn_ins_0["type"]); CJSON(buttonType, hw_btn_ins_0["type"]);
@ -178,6 +179,7 @@ void deserializeConfig() {
if (relay.containsKey("rev")) { if (relay.containsKey("rev")) {
rlyMde = !relay["rev"]; rlyMde = !relay["rev"];
} }
DEBUG_PRINTLN(F(" Done HW."));
//int hw_status_pin = hw[F("status")]["pin"]; // -1 //int hw_status_pin = hw[F("status")]["pin"]; // -1
@ -388,6 +390,7 @@ void deserializeConfig() {
} }
#endif #endif
DEBUG_PRINTLN(F("Starting usermod config."));
JsonObject usermods_settings = doc["um"]; JsonObject usermods_settings = doc["um"];
usermods.readFromConfig(usermods_settings); usermods.readFromConfig(usermods_settings);
} }

View File

@ -76,9 +76,9 @@ void loadPlaylist(JsonObject playlistObj) {
for (int i = it; i < playlistLen; i++) entries[i].dur = entries[it -1].dur; for (int i = it; i < playlistLen; i++) entries[i].dur = entries[it -1].dur;
it = 0; it = 0;
JsonArray tr = playlistObj["transition"]; JsonArray tr = playlistObj[F("transition")];
if (tr.isNull()) { if (tr.isNull()) {
entries[0].tr = playlistObj["transition"] | (transitionDelay / 100); entries[0].tr = playlistObj[F("transition")] | (transitionDelay / 100);
it = 1; it = 1;
} else { } else {
for (int transition : tr) { for (int transition : tr) {

View File

@ -258,7 +258,6 @@ void getSettingsJS(byte subPage, char* dest)
{ {
char nS[8]; char nS[8];
// (TODO: usermod config shouldn't use state. instead we should load "um" object from cfg.json)
// add reserved and usermod pins as d.um_p array // add reserved and usermod pins as d.um_p array
DynamicJsonDocument doc(JSON_BUFFER_SIZE/2); DynamicJsonDocument doc(JSON_BUFFER_SIZE/2);
JsonObject mods = doc.createNestedObject(F("um")); JsonObject mods = doc.createNestedObject(F("um"));
@ -279,7 +278,7 @@ void getSettingsJS(byte subPage, char* dest)
} }
} else { } else {
if (i++) oappend(SET_F(",")); if (i++) oappend(SET_F(","));
oappendi((int)obj["pin"]); oappendi(obj["pin"].as<int>());
} }
} }
} }
@ -317,16 +316,13 @@ void getSettingsJS(byte subPage, char* dest)
// set limits // set limits
oappend(SET_F("bLimits(")); oappend(SET_F("bLimits("));
oappend(itoa(WLED_MAX_BUSSES,nS,10)); oappend(itoa(WLED_MAX_BUSSES,nS,10)); oappend(",");
oappend(","); oappend(itoa(MAX_LEDS_PER_BUS,nS,10)); oappend(",");
oappend(itoa(MAX_LEDS_PER_BUS,nS,10));
oappend(",");
oappend(itoa(MAX_LED_MEMORY,nS,10)); oappend(itoa(MAX_LED_MEMORY,nS,10));
oappend(SET_F(");")); oappend(SET_F(");"));
sappend('v',SET_F("LC"),ledCount); sappend('v',SET_F("LC"),ledCount);
// bool skip = false;
for (uint8_t s=0; s < busses.getNumBusses(); s++) { for (uint8_t s=0; s < busses.getNumBusses(); s++) {
Bus* bus = busses.getBus(s); Bus* bus = busses.getBus(s);
char lp[4] = "L0"; lp[2] = 48+s; lp[3] = 0; //ascii 0-9 //strip data pin char lp[4] = "L0"; lp[2] = 48+s; lp[3] = 0; //ascii 0-9 //strip data pin
@ -351,7 +347,6 @@ void getSettingsJS(byte subPage, char* dest)
sappend('c',cv,bus->reversed); sappend('c',cv,bus->reversed);
sappend('c',sl,bus->skipFirstLed()); sappend('c',sl,bus->skipFirstLed());
// sappend('c',ew,bus->isRgbw()); // sappend('c',ew,bus->isRgbw());
// if (!skip) skip = bus->skipFirstLed()>0;
} }
sappend('v',SET_F("MA"),strip.ablMilliampsMax); sappend('v',SET_F("MA"),strip.ablMilliampsMax);
sappend('v',SET_F("LA"),strip.milliampsPerLed); sappend('v',SET_F("LA"),strip.milliampsPerLed);
@ -379,7 +374,6 @@ void getSettingsJS(byte subPage, char* dest)
sappend('v',SET_F("TL"),nightlightDelayMinsDefault); sappend('v',SET_F("TL"),nightlightDelayMinsDefault);
sappend('v',SET_F("TW"),nightlightMode); sappend('v',SET_F("TW"),nightlightMode);
sappend('i',SET_F("PB"),strip.paletteBlend); sappend('i',SET_F("PB"),strip.paletteBlend);
// sappend('c',SET_F("SL"),skip);
sappend('v',SET_F("RL"),rlyPin); sappend('v',SET_F("RL"),rlyPin);
sappend('c',SET_F("RM"),rlyMde); sappend('c',SET_F("RM"),rlyMde);
sappend('v',SET_F("BT"),btnPin); sappend('v',SET_F("BT"),btnPin);