Merge remote-tracking branch 'upstream/develop' into pinetimestyle-colorpicker
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
#include "BatteryController.h"
|
||||
#include <hal/nrf_gpio.h>
|
||||
#include <nrfx_saadc.h>
|
||||
#include <libraries/log/nrf_log.h>
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
using namespace Pinetime::Controllers;
|
||||
|
||||
@@ -18,7 +16,6 @@ void Battery::Init() {
|
||||
}
|
||||
|
||||
void Battery::Update() {
|
||||
|
||||
isCharging = !nrf_gpio_pin_read(chargingPin);
|
||||
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
|
||||
|
||||
@@ -33,13 +30,13 @@ void Battery::Update() {
|
||||
nrfx_saadc_sample();
|
||||
}
|
||||
|
||||
void Battery::adcCallbackStatic(nrfx_saadc_evt_t const* event) {
|
||||
void Battery::AdcCallbackStatic(nrfx_saadc_evt_t const* event) {
|
||||
instance->SaadcEventHandler(event);
|
||||
}
|
||||
|
||||
void Battery::SaadcInit() {
|
||||
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
|
||||
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic));
|
||||
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, AdcCallbackStatic));
|
||||
|
||||
nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
|
||||
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
|
||||
@@ -55,7 +52,6 @@ void Battery::SaadcInit() {
|
||||
}
|
||||
|
||||
void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
|
||||
|
||||
const uint16_t battery_max = 4180; // maximum voltage of battery ( max charging voltage is 4.21 )
|
||||
const uint16_t battery_min = 3200; // minimum voltage of battery before shutdown ( depends on the battery )
|
||||
|
||||
@@ -69,13 +65,10 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
|
||||
// reference_voltage is 0.6V
|
||||
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
|
||||
voltage = p_event->data.done.p_buffer[0] * 6000 / 1024;
|
||||
|
||||
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
|
||||
|
||||
percentRemaining = std::max(percentRemaining, 0);
|
||||
percentRemaining = std::min(percentRemaining, 100);
|
||||
|
||||
percentRemainingBuffer.insert(percentRemaining);
|
||||
percentRemainingBuffer.Insert(percentRemaining);
|
||||
|
||||
samples++;
|
||||
if (samples > percentRemainingSamples) {
|
||||
|
@@ -19,7 +19,7 @@ namespace Pinetime {
|
||||
insert member function overwrites the next data to the current
|
||||
HEAD and moves the HEAD to the newly inserted value.
|
||||
*/
|
||||
void insert(const int num) {
|
||||
void Insert(const uint8_t num) {
|
||||
head %= cap;
|
||||
arr[head++] = num;
|
||||
if (sz != cap) {
|
||||
@@ -27,13 +27,13 @@ namespace Pinetime {
|
||||
}
|
||||
}
|
||||
|
||||
int GetAverage() const {
|
||||
uint8_t GetAverage() const {
|
||||
int sum = std::accumulate(arr.begin(), arr.end(), 0);
|
||||
return (sum / sz);
|
||||
return static_cast<uint8_t>(sum / sz);
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<int, N> arr; /**< internal array used to store the values*/
|
||||
std::array<uint8_t, N> arr; /**< internal array used to store the values*/
|
||||
uint8_t sz; /**< The current size of the array.*/
|
||||
uint8_t cap; /**< Total capacity of the CircBuffer.*/
|
||||
uint8_t head; /**< The current head of the CircBuffer*/
|
||||
@@ -46,8 +46,11 @@ namespace Pinetime {
|
||||
void Init();
|
||||
void Update();
|
||||
|
||||
int PercentRemaining() const {
|
||||
return percentRemainingBuffer.GetAverage();
|
||||
uint8_t PercentRemaining() const {
|
||||
auto avg = percentRemainingBuffer.GetAverage();
|
||||
avg = std::min(avg, static_cast<uint8_t>(100));
|
||||
avg = std::max(avg, static_cast<uint8_t>(0));
|
||||
return avg;
|
||||
}
|
||||
|
||||
uint16_t Voltage() const {
|
||||
@@ -57,6 +60,7 @@ namespace Pinetime {
|
||||
bool IsCharging() const {
|
||||
return isCharging;
|
||||
}
|
||||
|
||||
bool IsPowerPresent() const {
|
||||
return isPowerPresent;
|
||||
}
|
||||
@@ -80,7 +84,7 @@ namespace Pinetime {
|
||||
void SaadcInit();
|
||||
|
||||
void SaadcEventHandler(nrfx_saadc_evt_t const* p_event);
|
||||
static void adcCallbackStatic(nrfx_saadc_evt_t const* event);
|
||||
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
|
||||
|
||||
bool isReading = false;
|
||||
uint8_t samples = 0;
|
||||
|
@@ -17,7 +17,7 @@ BatteryInformationService::BatteryInformationService(Controllers::Battery& batte
|
||||
characteristicDefinition {{.uuid = (ble_uuid_t*) &batteryLevelUuid,
|
||||
.access_cb = BatteryInformationServiceCallback,
|
||||
.arg = this,
|
||||
.flags = BLE_GATT_CHR_F_READ,
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
||||
.val_handle = &batteryLevelHandle},
|
||||
{0}},
|
||||
serviceDefinition {
|
||||
@@ -48,4 +48,8 @@ int BatteryInformationService::OnBatteryServiceRequested(uint16_t connectionHand
|
||||
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
void BatteryInformationService::NotifyBatteryLevel(uint16_t connectionHandle, uint8_t level) {
|
||||
auto* om = ble_hs_mbuf_from_flat(&level, 1);
|
||||
ble_gattc_notify_custom(connectionHandle, batteryLevelHandle, om);
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ namespace Pinetime {
|
||||
void Init();
|
||||
|
||||
int OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
|
||||
|
||||
void NotifyBatteryLevel(uint16_t connectionHandle, uint8_t level);
|
||||
private:
|
||||
Controllers::Battery& batteryController;
|
||||
static constexpr uint16_t batteryInformationServiceId {0x180F};
|
||||
|
@@ -235,3 +235,9 @@ void NimbleController::StartDiscovery() {
|
||||
uint16_t NimbleController::connHandle() {
|
||||
return connectionHandle;
|
||||
}
|
||||
|
||||
void NimbleController::NotifyBatteryLevel(uint8_t level) {
|
||||
if(connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
|
||||
batteryInformationService.NotifyBatteryLevel(connectionHandle, level);
|
||||
}
|
||||
}
|
||||
|
@@ -70,6 +70,7 @@ namespace Pinetime {
|
||||
};
|
||||
|
||||
uint16_t connHandle();
|
||||
void NotifyBatteryLevel(uint8_t level);
|
||||
|
||||
private:
|
||||
static constexpr const char* deviceName = "InfiniTime";
|
||||
@@ -92,7 +93,7 @@ namespace Pinetime {
|
||||
HeartRateService heartRateService;
|
||||
|
||||
uint8_t addrType; // 1 = Random, 0 = PUBLIC
|
||||
uint16_t connectionHandle = 0;
|
||||
uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE;
|
||||
|
||||
ble_uuid128_t dfuServiceUuid {
|
||||
.u {.type = BLE_UUID_TYPE_128},
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <bitset>
|
||||
#include "components/datetime/DateTimeController.h"
|
||||
#include "components/brightness/BrightnessController.h"
|
||||
#include "components/fs/FS.h"
|
||||
@@ -11,7 +12,11 @@ namespace Pinetime {
|
||||
public:
|
||||
enum class ClockType : uint8_t { H24, H12 };
|
||||
enum class Vibration : uint8_t { ON, OFF };
|
||||
enum class WakeUpMode : uint8_t { None, SingleTap, DoubleTap, RaiseWrist };
|
||||
enum class WakeUpMode : uint8_t {
|
||||
SingleTap = 0,
|
||||
DoubleTap = 1,
|
||||
RaiseWrist = 2,
|
||||
};
|
||||
|
||||
Settings(Pinetime::Controllers::FS& fs);
|
||||
|
||||
@@ -99,15 +104,33 @@ namespace Pinetime {
|
||||
return settings.screenTimeOut;
|
||||
};
|
||||
|
||||
void setWakeUpMode(WakeUpMode wakeUp) {
|
||||
if (wakeUp != settings.wakeUpMode) {
|
||||
void setWakeUpMode(WakeUpMode wakeUp, bool enabled) {
|
||||
if (!isWakeUpModeOn(wakeUp)) {
|
||||
settingsChanged = true;
|
||||
}
|
||||
settings.wakeUpMode = wakeUp;
|
||||
settings.wakeUpMode.set(static_cast<size_t>(wakeUp), enabled);
|
||||
// Handle special behavior
|
||||
if (enabled) {
|
||||
switch (wakeUp) {
|
||||
case WakeUpMode::SingleTap:
|
||||
settings.wakeUpMode.set(static_cast<size_t>(WakeUpMode::DoubleTap), false);
|
||||
break;
|
||||
case WakeUpMode::DoubleTap:
|
||||
settings.wakeUpMode.set(static_cast<size_t>(WakeUpMode::SingleTap), false);
|
||||
break;
|
||||
case WakeUpMode::RaiseWrist:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
WakeUpMode getWakeUpMode() const {
|
||||
|
||||
std::bitset<3> getWakeUpModes() const {
|
||||
return settings.wakeUpMode;
|
||||
};
|
||||
}
|
||||
|
||||
bool isWakeUpModeOn(const WakeUpMode mode) const {
|
||||
return getWakeUpModes()[static_cast<size_t>(mode)];
|
||||
}
|
||||
|
||||
void SetBrightness(Controllers::BrightnessController::Levels level) {
|
||||
if (level != settings.brightLevel) {
|
||||
@@ -147,7 +170,7 @@ namespace Pinetime {
|
||||
uint8_t PTSColorBar = 11;
|
||||
uint8_t PTSColorBG = 3;
|
||||
|
||||
WakeUpMode wakeUpMode = WakeUpMode::None;
|
||||
std::bitset<3> wakeUpMode {0};
|
||||
|
||||
Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium;
|
||||
};
|
||||
@@ -162,4 +185,4 @@ namespace Pinetime {
|
||||
void SaveSettingsToFile();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user