Help with a simplified Dallas Temperature library

This is my first post on this forum and I have no formal training on C. I have used Dallas Temperature sensors for a number of projects and have been helping out with projects with some friends. I wanted to make a very simple library for the DS18B20 sensor that made it bit easier to use. I have made up this library which works but its coding is not what I had wanted. The library with example code can be gotten here. http://www.hayriv.com/arduino/ds18b20-library

My intent was to have a constructor that could be fed a pin number. The library would create an instance of the OneWire class then an instance of the Dallas Temperature class and then begin() the Dallas Temperature instance. Functions are provided to read the temperature in C or F. None of the other features are implemented.

My attempts to make an instance of OneWire and DallasTemperature classes in the constructor and save them for use later failed. That is now done each time the temperature is read. I believe I was able to save a pointer to the OneWire class but was unable to even create a global variable for the DallasTemperature class.

Any pointers or advise would be appreciated. I have done some programming in Java. In Java I would have extended the DallasTemperature class and then had easy access to all the methods/functions in that class. It looks like C++ can inherit from multiple classes. Would this work better if it inherited from both the OneWire and DallasTemperature classes?

Here are a few answers to my own questions. If I don't have any of this correct please reply to this post.

You can set up a class that inherits from one or more other classes. This web pages explains it. http://playground.arduino.cc/Code/Hierarchy

I tried this with my class to see if it could inherit from OneWire and DallasTemperature. class DS18B20Solo{ was changed to class DS18B20Solo : public OneWire , public DallasTemperature{

It appears that this only works if the is a constructor for the class that takes no parameters. OneWire is set up as OneWire(uint8_t) and DallasTemperature(OneWire*) I change both of these libraries to have a blank constructor and the code compiled.

Let me know if there is some way to do this without changing the OneWire and DallasTemperature libraries.

Prior to adding these blank constructors I couldn't add OneWire _ourWire; or DallasTemperature _sensor; as private variables in my class. After making the changes that was allowed.

I don't think that the inheritance approach is a good idea because of the way these classes were constructed.

I tried setting the DallasTemperature _sensor in the constructor of DS18B20Solo like this

DS18B20Solo::DS18B20Solo(uint8_t pin) { _pin = pin; OneWire temp1Wire(_pin); DallasTemperature _sensor(&temp1Wire); }

I don't think _sensor gets saved as it doesn't work when it is used to get a temperature reading.

In the function to get a temperature this works

OneWire temp1Wire(_pin); DallasTemperature _sensor(&temp1Wire); _sensor.requestTemperatures(); float temp = _sensor.getTempCByIndex(0);

but this doesn't _sensor.requestTemperatures(); float temp = _sensor.getTempCByIndex(0);

and this doesn't work either

if(!_started){ OneWire temp1Wire(_pin); DallasTemperature _sensor(&temp1Wire); _started = true; } _sensor.requestTemperatures(); float temp = _sensor.getTempCByIndex(0);

I believe the DallasTemperature library is given a pointer to the OneWire class. Calls to functions in that class look like this _wire->select(deviceAddress); where _wire is the pointer to the OneWire instance.

Would it be possible to save a pointer to the DallasTemperature class where it could be accessed in this same way? _dallasTempPointer->.requestTemperatures(); float temp = _dallasTempPointer->.getTempCByIndex(0); would work?

I'm back once again. Looks like part of the problem is that DallasTemperature has a local scope. When it is done in the constructor it is no longer valid outside of the constructor. The same problem happens when it is created inside the if() statement. That is why it works if it is created each time a temperature is read but not otherwise.

As far as the last question about using a pointer to DallasTemperature This will work _dallasTempPointer->.requestTemperatures(); float temp = _dallasTempPointer->.getTempCByIndex(0);

as long as _dallasTempPointer is assigned within the function.

Is there any way to create a variable for the class that is an instance of DallasTemperature without changing DallasTemperature to have a blank constructor?

Is there any way to create a variable for the class that is an instance of DallasTemperature without changing DallasTemperature to have a blank constructor?

Yes, there is.

But before we head down that path, you can initialize the members correctly in the constructor using an 'initiaizer list'.

Before the open curly '{' of the constructor, you can initialize each member of the class ( and base classes if any ). In fact this is how you have to initialize any constant members or references.

class MyTemp{
  public:
    MyTemp( uint8_t pin );
  private:
    uint8_t pin;
    OneWire wire;
    DallasTemperature sensor;
};


MyTemp::MyTemp( uint8_t pin ) 
  : pin( pin ), wire( pin ), sensor( &wire ) {
    //Empty
}

Thanks. That was exactly what needed to be done to make this work. I cleaned up the code and posted it as DS18B20Solo.zip v.2 on the same web page here. http://www.hayriv.com/arduino/ds18b20-library