summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
Diffstat (limited to 'src/components')
-rw-r--r--src/components/battery/BatteryController.cpp16
-rw-r--r--src/components/battery/BatteryController.h9
-rw-r--r--src/components/ble/NimbleController.cpp125
-rw-r--r--src/components/ble/NimbleController.h7
-rw-r--r--src/components/brightness/BrightnessController.cpp32
-rw-r--r--src/components/brightness/BrightnessController.h3
-rw-r--r--src/components/motor/MotorController.cpp11
-rw-r--r--src/components/motor/MotorController.h2
-rw-r--r--src/components/settings/Settings.h2
9 files changed, 83 insertions, 124 deletions
diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp
index 4ef20a24..f8a64ecd 100644
--- a/src/components/battery/BatteryController.cpp
+++ b/src/components/battery/BatteryController.cpp
@@ -1,5 +1,4 @@
#include "BatteryController.h"
-#include "drivers/PinMap.h"
#include <hal/nrf_gpio.h>
#include <nrfx_saadc.h>
#include <algorithm>
@@ -10,12 +9,15 @@ Battery* Battery::instance = nullptr;
Battery::Battery() {
instance = this;
- nrf_gpio_cfg_input(PinMap::Charging, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled);
+}
+
+void Battery::Init() {
+ nrf_gpio_cfg_input(chargingPin, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Pullup);
}
void Battery::Update() {
- isCharging = !nrf_gpio_pin_read(PinMap::Charging);
- isPowerPresent = !nrf_gpio_pin_read(PinMap::PowerPresent);
+ isCharging = !nrf_gpio_pin_read(chargingPin);
+ isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
if (isReading) {
return;
@@ -73,11 +75,5 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
nrfx_saadc_uninit();
isReading = false;
-
- systemTask->PushMessage(System::Messages::BatteryMeasurementDone);
}
}
-
-void Battery::Register(Pinetime::System::SystemTask* systemTask) {
- this->systemTask = systemTask;
-}
diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h
index 8af27ea8..6f09b737 100644
--- a/src/components/battery/BatteryController.h
+++ b/src/components/battery/BatteryController.h
@@ -1,7 +1,8 @@
#pragma once
#include <cstdint>
#include <drivers/include/nrfx_saadc.h>
-#include <systemtask/SystemTask.h>
+#include <array>
+#include <numeric>
namespace Pinetime {
namespace Controllers {
@@ -10,8 +11,8 @@ namespace Pinetime {
public:
Battery();
+ void Init();
void Update();
- void Register(System::SystemTask* systemTask);
uint8_t PercentRemaining() const {
return percentRemaining;
@@ -33,6 +34,8 @@ namespace Pinetime {
static Battery* instance;
nrf_saadc_value_t saadc_value;
+ static constexpr uint32_t chargingPin = 12;
+ static constexpr uint32_t powerPresentPin = 19;
static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7;
uint16_t voltage = 0;
uint8_t percentRemaining = 0;
@@ -46,8 +49,6 @@ namespace Pinetime {
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
bool isReading = false;
-
- Pinetime::System::SystemTask* systemTask = nullptr;
};
}
}
diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp
index 879421e7..5eb227bf 100644
--- a/src/components/ble/NimbleController.cpp
+++ b/src/components/ble/NimbleController.cpp
@@ -42,19 +42,6 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
serviceDiscovery({&currentTimeClient, &alertNotificationClient}) {
}
-void nimble_on_reset(int reason) {
- NRF_LOG_INFO("Resetting state; reason=%d\n", reason);
-}
-
-void nimble_on_sync(void) {
- int rc;
-
- rc = ble_hs_util_ensure_addr(0);
- ASSERT(rc == 0);
-
- nptr->StartAdvertising();
-}
-
int GAPEventCallback(struct ble_gap_event* event, void* arg) {
auto nimbleController = static_cast<NimbleController*>(arg);
return nimbleController->OnGAPEvent(event);
@@ -64,10 +51,6 @@ void NimbleController::Init() {
while (!ble_hs_synced()) {
}
- nptr = this;
- ble_hs_cfg.reset_cb = nimble_on_reset;
- ble_hs_cfg.sync_cb = nimble_on_sync;
-
ble_svc_gap_init();
ble_svc_gatt_init();
@@ -81,31 +64,28 @@ void NimbleController::Init() {
batteryInformationService.Init();
immediateAlertService.Init();
heartRateService.Init();
-
- int rc;
- rc = ble_hs_util_ensure_addr(0);
- ASSERT(rc == 0);
- rc = ble_hs_id_infer_auto(0, &addrType);
- ASSERT(rc == 0);
- rc = ble_svc_gap_device_name_set(deviceName);
- ASSERT(rc == 0);
- rc = ble_svc_gap_device_appearance_set(0xC2);
- ASSERT(rc == 0);
+ int res;
+ res = ble_hs_util_ensure_addr(0);
+ ASSERT(res == 0);
+ res = ble_hs_id_infer_auto(0, &addrType);
+ ASSERT(res == 0);
+ res = ble_svc_gap_device_name_set(deviceName);
+ ASSERT(res == 0);
Pinetime::Controllers::Ble::BleAddress address;
- rc = ble_hs_id_copy_addr(addrType, address.data(), nullptr);
- ASSERT(rc == 0);
+ res = ble_hs_id_copy_addr(addrType, address.data(), nullptr);
+ ASSERT(res == 0);
bleController.AddressType((addrType == 0) ? Ble::AddressTypes::Public : Ble::AddressTypes::Random);
bleController.Address(std::move(address));
- rc = ble_gatts_start();
- ASSERT(rc == 0);
-
- if (!ble_gap_adv_active() && !bleController.IsConnected())
- StartAdvertising();
+ res = ble_gatts_start();
+ ASSERT(res == 0);
}
void NimbleController::StartAdvertising() {
- int rc;
+ if (bleController.IsConnected() || ble_gap_conn_active() || ble_gap_adv_active())
+ return;
+
+ ble_svc_gap_device_name_set(deviceName);
/* set adv parameters */
struct ble_gap_adv_params adv_params;
@@ -122,17 +102,11 @@ void NimbleController::StartAdvertising() {
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
- /* fast advertise for 30 sec */
- if (fastAdvCount < 15) {
- adv_params.itvl_min = 32;
- adv_params.itvl_max = 47;
- fastAdvCount++;
- } else {
- adv_params.itvl_min = 1636;
- adv_params.itvl_max = 1651;
- }
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
+ // fields.uuids128 = BLE_UUID128(BLE_UUID128_DECLARE(
+ // 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+ // 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff));
fields.uuids128 = &dfuServiceUuid;
fields.num_uuids128 = 1;
fields.uuids128_is_complete = 1;
@@ -142,25 +116,28 @@ void NimbleController::StartAdvertising() {
rsp_fields.name_len = strlen(deviceName);
rsp_fields.name_is_complete = 1;
- rc = ble_gap_adv_set_fields(&fields);
- ASSERT(rc == 0);
+ ble_gap_adv_set_fields(&fields);
+ // ASSERT(res == 0); // TODO this one sometimes fails with error 22 (notsync)
- rc = ble_gap_adv_rsp_set_fields(&rsp_fields);
- ASSERT(rc == 0);
+ ble_gap_adv_rsp_set_fields(&rsp_fields);
+ // ASSERT(res == 0);
- rc = ble_gap_adv_start(addrType, NULL, 2000, &adv_params, GAPEventCallback, this);
- ASSERT(rc == 0);
+ ble_gap_adv_start(addrType, NULL, 180000, &adv_params, GAPEventCallback, this);
+ // ASSERT(res == 0);// TODO I've disabled these ASSERT as they sometime asserts and reset the mcu.
+ // For now, the advertising is restarted as soon as it ends. There may be a race condition
+ // that prevent the advertising from restarting reliably.
+ // I remove the assert to prevent this uncesseray crash, but in the long term, the management of
+ // the advertising should be improve (better error handling, and advertise for 3 minutes after
+ // the application has been woken up, for example.
}
int NimbleController::OnGAPEvent(ble_gap_event* event) {
switch (event->type) {
case BLE_GAP_EVENT_ADV_COMPLETE:
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE");
- NRF_LOG_INFO("reason=%d; status=%d", event->adv_complete.reason, event->connect.status);
- StartAdvertising();
+ NRF_LOG_INFO("advertise complete; reason=%dn status=%d", event->adv_complete.reason, event->connect.status);
break;
-
- case BLE_GAP_EVENT_CONNECT:
+ case BLE_GAP_EVENT_CONNECT: {
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONNECT");
/* A new connection was established or a connection attempt failed. */
@@ -168,44 +145,35 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
if (event->connect.status != 0) {
/* Connection failed; resume advertising. */
- currentTimeClient.Reset();
- alertNotificationClient.Reset();
- connectionHandle = BLE_HS_CONN_HANDLE_NONE;
- bleController.Disconnect();
- fastAdvCount = 0;
StartAdvertising();
+ bleController.Disconnect();
} else {
- connectionHandle = event->connect.conn_handle;
bleController.Connect();
systemTask.PushMessage(Pinetime::System::Messages::BleConnected);
- // Service discovery is deferred via systemtask
+ connectionHandle = event->connect.conn_handle;
+ // Service discovery is deffered via systemtask
}
- break;
-
+ } break;
case BLE_GAP_EVENT_DISCONNECT:
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_DISCONNECT");
- NRF_LOG_INFO("disconnect reason=%d", event->disconnect.reason);
+ NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason);
/* Connection terminated; resume advertising. */
currentTimeClient.Reset();
alertNotificationClient.Reset();
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
bleController.Disconnect();
- fastAdvCount = 0;
StartAdvertising();
break;
-
case BLE_GAP_EVENT_CONN_UPDATE:
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONN_UPDATE");
/* The central has updated the connection parameters. */
- NRF_LOG_INFO("update status=%d ", event->conn_update.status);
+ NRF_LOG_INFO("connection updated; status=%d ", event->conn_update.status);
break;
-
case BLE_GAP_EVENT_ENC_CHANGE:
/* Encryption has been enabled or disabled for this connection. */
NRF_LOG_INFO("encryption change event; status=%d ", event->enc_change.status);
- break;
-
+ return 0;
case BLE_GAP_EVENT_SUBSCRIBE:
NRF_LOG_INFO("subscribe event; conn_handle=%d attr_handle=%d "
"reason=%d prevn=%d curn=%d previ=%d curi=???\n",
@@ -215,12 +183,10 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
event->subscribe.prev_notify,
event->subscribe.cur_notify,
event->subscribe.prev_indicate);
- break;
-
+ return 0;
case BLE_GAP_EVENT_MTU:
- NRF_LOG_INFO("mtu update event; conn_handle=%d cid=%d mtu=%d\n",
- event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value);
- break;
+ NRF_LOG_INFO("mtu update event; conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value);
+ return 0;
case BLE_GAP_EVENT_REPEAT_PAIRING: {
/* We already have a bond with the peer, but it is attempting to
@@ -251,7 +217,8 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
notifSize);
alertNotificationClient.OnNotification(event);
- } break;
+ return 0;
+ }
/* Attribute data is contained in event->notify_rx.attr_data. */
default:
@@ -262,9 +229,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
}
void NimbleController::StartDiscovery() {
- if (connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
- serviceDiscovery.StartDiscovery(connectionHandle);
- }
+ serviceDiscovery.StartDiscovery(connectionHandle);
}
uint16_t NimbleController::connHandle() {
@@ -272,7 +237,7 @@ uint16_t NimbleController::connHandle() {
}
void NimbleController::NotifyBatteryLevel(uint8_t level) {
- if (connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
+ if(connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
batteryInformationService.NotifyBatteryLevel(connectionHandle, level);
}
}
diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h
index 473bb1af..0cfe983c 100644
--- a/src/components/ble/NimbleController.h
+++ b/src/components/ble/NimbleController.h
@@ -72,10 +72,6 @@ namespace Pinetime {
uint16_t connHandle();
void NotifyBatteryLevel(uint8_t level);
- void RestartFastAdv() {
- fastAdvCount = 0;
- }
-
private:
static constexpr const char* deviceName = "InfiniTime";
Pinetime::System::SystemTask& systemTask;
@@ -98,7 +94,6 @@ namespace Pinetime {
uint8_t addrType; // 1 = Random, 0 = PUBLIC
uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE;
- uint8_t fastAdvCount = 0;
ble_uuid128_t dfuServiceUuid {
.u {.type = BLE_UUID_TYPE_128},
@@ -106,7 +101,5 @@ namespace Pinetime {
ServiceDiscovery serviceDiscovery;
};
-
- static NimbleController* nptr;
}
}
diff --git a/src/components/brightness/BrightnessController.cpp b/src/components/brightness/BrightnessController.cpp
index 6c524679..8ad987d1 100644
--- a/src/components/brightness/BrightnessController.cpp
+++ b/src/components/brightness/BrightnessController.cpp
@@ -1,13 +1,13 @@
#include "BrightnessController.h"
#include <hal/nrf_gpio.h>
#include "displayapp/screens/Symbols.h"
-#include "drivers/PinMap.h"
+
using namespace Pinetime::Controllers;
void BrightnessController::Init() {
- nrf_gpio_cfg_output(PinMap::LcdBacklightLow);
- nrf_gpio_cfg_output(PinMap::LcdBacklightMedium);
- nrf_gpio_cfg_output(PinMap::LcdBacklightHigh);
+ nrf_gpio_cfg_output(pinLcdBacklight1);
+ nrf_gpio_cfg_output(pinLcdBacklight2);
+ nrf_gpio_cfg_output(pinLcdBacklight3);
Set(level);
}
@@ -16,24 +16,24 @@ void BrightnessController::Set(BrightnessController::Levels level) {
switch (level) {
default:
case Levels::High:
- nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
- nrf_gpio_pin_clear(PinMap::LcdBacklightMedium);
- nrf_gpio_pin_clear(PinMap::LcdBacklightHigh);
+ nrf_gpio_pin_clear(pinLcdBacklight1);
+ nrf_gpio_pin_clear(pinLcdBacklight2);
+ nrf_gpio_pin_clear(pinLcdBacklight3);
break;
case Levels::Medium:
- nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
- nrf_gpio_pin_clear(PinMap::LcdBacklightMedium);
- nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
+ nrf_gpio_pin_clear(pinLcdBacklight1);
+ nrf_gpio_pin_clear(pinLcdBacklight2);
+ nrf_gpio_pin_set(pinLcdBacklight3);
break;
case Levels::Low:
- nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
- nrf_gpio_pin_set(PinMap::LcdBacklightMedium);
- nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
+ nrf_gpio_pin_clear(pinLcdBacklight1);
+ nrf_gpio_pin_set(pinLcdBacklight2);
+ nrf_gpio_pin_set(pinLcdBacklight3);
break;
case Levels::Off:
- nrf_gpio_pin_set(PinMap::LcdBacklightLow);
- nrf_gpio_pin_set(PinMap::LcdBacklightMedium);
- nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
+ nrf_gpio_pin_set(pinLcdBacklight1);
+ nrf_gpio_pin_set(pinLcdBacklight2);
+ nrf_gpio_pin_set(pinLcdBacklight3);
break;
}
}
diff --git a/src/components/brightness/BrightnessController.h b/src/components/brightness/BrightnessController.h
index 0d7ac2ff..c47158a9 100644
--- a/src/components/brightness/BrightnessController.h
+++ b/src/components/brightness/BrightnessController.h
@@ -22,6 +22,9 @@ namespace Pinetime {
const char* ToString();
private:
+ static constexpr uint8_t pinLcdBacklight1 = 14;
+ static constexpr uint8_t pinLcdBacklight2 = 22;
+ static constexpr uint8_t pinLcdBacklight3 = 23;
Levels level = Levels::High;
Levels backupLevel = Levels::High;
};
diff --git a/src/components/motor/MotorController.cpp b/src/components/motor/MotorController.cpp
index 42057a86..b25e6bc8 100644
--- a/src/components/motor/MotorController.cpp
+++ b/src/components/motor/MotorController.cpp
@@ -2,7 +2,6 @@
#include <hal/nrf_gpio.h>
#include "systemtask/SystemTask.h"
#include "app_timer.h"
-#include "drivers/PinMap.h"
APP_TIMER_DEF(shortVibTimer);
APP_TIMER_DEF(longVibTimer);
@@ -13,8 +12,8 @@ MotorController::MotorController(Controllers::Settings& settingsController) : se
}
void MotorController::Init() {
- nrf_gpio_cfg_output(PinMap::Motor);
- nrf_gpio_pin_set(PinMap::Motor);
+ nrf_gpio_cfg_output(pinMotor);
+ nrf_gpio_pin_set(pinMotor);
app_timer_init();
app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor);
@@ -31,7 +30,7 @@ void MotorController::RunForDuration(uint8_t motorDuration) {
return;
}
- nrf_gpio_pin_clear(PinMap::Motor);
+ nrf_gpio_pin_clear(pinMotor);
app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr);
}
@@ -45,9 +44,9 @@ void MotorController::StartRinging() {
void MotorController::StopRinging() {
app_timer_stop(longVibTimer);
- nrf_gpio_pin_set(PinMap::Motor);
+ nrf_gpio_pin_set(pinMotor);
}
void MotorController::StopMotor(void* p_context) {
- nrf_gpio_pin_set(PinMap::Motor);
+ nrf_gpio_pin_set(pinMotor);
}
diff --git a/src/components/motor/MotorController.h b/src/components/motor/MotorController.h
index cf78088e..d2c9fe5f 100644
--- a/src/components/motor/MotorController.h
+++ b/src/components/motor/MotorController.h
@@ -1,10 +1,12 @@
#pragma once
#include <cstdint>
+#include "app_timer.h"
#include "components/settings/Settings.h"
namespace Pinetime {
namespace Controllers {
+ static constexpr uint8_t pinMotor = 16;
class MotorController {
public:
diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h
index a54ba976..a294ab78 100644
--- a/src/components/settings/Settings.h
+++ b/src/components/settings/Settings.h
@@ -114,7 +114,7 @@ namespace Pinetime {
};
void setWakeUpMode(WakeUpMode wakeUp, bool enabled) {
- if (enabled != isWakeUpModeOn(wakeUp)) {
+ if (!isWakeUpModeOn(wakeUp)) {
settingsChanged = true;
}
settings.wakeUpMode.set(static_cast<size_t>(wakeUp), enabled);