Simple cpp wrapper of a ds18b20, onewire 'c' library. Supports multiple sensors.

Dependencies:   mbed

Dependents:   LPC11U68_DS18B20Sensor

Fork of DS18B20Sensor by Steve Spence

Committer:
jsteve
Date:
Sun Mar 03 01:41:51 2013 +0000
Revision:
0:1449f126b241
Child:
1:ea35ad346f25
Changed name from Sensor to DS18B20Sensor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jsteve 0:1449f126b241 1 #ifndef _SENSOR_H
jsteve 0:1449f126b241 2 #define _SENSOR_H
jsteve 0:1449f126b241 3
jsteve 0:1449f126b241 4 #include "mbed.h"
jsteve 0:1449f126b241 5 #include "DS18X20.h"
jsteve 0:1449f126b241 6 #include "onewire.h"
jsteve 0:1449f126b241 7
jsteve 0:1449f126b241 8 /** DS18B20Sensor class.
jsteve 0:1449f126b241 9 * A wrapper class for the DS18X20 and onewire 'c' library.
jsteve 0:1449f126b241 10 * Many thanks to Frederic Blanc (among others) for making this code available.
jsteve 0:1449f126b241 11 *
jsteve 0:1449f126b241 12 * Simple Example:
jsteve 0:1449f126b241 13 * @code
jsteve 0:1449f126b241 14 #include "mbed.h"
jsteve 0:1449f126b241 15 #include "DS18B20Sensor.h"
jsteve 0:1449f126b241 16
jsteve 0:1449f126b241 17 DS18B20Sensor sensor(p30);
jsteve 0:1449f126b241 18
jsteve 0:1449f126b241 19 int main()
jsteve 0:1449f126b241 20 {
jsteve 0:1449f126b241 21 char sensorBuf[25];
jsteve 0:1449f126b241 22
jsteve 0:1449f126b241 23 // count will search for sensors, if not already called
jsteve 0:1449f126b241 24 printf("Found %d sensor/s\r", sensor.count());
jsteve 0:1449f126b241 25
jsteve 0:1449f126b241 26 uint8_t result = sensor.startReading(true); // start sensor readings and wait
jsteve 0:1449f126b241 27
jsteve 0:1449f126b241 28 for (uint8_t i = 0; i < sensor.count(); i++) {
jsteve 0:1449f126b241 29 sensor.getReadingText(sensorBuf, i); // get result into buf
jsteve 0:1449f126b241 30 printf("Sensor %d : %s\r", i+1, sensorBuf); // display it to the world
jsteve 0:1449f126b241 31 }
jsteve 0:1449f126b241 32
jsteve 0:1449f126b241 33 }
jsteve 0:1449f126b241 34 * @endcode
jsteve 0:1449f126b241 35 */
jsteve 0:1449f126b241 36 class DS18B20Sensor
jsteve 0:1449f126b241 37 {
jsteve 0:1449f126b241 38 public:
jsteve 0:1449f126b241 39 /** Create Sensor instance
jsteve 0:1449f126b241 40 * @param pin pin number for the onewire bus
jsteve 0:1449f126b241 41 */
jsteve 0:1449f126b241 42 DS18B20Sensor(PinName pin);
jsteve 0:1449f126b241 43
jsteve 0:1449f126b241 44 /** Copies the sensor results into the parameter text.
jsteve 0:1449f126b241 45 * @param text The target text buffer.
jsteve 0:1449f126b241 46 * @param index The sensor number.
jsteve 0:1449f126b241 47 */
jsteve 0:1449f126b241 48 void getReadingText(char * text, uint8_t index);
jsteve 0:1449f126b241 49
jsteve 0:1449f126b241 50 /** Copies the sensor results into the parameter text.
jsteve 0:1449f126b241 51 * @param text The target text buffer.
jsteve 0:1449f126b241 52 * @param subzero 1 for less than zero.
jsteve 0:1449f126b241 53 * @param cel Degrees Cel
jsteve 0:1449f126b241 54 * @param cel Degrees fraction
jsteve 0:1449f126b241 55 */
jsteve 0:1449f126b241 56 void getReadingText(char * text, uint8_t subzero, uint8_t cel, uint8_t cel_frac_bits);
jsteve 0:1449f126b241 57
jsteve 0:1449f126b241 58 /** This searches for sensors on the onewire bus.
jsteve 0:1449f126b241 59 * It can also invoked by just using count()
jsteve 0:1449f126b241 60 * @returns The number of sensors found on the bus.
jsteve 0:1449f126b241 61 */
jsteve 0:1449f126b241 62 uint8_t search(void);
jsteve 0:1449f126b241 63
jsteve 0:1449f126b241 64 /** Starts the sensors reading.
jsteve 0:1449f126b241 65 * @param includeWait If true, the method will wait the specified time for the sensors to complete the reading.
jsteve 0:1449f126b241 66 * @returns
jsteve 0:1449f126b241 67 DS18X20_OK on success,
jsteve 0:1449f126b241 68 Other values in DS18X20.h on failure.
jsteve 0:1449f126b241 69 */
jsteve 0:1449f126b241 70 uint8_t startReading(bool includeWait = true);
jsteve 0:1449f126b241 71
jsteve 0:1449f126b241 72 /** Gets the sensors reading results.
jsteve 0:1449f126b241 73 * @param index The sensor number.
jsteve 0:1449f126b241 74 * @param subzero Returns 1 if less than zero.
jsteve 0:1449f126b241 75 * @param cel Returns degrees Cel
jsteve 0:1449f126b241 76 * @param cel Returns degrees fraction
jsteve 0:1449f126b241 77 */
jsteve 0:1449f126b241 78 void getReading(uint8_t index, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits);
jsteve 0:1449f126b241 79
jsteve 0:1449f126b241 80 /** If search() was not called before this, then search() is invoked.
jsteve 0:1449f126b241 81 * @returns The number of sensors found on the bus.
jsteve 0:1449f126b241 82 */
jsteve 0:1449f126b241 83 uint8_t count(void);
jsteve 0:1449f126b241 84
jsteve 0:1449f126b241 85 protected:
jsteve 0:1449f126b241 86
jsteve 0:1449f126b241 87 DigitalInOut _oneWirePort;
jsteve 0:1449f126b241 88 uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE];
jsteve 0:1449f126b241 89 uint8_t nSensors;
jsteve 0:1449f126b241 90 bool _init;
jsteve 0:1449f126b241 91
jsteve 0:1449f126b241 92 };
jsteve 0:1449f126b241 93
jsteve 0:1449f126b241 94 #endif // file