Merge branch 'batteryInformationService' of JF/PineTime into develop
This commit is contained in:
		
						commit
						efd0c9eff7
					
				@ -362,6 +362,7 @@ list(APPEND SOURCE_FILES
 | 
			
		||||
  Components/Ble/CurrentTimeService.cpp
 | 
			
		||||
  Components/Ble/AlertNotificationService.cpp
 | 
			
		||||
  Components/Ble/MusicService.cpp
 | 
			
		||||
  Components/Ble/BatteryInformationService.cpp
 | 
			
		||||
  Components/FirmwareValidator/FirmwareValidator.cpp
 | 
			
		||||
  drivers/Cst816s.cpp
 | 
			
		||||
  FreeRTOS/port.c
 | 
			
		||||
@ -435,6 +436,7 @@ set(INCLUDE_FILES
 | 
			
		||||
  Components/Ble/AlertNotificationClient.h
 | 
			
		||||
  Components/Ble/DfuService.h
 | 
			
		||||
  Components/FirmwareValidator/FirmwareValidator.h
 | 
			
		||||
  Components/Ble/BatteryInformationService.h
 | 
			
		||||
  drivers/Cst816s.h
 | 
			
		||||
  FreeRTOS/portmacro.h
 | 
			
		||||
  FreeRTOS/portmacro_cmsis.h
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
#include <drivers/include/nrfx_saadc.h>
 | 
			
		||||
#include <hal/nrf_gpio.h>
 | 
			
		||||
#include <libraries/log/nrf_log.h>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include "BatteryController.h"
 | 
			
		||||
 | 
			
		||||
using namespace Pinetime::Controllers;
 | 
			
		||||
@ -34,7 +35,9 @@ void Battery::Update() {
 | 
			
		||||
 | 
			
		||||
  // see https://forum.pine64.org/showthread.php?tid=8147
 | 
			
		||||
  voltage = (value * 2.0f) / (1024/3.0f);
 | 
			
		||||
  percentRemaining = ((voltage - 3.55)*100)*3.9;
 | 
			
		||||
  percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f;
 | 
			
		||||
  percentRemaining = std::max(percentRemaining, 0.0f);
 | 
			
		||||
  percentRemaining = std::min(percentRemaining, 100.0f);
 | 
			
		||||
 | 
			
		||||
//  NRF_LOG_INFO("BATTERY " NRF_LOG_FLOAT_MARKER " %% - " NRF_LOG_FLOAT_MARKER " v", NRF_LOG_FLOAT(percentRemaining), NRF_LOG_FLOAT(voltage));
 | 
			
		||||
//  NRF_LOG_INFO("POWER Charging : %d - Power : %d", isCharging, isPowerPresent);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										62
									
								
								src/Components/Ble/BatteryInformationService.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								src/Components/Ble/BatteryInformationService.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,62 @@
 | 
			
		||||
#include "BatteryInformationService.h"
 | 
			
		||||
#include "../Battery/BatteryController.h"
 | 
			
		||||
 | 
			
		||||
using namespace Pinetime::Controllers;
 | 
			
		||||
 | 
			
		||||
constexpr ble_uuid16_t BatteryInformationService::batteryInformationServiceUuid;
 | 
			
		||||
constexpr ble_uuid16_t BatteryInformationService::batteryLevelUuid;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int BatteryInformationServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
 | 
			
		||||
  auto* batteryInformationService = static_cast<BatteryInformationService*>(arg);
 | 
			
		||||
  return batteryInformationService->OnBatteryServiceRequested(conn_handle, attr_handle, ctxt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BatteryInformationService::BatteryInformationService(Controllers::Battery& batteryController) :
 | 
			
		||||
        batteryController{batteryController},
 | 
			
		||||
        characteristicDefinition{
 | 
			
		||||
                {
 | 
			
		||||
                        .uuid = (ble_uuid_t *) &batteryLevelUuid,
 | 
			
		||||
                        .access_cb = BatteryInformationServiceCallback,
 | 
			
		||||
                        .arg = this,
 | 
			
		||||
                        .flags = BLE_GATT_CHR_F_READ,
 | 
			
		||||
                        .val_handle = &batteryLevelHandle
 | 
			
		||||
                },
 | 
			
		||||
                {
 | 
			
		||||
                        0
 | 
			
		||||
                }
 | 
			
		||||
        },
 | 
			
		||||
        serviceDefinition{
 | 
			
		||||
                {
 | 
			
		||||
                        /* Device Information Service */
 | 
			
		||||
                        .type = BLE_GATT_SVC_TYPE_PRIMARY,
 | 
			
		||||
                        .uuid = (ble_uuid_t *) &batteryInformationServiceUuid,
 | 
			
		||||
                        .characteristics = characteristicDefinition
 | 
			
		||||
                },
 | 
			
		||||
                {
 | 
			
		||||
                        0
 | 
			
		||||
                },
 | 
			
		||||
        }{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BatteryInformationService::Init() {
 | 
			
		||||
  int res = 0;
 | 
			
		||||
  res = ble_gatts_count_cfg(serviceDefinition);
 | 
			
		||||
  ASSERT(res == 0);
 | 
			
		||||
 | 
			
		||||
  res = ble_gatts_add_svcs(serviceDefinition);
 | 
			
		||||
  ASSERT(res == 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int BatteryInformationService::OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle,
 | 
			
		||||
                                                         ble_gatt_access_ctxt *context) {
 | 
			
		||||
  if(attributeHandle == batteryLevelHandle) {
 | 
			
		||||
    NRF_LOG_INFO("BATTERY : handle = %d", batteryLevelHandle);
 | 
			
		||||
    static uint8_t batteryValue = batteryController.PercentRemaining();
 | 
			
		||||
    int res = os_mbuf_append(context->om, &batteryValue, 1);
 | 
			
		||||
    return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
 | 
			
		||||
  }
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										40
									
								
								src/Components/Ble/BatteryInformationService.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								src/Components/Ble/BatteryInformationService.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,40 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <host/ble_gap.h>
 | 
			
		||||
 | 
			
		||||
namespace Pinetime {
 | 
			
		||||
  namespace System {
 | 
			
		||||
    class SystemTask;
 | 
			
		||||
  }
 | 
			
		||||
  namespace Controllers {
 | 
			
		||||
    class Battery;
 | 
			
		||||
    class BatteryInformationService {
 | 
			
		||||
      public:
 | 
			
		||||
        BatteryInformationService(Controllers::Battery& batteryController);
 | 
			
		||||
        void Init();
 | 
			
		||||
 | 
			
		||||
        int
 | 
			
		||||
        OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context);
 | 
			
		||||
 | 
			
		||||
      private:
 | 
			
		||||
        Controllers::Battery& batteryController;
 | 
			
		||||
        static constexpr uint16_t batteryInformationServiceId {0x180F};
 | 
			
		||||
        static constexpr uint16_t batteryLevelId {0x2A19};
 | 
			
		||||
 | 
			
		||||
        static constexpr ble_uuid16_t batteryInformationServiceUuid {
 | 
			
		||||
                .u {.type = BLE_UUID_TYPE_16},
 | 
			
		||||
                .value = batteryInformationServiceId
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        static constexpr ble_uuid16_t batteryLevelUuid {
 | 
			
		||||
                .u {.type = BLE_UUID_TYPE_16},
 | 
			
		||||
                .value = batteryLevelId
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        struct ble_gatt_chr_def characteristicDefinition[3];
 | 
			
		||||
        struct ble_gatt_svc_def serviceDefinition[2];
 | 
			
		||||
 | 
			
		||||
        uint16_t batteryLevelHandle;
 | 
			
		||||
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -26,6 +26,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
 | 
			
		||||
                                   Pinetime::Controllers::Ble& bleController,
 | 
			
		||||
        DateTime& dateTimeController,
 | 
			
		||||
        Pinetime::Controllers::NotificationManager& notificationManager,
 | 
			
		||||
        Controllers::Battery& batteryController,
 | 
			
		||||
        Pinetime::Drivers::SpiNorFlash& spiNorFlash) :
 | 
			
		||||
        systemTask{systemTask},
 | 
			
		||||
        bleController{bleController},
 | 
			
		||||
@ -37,7 +38,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
 | 
			
		||||
        anService{systemTask, notificationManager},
 | 
			
		||||
        alertNotificationClient{systemTask, notificationManager},
 | 
			
		||||
        currentTimeService{dateTimeController},
 | 
			
		||||
        musicService{systemTask} {
 | 
			
		||||
        musicService{systemTask},
 | 
			
		||||
        batteryInformationService{batteryController} {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -83,10 +85,9 @@ void NimbleController::Init() {
 | 
			
		||||
  currentTimeClient.Init();
 | 
			
		||||
  currentTimeService.Init();
 | 
			
		||||
  musicService.Init();
 | 
			
		||||
 | 
			
		||||
  anService.Init();
 | 
			
		||||
 | 
			
		||||
  dfuService.Init();
 | 
			
		||||
  batteryInformationService.Init();
 | 
			
		||||
  int res;
 | 
			
		||||
  res = ble_hs_util_ensure_addr(0);
 | 
			
		||||
  ASSERT(res == 0);
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,7 @@
 | 
			
		||||
#include "DfuService.h"
 | 
			
		||||
#include "CurrentTimeService.h"
 | 
			
		||||
#include "MusicService.h"
 | 
			
		||||
#include "BatteryInformationService.h"
 | 
			
		||||
#include <host/ble_gap.h>
 | 
			
		||||
 | 
			
		||||
namespace Pinetime {
 | 
			
		||||
@ -22,7 +23,7 @@ namespace Pinetime {
 | 
			
		||||
      public:
 | 
			
		||||
        NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController,
 | 
			
		||||
                DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager,
 | 
			
		||||
                Pinetime::Drivers::SpiNorFlash& spiNorFlash);
 | 
			
		||||
                Controllers::Battery& batteryController, Pinetime::Drivers::SpiNorFlash& spiNorFlash);
 | 
			
		||||
        void Init();
 | 
			
		||||
        void StartAdvertising();
 | 
			
		||||
        int OnGAPEvent(ble_gap_event *event);
 | 
			
		||||
@ -57,6 +58,7 @@ namespace Pinetime {
 | 
			
		||||
        AlertNotificationClient alertNotificationClient;
 | 
			
		||||
        CurrentTimeService currentTimeService;
 | 
			
		||||
        MusicService musicService;
 | 
			
		||||
        BatteryInformationService batteryInformationService;
 | 
			
		||||
 | 
			
		||||
        uint8_t addrType; // 1 = Random, 0 = PUBLIC
 | 
			
		||||
        uint16_t connectionHandle = 0;
 | 
			
		||||
 | 
			
		||||
@ -35,7 +35,7 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,
 | 
			
		||||
                       twiMaster{twiMaster}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController},
 | 
			
		||||
                       bleController{bleController}, dateTimeController{dateTimeController},
 | 
			
		||||
                       watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager},
 | 
			
		||||
                       nimbleController(*this, bleController,dateTimeController, notificationManager, spiNorFlash) {
 | 
			
		||||
                       nimbleController(*this, bleController,dateTimeController, notificationManager, batteryController, spiNorFlash) {
 | 
			
		||||
  systemTaksMsgQueue = xQueueCreate(10, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user