Temp and Pressure get on BMP180
Dependencies: BLE_API BMP180 mbed nRF51822
Fork of nRF51822_SimpleControls by
main.cpp@4:b97fd2ecd539, 2016-06-10 (annotated)
- Committer:
- Yayg
- Date:
- Fri Jun 10 18:25:44 2016 +0000
- Revision:
- 4:b97fd2ecd539
- Parent:
- 3:823f105078c7
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 1:f03072e32ed3 | 1 | /* |
RedBearLab | 1:f03072e32ed3 | 2 | |
RedBearLab | 1:f03072e32ed3 | 3 | Copyright (c) 2012-2014 RedBearLab |
RedBearLab | 1:f03072e32ed3 | 4 | |
RedBearLab | 1:f03072e32ed3 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
RedBearLab | 1:f03072e32ed3 | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
RedBearLab | 1:f03072e32ed3 | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
RedBearLab | 1:f03072e32ed3 | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
RedBearLab | 1:f03072e32ed3 | 9 | subject to the following conditions: |
RedBearLab | 1:f03072e32ed3 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
RedBearLab | 1:f03072e32ed3 | 11 | |
RedBearLab | 1:f03072e32ed3 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
RedBearLab | 1:f03072e32ed3 | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
RedBearLab | 1:f03072e32ed3 | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
RedBearLab | 1:f03072e32ed3 | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
RedBearLab | 1:f03072e32ed3 | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
RedBearLab | 1:f03072e32ed3 | 17 | |
RedBearLab | 1:f03072e32ed3 | 18 | */ |
RedBearLab | 0:dfcebc1e442a | 19 | |
RedBearLab | 0:dfcebc1e442a | 20 | #include "mbed.h" |
Yayg | 4:b97fd2ecd539 | 21 | #include "BMP180.h" |
RedBearLab | 2:bbcdd23ba9ba | 22 | #include "ble/BLE.h" |
RedBearLab | 0:dfcebc1e442a | 23 | #include "Servo.h" |
RedBearLab | 2:bbcdd23ba9ba | 24 | #include "GattCallbackParamTypes.h" |
RedBearLab | 0:dfcebc1e442a | 25 | |
RedBearLab | 0:dfcebc1e442a | 26 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
RedBearLab | 0:dfcebc1e442a | 27 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
RedBearLab | 0:dfcebc1e442a | 28 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
RedBearLab | 0:dfcebc1e442a | 29 | |
RedBearLab | 0:dfcebc1e442a | 30 | #define TXRX_BUF_LEN 20 |
RedBearLab | 0:dfcebc1e442a | 31 | |
RedBearLab | 0:dfcebc1e442a | 32 | #define DIGITAL_OUT_PIN P0_17 //D7 |
RedBearLab | 0:dfcebc1e442a | 33 | #define DIGITAL_IN_PIN P0_5 //A4 |
RedBearLab | 0:dfcebc1e442a | 34 | #define PWM_PIN P0_16 //D6 |
RedBearLab | 0:dfcebc1e442a | 35 | #define SERVO_PIN P0_14 //D10 |
RedBearLab | 0:dfcebc1e442a | 36 | #define ANALOG_IN_PIN P0_6 //A5 |
RedBearLab | 0:dfcebc1e442a | 37 | |
Yayg | 4:b97fd2ecd539 | 38 | I2C i2c(D14, D15); |
Yayg | 4:b97fd2ecd539 | 39 | BMP180 bmp180(&i2c); |
Yayg | 4:b97fd2ecd539 | 40 | |
RedBearLab | 2:bbcdd23ba9ba | 41 | BLE ble; |
RedBearLab | 0:dfcebc1e442a | 42 | |
RedBearLab | 0:dfcebc1e442a | 43 | DigitalOut LED_SET(DIGITAL_OUT_PIN); |
RedBearLab | 0:dfcebc1e442a | 44 | DigitalIn BUTTON(DIGITAL_IN_PIN); |
RedBearLab | 0:dfcebc1e442a | 45 | PwmOut PWM(PWM_PIN); |
RedBearLab | 0:dfcebc1e442a | 46 | AnalogIn ANALOG(ANALOG_IN_PIN); |
RedBearLab | 0:dfcebc1e442a | 47 | Servo MYSERVO(SERVO_PIN); |
RedBearLab | 0:dfcebc1e442a | 48 | |
RedBearLab | 0:dfcebc1e442a | 49 | Serial pc(USBTX, USBRX); |
RedBearLab | 0:dfcebc1e442a | 50 | |
RedBearLab | 0:dfcebc1e442a | 51 | static uint8_t analog_enabled = 0; |
RedBearLab | 0:dfcebc1e442a | 52 | static uint8_t old_state = 0; |
RedBearLab | 0:dfcebc1e442a | 53 | |
RedBearLab | 0:dfcebc1e442a | 54 | // The Nordic UART Service |
RedBearLab | 0:dfcebc1e442a | 55 | static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:dfcebc1e442a | 56 | static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:dfcebc1e442a | 57 | static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:dfcebc1e442a | 58 | static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; |
RedBearLab | 0:dfcebc1e442a | 59 | |
RedBearLab | 0:dfcebc1e442a | 60 | |
RedBearLab | 0:dfcebc1e442a | 61 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:dfcebc1e442a | 62 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:dfcebc1e442a | 63 | |
RedBearLab | 0:dfcebc1e442a | 64 | //static uint8_t rx_buf[TXRX_BUF_LEN]; |
RedBearLab | 0:dfcebc1e442a | 65 | //static uint8_t rx_len=0; |
RedBearLab | 0:dfcebc1e442a | 66 | |
RedBearLab | 0:dfcebc1e442a | 67 | |
RedBearLab | 0:dfcebc1e442a | 68 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
RedBearLab | 0:dfcebc1e442a | 69 | |
RedBearLab | 0:dfcebc1e442a | 70 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
RedBearLab | 0:dfcebc1e442a | 71 | |
RedBearLab | 0:dfcebc1e442a | 72 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
RedBearLab | 0:dfcebc1e442a | 73 | |
RedBearLab | 0:dfcebc1e442a | 74 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:dfcebc1e442a | 75 | |
RedBearLab | 0:dfcebc1e442a | 76 | |
RedBearLab | 0:dfcebc1e442a | 77 | |
RedBearLab | 3:823f105078c7 | 78 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
RedBearLab | 0:dfcebc1e442a | 79 | { |
RedBearLab | 0:dfcebc1e442a | 80 | pc.printf("Disconnected \r\n"); |
RedBearLab | 0:dfcebc1e442a | 81 | pc.printf("Restart advertising \r\n"); |
RedBearLab | 0:dfcebc1e442a | 82 | ble.startAdvertising(); |
RedBearLab | 0:dfcebc1e442a | 83 | } |
RedBearLab | 0:dfcebc1e442a | 84 | |
RedBearLab | 2:bbcdd23ba9ba | 85 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
RedBearLab | 0:dfcebc1e442a | 86 | { |
RedBearLab | 0:dfcebc1e442a | 87 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:dfcebc1e442a | 88 | uint16_t bytesRead, index; |
RedBearLab | 0:dfcebc1e442a | 89 | |
RedBearLab | 2:bbcdd23ba9ba | 90 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
RedBearLab | 0:dfcebc1e442a | 91 | { |
RedBearLab | 0:dfcebc1e442a | 92 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
RedBearLab | 0:dfcebc1e442a | 93 | memset(txPayload, 0, TXRX_BUF_LEN); |
RedBearLab | 0:dfcebc1e442a | 94 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
RedBearLab | 0:dfcebc1e442a | 95 | |
RedBearLab | 0:dfcebc1e442a | 96 | for(index=0; index<bytesRead; index++) |
RedBearLab | 0:dfcebc1e442a | 97 | pc.putc(buf[index]); |
RedBearLab | 0:dfcebc1e442a | 98 | |
RedBearLab | 0:dfcebc1e442a | 99 | if(buf[0] == 0x01) |
RedBearLab | 0:dfcebc1e442a | 100 | { |
RedBearLab | 0:dfcebc1e442a | 101 | if(buf[1] == 0x01) |
RedBearLab | 0:dfcebc1e442a | 102 | LED_SET = 1; |
RedBearLab | 0:dfcebc1e442a | 103 | else |
RedBearLab | 0:dfcebc1e442a | 104 | LED_SET = 0; |
RedBearLab | 0:dfcebc1e442a | 105 | } |
RedBearLab | 0:dfcebc1e442a | 106 | else if(buf[0] == 0xA0) |
RedBearLab | 0:dfcebc1e442a | 107 | { |
Yayg | 4:b97fd2ecd539 | 108 | int pres = 0; |
Yayg | 4:b97fd2ecd539 | 109 | bmp180.startPressure(BMP180::ULTRA_LOW_POWER); |
Yayg | 4:b97fd2ecd539 | 110 | wait_ms(5); |
Yayg | 4:b97fd2ecd539 | 111 | if(bmp180.getPressure(&pres) != 0) |
Yayg | 4:b97fd2ecd539 | 112 | printf("Error getting temperature\r\n"); |
Yayg | 4:b97fd2ecd539 | 113 | uint8_t buf[3]; |
Yayg | 4:b97fd2ecd539 | 114 | uint16_t value = pres/100; |
Yayg | 4:b97fd2ecd539 | 115 | buf[0] = (0x0B); |
Yayg | 4:b97fd2ecd539 | 116 | buf[1] = (value >> 8); |
Yayg | 4:b97fd2ecd539 | 117 | buf[2] = (value); |
Yayg | 4:b97fd2ecd539 | 118 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); |
Yayg | 4:b97fd2ecd539 | 119 | } |
Yayg | 4:b97fd2ecd539 | 120 | else if(buf[0] == 0xA1) |
Yayg | 4:b97fd2ecd539 | 121 | { |
Yayg | 4:b97fd2ecd539 | 122 | float temp = 0; |
Yayg | 4:b97fd2ecd539 | 123 | bmp180.startTemperature(); |
Yayg | 4:b97fd2ecd539 | 124 | wait_ms(5); |
Yayg | 4:b97fd2ecd539 | 125 | if(bmp180.getTemperature(&temp) != 0) |
Yayg | 4:b97fd2ecd539 | 126 | printf("Error getting temperature\r\n"); |
Yayg | 4:b97fd2ecd539 | 127 | uint8_t buf[3]; |
Yayg | 4:b97fd2ecd539 | 128 | uint16_t value = temp; |
Yayg | 4:b97fd2ecd539 | 129 | buf[0] = (0x0B); |
Yayg | 4:b97fd2ecd539 | 130 | buf[1] = (value >> 8); |
Yayg | 4:b97fd2ecd539 | 131 | buf[2] = (value); |
Yayg | 4:b97fd2ecd539 | 132 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); |
RedBearLab | 0:dfcebc1e442a | 133 | } |
RedBearLab | 0:dfcebc1e442a | 134 | else if(buf[0] == 0x02) |
RedBearLab | 0:dfcebc1e442a | 135 | { |
RedBearLab | 0:dfcebc1e442a | 136 | float value = (float)buf[1]/255; |
RedBearLab | 0:dfcebc1e442a | 137 | PWM = value; |
RedBearLab | 0:dfcebc1e442a | 138 | } |
RedBearLab | 0:dfcebc1e442a | 139 | else if(buf[0] == 0x03) |
RedBearLab | 0:dfcebc1e442a | 140 | { |
RedBearLab | 0:dfcebc1e442a | 141 | MYSERVO.write(buf[1]); |
RedBearLab | 0:dfcebc1e442a | 142 | } |
RedBearLab | 0:dfcebc1e442a | 143 | else if(buf[0] == 0x04) |
RedBearLab | 0:dfcebc1e442a | 144 | { |
RedBearLab | 0:dfcebc1e442a | 145 | analog_enabled = 0; |
RedBearLab | 0:dfcebc1e442a | 146 | PWM = 0; |
RedBearLab | 0:dfcebc1e442a | 147 | MYSERVO.write(0); |
RedBearLab | 0:dfcebc1e442a | 148 | LED_SET = 0; |
RedBearLab | 0:dfcebc1e442a | 149 | old_state = 0; |
RedBearLab | 0:dfcebc1e442a | 150 | } |
RedBearLab | 0:dfcebc1e442a | 151 | } |
RedBearLab | 0:dfcebc1e442a | 152 | } |
RedBearLab | 0:dfcebc1e442a | 153 | |
RedBearLab | 0:dfcebc1e442a | 154 | int main(void) |
RedBearLab | 0:dfcebc1e442a | 155 | { |
Yayg | 4:b97fd2ecd539 | 156 | //Ticker ticker; |
Yayg | 4:b97fd2ecd539 | 157 | //ticker.attach_us(m_status_check_handle, 200000); |
RedBearLab | 0:dfcebc1e442a | 158 | |
RedBearLab | 0:dfcebc1e442a | 159 | ble.init(); |
RedBearLab | 0:dfcebc1e442a | 160 | ble.onDisconnection(disconnectionCallback); |
RedBearLab | 0:dfcebc1e442a | 161 | ble.onDataWritten(WrittenHandler); |
RedBearLab | 0:dfcebc1e442a | 162 | |
RedBearLab | 0:dfcebc1e442a | 163 | pc.baud(9600); |
RedBearLab | 0:dfcebc1e442a | 164 | pc.printf("SimpleChat Init \r\n"); |
RedBearLab | 0:dfcebc1e442a | 165 | |
RedBearLab | 0:dfcebc1e442a | 166 | //pc.attach( uartCB , pc.RxIrq); |
RedBearLab | 0:dfcebc1e442a | 167 | |
RedBearLab | 0:dfcebc1e442a | 168 | // setup advertising |
RedBearLab | 0:dfcebc1e442a | 169 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:dfcebc1e442a | 170 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
RedBearLab | 0:dfcebc1e442a | 171 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Yayg | 4:b97fd2ecd539 | 172 | (const uint8_t *)"Pepito", sizeof("Pepito") - 1); |
RedBearLab | 0:dfcebc1e442a | 173 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
RedBearLab | 0:dfcebc1e442a | 174 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
RedBearLab | 0:dfcebc1e442a | 175 | // 100ms; in multiples of 0.625ms. |
RedBearLab | 0:dfcebc1e442a | 176 | ble.setAdvertisingInterval(160); |
RedBearLab | 0:dfcebc1e442a | 177 | |
RedBearLab | 0:dfcebc1e442a | 178 | ble.addService(uartService); |
RedBearLab | 0:dfcebc1e442a | 179 | |
RedBearLab | 0:dfcebc1e442a | 180 | ble.startAdvertising(); |
RedBearLab | 0:dfcebc1e442a | 181 | |
RedBearLab | 0:dfcebc1e442a | 182 | pc.printf("Advertising Start \r\n"); |
RedBearLab | 0:dfcebc1e442a | 183 | |
Yayg | 4:b97fd2ecd539 | 184 | |
Yayg | 4:b97fd2ecd539 | 185 | while(1) { |
Yayg | 4:b97fd2ecd539 | 186 | if (bmp180.init() != 0) { |
Yayg | 4:b97fd2ecd539 | 187 | printf("Error communicating with BMP180\r\n"); |
Yayg | 4:b97fd2ecd539 | 188 | } else { |
Yayg | 4:b97fd2ecd539 | 189 | printf("Initialized BMP180\r\n"); |
Yayg | 4:b97fd2ecd539 | 190 | break; |
Yayg | 4:b97fd2ecd539 | 191 | } |
Yayg | 4:b97fd2ecd539 | 192 | wait(1); |
Yayg | 4:b97fd2ecd539 | 193 | } |
Yayg | 4:b97fd2ecd539 | 194 | |
Yayg | 4:b97fd2ecd539 | 195 | |
RedBearLab | 0:dfcebc1e442a | 196 | while(1) |
Yayg | 4:b97fd2ecd539 | 197 | { |
RedBearLab | 0:dfcebc1e442a | 198 | ble.waitForEvent(); |
RedBearLab | 0:dfcebc1e442a | 199 | } |
Yayg | 4:b97fd2ecd539 | 200 | } |