Simple cpp wrapper of a ds18b20, onewire 'c' library. Supports multiple sensors.
Dependents: DS18B20Sensor DS18B201
DS18B20Sensor.h@3:9fd95d590149, 2013-03-03 (annotated)
- Committer:
- jsteve
- Date:
- Sun Mar 03 02:22:27 2013 +0000
- Revision:
- 3:9fd95d590149
- Parent:
- 1:ea35ad346f25
Updated documentation.
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:ea35ad346f25 | 9 | * A wrapper class for the DS18B20 (temperature sensor) and onewire 'c' library. |
jsteve | 1:ea35ad346f25 | 10 | * Based on the OneWireDrv Library by Frederic Blanc. Many thanks to him for making this code available. |
jsteve | 1:ea35ad346f25 | 11 | * |
jsteve | 0:1449f126b241 | 12 | * Simple Example: |
jsteve | 0:1449f126b241 | 13 | * @code |
jsteve | 3:9fd95d590149 | 14 | #include "mbed.h" |
jsteve | 3:9fd95d590149 | 15 | #include "DS18B20Sensor.h" |
jsteve | 1:ea35ad346f25 | 16 | |
jsteve | 3:9fd95d590149 | 17 | // Instantiate sensor with hardware in pin p30 |
jsteve | 3:9fd95d590149 | 18 | DS18B20Sensor sensor(p30); |
jsteve | 1:ea35ad346f25 | 19 | |
jsteve | 3:9fd95d590149 | 20 | // Setup a window to the world |
jsteve | 3:9fd95d590149 | 21 | Serial pc(p13, p14); |
jsteve | 3:9fd95d590149 | 22 | //Serial pc(USBTX, USBRX); |
jsteve | 3:9fd95d590149 | 23 | |
jsteve | 3:9fd95d590149 | 24 | int main() |
jsteve | 3:9fd95d590149 | 25 | { |
jsteve | 3:9fd95d590149 | 26 | char sensorBuf[25]; |
jsteve | 3:9fd95d590149 | 27 | |
jsteve | 3:9fd95d590149 | 28 | // count will search for sensors, if not already called |
jsteve | 3:9fd95d590149 | 29 | pc.printf("Found %d sensor/s\r", sensor.count()); |
jsteve | 3:9fd95d590149 | 30 | |
jsteve | 3:9fd95d590149 | 31 | uint8_t result = sensor.startReading(true); // start sensor readings and wait |
jsteve | 3:9fd95d590149 | 32 | |
jsteve | 3:9fd95d590149 | 33 | for (uint8_t i = 0; i < sensor.count(); i++) { |
jsteve | 3:9fd95d590149 | 34 | sensor.getReading(sensorBuf, i); // get result into buf |
jsteve | 3:9fd95d590149 | 35 | pc.printf("Sensor %d : %s\r", i+1, sensorBuf); // display it to the world |
jsteve | 3:9fd95d590149 | 36 | } |
jsteve | 3:9fd95d590149 | 37 | |
jsteve | 3:9fd95d590149 | 38 | } |
jsteve | 0:1449f126b241 | 39 | * @endcode |
jsteve | 0:1449f126b241 | 40 | */ |
jsteve | 0:1449f126b241 | 41 | class DS18B20Sensor |
jsteve | 0:1449f126b241 | 42 | { |
jsteve | 0:1449f126b241 | 43 | public: |
jsteve | 1:ea35ad346f25 | 44 | /** Create DS18B20Sensor instance |
jsteve | 1:ea35ad346f25 | 45 | * @param pin The pin number used by the onewire bus. |
jsteve | 0:1449f126b241 | 46 | */ |
jsteve | 0:1449f126b241 | 47 | DS18B20Sensor(PinName pin); |
jsteve | 0:1449f126b241 | 48 | |
jsteve | 0:1449f126b241 | 49 | /** Copies the sensor results into the parameter text. |
jsteve | 0:1449f126b241 | 50 | * @param text The target text buffer. |
jsteve | 0:1449f126b241 | 51 | * @param index The sensor number. |
jsteve | 0:1449f126b241 | 52 | */ |
jsteve | 1:ea35ad346f25 | 53 | void getReading(char * text, uint8_t index); |
jsteve | 0:1449f126b241 | 54 | |
jsteve | 0:1449f126b241 | 55 | /** Copies the sensor results into the parameter text. |
jsteve | 0:1449f126b241 | 56 | * @param text The target text buffer. |
jsteve | 0:1449f126b241 | 57 | * @param subzero 1 for less than zero. |
jsteve | 0:1449f126b241 | 58 | * @param cel Degrees Cel |
jsteve | 0:1449f126b241 | 59 | * @param cel Degrees fraction |
jsteve | 0:1449f126b241 | 60 | */ |
jsteve | 1:ea35ad346f25 | 61 | void getReading(char * text, uint8_t subzero, uint8_t cel, uint8_t cel_frac_bits); |
jsteve | 1:ea35ad346f25 | 62 | |
jsteve | 1:ea35ad346f25 | 63 | /** Gets the sensors reading results. |
jsteve | 1:ea35ad346f25 | 64 | * @param index The sensor number. |
jsteve | 1:ea35ad346f25 | 65 | * @param subzero Returns 1 if less than zero. |
jsteve | 1:ea35ad346f25 | 66 | * @param cel Returns degrees Cel |
jsteve | 1:ea35ad346f25 | 67 | * @param cel Returns degrees fraction |
jsteve | 1:ea35ad346f25 | 68 | */ |
jsteve | 1:ea35ad346f25 | 69 | void getReading(uint8_t index, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits); |
jsteve | 0:1449f126b241 | 70 | |
jsteve | 0:1449f126b241 | 71 | /** This searches for sensors on the onewire bus. |
jsteve | 0:1449f126b241 | 72 | * It can also invoked by just using count() |
jsteve | 0:1449f126b241 | 73 | * @returns The number of sensors found on the bus. |
jsteve | 0:1449f126b241 | 74 | */ |
jsteve | 0:1449f126b241 | 75 | uint8_t search(void); |
jsteve | 0:1449f126b241 | 76 | |
jsteve | 0:1449f126b241 | 77 | /** Starts the sensors reading. |
jsteve | 0:1449f126b241 | 78 | * @param includeWait If true, the method will wait the specified time for the sensors to complete the reading. |
jsteve | 0:1449f126b241 | 79 | * @returns |
jsteve | 0:1449f126b241 | 80 | DS18X20_OK on success, |
jsteve | 0:1449f126b241 | 81 | Other values in DS18X20.h on failure. |
jsteve | 0:1449f126b241 | 82 | */ |
jsteve | 0:1449f126b241 | 83 | uint8_t startReading(bool includeWait = true); |
jsteve | 0:1449f126b241 | 84 | |
jsteve | 0:1449f126b241 | 85 | /** If search() was not called before this, then search() is invoked. |
jsteve | 0:1449f126b241 | 86 | * @returns The number of sensors found on the bus. |
jsteve | 0:1449f126b241 | 87 | */ |
jsteve | 0:1449f126b241 | 88 | uint8_t count(void); |
jsteve | 0:1449f126b241 | 89 | |
jsteve | 0:1449f126b241 | 90 | protected: |
jsteve | 0:1449f126b241 | 91 | |
jsteve | 0:1449f126b241 | 92 | DigitalInOut _oneWirePort; |
jsteve | 0:1449f126b241 | 93 | uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE]; |
jsteve | 0:1449f126b241 | 94 | uint8_t nSensors; |
jsteve | 0:1449f126b241 | 95 | bool _init; |
jsteve | 0:1449f126b241 | 96 | |
jsteve | 0:1449f126b241 | 97 | }; |
jsteve | 0:1449f126b241 | 98 | |
jsteve | 0:1449f126b241 | 99 | #endif // file |