ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Thu Sep 15 09:31:05 2016 +0900
Revision:
86:e0fab77e669d
Parent:
54:899fc2b0a76b
support consumer keys

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cho45 42:2c3be8694896 1 class BatteryLevel {
cho45 42:2c3be8694896 2
cho45 42:2c3be8694896 3 public:
cho45 54:899fc2b0a76b 4 static const float BATTERY_MAX = 2.8;
cho45 44:916b70fd1c40 5 static const float REFERNECE = 1.2;
cho45 44:916b70fd1c40 6 static const float PRESCALE = 3;
cho45 44:916b70fd1c40 7 static const float BATTERY_LOW = 2.0;
cho45 44:916b70fd1c40 8
cho45 48:d6938de02f62 9 static uint8_t readBatteryPercentage(const float voltage) {
cho45 44:916b70fd1c40 10 uint16_t percentage = (voltage - BATTERY_LOW) / (BATTERY_MAX - BATTERY_LOW) * 100;
cho45 44:916b70fd1c40 11 if (percentage > 100) percentage = 100;
cho45 44:916b70fd1c40 12 return percentage;
cho45 44:916b70fd1c40 13 }
cho45 44:916b70fd1c40 14
cho45 44:916b70fd1c40 15 static float readBatteryVoltage() {
cho45 44:916b70fd1c40 16 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
cho45 44:916b70fd1c40 17
cho45 44:916b70fd1c40 18 // Use internal 1.2V reference for batteryInput
cho45 44:916b70fd1c40 19 // 1/3 pre-scaled input and 1.2V internal band gap reference
cho45 44:916b70fd1c40 20 // ref. mbed-src/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/analogin_api.c
cho45 44:916b70fd1c40 21 NRF_ADC->CONFIG =
cho45 44:916b70fd1c40 22 (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
cho45 44:916b70fd1c40 23 // Use VDD 1/3 for input
cho45 44:916b70fd1c40 24 (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
cho45 44:916b70fd1c40 25 // Use internal band gap for reference
cho45 44:916b70fd1c40 26 (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
cho45 44:916b70fd1c40 27 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
cho45 44:916b70fd1c40 28
cho45 44:916b70fd1c40 29 // Start ADC
cho45 44:916b70fd1c40 30 NRF_ADC->TASKS_START = 1;
cho45 44:916b70fd1c40 31 while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {
cho45 44:916b70fd1c40 32 // busy loop
cho45 44:916b70fd1c40 33 }
cho45 44:916b70fd1c40 34
cho45 44:916b70fd1c40 35 // Read ADC result
cho45 44:916b70fd1c40 36 uint16_t raw10bit = static_cast<uint16_t>(NRF_ADC->RESULT);
cho45 44:916b70fd1c40 37
cho45 44:916b70fd1c40 38 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled;
cho45 44:916b70fd1c40 39
cho45 44:916b70fd1c40 40 float ratio = raw10bit / static_cast<float>(1<<10);
cho45 44:916b70fd1c40 41
cho45 44:916b70fd1c40 42 float batteryVoltage = ratio * (REFERNECE * PRESCALE);
cho45 44:916b70fd1c40 43 return batteryVoltage;
cho45 44:916b70fd1c40 44 }
cho45 44:916b70fd1c40 45 };