syscom project
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleControls by
Revision 4:608e9604ca26, committed 2017-01-08
- Comitter:
- mehdiham
- Date:
- Sun Jan 08 22:06:54 2017 +0000
- Parent:
- 3:823f105078c7
- Commit message:
- 1
Changed in this revision
diff -r 823f105078c7 -r 608e9604ca26 BMP085.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BMP085.cpp Sun Jan 08 22:06:54 2017 +0000 @@ -0,0 +1,152 @@ +/* + * mbed library to use a Bosch Sensortec BMP085/BMP180 sensor + * Copyright (c) 2010 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ + +/** @file BMP085.cpp + * @brief mbed library to use a Bosch Sensortec BMP085/BMP180 sensor + * barometric pressure sensor BMP085/BMP180 (Bosch Sensortec) + * interface: I2C digital + */ + +#include "mbed.h" +#include "BMP085.h" + +#define WEATHER_BMP085 0xee +#define xpow(x, y) ((long)1 << y) + +/** + * @brief Initializes interface (private I2C) + * @param p_sda port of I2C SDA + * @param p_scl port of I2C SCL + * @param p_oss parameter of OSS + */ +BMP085::BMP085 (PinName p_sda, PinName p_scl, BMP085_oss p_oss) : i2c(p_sda, p_scl) { + init(p_oss); +} + +/** + * @brief Initializes interface (public I2C) + * @param p_i2c instance of I2C class + * @param p_oss parameter of OSS + */ +BMP085::BMP085 (I2C& p_i2c, BMP085_oss p_oss) : i2c(p_i2c) { + init(p_oss); +} + +/** + * @brief Get temperature + * @return temperature (`C) + */ +float BMP085::get_temperature() { + return temperature; +} + +/** + * @brief Get pressure + * @return pressure (hPa) + */ +float BMP085::get_pressure() { + return pressure; +} + +/** + * @brief Update results + */ +void BMP085::update () { + long t, p, ut, up, x1, x2, x3, b3, b5, b6; + unsigned long b4, b7; + + twi_writechar(WEATHER_BMP085, 0xf4, 0x2e); + wait(0.01); + ut = twi_readshort(WEATHER_BMP085, 0xf6); + + twi_writechar(WEATHER_BMP085, 0xf4, 0x34 | (oss << 6)); + wait(0.05); + up = twi_readlong(WEATHER_BMP085, 0xf6) >> (8 - oss); + + x1 = (ut - ac6) * ac5 / xpow(2, 15); + x2 = (long)mc * xpow(2, 11) / (x1 + md); + b5 = x1 + x2; + t = (b5 + 8) / xpow(2, 4); + temperature = (float)t / 10.0; + + b6 = b5 - 4000; + x1 = (b2 * (b6 * b6 / xpow(2, 12))) / xpow(2, 11); + x2 = ac2 * b6 / xpow(2, 11); + x3 = x1 + x2; + b3 = ((((unsigned long)ac1 * 4 + x3) << oss) + 2) / 4; + x1 = ac3 * b6 / xpow(2, 13); + x2 = (b1 * (b6 * b6 / xpow(2, 12))) / xpow(2, 16); + x3 = ((x1 + x2) + 2) / xpow(2, 2); + b4 = ac4 * (unsigned long)(x3 + 32768) / xpow(2, 15); + b7 = ((unsigned long)up - b3) * (50000 >> oss); + if (b7 < (unsigned long)0x80000000) { + p = (b7 * 2) / b4; + } else { + p = (b7 / b4) * 2; + } + x1 = (p / xpow(2, 8)) * (p / xpow(2, 8)); + x1 = (x1 * 3038) / xpow(2, 16); + x2 = (-7357 * p) / xpow(2, 16); + p = p + (x1 + x2 + 3791) / xpow(2, 4); + pressure = (float)p / 100.0; +} + +void BMP085::init (BMP085_oss p_oss) { + ac1 = twi_readshort(WEATHER_BMP085, 0xaa); + ac2 = twi_readshort(WEATHER_BMP085, 0xac); + ac3 = twi_readshort(WEATHER_BMP085, 0xae); + ac4 = twi_readshort(WEATHER_BMP085, 0xb0); + ac5 = twi_readshort(WEATHER_BMP085, 0xb2); + ac6 = twi_readshort(WEATHER_BMP085, 0xb4); + b1 = twi_readshort(WEATHER_BMP085, 0xb6); + b2 = twi_readshort(WEATHER_BMP085, 0xb8); + mb = twi_readshort(WEATHER_BMP085, 0xba); + mc = twi_readshort(WEATHER_BMP085, 0xbc); + md = twi_readshort(WEATHER_BMP085, 0xbe); + oss = p_oss; +} + +unsigned short BMP085::twi_readshort (int id, int addr) { + unsigned short i; + + i2c.start(); + i2c.write(id); + i2c.write(addr); + + i2c.start(); + i2c.write(id | 1); + i = i2c.read(1) << 8; + i |= i2c.read(0); + i2c.stop(); + + return i; +} + +unsigned long BMP085::twi_readlong (int id, int addr) { + unsigned long i; + + i2c.start(); + i2c.write(id); + i2c.write(addr); + + i2c.start(); + i2c.write(id | 1); + i = i2c.read(1) << 16; + i |= i2c.read(1) << 8; + i |= i2c.read(0); + i2c.stop(); + + return i; +} + +void BMP085::twi_writechar (int id, int addr, int dat) { + + i2c.start(); + i2c.write(id); + i2c.write(addr); + i2c.write(dat); + i2c.stop(); +}
diff -r 823f105078c7 -r 608e9604ca26 BMP085.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BMP085.h Sun Jan 08 22:06:54 2017 +0000 @@ -0,0 +1,56 @@ +/* + * mbed library to use a Bosch Sensortec BMP085/BMP180 sensor + * Copyright (c) 2010 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ + +/** @file BMP085.h + * @brief mbed library to use a Bosch Sensortec BMP085/BMP180 sensor + * barometric pressure sensor BMP085/BMP180 (Bosch Sensortec) + * interface: I2C digital + */ + +#ifndef BMP085_H +#define BMP085_H + +#include "mbed.h" + +/** + * @brief over sampling setting + */ +enum BMP085_oss { + BMP085_oss1 = 0, ///< ultra low power (1 time) + BMP085_oss2 = 1, ///< standard (2 times) + BMP085_oss4 = 2, ///< high resolution (4 times) + BMP085_oss8 = 3 ///< ultra high resolution (8 times) +}; + +/** + * @brief BMP085 class + */ +class BMP085 { +public: + BMP085(PinName p_sda, PinName p_scl, BMP085_oss p_oss = BMP085_oss1); + BMP085(I2C& p_i2c, BMP085_oss p_oss = BMP085_oss1); + + float get_temperature(); + float get_pressure(); + void update(); + +protected: + void init(BMP085_oss); + unsigned short twi_readshort (int, int); + unsigned long twi_readlong (int, int); + void twi_writechar (int, int, int); + + I2C i2c; + float temperature; + float pressure; + +private: + + short ac1, ac2, ac3, b1, b2, mb, mc, md, oss; + unsigned short ac4, ac5, ac6; +}; + +#endif \ No newline at end of file
diff -r 823f105078c7 -r 608e9604ca26 main.cpp --- a/main.cpp Thu Jan 07 02:49:37 2016 +0000 +++ b/main.cpp Sun Jan 08 22:06:54 2017 +0000 @@ -21,6 +21,8 @@ #include "ble/BLE.h" #include "Servo.h" #include "GattCallbackParamTypes.h" +#include "ble/FunctionPointerWithContext.h" +#include "BMP085.h" #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ @@ -32,7 +34,7 @@ #define DIGITAL_IN_PIN P0_5 //A4 #define PWM_PIN P0_16 //D6 #define SERVO_PIN P0_14 //D10 -#define ANALOG_IN_PIN P0_6 //A5 +#define ANALOG_IN_PIN P0_1 //A0 BLE ble; @@ -41,11 +43,14 @@ PwmOut PWM(PWM_PIN); AnalogIn ANALOG(ANALOG_IN_PIN); Servo MYSERVO(SERVO_PIN); +BMP085 myCaptor(P0_29, P0_28); Serial pc(USBTX, USBRX); static uint8_t analog_enabled = 0; static uint8_t old_state = 0; +static uint8_t captor_enabled = 0; +static uint8_t captor_enabled_for_pressure = 0; // The Nordic UART Service static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; @@ -78,6 +83,12 @@ ble.startAdvertising(); } +void confirmationHandler(uint16_t Handler) +{ + if (captor_enabled) + captor_enabled = false; +} + void WrittenHandler(const GattWriteCallbackParams *Handler) { uint8_t buf[TXRX_BUF_LEN]; @@ -101,13 +112,17 @@ } else if(buf[0] == 0xA0) { - if(buf[1] == 0x01) + if(buf[1] == 0x01){ analog_enabled = 1; + captor_enabled = 1; + } else analog_enabled = 0; + } else if(buf[0] == 0x02) { + captor_enabled_for_pressure= 1; float value = (float)buf[1]/255; PWM = value; } @@ -159,6 +174,35 @@ ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); } + if (captor_enabled) + { + // Read and send out + captor_enabled = false; + myCaptor.update(); + float s = myCaptor.get_temperature() * 100; + int value = s; + buf[0] = (value >> 24); + buf[1] = (value >> 16); + buf[2] = (value >> 8); + buf[3] = value; + //for(int i = 0; i < 200000; ++i) + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); + } + + if (captor_enabled_for_pressure) + { + // Read and send out + captor_enabled_for_pressure = false; + myCaptor.update(); + float s = myCaptor.get_pressure() * 100; + uint32_t value = s; + buf[0] = (value >> 24); + buf[1] = (value >> 16); + buf[2] = (value >> 8); + buf[3] = (uint8_t) value; + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); + } + // If digital in changes, report the state if (BUTTON != old_state) { @@ -190,6 +234,7 @@ ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); + ble.onConfirmationReceived(confirmationHandler); pc.baud(9600); pc.printf("SimpleChat Init \r\n");