BM1383AGLV sensor library
Dependents: simple-sensor-client rohm-SensorShield-example
BM1383AGLV.cpp@0:d505f3c67a62, 2018-02-03 (annotated)
- Committer:
- MACRUM
- Date:
- Sat Feb 03 14:26:39 2018 +0000
- Revision:
- 0:d505f3c67a62
- Child:
- 1:997a1eb76a4f
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:d505f3c67a62 | 1 | /* Copyright (c) 2015 ARM Ltd., MIT License |
MACRUM | 0:d505f3c67a62 | 2 | * |
MACRUM | 0:d505f3c67a62 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
MACRUM | 0:d505f3c67a62 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
MACRUM | 0:d505f3c67a62 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
MACRUM | 0:d505f3c67a62 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
MACRUM | 0:d505f3c67a62 | 7 | * Software is furnished to do so, subject to the following conditions: |
MACRUM | 0:d505f3c67a62 | 8 | * |
MACRUM | 0:d505f3c67a62 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
MACRUM | 0:d505f3c67a62 | 10 | * substantial portions of the Software. |
MACRUM | 0:d505f3c67a62 | 11 | * |
MACRUM | 0:d505f3c67a62 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
MACRUM | 0:d505f3c67a62 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
MACRUM | 0:d505f3c67a62 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
MACRUM | 0:d505f3c67a62 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
MACRUM | 0:d505f3c67a62 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
MACRUM | 0:d505f3c67a62 | 17 | * |
MACRUM | 0:d505f3c67a62 | 18 | * |
MACRUM | 0:d505f3c67a62 | 19 | * BM1383AGLV Pressure sensor library |
MACRUM | 0:d505f3c67a62 | 20 | * |
MACRUM | 0:d505f3c67a62 | 21 | * @author Toyomasa Watarai |
MACRUM | 0:d505f3c67a62 | 22 | * @version 1.0 |
MACRUM | 0:d505f3c67a62 | 23 | * @date 30-December-2015 |
MACRUM | 0:d505f3c67a62 | 24 | * |
MACRUM | 0:d505f3c67a62 | 25 | * Library for "BM1383AGLV Pressure sensor library" |
MACRUM | 0:d505f3c67a62 | 26 | * http://www.rohm.com/web/eu/news-detail?news-title=2014-12-11_ad_pressure&defaultGroupId=false |
MACRUM | 0:d505f3c67a62 | 27 | * |
MACRUM | 0:d505f3c67a62 | 28 | */ |
MACRUM | 0:d505f3c67a62 | 29 | |
MACRUM | 0:d505f3c67a62 | 30 | #include "mbed.h" |
MACRUM | 0:d505f3c67a62 | 31 | #include "BM1383AGLV.h" |
MACRUM | 0:d505f3c67a62 | 32 | |
MACRUM | 0:d505f3c67a62 | 33 | BM1383AGLV::BM1383AGLV(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) |
MACRUM | 0:d505f3c67a62 | 34 | { |
MACRUM | 0:d505f3c67a62 | 35 | initialize(); |
MACRUM | 0:d505f3c67a62 | 36 | } |
MACRUM | 0:d505f3c67a62 | 37 | |
MACRUM | 0:d505f3c67a62 | 38 | BM1383AGLV::BM1383AGLV(I2C &i2c_obj, int addr) : m_i2c(i2c_obj), m_addr(addr) |
MACRUM | 0:d505f3c67a62 | 39 | { |
MACRUM | 0:d505f3c67a62 | 40 | initialize(); |
MACRUM | 0:d505f3c67a62 | 41 | } |
MACRUM | 0:d505f3c67a62 | 42 | |
MACRUM | 0:d505f3c67a62 | 43 | BM1383AGLV::~BM1383AGLV() |
MACRUM | 0:d505f3c67a62 | 44 | { |
MACRUM | 0:d505f3c67a62 | 45 | } |
MACRUM | 0:d505f3c67a62 | 46 | |
MACRUM | 0:d505f3c67a62 | 47 | void BM1383AGLV::initialize() |
MACRUM | 0:d505f3c67a62 | 48 | { |
MACRUM | 0:d505f3c67a62 | 49 | unsigned char buf1, buf2; |
MACRUM | 0:d505f3c67a62 | 50 | unsigned char reg[2]; |
MACRUM | 0:d505f3c67a62 | 51 | |
MACRUM | 0:d505f3c67a62 | 52 | readRegs(BM1383AGLV_ID1, &buf1, sizeof(buf1)); |
MACRUM | 0:d505f3c67a62 | 53 | readRegs(BM1383AGLV_ID2, &buf2, sizeof(buf2)); |
MACRUM | 0:d505f3c67a62 | 54 | if ((buf1 != BM1383AGLV_ID1_VAL) || (buf2 != BM1383AGLV_ID2_VAL)) { |
MACRUM | 0:d505f3c67a62 | 55 | DEBUG_PRINT("BM1383AGLV initialization error. (%d, %d)\n", buf1, buf2); |
MACRUM | 0:d505f3c67a62 | 56 | return; |
MACRUM | 0:d505f3c67a62 | 57 | } |
MACRUM | 0:d505f3c67a62 | 58 | |
MACRUM | 0:d505f3c67a62 | 59 | reg[0] = BM1383AGLV_POWER_DOWN; |
MACRUM | 0:d505f3c67a62 | 60 | reg[1] = 0x01; |
MACRUM | 0:d505f3c67a62 | 61 | writeRegs(reg, 2); |
MACRUM | 0:d505f3c67a62 | 62 | |
MACRUM | 0:d505f3c67a62 | 63 | wait(1); |
MACRUM | 0:d505f3c67a62 | 64 | |
MACRUM | 0:d505f3c67a62 | 65 | reg[0] = BM1383AGLV_RESET; |
MACRUM | 0:d505f3c67a62 | 66 | reg[1] = 0x01; |
MACRUM | 0:d505f3c67a62 | 67 | writeRegs(reg, 2); |
MACRUM | 0:d505f3c67a62 | 68 | |
MACRUM | 0:d505f3c67a62 | 69 | reg[0] = BM1383AGLV_MODE_CONTROL; |
MACRUM | 0:d505f3c67a62 | 70 | reg[1] = 0xC2; // x32, continuous measurement |
MACRUM | 0:d505f3c67a62 | 71 | writeRegs(reg, 2); |
MACRUM | 0:d505f3c67a62 | 72 | } |
MACRUM | 0:d505f3c67a62 | 73 | |
MACRUM | 0:d505f3c67a62 | 74 | float BM1383AGLV::getPressure() |
MACRUM | 0:d505f3c67a62 | 75 | { |
MACRUM | 0:d505f3c67a62 | 76 | uint32_t rawPressure; |
MACRUM | 0:d505f3c67a62 | 77 | |
MACRUM | 0:d505f3c67a62 | 78 | readRegs(BM1383AGLV_PRESSURE_MSB, m_buf, 3); |
MACRUM | 0:d505f3c67a62 | 79 | rawPressure = (((uint32_t)m_buf[0] << 16) | ((uint32_t)m_buf[1] << 8) | m_buf[2]&0xFC) >> 2; |
MACRUM | 0:d505f3c67a62 | 80 | return (float)rawPressure / (2048); |
MACRUM | 0:d505f3c67a62 | 81 | } |
MACRUM | 0:d505f3c67a62 | 82 | |
MACRUM | 0:d505f3c67a62 | 83 | float BM1383AGLV::getTemperature() |
MACRUM | 0:d505f3c67a62 | 84 | { |
MACRUM | 0:d505f3c67a62 | 85 | int32_t rawTemerature; |
MACRUM | 0:d505f3c67a62 | 86 | |
MACRUM | 0:d505f3c67a62 | 87 | readRegs(BM1383AGLV_TEMPERATURE_MSB, m_buf, 2); |
MACRUM | 0:d505f3c67a62 | 88 | rawTemerature = ((int32_t)m_buf[0] << 8) | (m_buf[1]); |
MACRUM | 0:d505f3c67a62 | 89 | return (float)rawTemerature / 32; |
MACRUM | 0:d505f3c67a62 | 90 | } |
MACRUM | 0:d505f3c67a62 | 91 | |
MACRUM | 0:d505f3c67a62 | 92 | void BM1383AGLV::readRegs(int addr, uint8_t * data, int len) |
MACRUM | 0:d505f3c67a62 | 93 | { |
MACRUM | 0:d505f3c67a62 | 94 | char t[1] = {addr}; |
MACRUM | 0:d505f3c67a62 | 95 | m_i2c.write(m_addr, t, 1, true); |
MACRUM | 0:d505f3c67a62 | 96 | m_i2c.read(m_addr, (char *)data, len); |
MACRUM | 0:d505f3c67a62 | 97 | } |
MACRUM | 0:d505f3c67a62 | 98 | |
MACRUM | 0:d505f3c67a62 | 99 | void BM1383AGLV::writeRegs(uint8_t * data, int len) |
MACRUM | 0:d505f3c67a62 | 100 | { |
MACRUM | 0:d505f3c67a62 | 101 | m_i2c.write(m_addr, (char *)data, len); |
MACRUM | 0:d505f3c67a62 | 102 | } |