PLANET-Q LPS22HB Library
Dependents: IZU2020_AVIONICS qqq_izu_main_test IZU2020_AVIONICS Hybrid_OB2021_MAIN
PQLPS22HB.cpp@3:b2e1b4e9fdf8, 2019-12-23 (annotated)
- Committer:
- tanahashi
- Date:
- Mon Dec 23 23:59:48 2019 +0000
- Revision:
- 3:b2e1b4e9fdf8
- Parent:
- 1:e77331b61ce6
fix: short to int
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tanahashi | 0:9b51ce752415 | 1 | #include "mbed.h" |
tanahashi | 0:9b51ce752415 | 2 | #include "PQLPS22HB.h" |
tanahashi | 0:9b51ce752415 | 3 | |
tanahashi | 0:9b51ce752415 | 4 | LPS22HB :: LPS22HB(I2C &i2c, SA0_t SA0) |
tanahashi | 0:9b51ce752415 | 5 | { |
tanahashi | 0:9b51ce752415 | 6 | _addr = SA0; |
tanahashi | 0:9b51ce752415 | 7 | _i2c = &i2c; |
tanahashi | 0:9b51ce752415 | 8 | _i2c -> frequency(400000); |
tanahashi | 0:9b51ce752415 | 9 | } |
tanahashi | 0:9b51ce752415 | 10 | |
tanahashi | 0:9b51ce752415 | 11 | void LPS22HB :: begin() |
tanahashi | 0:9b51ce752415 | 12 | { |
tanahashi | 1:e77331b61ce6 | 13 | cmd[0] = LPS22HB_CTRL_REG1; |
tanahashi | 0:9b51ce752415 | 14 | cmd[1] = 0x40; |
tanahashi | 0:9b51ce752415 | 15 | _i2c -> write(_addr, cmd, 2); |
tanahashi | 0:9b51ce752415 | 16 | } |
tanahashi | 0:9b51ce752415 | 17 | |
tanahashi | 0:9b51ce752415 | 18 | bool LPS22HB :: test() |
tanahashi | 0:9b51ce752415 | 19 | { |
tanahashi | 1:e77331b61ce6 | 20 | cmd[0] = LPS22HB_WHO_AM_I; |
tanahashi | 0:9b51ce752415 | 21 | _i2c -> write(_addr, cmd, 1); |
tanahashi | 0:9b51ce752415 | 22 | _i2c -> read(_addr, buff, 1); |
tanahashi | 0:9b51ce752415 | 23 | if(buff[0] == 0xB1) { |
tanahashi | 0:9b51ce752415 | 24 | return true; |
tanahashi | 0:9b51ce752415 | 25 | } else { |
tanahashi | 0:9b51ce752415 | 26 | return false; |
tanahashi | 0:9b51ce752415 | 27 | } |
tanahashi | 0:9b51ce752415 | 28 | } |
tanahashi | 0:9b51ce752415 | 29 | |
tanahashi | 0:9b51ce752415 | 30 | void LPS22HB :: read(float *press, float *temp) |
tanahashi | 0:9b51ce752415 | 31 | { |
tanahashi | 0:9b51ce752415 | 32 | read_press(press); |
tanahashi | 0:9b51ce752415 | 33 | read_temp(temp); |
tanahashi | 0:9b51ce752415 | 34 | } |
tanahashi | 0:9b51ce752415 | 35 | |
tanahashi | 0:9b51ce752415 | 36 | void LPS22HB :: read_press(float *press) |
tanahashi | 0:9b51ce752415 | 37 | { |
tanahashi | 1:e77331b61ce6 | 38 | cmd[0] = LPS22HB_PRESS_XL; |
tanahashi | 0:9b51ce752415 | 39 | _i2c -> write(_addr, cmd, 1); |
tanahashi | 0:9b51ce752415 | 40 | _i2c -> read(_addr, buff, 3); |
tanahashi | 3:b2e1b4e9fdf8 | 41 | *press = (int)(buff[0] | buff[1] << 8 | buff[2] << 16) / 4096.0f; |
tanahashi | 0:9b51ce752415 | 42 | } |
tanahashi | 0:9b51ce752415 | 43 | |
tanahashi | 0:9b51ce752415 | 44 | void LPS22HB :: read_temp(float *temp) |
tanahashi | 0:9b51ce752415 | 45 | { |
tanahashi | 1:e77331b61ce6 | 46 | cmd[0] = LPS22HB_TEMP_L; |
tanahashi | 0:9b51ce752415 | 47 | _i2c -> write(_addr, cmd, 1); |
tanahashi | 0:9b51ce752415 | 48 | _i2c -> read(_addr, buff, 2); |
tanahashi | 0:9b51ce752415 | 49 | *temp = (short)(buff[0] | buff[1] << 8) * 0.01; |
tanahashi | 0:9b51ce752415 | 50 | } |