Browse Source

Auto try pulling up the line for Dallas sensor

fastled
Xose Pérez 6 years ago
parent
commit
54c5fd8655
3 changed files with 61 additions and 57 deletions
  1. +0
    -4
      code/espurna/config/sensors.h
  2. +42
    -36
      code/espurna/sensor.ino
  3. +19
    -17
      code/espurna/sensors/DallasSensor.h

+ 0
- 4
code/espurna/config/sensors.h View File

@ -115,10 +115,6 @@
#define DALLAS_PIN 13 #define DALLAS_PIN 13
#endif #endif
#ifndef DALLAS_PULLUP
#define DALLAS_PULLUP 1
#endif
#define DALLAS_RESOLUTION 9 // Not used atm #define DALLAS_RESOLUTION 9 // Not used atm
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------


+ 42
- 36
code/espurna/sensor.ino View File

@ -220,7 +220,6 @@ void _sensorInit() {
{ {
DallasSensor * sensor = new DallasSensor(); DallasSensor * sensor = new DallasSensor();
sensor->setGPIO(DALLAS_PIN); sensor->setGPIO(DALLAS_PIN);
sensor->setPullUp(DALLAS_PULLUP);
_sensorRegister(sensor); _sensorRegister(sensor);
} }
#endif #endif
@ -320,46 +319,14 @@ void _sensorInit() {
} }
// -----------------------------------------------------------------------------
// Values
// -----------------------------------------------------------------------------
unsigned char sensorCount() {
return _sensors.size();
}
unsigned char magnitudeCount() {
return _magnitudes.size();
}
String magnitudeName(unsigned char index) {
if (index < _magnitudes.size()) {
sensor_magnitude_t magnitude = _magnitudes[index];
return magnitude.sensor->slot(magnitude.local);
}
return String();
}
void _magnitudesInit() {
unsigned char magnitudeType(unsigned char index) {
if (index < _magnitudes.size()) {
return int(_magnitudes[index].type);
}
return MAGNITUDE_NONE;
}
// -----------------------------------------------------------------------------
void sensorSetup() {
// Load sensors
_sensorInit();
// Load magnitudes
for (unsigned char i=0; i<_sensors.size(); i++) { for (unsigned char i=0; i<_sensors.size(); i++) {
BaseSensor * sensor = _sensors[i]; BaseSensor * sensor = _sensors[i];
DEBUG_MSG("[SENSOR] %s\n", sensor->name().c_str()); DEBUG_MSG("[SENSOR] %s\n", sensor->name().c_str());
if (sensor->count() == 0) DEBUG_MSG("[SENSOR] -> NOTHING FOUND\n");
if (sensor->error() != 0) DEBUG_MSG("[SENSOR] -> ERROR %d\n", sensor->error());
for (unsigned char k=0; k<sensor->count(); k++) { for (unsigned char k=0; k<sensor->count(); k++) {
@ -391,6 +358,45 @@ void sensorSetup() {
} }
}
// -----------------------------------------------------------------------------
// Public
// -----------------------------------------------------------------------------
unsigned char sensorCount() {
return _sensors.size();
}
unsigned char magnitudeCount() {
return _magnitudes.size();
}
String magnitudeName(unsigned char index) {
if (index < _magnitudes.size()) {
sensor_magnitude_t magnitude = _magnitudes[index];
return magnitude.sensor->slot(magnitude.local);
}
return String();
}
unsigned char magnitudeType(unsigned char index) {
if (index < _magnitudes.size()) {
return int(_magnitudes[index].type);
}
return MAGNITUDE_NONE;
}
// -----------------------------------------------------------------------------
void sensorSetup() {
// Load sensors
_sensorInit();
// Load magnitudes
_magnitudesInit();
#if WEB_SUPPORT #if WEB_SUPPORT
// Websockets // Websockets


+ 19
- 17
code/espurna/sensors/DallasSensor.h View File

@ -39,22 +39,12 @@ class DallasSensor : public BaseSensor {
_dirty = true; _dirty = true;
} }
void setPullUp(bool pullup) {
if (_pullup == pullup) return;
_pullup = pullup;
_dirty = true;
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
unsigned char getGPIO() { unsigned char getGPIO() {
return _gpio; return _gpio;
} }
bool getPullUp() {
return _pullup;
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Sensor API // Sensor API
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@ -70,12 +60,15 @@ class DallasSensor : public BaseSensor {
if (_wire) delete _wire; if (_wire) delete _wire;
_wire = new OneWire(_gpio); _wire = new OneWire(_gpio);
// Must be done after the OneWire initialization
if (_pullup) pinMode(_gpio, INPUT_PULLUP);
// Search devices // Search devices
loadDevices(); loadDevices();
// If no devices found check again pulling up the line
if (_count == 0) {
pinMode(_gpio, INPUT_PULLUP);
loadDevices();
}
} }
// Loop-like method, call it in your main loop // Loop-like method, call it in your main loop
@ -230,6 +223,10 @@ class DallasSensor : public BaseSensor {
// Protected // Protected
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
bool validateID(unsigned char id) {
return (id == DS_CHIP_DS18S20) || (id == DS_CHIP_DS18B20) || (id == DS_CHIP_DS1822) || (id == DS_CHIP_DS1825);
}
unsigned char chip(unsigned char index) { unsigned char chip(unsigned char index) {
if (index < _count) return _devices[index].address[0]; if (index < _count) return _devices[index].address[0];
return 0; return 0;
@ -247,14 +244,20 @@ class DallasSensor : public BaseSensor {
void loadDevices() { void loadDevices() {
uint8_t address[8]; uint8_t address[8];
_wire->reset();
_wire->reset_search(); _wire->reset_search();
while (_wire->search(address)) { while (_wire->search(address)) {
// Check CRC // Check CRC
if (_wire->crc8(address, 7) == address[7]) { if (_wire->crc8(address, 7) == address[7]) {
ds_device_t device;
memcpy(device.address, address, 8);
_devices.push_back(device);
// Check ID
if (validateID(address[0])) {
ds_device_t device;
memcpy(device.address, address, 8);
_devices.push_back(device);
}
} }
} }
@ -270,7 +273,6 @@ class DallasSensor : public BaseSensor {
unsigned char _gpio; unsigned char _gpio;
unsigned long _interval; unsigned long _interval;
bool _pullup;
OneWire * _wire; OneWire * _wire;
}; };

Loading…
Cancel
Save