BLE VMM Console
Dependencies: BLE_API mbed nRF51822
Fork of BLE_UARTConsole by
main.cpp@10:053397a8dc40, 2018-09-06 (annotated)
- Committer:
- suntopbd
- Date:
- Thu Sep 06 22:56:26 2018 +0000
- Revision:
- 10:053397a8dc40
- Parent:
- 9:5f0732aa3008
Bluetooth Vmm for Car
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:2130d7f559dc | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:2130d7f559dc | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:2130d7f559dc | 3 | * |
rgrover1 | 0:2130d7f559dc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:2130d7f559dc | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:2130d7f559dc | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:2130d7f559dc | 7 | * |
rgrover1 | 0:2130d7f559dc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:2130d7f559dc | 9 | * |
rgrover1 | 0:2130d7f559dc | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:2130d7f559dc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:2130d7f559dc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:2130d7f559dc | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:2130d7f559dc | 14 | * limitations under the License. |
rgrover1 | 0:2130d7f559dc | 15 | */ |
suntopbd | 10:053397a8dc40 | 16 | ////////////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 17 | ///////////////////////NOTE /////////////////////////////// |
suntopbd | 10:053397a8dc40 | 18 | // This program uses BBC microbit as a NRF51822 board // |
suntopbd | 10:053397a8dc40 | 19 | // with limited onboard functionality, mainly focused // |
suntopbd | 10:053397a8dc40 | 20 | // BLE-uart/ADC/DIO/PWM capability, see pin maping below // |
suntopbd | 10:053397a8dc40 | 21 | /////////////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 22 | /////////////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 23 | |
rgrover1 | 5:2607923acfa1 | 24 | #include <string.h> |
rgrover1 | 0:2130d7f559dc | 25 | #include "mbed.h" |
rgrover1 | 8:f9bf6d403cde | 26 | #include "BLE.h" |
suntopbd | 10:053397a8dc40 | 27 | #include "stdio.h" |
rgrover1 | 0:2130d7f559dc | 28 | #include "UARTService.h" |
rgrover1 | 0:2130d7f559dc | 29 | |
rgrover1 | 0:2130d7f559dc | 30 | |
suntopbd | 10:053397a8dc40 | 31 | #define NEED_CONSOLE_OUTPUT 1 // if BLE printf messages needed on the remote console (PC/phone/host); |
rgrover1 | 0:2130d7f559dc | 32 | #if NEED_CONSOLE_OUTPUT |
suntopbd | 10:053397a8dc40 | 33 | #define BLEprintf(STR) { if (uart) uart->write(STR, strlen(STR)); } |
rgrover1 | 0:2130d7f559dc | 34 | #else |
suntopbd | 10:053397a8dc40 | 35 | #define BLEprintf(...) |
suntopbd | 10:053397a8dc40 | 36 | #endif |
suntopbd | 10:053397a8dc40 | 37 | |
suntopbd | 10:053397a8dc40 | 38 | ///////////////// pin table ///////////////////////// |
suntopbd | 10:053397a8dc40 | 39 | ///////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 40 | // ubit.pin nrf51822pin functions note // |
suntopbd | 10:053397a8dc40 | 41 | ///////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 42 | // P2 P0_1 ADC/PWM/DIO 2 // |
suntopbd | 10:053397a8dc40 | 43 | // P1 P0_2 ADC/PWM/DIO 1 // |
suntopbd | 10:053397a8dc40 | 44 | // P0 P0_3 ADC/PWM/DIO 0 // |
suntopbd | 10:053397a8dc40 | 45 | // P16 P_16 DIO // |
suntopbd | 10:053397a8dc40 | 46 | // P14 P0_25 SPI MIS/DIO // |
suntopbd | 10:053397a8dc40 | 47 | // P5 P_17 Button A/DI pullup // |
suntopbd | 10:053397a8dc40 | 48 | // P11 P_26 Button B/DI pullup // |
suntopbd | 10:053397a8dc40 | 49 | |
suntopbd | 10:053397a8dc40 | 50 | ///////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 51 | ///////////////// not mapped yet //////////////////// |
suntopbd | 10:053397a8dc40 | 52 | // P20 I2C SDA/DIO pullup // |
suntopbd | 10:053397a8dc40 | 53 | // P19 I2C SCL/DIO pullup // |
suntopbd | 10:053397a8dc40 | 54 | // P15 SPI MOS/DIO // |
suntopbd | 10:053397a8dc40 | 55 | // P13 SPI SCK/DIO // |
suntopbd | 10:053397a8dc40 | 56 | // P16 // |
suntopbd | 10:053397a8dc40 | 57 | // P8 // |
suntopbd | 10:053397a8dc40 | 58 | |
suntopbd | 10:053397a8dc40 | 59 | ///////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 60 | // LED Matrix pins are not mapped // |
suntopbd | 10:053397a8dc40 | 61 | ///////////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 62 | |
rgrover1 | 0:2130d7f559dc | 63 | |
rgrover1 | 0:2130d7f559dc | 64 | BLEDevice ble; |
rgrover1 | 5:2607923acfa1 | 65 | UARTService *uart; |
suntopbd | 10:053397a8dc40 | 66 | AnalogIn ain(P0_3); |
suntopbd | 10:053397a8dc40 | 67 | DigitalOut dout(P0_16); |
suntopbd | 10:053397a8dc40 | 68 | DigitalIn din(P0_17); |
suntopbd | 10:053397a8dc40 | 69 | |
suntopbd | 10:053397a8dc40 | 70 | int dVal, dec, i,j; |
suntopbd | 10:053397a8dc40 | 71 | float val,exBatt,f; |
suntopbd | 10:053397a8dc40 | 72 | char buffer[10]; |
suntopbd | 10:053397a8dc40 | 73 | /////////////////// functions /////////////////////// |
suntopbd | 10:053397a8dc40 | 74 | |
suntopbd | 10:053397a8dc40 | 75 | |
rgrover1 | 0:2130d7f559dc | 76 | |
rgrover1 | 9:5f0732aa3008 | 77 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
rgrover1 | 0:2130d7f559dc | 78 | { |
rgrover1 | 0:2130d7f559dc | 79 | ble.startAdvertising(); |
rgrover1 | 0:2130d7f559dc | 80 | } |
rgrover1 | 0:2130d7f559dc | 81 | |
rgrover1 | 0:2130d7f559dc | 82 | void periodicCallback(void) |
rgrover1 | 0:2130d7f559dc | 83 | { |
suntopbd | 10:053397a8dc40 | 84 | BLEprintf("\n"); |
suntopbd | 10:053397a8dc40 | 85 | BLEprintf("Battery Volt: "); |
suntopbd | 10:053397a8dc40 | 86 | BLEprintf(buffer); |
suntopbd | 10:053397a8dc40 | 87 | // BLEprintf("\r\n"); |
suntopbd | 10:053397a8dc40 | 88 | |
rgrover1 | 0:2130d7f559dc | 89 | } |
suntopbd | 10:053397a8dc40 | 90 | /////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 91 | ///////////////// MAIN FUNC /////////////////// |
suntopbd | 10:053397a8dc40 | 92 | /////////////////////////////////////////////// |
suntopbd | 10:053397a8dc40 | 93 | |
rgrover1 | 0:2130d7f559dc | 94 | int main(void) |
rgrover1 | 0:2130d7f559dc | 95 | { |
rgrover1 | 0:2130d7f559dc | 96 | Ticker ticker; |
suntopbd | 10:053397a8dc40 | 97 | ticker.attach(periodicCallback, 2); |
rgrover1 | 0:2130d7f559dc | 98 | ble.init(); |
suntopbd | 10:053397a8dc40 | 99 | ble.onDisconnection(disconnectionCallback); |
rgrover1 | 5:2607923acfa1 | 100 | uart = new UARTService(ble); |
rgrover1 | 0:2130d7f559dc | 101 | |
rgrover1 | 0:2130d7f559dc | 102 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
rgrover1 | 0:2130d7f559dc | 103 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 0:2130d7f559dc | 104 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
suntopbd | 10:053397a8dc40 | 105 | (const uint8_t *)"uBit BLE", sizeof("uBit BLE") - 1); |
rgrover1 | 0:2130d7f559dc | 106 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
rgrover1 | 0:2130d7f559dc | 107 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
rgrover1 | 0:2130d7f559dc | 108 | |
suntopbd | 10:053397a8dc40 | 109 | ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. // |
rgrover1 | 0:2130d7f559dc | 110 | ble.startAdvertising(); |
rgrover1 | 0:2130d7f559dc | 111 | |
suntopbd | 10:053397a8dc40 | 112 | while (true) |
suntopbd | 10:053397a8dc40 | 113 | { |
rgrover1 | 0:2130d7f559dc | 114 | ble.waitForEvent(); |
suntopbd | 10:053397a8dc40 | 115 | dout = din.read(); |
suntopbd | 10:053397a8dc40 | 116 | val = ain.read_u16(); |
suntopbd | 10:053397a8dc40 | 117 | |
suntopbd | 10:053397a8dc40 | 118 | |
suntopbd | 10:053397a8dc40 | 119 | // 100k+22k voltage divider |
suntopbd | 10:053397a8dc40 | 120 | // 3.3 Vcc board voltage |
suntopbd | 10:053397a8dc40 | 121 | |
suntopbd | 10:053397a8dc40 | 122 | exBatt = (122/22)*3.6*val/1023.0; // ext batt volt |
suntopbd | 10:053397a8dc40 | 123 | //exBatt =12.37; |
suntopbd | 10:053397a8dc40 | 124 | |
suntopbd | 10:053397a8dc40 | 125 | dVal = exBatt; |
suntopbd | 10:053397a8dc40 | 126 | |
suntopbd | 10:053397a8dc40 | 127 | |
suntopbd | 10:053397a8dc40 | 128 | dec = (int)(exBatt * 100) % 100; |
suntopbd | 10:053397a8dc40 | 129 | if(exBatt>1 && exBatt<10){i=0;} |
suntopbd | 10:053397a8dc40 | 130 | if(exBatt>=10 && exBatt<100){i=1;} |
suntopbd | 10:053397a8dc40 | 131 | j=i; |
suntopbd | 10:053397a8dc40 | 132 | memset(buffer, 0, 10); |
suntopbd | 10:053397a8dc40 | 133 | |
suntopbd | 10:053397a8dc40 | 134 | while (dVal > 0) |
suntopbd | 10:053397a8dc40 | 135 | { |
suntopbd | 10:053397a8dc40 | 136 | buffer[i] = (dVal % 10) + '0'; |
suntopbd | 10:053397a8dc40 | 137 | dVal /= 10; |
suntopbd | 10:053397a8dc40 | 138 | i--; |
suntopbd | 10:053397a8dc40 | 139 | } |
suntopbd | 10:053397a8dc40 | 140 | buffer[j+1] = '.'; |
suntopbd | 10:053397a8dc40 | 141 | buffer[j+2] = (dec / 10) + '0'; |
suntopbd | 10:053397a8dc40 | 142 | buffer[j+3] = (dec % 10) + '0'; |
suntopbd | 10:053397a8dc40 | 143 | |
suntopbd | 10:053397a8dc40 | 144 | } |
suntopbd | 10:053397a8dc40 | 145 | ///////////// end of while(1) ////////////// |
rgrover1 | 0:2130d7f559dc | 146 | } |
suntopbd | 10:053397a8dc40 | 147 | ///////////// end of main ///////////////// |