PaulStoffregen / OneWire

Library for Dallas/Maxim 1-Wire Chips
http://www.pjrc.com/teensy/td_libs_OneWire.html
607 stars 391 forks source link

ds.search() isn't working when used in class method #61

Open z4nD4R opened 6 years ago

z4nD4R commented 6 years ago

Description

Hello Everyone, I'm building my own ESP32 based app in which I'm using modular approach and so I'm using my own created libs for particular devices/sensors. So I did for ds18b20. Basicaly I've created a main myDevice class and all libs are inheriting the required method etc.

Pretty easy yeah? The problem with OneWire lib is: when I use it the eample code wrapped inside of my own libs, the ds.search won't find any device on the bus.

I'm pretty sure that my wiring and the code is corret as:

  1. Works like charm when using the same code in loop function
  2. also the basic scatch example works fine
  3. It also works in class method when I set the address correctly

Steps To Reproduce Problem

  1. Create generinc class
  2. Create inherited class with OneWire device
  3. Call i.e. status message from main lib a.ka. myDevice->status();
  4. Watch the magic

Hardware & Software

ESP32 dev board + ds18b20. I believe it is not hw related

Arduino IDE version 1.8.5

Version info & package name (from Tools > Boards > Board Manager)

Operating system & version Xubuntu 18.04

Arduino Sketch


// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem)
#include <Arduino.h>

// libraries: give links/details so anyone can compile your code for the same result

void setup() {
}

void loop() {
      Serial.println(device->get_id());
      String resp = device->status();
}

### Errors or Incorrect Output
"No more addresses!" - out of the lib. 
Sorry I didn't put exact code here as it is pretty complex and so I believe to keep it in this form would much usefull.

I'd appreciate any hint.

Than kyou

Y
PaulStoffregen commented 6 years ago

I copied your program into Arduino and clicked Verify.

sketch_jun13b: In function 'void loop()':
sketch_jun13b:10: error: 'device' was not declared in this scope
       Serial.println(device->get_id());
                      ^
'device' was not declared in this scope
z4nD4R commented 6 years ago

Hello,

thnak you. As I stated in my post "Sorry I didn't put exact code here as it is pretty complex and so I believe to keep it in this form would much usefull."

If you require the whole code I need to prepare a code sample for you.

Y

mathompl commented 2 years ago

I have a similar problem.

I store pointer to OneWire object in my Sensors class. It works when I call it directly from this class, but when sensors.class is passed to another class, search doesn't work in that class (zero results). Pointers to sensors are the same everywhere, so it doesnt copy the object.

Everything works fine when OneWire object is defined globally.

ps. i'm creating an object with "new" :

static DataStorage dataStorage;
static _Time time (&dataStorage);
static PWM pwm (&time);
static Sensors sensors;
static Nextion nextion (&time, &pwm, &sensors, &dataStorage);

class Sensors
{
public:
      Sensors ();
private:
      OneWire* __oneWire;
      DS18B20*  __sensorsWire;     
};

Sensors::Sensors ()
{
        __oneWire = new OneWire (ONEWIRE_PIN);
        __sensorsWire = new DS18B20 (__oneWire);
}

class Nextion
{
public:
        Nextion (_Time *_time, PWM *_pwm, Sensors *_sensors, DataStorage *_dataStorage);

private:
        _Time *__time;
        PWM *__pwm;
        Sensors *__sensors;
        DataStorage *__dataStorage;
}

Nextion::Nextion (_Time *_time, PWM *_pwm, Sensors *_sensors, DataStorage *_dataStorage)
{
        __time = _time;
        __pwm = _pwm;
        __sensors = _sensors;
        __dataStorage = _dataStorage;
}

Search doesnt work from Nextion class.