MAX30205 Human Body Temperature Sensor
MAX30205.cpp@0:6cf99fa71e1b, 2017-04-25 (annotated)
- Committer:
- Rhyme
- Date:
- Tue Apr 25 05:04:06 2017 +0000
- Revision:
- 0:6cf99fa71e1b
First working version, minimum test is done, as usual
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:6cf99fa71e1b | 1 | #include "mbed.h" |
Rhyme | 0:6cf99fa71e1b | 2 | #include "MAX30205.h" |
Rhyme | 0:6cf99fa71e1b | 3 | |
Rhyme | 0:6cf99fa71e1b | 4 | /** |
Rhyme | 0:6cf99fa71e1b | 5 | * Internal Registers |
Rhyme | 0:6cf99fa71e1b | 6 | * The device contains four registers, |
Rhyme | 0:6cf99fa71e1b | 7 | * each of which consists of 2bytes. |
Rhyme | 0:6cf99fa71e1b | 8 | * The configuration register contains only 1 byte of |
Rhyme | 0:6cf99fa71e1b | 9 | * actual data and, when read as a 2-byte register, |
Rhyme | 0:6cf99fa71e1b | 10 | * repeats the same data for the second byte. |
Rhyme | 0:6cf99fa71e1b | 11 | */ |
Rhyme | 0:6cf99fa71e1b | 12 | |
Rhyme | 0:6cf99fa71e1b | 13 | #define REG_TEMP 0x00 |
Rhyme | 0:6cf99fa71e1b | 14 | #define REG_CONF 0x01 |
Rhyme | 0:6cf99fa71e1b | 15 | #define REG_THYST 0x02 |
Rhyme | 0:6cf99fa71e1b | 16 | #define REG_TOS 0x03 |
Rhyme | 0:6cf99fa71e1b | 17 | |
Rhyme | 0:6cf99fa71e1b | 18 | MAX30205::MAX30205(PinName sda, PinName scl, int addr): m_i2c(sda, scl), m_addr(addr<<1) |
Rhyme | 0:6cf99fa71e1b | 19 | { |
Rhyme | 0:6cf99fa71e1b | 20 | // activate the peripheral |
Rhyme | 0:6cf99fa71e1b | 21 | } |
Rhyme | 0:6cf99fa71e1b | 22 | |
Rhyme | 0:6cf99fa71e1b | 23 | MAX30205::~MAX30205() |
Rhyme | 0:6cf99fa71e1b | 24 | { |
Rhyme | 0:6cf99fa71e1b | 25 | } |
Rhyme | 0:6cf99fa71e1b | 26 | |
Rhyme | 0:6cf99fa71e1b | 27 | uint16_t MAX30205::getTemp(void) |
Rhyme | 0:6cf99fa71e1b | 28 | { |
Rhyme | 0:6cf99fa71e1b | 29 | uint16_t temp ; |
Rhyme | 0:6cf99fa71e1b | 30 | uint8_t data[2] ; |
Rhyme | 0:6cf99fa71e1b | 31 | readRegs(REG_TEMP, data, 2) ; |
Rhyme | 0:6cf99fa71e1b | 32 | temp = (data[0] << 8) | data[1] ; |
Rhyme | 0:6cf99fa71e1b | 33 | return(temp) ; |
Rhyme | 0:6cf99fa71e1b | 34 | } |
Rhyme | 0:6cf99fa71e1b | 35 | |
Rhyme | 0:6cf99fa71e1b | 36 | uint8_t MAX30205::getConf(void) |
Rhyme | 0:6cf99fa71e1b | 37 | { |
Rhyme | 0:6cf99fa71e1b | 38 | uint8_t data[2] ; |
Rhyme | 0:6cf99fa71e1b | 39 | readRegs(REG_CONF, data, 2) ; |
Rhyme | 0:6cf99fa71e1b | 40 | return(data[0]) ; |
Rhyme | 0:6cf99fa71e1b | 41 | } |
Rhyme | 0:6cf99fa71e1b | 42 | |
Rhyme | 0:6cf99fa71e1b | 43 | void MAX30205::setConf(uint8_t conf) |
Rhyme | 0:6cf99fa71e1b | 44 | { |
Rhyme | 0:6cf99fa71e1b | 45 | uint8_t data[3] ; |
Rhyme | 0:6cf99fa71e1b | 46 | data[0] = REG_CONF ; |
Rhyme | 0:6cf99fa71e1b | 47 | data[1] = conf ; |
Rhyme | 0:6cf99fa71e1b | 48 | data[2] = conf ; |
Rhyme | 0:6cf99fa71e1b | 49 | writeRegs(data, 3) ; |
Rhyme | 0:6cf99fa71e1b | 50 | } |
Rhyme | 0:6cf99fa71e1b | 51 | |
Rhyme | 0:6cf99fa71e1b | 52 | uint16_t MAX30205::getThyst(void) |
Rhyme | 0:6cf99fa71e1b | 53 | { |
Rhyme | 0:6cf99fa71e1b | 54 | uint16_t thyst ; |
Rhyme | 0:6cf99fa71e1b | 55 | uint8_t data[2] ; |
Rhyme | 0:6cf99fa71e1b | 56 | readRegs(REG_THYST, data, 2) ; |
Rhyme | 0:6cf99fa71e1b | 57 | thyst = (data[0] << 8) | data[1] ; |
Rhyme | 0:6cf99fa71e1b | 58 | return(thyst) ; |
Rhyme | 0:6cf99fa71e1b | 59 | } |
Rhyme | 0:6cf99fa71e1b | 60 | |
Rhyme | 0:6cf99fa71e1b | 61 | void MAX30205::setThyst(uint16_t thyst) |
Rhyme | 0:6cf99fa71e1b | 62 | { |
Rhyme | 0:6cf99fa71e1b | 63 | uint8_t data[3] ; |
Rhyme | 0:6cf99fa71e1b | 64 | data[0] = REG_THYST ; |
Rhyme | 0:6cf99fa71e1b | 65 | data[1] = (thyst >> 8) & 0xFF ; |
Rhyme | 0:6cf99fa71e1b | 66 | data[2] = thyst & 0xFF ; |
Rhyme | 0:6cf99fa71e1b | 67 | writeRegs(data, 3) ; |
Rhyme | 0:6cf99fa71e1b | 68 | } |
Rhyme | 0:6cf99fa71e1b | 69 | |
Rhyme | 0:6cf99fa71e1b | 70 | uint16_t MAX30205::getTos(void) |
Rhyme | 0:6cf99fa71e1b | 71 | { |
Rhyme | 0:6cf99fa71e1b | 72 | uint16_t tos ; |
Rhyme | 0:6cf99fa71e1b | 73 | uint8_t data[2] ; |
Rhyme | 0:6cf99fa71e1b | 74 | readRegs(REG_TOS, data, 2) ; |
Rhyme | 0:6cf99fa71e1b | 75 | tos = (data[0] << 8) | data[1] ; |
Rhyme | 0:6cf99fa71e1b | 76 | return(tos) ; |
Rhyme | 0:6cf99fa71e1b | 77 | } |
Rhyme | 0:6cf99fa71e1b | 78 | |
Rhyme | 0:6cf99fa71e1b | 79 | void MAX30205::setTos(uint16_t tos) |
Rhyme | 0:6cf99fa71e1b | 80 | { |
Rhyme | 0:6cf99fa71e1b | 81 | uint8_t data[3] ; |
Rhyme | 0:6cf99fa71e1b | 82 | data[0] = REG_TOS ; |
Rhyme | 0:6cf99fa71e1b | 83 | data[1] = (tos >> 8) & 0xFF ; |
Rhyme | 0:6cf99fa71e1b | 84 | data[2] = tos & 0xFF ; |
Rhyme | 0:6cf99fa71e1b | 85 | writeRegs(data, 3) ; |
Rhyme | 0:6cf99fa71e1b | 86 | } |
Rhyme | 0:6cf99fa71e1b | 87 | |
Rhyme | 0:6cf99fa71e1b | 88 | void MAX30205::readRegs(int addr, uint8_t * data, int len) { |
Rhyme | 0:6cf99fa71e1b | 89 | char t[1] = {addr}; |
Rhyme | 0:6cf99fa71e1b | 90 | m_i2c.write(m_addr, t, 1, true); |
Rhyme | 0:6cf99fa71e1b | 91 | m_i2c.read(m_addr, (char *)data, len); |
Rhyme | 0:6cf99fa71e1b | 92 | } |
Rhyme | 0:6cf99fa71e1b | 93 | |
Rhyme | 0:6cf99fa71e1b | 94 | void MAX30205::writeRegs(uint8_t * data, int len) { |
Rhyme | 0:6cf99fa71e1b | 95 | m_i2c.write(m_addr, (char *)data, len); |
Rhyme | 0:6cf99fa71e1b | 96 | } |