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

Dependencies:   mbed

Dependents:   DS18B20Sensor DS18B201

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?

UserRevisionLine numberNew contents of line
jsteve 1:ea35ad346f25 1 /**
jsteve 1:ea35ad346f25 2 * @file DS18B20Sensor.cpp
jsteve 1:ea35ad346f25 3 * @brief Wrapper for the OneWireDrv library by Frederic Blanc.
jsteve 1:ea35ad346f25 4 * @author Steve Spence (Published 3 March 2013 www.mbed.org)
jsteve 1:ea35ad346f25 5 */
jsteve 0:1449f126b241 6 #include "DS18B20Sensor.h"
jsteve 0:1449f126b241 7
jsteve 0:1449f126b241 8 DS18B20Sensor::DS18B20Sensor(PinName pin) : _oneWirePort(pin)
jsteve 0:1449f126b241 9 {
jsteve 0:1449f126b241 10 _init = false;
jsteve 0:1449f126b241 11 nSensors = 0;
jsteve 0:1449f126b241 12 }
jsteve 0:1449f126b241 13
jsteve 1:ea35ad346f25 14 void DS18B20Sensor::getReading(char * text, uint8_t index)
jsteve 0:1449f126b241 15 {
jsteve 0:1449f126b241 16 uint8_t subzero, cel, cel_frac_bits;
jsteve 0:1449f126b241 17 DS18X20_read_meas( &gSensorIDs[index][0], &subzero, &cel, &cel_frac_bits);
jsteve 1:ea35ad346f25 18 DS18B20Sensor::getReading(text, subzero, cel, cel_frac_bits);
jsteve 0:1449f126b241 19 }
jsteve 0:1449f126b241 20
jsteve 1:ea35ad346f25 21 void DS18B20Sensor::getReading(char * text, uint8_t subzero, uint8_t cel, uint8_t cel_frac_bits)
jsteve 0:1449f126b241 22 {
jsteve 0:1449f126b241 23 uint16_t decicelsius;
jsteve 0:1449f126b241 24 char s[10];
jsteve 0:1449f126b241 25 float temperature;
jsteve 0:1449f126b241 26 sprintf(text,"");
jsteve 0:1449f126b241 27 sprintf(s,"%s", (subzero)?"-":"+");
jsteve 0:1449f126b241 28 strcat(text,s);
jsteve 0:1449f126b241 29 decicelsius = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);
jsteve 0:1449f126b241 30 temperature = decicelsius;
jsteve 0:1449f126b241 31 temperature = temperature/10;
jsteve 0:1449f126b241 32 sprintf(s,"%4.1f", temperature);
jsteve 0:1449f126b241 33 strcat(text,s);
jsteve 0:1449f126b241 34
jsteve 0:1449f126b241 35 }
jsteve 0:1449f126b241 36
jsteve 0:1449f126b241 37 uint8_t DS18B20Sensor::search(void)
jsteve 0:1449f126b241 38 {
jsteve 0:1449f126b241 39 search_sensors(&nSensors, &gSensorIDs[0][0]);
jsteve 0:1449f126b241 40 _init = true;
jsteve 0:1449f126b241 41 return nSensors;
jsteve 0:1449f126b241 42 }
jsteve 0:1449f126b241 43
jsteve 0:1449f126b241 44 uint8_t DS18B20Sensor::count(void)
jsteve 0:1449f126b241 45 {
jsteve 0:1449f126b241 46 if (_init==false)
jsteve 0:1449f126b241 47 DS18B20Sensor::search();
jsteve 0:1449f126b241 48 return nSensors;
jsteve 0:1449f126b241 49 }
jsteve 0:1449f126b241 50
jsteve 0:1449f126b241 51 uint8_t DS18B20Sensor::startReading(bool includeWait)
jsteve 0:1449f126b241 52 {
jsteve 0:1449f126b241 53 uint8_t r = DS18X20_start_meas(DS18X20_POWER_EXTERN, 0 );
jsteve 0:1449f126b241 54 if ((r == DS18X20_OK) && includeWait)
jsteve 0:1449f126b241 55 wait_ms(DS18B20_TCONV_12BIT);
jsteve 0:1449f126b241 56 return r;
jsteve 0:1449f126b241 57 }
jsteve 0:1449f126b241 58
jsteve 0:1449f126b241 59 void DS18B20Sensor::getReading(uint8_t index, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits)
jsteve 0:1449f126b241 60 {
jsteve 0:1449f126b241 61 DS18X20_read_meas( &gSensorIDs[index][0], subzero, cel, cel_frac_bits);
jsteve 0:1449f126b241 62 }