A class library development test using TMP102 (I2C temperature sensor).
test_TMP102.h@2:494a328aed3e, 2017-11-26 (annotated)
- Committer:
- yasubumi
- Date:
- Sun Nov 26 01:01:05 2017 +0000
- Revision:
- 2:494a328aed3e
- Parent:
- 1:c9c43b132ed7
add comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yasubumi | 2:494a328aed3e | 1 | /** A practice code for class library development |
yasubumi | 2:494a328aed3e | 2 | * |
yasubumi | 2:494a328aed3e | 3 | * @author Yasubumi KANAZAWA |
yasubumi | 2:494a328aed3e | 4 | * @version 1.0 |
yasubumi | 2:494a328aed3e | 5 | * @date 26-Nov-2017 |
yasubumi | 2:494a328aed3e | 6 | * |
yasubumi | 2:494a328aed3e | 7 | * This code is plactice of the class code development. |
yasubumi | 2:494a328aed3e | 8 | * The sample is a I2C temperature sensor TMP102. |
yasubumi | 2:494a328aed3e | 9 | * |
yasubumi | 2:494a328aed3e | 10 | * Reference page |
yasubumi | 2:494a328aed3e | 11 | * https://os.mbed.com/users/okano/notebook/mbed-library-study-meeting-2014-nov-07/" |
yasubumi | 2:494a328aed3e | 12 | * |
yasubumi | 2:494a328aed3e | 13 | * About TMP102: |
yasubumi | 2:494a328aed3e | 14 | * http://www.sparkfun.com/datasheets/Sensors/Temperature/tmp102.pdf |
yasubumi | 2:494a328aed3e | 15 | */ |
yasubumi | 2:494a328aed3e | 16 | |
yasubumi | 0:89475bc39c8b | 17 | #include "mbed.h" |
yasubumi | 0:89475bc39c8b | 18 | |
yasubumi | 2:494a328aed3e | 19 | /** Default slave address(ADD0 connected to GND) */ |
yasubumi | 0:89475bc39c8b | 20 | #define ADDR_TMP102 0x90 |
yasubumi | 0:89475bc39c8b | 21 | |
yasubumi | 2:494a328aed3e | 22 | /** TMP102 register names and addresses */ |
yasubumi | 0:89475bc39c8b | 23 | #define TMP102_Temp 0x00 |
yasubumi | 0:89475bc39c8b | 24 | #define TMP102_Conf 0x01 |
yasubumi | 0:89475bc39c8b | 25 | #define TMP102_Tlow 0x02 |
yasubumi | 0:89475bc39c8b | 26 | #define TMP102_Thigh 0x03 |
yasubumi | 0:89475bc39c8b | 27 | |
yasubumi | 2:494a328aed3e | 28 | /** test_TMP102 Class Library |
yasubumi | 2:494a328aed3e | 29 | * to provide very simple interface for mbed |
yasubumi | 2:494a328aed3e | 30 | * |
yasubumi | 2:494a328aed3e | 31 | * Example: |
yasubumi | 2:494a328aed3e | 32 | * @code |
yasubumi | 2:494a328aed3e | 33 | * #include "mbed.h" |
yasubumi | 2:494a328aed3e | 34 | * #include "test_TMP102.h" |
yasubumi | 2:494a328aed3e | 35 | * |
yasubumi | 2:494a328aed3e | 36 | * // make test_TMP102 instance using GPIO pin name. |
yasubumi | 2:494a328aed3e | 37 | * test_TMP102 temp0(dp4,dp5); |
yasubumi | 2:494a328aed3e | 38 | * |
yasubumi | 2:494a328aed3e | 39 | * // make test_TMP102 instance using I2C object. |
yasubumi | 2:494a328aed3e | 40 | * I2C i2c(dp4,dp5); |
yasubumi | 2:494a328aed3e | 41 | * test_TMP102 temp1(i2c); |
yasubumi | 2:494a328aed3e | 42 | * |
yasubumi | 2:494a328aed3e | 43 | * int main() |
yasubumi | 2:494a328aed3e | 44 | * { |
yasubumi | 2:494a328aed3e | 45 | * float t0,t1; |
yasubumi | 2:494a328aed3e | 46 | * |
yasubumi | 2:494a328aed3e | 47 | * i2c.frequency(400*1000); |
yasubumi | 2:494a328aed3e | 48 | * |
yasubumi | 2:494a328aed3e | 49 | * while(1) { |
yasubumi | 2:494a328aed3e | 50 | * t0=temp0; |
yasubumi | 2:494a328aed3e | 51 | * t1=temp1; |
yasubumi | 2:494a328aed3e | 52 | * printf("Temp: %7.3f, %7.3f\r\n", t0, t1); |
yasubumi | 2:494a328aed3e | 53 | * wait(1.0); |
yasubumi | 2:494a328aed3e | 54 | * } |
yasubumi | 2:494a328aed3e | 55 | * } |
yasubumi | 2:494a328aed3e | 56 | * @endcode |
yasubumi | 2:494a328aed3e | 57 | */ |
yasubumi | 0:89475bc39c8b | 58 | class test_TMP102 |
yasubumi | 0:89475bc39c8b | 59 | { |
yasubumi | 0:89475bc39c8b | 60 | public: |
yasubumi | 2:494a328aed3e | 61 | /** Create a test_TMP102 instance connected to specified I2C pins with specified address |
yasubumi | 2:494a328aed3e | 62 | * |
yasubumi | 2:494a328aed3e | 63 | * @param sda I2C-bus SDA pin |
yasubumi | 2:494a328aed3e | 64 | * @param scl I2C-bus SCL pin |
yasubumi | 2:494a328aed3e | 65 | * @param address (option) I2C-bus slave address (default: 0x90) |
yasubumi | 2:494a328aed3e | 66 | */ |
yasubumi | 1:c9c43b132ed7 | 67 | test_TMP102(PinName sda, PinName scl, char address = ADDR_TMP102); |
yasubumi | 2:494a328aed3e | 68 | |
yasubumi | 2:494a328aed3e | 69 | /** Create a test_TMP102 instance connected to specified I2C pins with specified address |
yasubumi | 2:494a328aed3e | 70 | * |
yasubumi | 2:494a328aed3e | 71 | * @param i2c_obj I2C object (instance) |
yasubumi | 2:494a328aed3e | 72 | * @param address (option) I2C-bus slave address (default: 0x90) |
yasubumi | 2:494a328aed3e | 73 | */ |
yasubumi | 1:c9c43b132ed7 | 74 | test_TMP102(I2C &i2c_obj, char address = ADDR_TMP102); |
yasubumi | 2:494a328aed3e | 75 | |
yasubumi | 2:494a328aed3e | 76 | /** Destractor */ |
yasubumi | 0:89475bc39c8b | 77 | ~test_TMP102(); |
yasubumi | 2:494a328aed3e | 78 | |
yasubumi | 2:494a328aed3e | 79 | /** Initialization */ |
yasubumi | 0:89475bc39c8b | 80 | void init(void); |
yasubumi | 2:494a328aed3e | 81 | |
yasubumi | 2:494a328aed3e | 82 | /** Read temperature |
yasubumi | 2:494a328aed3e | 83 | * |
yasubumi | 2:494a328aed3e | 84 | * @return value of degree Celsius (in float) |
yasubumi | 2:494a328aed3e | 85 | */ |
yasubumi | 0:89475bc39c8b | 86 | float read(void); |
yasubumi | 2:494a328aed3e | 87 | |
yasubumi | 2:494a328aed3e | 88 | /** Read temperature |
yasubumi | 2:494a328aed3e | 89 | * |
yasubumi | 2:494a328aed3e | 90 | * @return the object returns the read value |
yasubumi | 2:494a328aed3e | 91 | */ |
yasubumi | 1:c9c43b132ed7 | 92 | operator float(void); |
yasubumi | 0:89475bc39c8b | 93 | private: |
yasubumi | 1:c9c43b132ed7 | 94 | I2C *i2c_p; |
yasubumi | 1:c9c43b132ed7 | 95 | I2C &i2c; |
yasubumi | 1:c9c43b132ed7 | 96 | char adr; |
yasubumi | 0:89475bc39c8b | 97 | }; |