ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Revision:
37:4ce71fa47fc3
Parent:
34:7da766a8aa96
Child:
38:115875b8cb6c
--- a/HIDController_BLE.cpp	Thu Aug 25 15:33:42 2016 +0000
+++ b/HIDController_BLE.cpp	Fri Aug 26 00:22:05 2016 +0000
@@ -34,48 +34,6 @@
 
 static volatile Status_t controllerStatus;
 
-static void updateBatteryLevel() {
-	if (!batteryService) return;
-	static const float BATTERY_MAX = 2.4;
-	static const float REFERNECE = 1.2;
-	static const float PRESCALE = 3;
-
-	NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
-
-	// Use internal 1.2V reference for batteryInput
-	//	1/3 pre-scaled input and 1.2V internal band gap reference
-	// ref. mbed-src/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/analogin_api.c
-	NRF_ADC->CONFIG =
-		(ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
-		// Use VDD 1/3 for input
-		(ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
-		// Use internal band gap for reference
-		(ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
-		(ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
-
-	// Start ADC
-	NRF_ADC->TASKS_START = 1;
-	while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {
-		// busy loop
-	}
-
-	// Read ADC result
-	uint16_t raw10bit = static_cast<uint16_t>(NRF_ADC->RESULT);
-	
-	NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled;
-	
-	float ratio = raw10bit / static_cast<float>(1<<10);
-
-	float batteryVoltage = ratio * (REFERNECE * PRESCALE);
-	float percentage = (batteryVoltage / BATTERY_MAX) * 100;
-	if (percentage > 100) {
-		percentage = 100;
-	}
-	printf("updateBatteryLevel %f V : %d/100\r\n", batteryVoltage, static_cast<uint8_t>(percentage));
-	batteryService->updateBatteryLevel(static_cast<uint8_t>(percentage));
-}
-
-
 static void onConnect(const Gap::ConnectionCallbackParams_t *params) {
     peerAddress.type = params->peerAddrType;
     memcpy(peerAddress.address, params->peerAddr, Gap::ADDR_LEN);
@@ -178,13 +136,11 @@
 	deviceInformationService = new DeviceInformationService(ble, "lowreal.net", MODEL_NAME, SERIAL_NUMBER, HARDWARE_REVISION, FIRMWARE_REVISION, SOFTWARE_REVISION);
 	// printf("new BatteryService\r\n");
 	batteryService = new BatteryService(ble, 100);
-	/** TODO STUCK with BLE NANO
+	/** TODO
 	printf("new DFUService\r\n");
 	dfuService = new DFUService(ble);
 	*/
 	
-	updateBatteryLevel();
-
 	//printf("setup connection params\r\n");
 
     ble.gap().setPreferredConnectionParams(&connectionParams);
@@ -314,3 +270,9 @@
 		keyboardService->queueCurrentReportData();
 	}
 }
+
+void HIDController::updateBatteryLevel(uint8_t percentage) {
+	if (!batteryService) return;
+	batteryService->updateBatteryLevel(percentage);
+}
+