Simple cpp wrapper of a ds18b20, onewire 'c' library. Supports multiple sensors.
Dependents: LPC11U68_DS18B20Sensor
Fork of DS18B20Sensor by
DS18B20Sensor.h
00001 #ifndef _SENSOR_H 00002 #define _SENSOR_H 00003 00004 #include "mbed.h" 00005 #include "DS18X20.h" 00006 #include "onewire.h" 00007 00008 /** DS18B20Sensor class. 00009 * A wrapper class for the DS18B20 (temperature sensor) and onewire 'c' library. 00010 * Based on the OneWireDrv Library by Frederic Blanc. Many thanks to him for making this code available. 00011 * 00012 * Simple Example: 00013 * @code 00014 #include "mbed.h" 00015 #include "DS18B20Sensor.h" 00016 00017 // Instantiate sensor with hardware in pin p30 00018 DS18B20Sensor sensor(p30); 00019 00020 // Setup a window to the world 00021 Serial pc(p13, p14); 00022 //Serial pc(USBTX, USBRX); 00023 00024 int main() 00025 { 00026 char sensorBuf[25]; 00027 00028 // count will search for sensors, if not already called 00029 pc.printf("Found %d sensor/s\r", sensor.count()); 00030 00031 uint8_t result = sensor.startReading(true); // start sensor readings and wait 00032 00033 for (uint8_t i = 0; i < sensor.count(); i++) { 00034 sensor.getReading(sensorBuf, i); // get result into buf 00035 pc.printf("Sensor %d : %s\r", i+1, sensorBuf); // display it to the world 00036 } 00037 00038 } 00039 * @endcode 00040 */ 00041 class DS18B20Sensor 00042 { 00043 public: 00044 /** Create DS18B20Sensor instance 00045 * @param pin The pin number used by the onewire bus. 00046 */ 00047 DS18B20Sensor(PinName pin); 00048 00049 /** Copies the sensor results into the parameter text. 00050 * @param text The target text buffer. 00051 * @param index The sensor number. 00052 */ 00053 void getReading(char * text, uint8_t index); 00054 00055 /** Copies the sensor results into the parameter text. 00056 * @param text The target text buffer. 00057 * @param subzero 1 for less than zero. 00058 * @param cel Degrees Cel 00059 * @param cel Degrees fraction 00060 */ 00061 void getReading(char * text, uint8_t subzero, uint8_t cel, uint8_t cel_frac_bits); 00062 00063 /** Gets the sensors reading results. 00064 * @param index The sensor number. 00065 * @param subzero Returns 1 if less than zero. 00066 * @param cel Returns degrees Cel 00067 * @param cel Returns degrees fraction 00068 */ 00069 void getReading(uint8_t index, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits); 00070 00071 /** This searches for sensors on the onewire bus. 00072 * It can also invoked by just using count() 00073 * @returns The number of sensors found on the bus. 00074 */ 00075 uint8_t search(void); 00076 00077 /** Starts the sensors reading. 00078 * @param includeWait If true, the method will wait the specified time for the sensors to complete the reading. 00079 * @returns 00080 DS18X20_OK on success, 00081 Other values in DS18X20.h on failure. 00082 */ 00083 uint8_t startReading(bool includeWait = true); 00084 00085 /** If search() was not called before this, then search() is invoked. 00086 * @returns The number of sensors found on the bus. 00087 */ 00088 uint8_t count(void); 00089 00090 protected: 00091 00092 DigitalInOut _oneWirePort; 00093 uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE]; 00094 uint8_t nSensors; 00095 bool _init; 00096 00097 }; 00098 00099 #endif // file
Generated on Wed Jul 13 2022 23:17:04 by 1.7.2