SAADC battery meter.

Dependencies:   aconno_bsp adc52832_common

Committer:
Dautor
Date:
Wed Aug 23 09:28:50 2017 +0000
Revision:
4:6f461a82c625
Parent:
2:f25e171b8bc1
Small change

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 0:b5d49f70dcb3 1 /*
jurica238814 0:b5d49f70dcb3 2 * aconno.de
jurica238814 0:b5d49f70dcb3 3 * Made by Jurica Resetar
jurica238814 0:b5d49f70dcb3 4 * All right reserved
jurica238814 0:b5d49f70dcb3 5 *
jurica238814 0:b5d49f70dcb3 6 */
jurica238814 0:b5d49f70dcb3 7
jurica238814 0:b5d49f70dcb3 8 #include "stdio.h"
jurica238814 0:b5d49f70dcb3 9 #include "mbed.h"
jurica238814 0:b5d49f70dcb3 10 #include "ble/BLE.h"
jurica238814 0:b5d49f70dcb3 11 #include "acd52832_bsp.h"
jurica238814 0:b5d49f70dcb3 12 #include "GapAdvertisingData.h"
jurica238814 0:b5d49f70dcb3 13 #include "nrf52_uart.h"
jurica238814 0:b5d49f70dcb3 14
jurica238814 0:b5d49f70dcb3 15 #define SLEEP_TIME (2.0) /* Sleep time in seconds */
jurica238814 0:b5d49f70dcb3 16 #define WAKE_UP_TIME (2000) /* Awake time in ms */
jurica238814 0:b5d49f70dcb3 17 #define MSD_SIZE (1) /* Manufacturer Specific Data lenght (in B) */
jurica238814 0:b5d49f70dcb3 18 #define ADV_INTERVAL (1000) /* Advertising interval in ms */
jurica238814 0:b5d49f70dcb3 19 #define TX_POWER (4) /* TX power (in dB) */
jurica238814 0:b5d49f70dcb3 20 #define GO_TO_SLEEP (0) /* Sleep flag: 0 -> Device will not go to sleep, 1 -> Will go to sleep mode */
jurica238814 0:b5d49f70dcb3 21
Dautor 2:f25e171b8bc1 22 #define TX (p27)
Dautor 2:f25e171b8bc1 23 #define RX (p31)
jurica238814 0:b5d49f70dcb3 24
jurica238814 0:b5d49f70dcb3 25 bool SLEEP = true;
jurica238814 0:b5d49f70dcb3 26
jurica238814 0:b5d49f70dcb3 27 BLE &ble = BLE::Instance();
jurica238814 0:b5d49f70dcb3 28 GapAdvertisingData adv_data = GapAdvertisingData();
jurica238814 0:b5d49f70dcb3 29 uint8_t MSD[MSD_SIZE] = {0x00};
jurica238814 0:b5d49f70dcb3 30
jurica238814 0:b5d49f70dcb3 31 /**
jurica238814 0:b5d49f70dcb3 32 * Restart Advertising on disconnection
jurica238814 0:b5d49f70dcb3 33 */
jurica238814 0:b5d49f70dcb3 34 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
jurica238814 0:b5d49f70dcb3 35 BLE::Instance().gap().startAdvertising();
jurica238814 0:b5d49f70dcb3 36 }
jurica238814 0:b5d49f70dcb3 37
jurica238814 0:b5d49f70dcb3 38 /**
jurica238814 0:b5d49f70dcb3 39 * Function for waking the core up
jurica238814 0:b5d49f70dcb3 40 */
jurica238814 0:b5d49f70dcb3 41 void wakeMeUp(void){
Dautor 2:f25e171b8bc1 42 SLEEP = false;
Dautor 2:f25e171b8bc1 43 }
jurica238814 0:b5d49f70dcb3 44
jurica238814 0:b5d49f70dcb3 45 /**
jurica238814 0:b5d49f70dcb3 46 * This function is called when the ble initialization process has failed
jurica238814 0:b5d49f70dcb3 47 */
jurica238814 0:b5d49f70dcb3 48 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:b5d49f70dcb3 49 /* Avoid compiler warnings */
jurica238814 0:b5d49f70dcb3 50 (void) ble;
jurica238814 0:b5d49f70dcb3 51 (void) error;
jurica238814 0:b5d49f70dcb3 52 /* Initialization error handling should go here */
jurica238814 0:b5d49f70dcb3 53 }
jurica238814 0:b5d49f70dcb3 54
jurica238814 0:b5d49f70dcb3 55 /**
jurica238814 0:b5d49f70dcb3 56 * Callback triggered when the ble initialization process has finished
jurica238814 0:b5d49f70dcb3 57 */
jurica238814 0:b5d49f70dcb3 58 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:b5d49f70dcb3 59 BLE& ble = params->ble;
jurica238814 0:b5d49f70dcb3 60 ble_error_t error = params->error;
jurica238814 0:b5d49f70dcb3 61
jurica238814 0:b5d49f70dcb3 62 if (error != BLE_ERROR_NONE) {
jurica238814 0:b5d49f70dcb3 63 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:b5d49f70dcb3 64 onBleInitError(ble, error);
jurica238814 0:b5d49f70dcb3 65 return;
jurica238814 0:b5d49f70dcb3 66 }
jurica238814 0:b5d49f70dcb3 67
jurica238814 0:b5d49f70dcb3 68 /* Ensure that it is the default instance of BLE */
jurica238814 0:b5d49f70dcb3 69 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:b5d49f70dcb3 70 return;
jurica238814 0:b5d49f70dcb3 71 }
jurica238814 0:b5d49f70dcb3 72
jurica238814 0:b5d49f70dcb3 73 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 0:b5d49f70dcb3 74
jurica238814 0:b5d49f70dcb3 75 /* setup advertising */
jurica238814 0:b5d49f70dcb3 76 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 0:b5d49f70dcb3 77 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
jurica238814 0:b5d49f70dcb3 78 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
jurica238814 0:b5d49f70dcb3 79 ble.gap().setAdvertisingInterval(ADV_INTERVAL);
jurica238814 0:b5d49f70dcb3 80 ble.gap().startAdvertising();
jurica238814 0:b5d49f70dcb3 81 }
jurica238814 0:b5d49f70dcb3 82
jurica238814 0:b5d49f70dcb3 83 void updateData(){
jurica238814 0:b5d49f70dcb3 84 static uint8_t simple_counter = 0;
jurica238814 0:b5d49f70dcb3 85
jurica238814 0:b5d49f70dcb3 86 MSD[0] = simple_counter++;
jurica238814 0:b5d49f70dcb3 87 adv_data = ble.getAdvertisingData();
jurica238814 0:b5d49f70dcb3 88 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, MSD, MSD_SIZE);
jurica238814 0:b5d49f70dcb3 89 ble.setAdvertisingData(adv_data);
jurica238814 0:b5d49f70dcb3 90 }
jurica238814 0:b5d49f70dcb3 91
Dautor 2:f25e171b8bc1 92 char buffer[255] = {};
Dautor 2:f25e171b8bc1 93 NRF52_UART serial = NRF52_UART(TX, RX, Baud9600);
Dautor 2:f25e171b8bc1 94 #include "acd_nrf52_saadc.h"
jurica238814 0:b5d49f70dcb3 95
jurica238814 0:b5d49f70dcb3 96 int main(void){
jurica238814 0:b5d49f70dcb3 97 uint8_t buffer_s;
jurica238814 0:b5d49f70dcb3 98 int printBufferLen;
jurica238814 0:b5d49f70dcb3 99
jurica238814 0:b5d49f70dcb3 100 Ticker ticker;
jurica238814 0:b5d49f70dcb3 101 ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up
jurica238814 0:b5d49f70dcb3 102
jurica238814 0:b5d49f70dcb3 103 ble.init(bleInitComplete);
jurica238814 0:b5d49f70dcb3 104 ble.gap().setTxPower(TX_POWER); // Set TX power to TX_POWER
jurica238814 0:b5d49f70dcb3 105
jurica238814 0:b5d49f70dcb3 106 /* SpinWait for initialization to complete. This is necessary because the
jurica238814 0:b5d49f70dcb3 107 * BLE object is used in the main loop below. */
Dautor 2:f25e171b8bc1 108 while(ble.hasInitialized() == false) { /* spin loop */ }
Dautor 2:f25e171b8bc1 109
Dautor 2:f25e171b8bc1 110 NRF52_SAADC analogIn;
Dautor 2:f25e171b8bc1 111 analogIn.addChannel(9); // vdd
Dautor 2:f25e171b8bc1 112 analogIn.addChannel(5); // potentiometer
Dautor 2:f25e171b8bc1 113 analogIn.calibrate(); // nije previse bitno
jurica238814 0:b5d49f70dcb3 114 while(1){
jurica238814 0:b5d49f70dcb3 115 buffer_s = 0x0C;
jurica238814 0:b5d49f70dcb3 116 serial.send(&buffer_s, sizeof(buffer_s));
Dautor 2:f25e171b8bc1 117 analogIn.updateData();
jurica238814 0:b5d49f70dcb3 118
Dautor 2:f25e171b8bc1 119 printBufferLen = sprintf(buffer, "VDD voltage: %f\r\n", (analogIn.getData()[0])*(1.0/1024)*3.6);
jurica238814 0:b5d49f70dcb3 120 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 121
Dautor 2:f25e171b8bc1 122 printBufferLen = sprintf(buffer, "POTENTIOMETER voltage: %f\r\n", (analogIn.getData()[1])*(1.0/1024)*3.6);
jurica238814 0:b5d49f70dcb3 123 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 124 wait_ms(3000);
jurica238814 0:b5d49f70dcb3 125 }
jurica238814 0:b5d49f70dcb3 126
Dautor 2:f25e171b8bc1 127 while(true){
jurica238814 0:b5d49f70dcb3 128 if (SLEEP && GO_TO_SLEEP){
jurica238814 0:b5d49f70dcb3 129 ble.gap().stopAdvertising();
jurica238814 0:b5d49f70dcb3 130 sleep();
jurica238814 0:b5d49f70dcb3 131 ble.waitForEvent();
jurica238814 0:b5d49f70dcb3 132 }
jurica238814 0:b5d49f70dcb3 133 else{
jurica238814 0:b5d49f70dcb3 134 // I'm awake
jurica238814 0:b5d49f70dcb3 135 updateData();
jurica238814 0:b5d49f70dcb3 136 ble.gap().startAdvertising();
jurica238814 0:b5d49f70dcb3 137 wait_ms(WAKE_UP_TIME);
jurica238814 0:b5d49f70dcb3 138 SLEEP = true;
Dautor 2:f25e171b8bc1 139 }
jurica238814 0:b5d49f70dcb3 140 }
jurica238814 0:b5d49f70dcb3 141 }