Slightly revised version of the TMP102 library created by Craig A. Evans
Fork of TMP102 by
TMP102.h@0:8818842a3573, 2016-02-05 (annotated)
- Committer:
- eencae
- Date:
- Fri Feb 05 17:24:06 2016 +0000
- Revision:
- 0:8818842a3573
- Child:
- 1:1b601445b336
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:8818842a3573 | 1 | // it is good practice to put a comment block on the top, with the name and details of the library |
eencae | 0:8818842a3573 | 2 | // you can also include version numbers to keep track of changes/additions |
eencae | 0:8818842a3573 | 3 | |
eencae | 0:8818842a3573 | 4 | /* TMP102 |
eencae | 0:8818842a3573 | 5 | |
eencae | 0:8818842a3573 | 6 | Simple library to get the temperature data from a TMP102 sensor over I2C. |
eencae | 0:8818842a3573 | 7 | |
eencae | 0:8818842a3573 | 8 | v 1.0 - initial release |
eencae | 0:8818842a3573 | 9 | |
eencae | 0:8818842a3573 | 10 | (c) Craig A. Evans, University of Leeds, Jan 2016 |
eencae | 0:8818842a3573 | 11 | |
eencae | 0:8818842a3573 | 12 | */ |
eencae | 0:8818842a3573 | 13 | |
eencae | 0:8818842a3573 | 14 | // this is called a header guard and prevents a library from being included more than once |
eencae | 0:8818842a3573 | 15 | // if it is not already defined, it is defined. If it it already defined, it won't be defined again |
eencae | 0:8818842a3573 | 16 | // the header guard is closed at the end of the file |
eencae | 0:8818842a3573 | 17 | #ifndef TMP102_H |
eencae | 0:8818842a3573 | 18 | #define TMP102_H |
eencae | 0:8818842a3573 | 19 | |
eencae | 0:8818842a3573 | 20 | // the next thing in a library is usually any required defines |
eencae | 0:8818842a3573 | 21 | // addresses for ADD0 connected to GND |
eencae | 0:8818842a3573 | 22 | #define TMP102_ADD 0x48 |
eencae | 0:8818842a3573 | 23 | #define TMP102_R_ADD 0x91 |
eencae | 0:8818842a3573 | 24 | #define TMP102_W_ADD 0x90 |
eencae | 0:8818842a3573 | 25 | |
eencae | 0:8818842a3573 | 26 | // register addresses |
eencae | 0:8818842a3573 | 27 | #define TEMP_REG 0x00 |
eencae | 0:8818842a3573 | 28 | #define CONFIG_REG 0x01 |
eencae | 0:8818842a3573 | 29 | #define THIGH_REG 0x02 |
eencae | 0:8818842a3573 | 30 | #define TLOW_REG 0x03 |
eencae | 0:8818842a3573 | 31 | |
eencae | 0:8818842a3573 | 32 | // we need to include the mbed header (this will also have a header guard to prevent it being included more than once) |
eencae | 0:8818842a3573 | 33 | #include "mbed.h" |
eencae | 0:8818842a3573 | 34 | |
eencae | 0:8818842a3573 | 35 | // a library is actually just a C++ class, we will create an instance of this class (an object) in our main code |
eencae | 0:8818842a3573 | 36 | class TMP102 |
eencae | 0:8818842a3573 | 37 | { |
eencae | 0:8818842a3573 | 38 | // we define the methods that are 'public' i.e. able to be used by the user |
eencae | 0:8818842a3573 | 39 | public: |
eencae | 0:8818842a3573 | 40 | |
eencae | 0:8818842a3573 | 41 | // this is a 'constructor' and is used to create the object |
eencae | 0:8818842a3573 | 42 | TMP102(PinName sda, PinName scl); |
eencae | 0:8818842a3573 | 43 | |
eencae | 0:8818842a3573 | 44 | // we will also create an initialisation method |
eencae | 0:8818842a3573 | 45 | void init(); |
eencae | 0:8818842a3573 | 46 | |
eencae | 0:8818842a3573 | 47 | // and a method to get the temperature - note the return type |
eencae | 0:8818842a3573 | 48 | float get_temperature(); |
eencae | 0:8818842a3573 | 49 | |
eencae | 0:8818842a3573 | 50 | // we also have 'private' methods that can be used in the library itself, but not called directly by the user |
eencae | 0:8818842a3573 | 51 | private: |
eencae | 0:8818842a3573 | 52 | |
eencae | 0:8818842a3573 | 53 | // called in event of error - flashes LED and hangs |
eencae | 0:8818842a3573 | 54 | void error(); |
eencae | 0:8818842a3573 | 55 | // reads temperature from the sensor |
eencae | 0:8818842a3573 | 56 | void read_temperature(); |
eencae | 0:8818842a3573 | 57 | |
eencae | 0:8818842a3573 | 58 | |
eencae | 0:8818842a3573 | 59 | // we also do the same for any variables |
eencae | 0:8818842a3573 | 60 | public: |
eencae | 0:8818842a3573 | 61 | // don't generally allow direct access to variables, instead use 'accessor' and 'mutator' methods |
eencae | 0:8818842a3573 | 62 | |
eencae | 0:8818842a3573 | 63 | private: |
eencae | 0:8818842a3573 | 64 | |
eencae | 0:8818842a3573 | 65 | // class data member names often have a trailing underscore to make them easily identifiable |
eencae | 0:8818842a3573 | 66 | I2C* i2c_; |
eencae | 0:8818842a3573 | 67 | DigitalOut* led_; |
eencae | 0:8818842a3573 | 68 | float temperature_; |
eencae | 0:8818842a3573 | 69 | |
eencae | 0:8818842a3573 | 70 | }; |
eencae | 0:8818842a3573 | 71 | |
eencae | 0:8818842a3573 | 72 | // end of the header guard |
eencae | 0:8818842a3573 | 73 | #endif |