added
Dependencies: mbed microbit BLE_API nRF51822
main.cpp
- Committer:
- suntopbd
- Date:
- 2018-09-06
- Revision:
- 10:053397a8dc40
- Parent:
- 9:5f0732aa3008
- Child:
- 11:6916c05fde52
File content as of revision 10:053397a8dc40:
/* mbed Microcontroller Library * Copyright (c) 2006-2013 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ ////////////////////////////////////////////////////////// ///////////////////////NOTE /////////////////////////////// // This program uses BBC microbit as a NRF51822 board // // with limited onboard functionality, mainly focused // // BLE-uart/ADC/DIO/PWM capability, see pin maping below // /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// #include <string.h> #include "mbed.h" #include "BLE.h" #include "stdio.h" #include "UARTService.h" #define NEED_CONSOLE_OUTPUT 1 // if BLE printf messages needed on the remote console (PC/phone/host); #if NEED_CONSOLE_OUTPUT #define BLEprintf(STR) { if (uart) uart->write(STR, strlen(STR)); } #else #define BLEprintf(...) #endif ///////////////// pin table ///////////////////////// ///////////////////////////////////////////////////// // ubit.pin nrf51822pin functions note // ///////////////////////////////////////////////////// // P2 P0_1 ADC/PWM/DIO 2 // // P1 P0_2 ADC/PWM/DIO 1 // // P0 P0_3 ADC/PWM/DIO 0 // // P16 P_16 DIO // // P14 P0_25 SPI MIS/DIO // // P5 P_17 Button A/DI pullup // // P11 P_26 Button B/DI pullup // ///////////////////////////////////////////////////// ///////////////// not mapped yet //////////////////// // P20 I2C SDA/DIO pullup // // P19 I2C SCL/DIO pullup // // P15 SPI MOS/DIO // // P13 SPI SCK/DIO // // P16 // // P8 // ///////////////////////////////////////////////////// // LED Matrix pins are not mapped // ///////////////////////////////////////////////////// BLEDevice ble; UARTService *uart; AnalogIn ain(P0_3); DigitalOut dout(P0_16); DigitalIn din(P0_17); int dVal, dec, i,j; float val,exBatt,f; char buffer[10]; /////////////////// functions /////////////////////// void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { ble.startAdvertising(); } void periodicCallback(void) { BLEprintf("\n"); BLEprintf("Battery Volt: "); BLEprintf(buffer); // BLEprintf("\r\n"); } /////////////////////////////////////////////// ///////////////// MAIN FUNC /////////////////// /////////////////////////////////////////////// int main(void) { Ticker ticker; ticker.attach(periodicCallback, 2); ble.init(); ble.onDisconnection(disconnectionCallback); uart = new UARTService(ble); ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"uBit BLE", sizeof("uBit BLE") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. // ble.startAdvertising(); while (true) { ble.waitForEvent(); dout = din.read(); val = ain.read_u16(); // 100k+22k voltage divider // 3.3 Vcc board voltage exBatt = (122/22)*3.6*val/1023.0; // ext batt volt //exBatt =12.37; dVal = exBatt; dec = (int)(exBatt * 100) % 100; if(exBatt>1 && exBatt<10){i=0;} if(exBatt>=10 && exBatt<100){i=1;} j=i; memset(buffer, 0, 10); while (dVal > 0) { buffer[i] = (dVal % 10) + '0'; dVal /= 10; i--; } buffer[j+1] = '.'; buffer[j+2] = (dec / 10) + '0'; buffer[j+3] = (dec % 10) + '0'; } ///////////// end of while(1) ////////////// } ///////////// end of main /////////////////