added

Dependencies:   mbed microbit BLE_API nRF51822

Committer:
suntopbd
Date:
Fri Sep 07 20:17:07 2018 +0000
Revision:
11:6916c05fde52
Parent:
10:053397a8dc40
Child:
12:deed12c7f372
Microbit BLE UART IoT

Who changed what in which revision?

UserRevisionLine numberNew 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
suntopbd 11:6916c05fde52 30 #include "ble/BLE.h"
suntopbd 11:6916c05fde52 31 #include "ble/services/URIBeaconConfigService.h"
suntopbd 11:6916c05fde52 32 #include "ble/services/DFUService.h"
suntopbd 11:6916c05fde52 33 #include "ble/services/DeviceInformationService.h"
suntopbd 11:6916c05fde52 34 #include "ConfigParamsPersistence.h"
suntopbd 11:6916c05fde52 35
rgrover1 0:2130d7f559dc 36
suntopbd 10:053397a8dc40 37 #define NEED_CONSOLE_OUTPUT 1 // if BLE printf messages needed on the remote console (PC/phone/host);
rgrover1 0:2130d7f559dc 38 #if NEED_CONSOLE_OUTPUT
suntopbd 11:6916c05fde52 39 #define blePrintf(STR) { if (uart) uart->write(STR, strlen(STR)); }
rgrover1 0:2130d7f559dc 40 #else
suntopbd 11:6916c05fde52 41 #define blePrintf(...)
suntopbd 10:053397a8dc40 42 #endif
suntopbd 10:053397a8dc40 43
suntopbd 10:053397a8dc40 44 ///////////////// pin table /////////////////////////
suntopbd 10:053397a8dc40 45 /////////////////////////////////////////////////////
suntopbd 10:053397a8dc40 46 // ubit.pin nrf51822pin functions note //
suntopbd 10:053397a8dc40 47 /////////////////////////////////////////////////////
suntopbd 10:053397a8dc40 48 // P2 P0_1 ADC/PWM/DIO 2 //
suntopbd 10:053397a8dc40 49 // P1 P0_2 ADC/PWM/DIO 1 //
suntopbd 10:053397a8dc40 50 // P0 P0_3 ADC/PWM/DIO 0 //
suntopbd 10:053397a8dc40 51 // P16 P_16 DIO //
suntopbd 10:053397a8dc40 52 // P14 P0_25 SPI MIS/DIO //
suntopbd 10:053397a8dc40 53 // P5 P_17 Button A/DI pullup //
suntopbd 10:053397a8dc40 54 // P11 P_26 Button B/DI pullup //
suntopbd 10:053397a8dc40 55
suntopbd 10:053397a8dc40 56 /////////////////////////////////////////////////////
suntopbd 10:053397a8dc40 57 ///////////////// not mapped yet ////////////////////
suntopbd 10:053397a8dc40 58 // P20 I2C SDA/DIO pullup //
suntopbd 10:053397a8dc40 59 // P19 I2C SCL/DIO pullup //
suntopbd 10:053397a8dc40 60 // P15 SPI MOS/DIO //
suntopbd 10:053397a8dc40 61 // P13 SPI SCK/DIO //
suntopbd 10:053397a8dc40 62 // P16 //
suntopbd 10:053397a8dc40 63 // P8 //
suntopbd 10:053397a8dc40 64
suntopbd 10:053397a8dc40 65 /////////////////////////////////////////////////////
suntopbd 10:053397a8dc40 66 // LED Matrix pins are not mapped //
suntopbd 10:053397a8dc40 67 /////////////////////////////////////////////////////
suntopbd 10:053397a8dc40 68
rgrover1 0:2130d7f559dc 69
rgrover1 0:2130d7f559dc 70 BLEDevice ble;
suntopbd 11:6916c05fde52 71 DigitalOut led1(LED1);
rgrover1 5:2607923acfa1 72 UARTService *uart;
suntopbd 10:053397a8dc40 73 AnalogIn ain(P0_3);
suntopbd 11:6916c05fde52 74 int val,dVal, dec, i;
suntopbd 11:6916c05fde52 75 char result[100];
suntopbd 11:6916c05fde52 76 float batt = 0.03;
suntopbd 10:053397a8dc40 77
rgrover1 0:2130d7f559dc 78
rgrover1 9:5f0732aa3008 79 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
rgrover1 0:2130d7f559dc 80 {
rgrover1 0:2130d7f559dc 81 ble.startAdvertising();
rgrover1 0:2130d7f559dc 82 }
rgrover1 0:2130d7f559dc 83
rgrover1 0:2130d7f559dc 84 void periodicCallback(void)
rgrover1 0:2130d7f559dc 85 {
suntopbd 10:053397a8dc40 86
suntopbd 11:6916c05fde52 87 blePrintf("BATT VOlT: ");
suntopbd 11:6916c05fde52 88 blePrintf(result);
suntopbd 11:6916c05fde52 89 blePrintf("\r\n");
rgrover1 0:2130d7f559dc 90 }
suntopbd 11:6916c05fde52 91
rgrover1 0:2130d7f559dc 92 int main(void)
rgrover1 0:2130d7f559dc 93 {
rgrover1 0:2130d7f559dc 94 Ticker ticker;
suntopbd 11:6916c05fde52 95 ticker.attach(periodicCallback, 5);
suntopbd 11:6916c05fde52 96
suntopbd 11:6916c05fde52 97 blePrintf("Initialising the nRF51822\n\r");
rgrover1 0:2130d7f559dc 98 ble.init();
suntopbd 11:6916c05fde52 99 ble.onDisconnection(disconnectionCallback);
suntopbd 11:6916c05fde52 100
rgrover1 5:2607923acfa1 101 uart = new UARTService(ble);
rgrover1 0:2130d7f559dc 102
suntopbd 11:6916c05fde52 103 /* setup advertising */
rgrover1 0:2130d7f559dc 104 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
rgrover1 0:2130d7f559dc 105 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 0:2130d7f559dc 106 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
suntopbd 10:053397a8dc40 107 (const uint8_t *)"uBit BLE", sizeof("uBit BLE") - 1);
suntopbd 11:6916c05fde52 108 // device name on BLE //
rgrover1 0:2130d7f559dc 109 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 0:2130d7f559dc 110 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
rgrover1 0:2130d7f559dc 111
suntopbd 11:6916c05fde52 112 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
rgrover1 0:2130d7f559dc 113 ble.startAdvertising();
rgrover1 0:2130d7f559dc 114
suntopbd 11:6916c05fde52 115 while (true) {
rgrover1 0:2130d7f559dc 116 ble.waitForEvent();
suntopbd 10:053397a8dc40 117 val = ain.read_u16();
suntopbd 11:6916c05fde52 118 // 22k+100K VOLTAGE DIVIDER AND 0.15 IS OFFSET
suntopbd 11:6916c05fde52 119 batt = (122/22)*3.6*val/1023.0 - 0.15
suntopbd 11:6916c05fde52 120 dVal = batt;
suntopbd 11:6916c05fde52 121 dec = (int)(batt * 100) % 100;
suntopbd 10:053397a8dc40 122
suntopbd 11:6916c05fde52 123 memset(result, 0, 100);
suntopbd 11:6916c05fde52 124 result[0] = (dVal / 10) + '0';
suntopbd 11:6916c05fde52 125 result[1] = (dVal % 10) + '0';
suntopbd 11:6916c05fde52 126 result[2] = '.';
suntopbd 11:6916c05fde52 127 result[3] = (dec / 10) + '0';
suntopbd 11:6916c05fde52 128 result[4] = (dec % 10) + '0';
suntopbd 11:6916c05fde52 129
suntopbd 10:053397a8dc40 130
suntopbd 11:6916c05fde52 131 for (i=strlen(result)-1; i>=0; i--)
suntopbd 11:6916c05fde52 132 putc(result[i], stdout);
suntopbd 11:6916c05fde52 133
suntopbd 11:6916c05fde52 134 }
rgrover1 0:2130d7f559dc 135 }
suntopbd 11:6916c05fde52 136
suntopbd 11:6916c05fde52 137
suntopbd 11:6916c05fde52 138