Louis Marr / TMP102
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TMP102.h Source File

TMP102.h

00001 // it is good practice to put a comment block on the top, with the name and details of the library
00002 // you can also include version numbers to keep track of changes/additions
00003 
00004 /* TMP102
00005 
00006 Simple library to get the temperature data from a TMP102 sensor over I2C.
00007 
00008 v 1.0 - initial release
00009 
00010 (c) Craig A. Evans, University of Leeds, Jan 2016
00011 
00012 */
00013 
00014 // this is called a header guard and prevents a library from being included more than once
00015 // if it is not already defined, it is defined. If it it already defined, it won't be defined again
00016 // the header guard is closed at the end of the file
00017 #ifndef TMP102_H
00018 #define TMP102_H
00019 
00020 // the next thing in a library is usually any required defines
00021 // addresses for ADD0 connected to GND
00022 #define TMP102_ADD      0x48
00023 #define TMP102_R_ADD    0x91
00024 #define TMP102_W_ADD    0x90
00025 
00026 // register addresses
00027 #define TEMP_REG    0x00
00028 #define CONFIG_REG  0x01
00029 #define THIGH_REG   0x02
00030 #define TLOW_REG    0x03
00031 
00032 // we need to include the mbed header (this will also have a header guard to prevent it being included more than once)
00033 #include "mbed.h"
00034 
00035 // a library is actually just a C++ class, we will create an instance of this class (an object) in our main code
00036 class TMP102
00037 {
00038     // we define the methods that are 'public' i.e. able to be used by the user
00039 public:
00040     
00041     // this is a 'constructor' and is used to create the object
00042     TMP102(PinName sda, PinName scl);
00043     
00044     // we will also create an initialisation method
00045     void init();
00046     
00047     // and a method to get the temperature - note the return type
00048     float get_temperature();
00049     
00050     // we also have 'private' methods that can be used in the library itself, but not called directly by the user
00051 private:
00052 
00053     // called in event of error - flashes LED and hangs 
00054     void error();
00055     // reads temperature from the sensor
00056     void read_temperature();
00057     
00058 
00059     // we also do the same for any variables
00060 public:
00061     // don't generally allow direct access to variables, instead use 'accessor' and 'mutator' methods
00062     
00063 private:
00064 
00065     // class data member names often have a trailing underscore to make them easily identifiable
00066     I2C* i2c_;
00067     DigitalOut* led_;
00068     float temperature_;
00069 
00070 };
00071 
00072 // end of the header guard
00073 #endif