summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
Diffstat (limited to 'src/components')
-rw-r--r--src/components/alarm/AlarmController.cpp114
-rw-r--r--src/components/alarm/AlarmController.h68
-rw-r--r--src/components/battery/BatteryController.cpp54
-rw-r--r--src/components/battery/BatteryController.h57
-rw-r--r--src/components/ble/AlertNotificationClient.cpp14
-rw-r--r--src/components/ble/AlertNotificationService.cpp11
-rw-r--r--src/components/ble/BatteryInformationService.cpp6
-rw-r--r--src/components/ble/CurrentTimeClient.cpp4
-rw-r--r--src/components/ble/CurrentTimeService.cpp4
-rw-r--r--src/components/ble/DeviceInformationService.cpp14
-rw-r--r--src/components/ble/DfuService.cpp26
-rw-r--r--src/components/ble/HeartRateService.cpp4
-rw-r--r--src/components/ble/ImmediateAlertService.cpp6
-rw-r--r--src/components/ble/NavigationService.cpp32
-rw-r--r--src/components/ble/NimbleController.cpp125
-rw-r--r--src/components/ble/NimbleController.h7
-rw-r--r--src/components/ble/NotificationManager.cpp8
-rw-r--r--src/components/ble/NotificationManager.h5
-rw-r--r--src/components/brightness/BrightnessController.cpp32
-rw-r--r--src/components/brightness/BrightnessController.h3
-rw-r--r--src/components/datetime/DateTimeController.cpp27
-rw-r--r--src/components/datetime/DateTimeController.h1
-rw-r--r--src/components/fs/FS.h2
-rw-r--r--src/components/motor/MotorController.cpp42
-rw-r--r--src/components/motor/MotorController.h14
-rw-r--r--src/components/settings/Settings.h59
26 files changed, 483 insertions, 256 deletions
diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp
new file mode 100644
index 00000000..67ca05a9
--- /dev/null
+++ b/src/components/alarm/AlarmController.cpp
@@ -0,0 +1,114 @@
+/* Copyright (C) 2021 mruss77, Florian
+
+ This file is part of InfiniTime.
+
+ InfiniTime is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ InfiniTime is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+#include "AlarmController.h"
+#include "systemtask/SystemTask.h"
+#include "app_timer.h"
+#include "task.h"
+#include <chrono>
+
+using namespace Pinetime::Controllers;
+using namespace std::chrono_literals;
+
+AlarmController::AlarmController(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} {
+}
+
+APP_TIMER_DEF(alarmAppTimer);
+
+namespace {
+ void SetOffAlarm(void* p_context) {
+ auto* controller = static_cast<Pinetime::Controllers::AlarmController*>(p_context);
+ if (controller != nullptr) {
+ controller->SetOffAlarmNow();
+ }
+ }
+}
+
+void AlarmController::Init(System::SystemTask* systemTask) {
+ app_timer_create(&alarmAppTimer, APP_TIMER_MODE_SINGLE_SHOT, SetOffAlarm);
+ this->systemTask = systemTask;
+}
+
+void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
+ hours = alarmHr;
+ minutes = alarmMin;
+}
+
+void AlarmController::ScheduleAlarm() {
+ // Determine the next time the alarm needs to go off and set the app_timer
+ app_timer_stop(alarmAppTimer);
+
+ auto now = dateTimeController.CurrentDateTime();
+ alarmTime = now;
+ time_t ttAlarmTime = std::chrono::system_clock::to_time_t(alarmTime);
+ tm* tmAlarmTime = std::localtime(&ttAlarmTime);
+
+ // If the time being set has already passed today,the alarm should be set for tomorrow
+ if (hours < dateTimeController.Hours() || (hours == dateTimeController.Hours() && minutes <= dateTimeController.Minutes())) {
+ tmAlarmTime->tm_mday += 1;
+ // tm_wday doesn't update automatically
+ tmAlarmTime->tm_wday = (tmAlarmTime->tm_wday + 1) % 7;
+ }
+
+ tmAlarmTime->tm_hour = hours;
+ tmAlarmTime->tm_min = minutes;
+ tmAlarmTime->tm_sec = 0;
+
+ // if alarm is in weekday-only mode, make sure it shifts to the next weekday
+ if (recurrence == RecurType::Weekdays) {
+ if (tmAlarmTime->tm_wday == 0) { // Sunday, shift 1 day
+ tmAlarmTime->tm_mday += 1;
+ } else if (tmAlarmTime->tm_wday == 6) { // Saturday, shift 2 days
+ tmAlarmTime->tm_mday += 2;
+ }
+ }
+ tmAlarmTime->tm_isdst = -1; // use system timezone setting to determine DST
+
+ // now can convert back to a time_point
+ alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime));
+ auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count();
+ app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this);
+
+ state = AlarmState::Set;
+}
+
+uint32_t AlarmController::SecondsToAlarm() {
+ return std::chrono::duration_cast<std::chrono::seconds>(alarmTime - dateTimeController.CurrentDateTime()).count();
+}
+
+void AlarmController::DisableAlarm() {
+ app_timer_stop(alarmAppTimer);
+ state = AlarmState::Not_Set;
+}
+
+void AlarmController::SetOffAlarmNow() {
+ state = AlarmState::Alerting;
+ systemTask->PushMessage(System::Messages::SetOffAlarm);
+}
+
+void AlarmController::StopAlerting() {
+ systemTask->PushMessage(System::Messages::StopRinging);
+
+ // Alarm state is off unless this is a recurring alarm
+ if (recurrence == RecurType::None) {
+ state = AlarmState::Not_Set;
+ } else {
+ state = AlarmState::Set;
+ // set next instance
+ ScheduleAlarm();
+ }
+}
diff --git a/src/components/alarm/AlarmController.h b/src/components/alarm/AlarmController.h
new file mode 100644
index 00000000..bf85d431
--- /dev/null
+++ b/src/components/alarm/AlarmController.h
@@ -0,0 +1,68 @@
+/* Copyright (C) 2021 mruss77, Florian
+
+ This file is part of InfiniTime.
+
+ InfiniTime is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ InfiniTime is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+#pragma once
+
+#include <cstdint>
+#include "app_timer.h"
+#include "components/datetime/DateTimeController.h"
+
+namespace Pinetime {
+ namespace System {
+ class SystemTask;
+ }
+ namespace Controllers {
+ class AlarmController {
+ public:
+ AlarmController(Controllers::DateTime& dateTimeController);
+
+ void Init(System::SystemTask* systemTask);
+ void SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin);
+ void ScheduleAlarm();
+ void DisableAlarm();
+ void SetOffAlarmNow();
+ uint32_t SecondsToAlarm();
+ void StopAlerting();
+ enum class AlarmState { Not_Set, Set, Alerting };
+ enum class RecurType { None, Daily, Weekdays };
+ uint8_t Hours() const {
+ return hours;
+ }
+ uint8_t Minutes() const {
+ return minutes;
+ }
+ AlarmState State() const {
+ return state;
+ }
+ RecurType Recurrence() const {
+ return recurrence;
+ }
+ void SetRecurrence(RecurType recurType) {
+ recurrence = recurType;
+ }
+
+ private:
+ Controllers::DateTime& dateTimeController;
+ System::SystemTask* systemTask = nullptr;
+ uint8_t hours = 7;
+ uint8_t minutes = 0;
+ std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> alarmTime;
+ AlarmState state = AlarmState::Not_Set;
+ RecurType recurrence = RecurType::None;
+ };
+ }
+}
diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp
index fa476ea3..b43b229f 100644
--- a/src/components/battery/BatteryController.cpp
+++ b/src/components/battery/BatteryController.cpp
@@ -1,4 +1,5 @@
#include "BatteryController.h"
+#include "drivers/PinMap.h"
#include <hal/nrf_gpio.h>
#include <nrfx_saadc.h>
#include <algorithm>
@@ -9,21 +10,23 @@ Battery* Battery::instance = nullptr;
Battery::Battery() {
instance = this;
-}
-
-void Battery::Init() {
- nrf_gpio_cfg_input(chargingPin, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Pullup);
+ nrf_gpio_cfg_input(PinMap::Charging, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled);
}
void Battery::Update() {
- isCharging = !nrf_gpio_pin_read(chargingPin);
- isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
+ isCharging = !nrf_gpio_pin_read(PinMap::Charging);
+ isPowerPresent = !nrf_gpio_pin_read(PinMap::PowerPresent);
+
+ if (isPowerPresent && !isCharging) {
+ isFull = true;
+ } else if (!isPowerPresent) {
+ isFull = false;
+ }
if (isReading) {
return;
}
// Non blocking read
- samples = 0;
isReading = true;
SaadcInit();
@@ -40,9 +43,9 @@ void Battery::SaadcInit() {
nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
- .gain = NRF_SAADC_GAIN1_5,
+ .gain = NRF_SAADC_GAIN1_4,
.reference = NRF_SAADC_REFERENCE_INTERNAL,
- .acq_time = NRF_SAADC_ACQTIME_3US,
+ .acq_time = NRF_SAADC_ACQTIME_40US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_ENABLED,
.pin_p = batteryVoltageAdcInput,
@@ -60,22 +63,27 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1));
// A hardware voltage divider divides the battery voltage by 2
- // ADC gain is 1/5
- // thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 10
- // reference_voltage is 0.6V
+ // ADC gain is 1/4
+ // thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 8
+ // reference_voltage is 600mV
// 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);
-
- samples++;
- if (samples > percentRemainingSamples) {
- nrfx_saadc_uninit();
- isReading = false;
+ voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
+
+ if (isFull) {
+ percentRemaining = 100;
+ } else if (voltage < battery_min) {
+ percentRemaining = 0;
} else {
- nrfx_saadc_sample();
+ percentRemaining = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100);
}
+
+ 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 1333ad0e..c78ffb3f 100644
--- a/src/components/battery/BatteryController.h
+++ b/src/components/battery/BatteryController.h
@@ -1,56 +1,20 @@
#pragma once
#include <cstdint>
#include <drivers/include/nrfx_saadc.h>
-#include <array>
-#include <numeric>
+#include <systemtask/SystemTask.h>
namespace Pinetime {
namespace Controllers {
- /** A simple circular buffer that can be used to average
- out the sensor values. The total capacity of the CircBuffer
- is given as the template parameter N.
- */
- template <int N> class CircBuffer {
- public:
- CircBuffer() : arr {}, sz {}, cap {N}, head {} {
- }
- /**
- insert member function overwrites the next data to the current
- HEAD and moves the HEAD to the newly inserted value.
- */
- void Insert(const uint8_t num) {
- head %= cap;
- arr[head++] = num;
- if (sz != cap) {
- sz++;
- }
- }
-
- uint8_t GetAverage() const {
- int sum = std::accumulate(arr.begin(), arr.end(), 0);
- return static_cast<uint8_t>(sum / sz);
- }
-
- private:
- 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*/
- };
-
class Battery {
public:
Battery();
- void Init();
void Update();
+ void Register(System::SystemTask* systemTask);
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;
+ return percentRemaining;
}
uint16_t Voltage() const {
@@ -58,7 +22,9 @@ namespace Pinetime {
}
bool IsCharging() const {
- return isCharging;
+ // isCharging will go up and down when fully charged
+ // isFull makes sure this returns false while fully charged.
+ return isCharging && !isFull;
}
bool IsPowerPresent() const {
@@ -69,15 +35,11 @@ namespace Pinetime {
static Battery* instance;
nrf_saadc_value_t saadc_value;
- static constexpr uint8_t percentRemainingSamples = 5;
- CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
-
- 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;
- int percentRemaining = -1;
+ uint8_t percentRemaining = 0;
+ bool isFull = false;
bool isCharging = false;
bool isPowerPresent = false;
@@ -87,7 +49,8 @@ namespace Pinetime {
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
bool isReading = false;
- uint8_t samples = 0;
+
+ Pinetime::System::SystemTask* systemTask = nullptr;
};
}
}
diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp
index c3d1d69a..5e5c25cf 100644
--- a/src/components/ble/AlertNotificationClient.cpp
+++ b/src/components/ble/AlertNotificationClient.cpp
@@ -55,7 +55,7 @@ bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const
return true;
}
- if (service != nullptr && ble_uuid_cmp(((ble_uuid_t*) &ansServiceUuid), &service->uuid.u) == 0) {
+ if (service != nullptr && ble_uuid_cmp(&ansServiceUuid.u, &service->uuid.u) == 0) {
NRF_LOG_INFO("ANS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle);
ansStartHandle = service->start_handle;
ansEndHandle = service->end_handle;
@@ -80,21 +80,21 @@ int AlertNotificationClient::OnCharacteristicsDiscoveryEvent(uint16_t connection
} else
onServiceDiscovered(connectionHandle);
} else {
- if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &supportedNewAlertCategoryUuid), &characteristic->uuid.u) == 0) {
+ if (characteristic != nullptr && ble_uuid_cmp(&supportedNewAlertCategoryUuid.u, &characteristic->uuid.u) == 0) {
NRF_LOG_INFO("ANS Characteristic discovered : supportedNewAlertCategoryUuid");
supportedNewAlertCategoryHandle = characteristic->val_handle;
- } else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &supportedUnreadAlertCategoryUuid), &characteristic->uuid.u) == 0) {
+ } else if (characteristic != nullptr && ble_uuid_cmp(&supportedUnreadAlertCategoryUuid.u, &characteristic->uuid.u) == 0) {
NRF_LOG_INFO("ANS Characteristic discovered : supportedUnreadAlertCategoryUuid");
supportedUnreadAlertCategoryHandle = characteristic->val_handle;
- } else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &newAlertUuid), &characteristic->uuid.u) == 0) {
+ } else if (characteristic != nullptr && ble_uuid_cmp(&newAlertUuid.u, &characteristic->uuid.u) == 0) {
NRF_LOG_INFO("ANS Characteristic discovered : newAlertUuid");
newAlertHandle = characteristic->val_handle;
newAlertDefHandle = characteristic->def_handle;
isCharacteristicDiscovered = true;
- } else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &unreadAlertStatusUuid), &characteristic->uuid.u) == 0) {
+ } else if (characteristic != nullptr && ble_uuid_cmp(&unreadAlertStatusUuid.u, &characteristic->uuid.u) == 0) {
NRF_LOG_INFO("ANS Characteristic discovered : unreadAlertStatusUuid");
unreadAlertStatusHandle = characteristic->val_handle;
- } else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &controlPointUuid), &characteristic->uuid.u) == 0) {
+ } else if (characteristic != nullptr && ble_uuid_cmp(&controlPointUuid.u, &characteristic->uuid.u) == 0) {
NRF_LOG_INFO("ANS Characteristic discovered : controlPointUuid");
controlPointHandle = characteristic->val_handle;
} else
@@ -119,7 +119,7 @@ int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connect
uint16_t characteristicValueHandle,
const ble_gatt_dsc* descriptor) {
if (error->status == 0) {
- if (characteristicValueHandle == newAlertHandle && ble_uuid_cmp(((ble_uuid_t*) &newAlertUuid), &descriptor->uuid.u)) {
+ if (characteristicValueHandle == newAlertHandle && ble_uuid_cmp(&newAlertUuid.u, &descriptor->uuid.u)) {
if (newAlertDescriptorHandle == 0) {
NRF_LOG_INFO("ANS Descriptor discovered : %d", descriptor->handle);
newAlertDescriptorHandle = descriptor->handle;
diff --git a/src/components/ble/AlertNotificationService.cpp b/src/components/ble/AlertNotificationService.cpp
index d5fc7f65..56fc595f 100644
--- a/src/components/ble/AlertNotificationService.cpp
+++ b/src/components/ble/AlertNotificationService.cpp
@@ -26,11 +26,8 @@ void AlertNotificationService::Init() {
}
AlertNotificationService::AlertNotificationService(System::SystemTask& systemTask, NotificationManager& notificationManager)
- : characteristicDefinition {{.uuid = (ble_uuid_t*) &ansCharUuid,
- .access_cb = AlertNotificationCallback,
- .arg = this,
- .flags = BLE_GATT_CHR_F_WRITE},
- {.uuid = (ble_uuid_t*) &notificationEventUuid,
+ : characteristicDefinition {{.uuid = &ansCharUuid.u, .access_cb = AlertNotificationCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE},
+ {.uuid = &notificationEventUuid.u,
.access_cb = AlertNotificationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_NOTIFY,
@@ -39,7 +36,7 @@ AlertNotificationService::AlertNotificationService(System::SystemTask& systemTas
serviceDefinition {
{/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid = (ble_uuid_t*) &ansUuid,
+ .uuid = &ansUuid.u,
.characteristics = characteristicDefinition},
{0},
},
@@ -123,4 +120,4 @@ void AlertNotificationService::MuteIncomingCall() {
}
ble_gattc_notify_custom(connectionHandle, eventHandle, om);
-} \ No newline at end of file
+}
diff --git a/src/components/ble/BatteryInformationService.cpp b/src/components/ble/BatteryInformationService.cpp
index 7f176904..29178667 100644
--- a/src/components/ble/BatteryInformationService.cpp
+++ b/src/components/ble/BatteryInformationService.cpp
@@ -14,7 +14,7 @@ int BatteryInformationServiceCallback(uint16_t conn_handle, uint16_t attr_handle
BatteryInformationService::BatteryInformationService(Controllers::Battery& batteryController)
: batteryController {batteryController},
- characteristicDefinition {{.uuid = (ble_uuid_t*) &batteryLevelUuid,
+ characteristicDefinition {{.uuid = &batteryLevelUuid.u,
.access_cb = BatteryInformationServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
@@ -23,7 +23,7 @@ BatteryInformationService::BatteryInformationService(Controllers::Battery& batte
serviceDefinition {
{/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid = (ble_uuid_t*) &batteryInformationServiceUuid,
+ .uuid = &batteryInformationServiceUuid.u,
.characteristics = characteristicDefinition},
{0},
} {
@@ -43,7 +43,7 @@ int BatteryInformationService::OnBatteryServiceRequested(uint16_t connectionHand
ble_gatt_access_ctxt* context) {
if (attributeHandle == batteryLevelHandle) {
NRF_LOG_INFO("BATTERY : handle = %d", batteryLevelHandle);
- static uint8_t batteryValue = batteryController.PercentRemaining();
+ uint8_t batteryValue = batteryController.PercentRemaining();
int res = os_mbuf_append(context->om, &batteryValue, 1);
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
diff --git a/src/components/ble/CurrentTimeClient.cpp b/src/components/ble/CurrentTimeClient.cpp
index c6e68312..90d1f0c7 100644
--- a/src/components/ble/CurrentTimeClient.cpp
+++ b/src/components/ble/CurrentTimeClient.cpp
@@ -47,7 +47,7 @@ bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_ga
return true;
}
- if (service != nullptr && ble_uuid_cmp(((ble_uuid_t*) &ctsServiceUuid), &service->uuid.u) == 0) {
+ if (service != nullptr && ble_uuid_cmp(&ctsServiceUuid.u, &service->uuid.u) == 0) {
NRF_LOG_INFO("CTS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle);
isDiscovered = true;
ctsStartHandle = service->start_handle;
@@ -72,7 +72,7 @@ int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle,
return 0;
}
- if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &currentTimeCharacteristicUuid), &characteristic->uuid.u) == 0) {
+ if (characteristic != nullptr && ble_uuid_cmp(&currentTimeCharacteristicUuid.u, &characteristic->uuid.u) == 0) {
NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);
isCharacteristicDiscovered = true;
currentTimeHandle = characteristic->val_handle;
diff --git a/src/components/ble/CurrentTimeService.cpp b/src/components/ble/CurrentTimeService.cpp
index b49be39c..eefb7ec1 100644
--- a/src/components/ble/CurrentTimeService.cpp
+++ b/src/components/ble/CurrentTimeService.cpp
@@ -53,7 +53,7 @@ int CurrentTimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handl
}
CurrentTimeService::CurrentTimeService(DateTime& dateTimeController)
- : characteristicDefinition {{.uuid = (ble_uuid_t*) &ctChrUuid,
+ : characteristicDefinition {{.uuid = &ctChrUuid.u,
.access_cb = CTSCallback,
.arg = this,
@@ -62,7 +62,7 @@ CurrentTimeService::CurrentTimeService(DateTime& dateTimeController)
serviceDefinition {
{/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid = (ble_uuid_t*) &ctsUuid,
+ .uuid = &ctsUuid.u,
.characteristics = characteristicDefinition},
{0},
},
diff --git a/src/components/ble/DeviceInformationService.cpp b/src/components/ble/DeviceInformationService.cpp
index cf482079..778d6e35 100644
--- a/src/components/ble/DeviceInformationService.cpp
+++ b/src/components/ble/DeviceInformationService.cpp
@@ -56,37 +56,37 @@ int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16
DeviceInformationService::DeviceInformationService()
: characteristicDefinition {{
- .uuid = (ble_uuid_t*) &manufacturerNameUuid,
+ .uuid = &manufacturerNameUuid.u,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
- .uuid = (ble_uuid_t*) &modelNumberUuid,
+ .uuid = &modelNumberUuid.u,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
- .uuid = (ble_uuid_t*) &serialNumberUuid,
+ .uuid = &serialNumberUuid.u,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
- .uuid = (ble_uuid_t*) &fwRevisionUuid,
+ .uuid = &fwRevisionUuid.u,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
- .uuid = (ble_uuid_t*) &hwRevisionUuid,
+ .uuid = &hwRevisionUuid.u,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
- .uuid = (ble_uuid_t*) &swRevisionUuid,
+ .uuid = &swRevisionUuid.u,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
@@ -95,7 +95,7 @@ DeviceInformationService::DeviceInformationService()
serviceDefinition {
{/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid = (ble_uuid_t*) &deviceInfoUuid,
+ .uuid = &deviceInfoUuid.u,
.characteristics = characteristicDefinition},
{0},
} {
diff --git a/src/components/ble/DfuService.cpp b/src/components/ble/DfuService.cpp
index 4179994d..3d6416fa 100644
--- a/src/components/ble/DfuService.cpp
+++ b/src/components/ble/DfuService.cpp
@@ -33,21 +33,21 @@ DfuService::DfuService(Pinetime::System::SystemTask& systemTask,
bleController {bleController},
dfuImage {spiNorFlash},
characteristicDefinition {{
- .uuid = (ble_uuid_t*) &packetCharacteristicUuid,
+ .uuid = &packetCharacteristicUuid.u,
.access_cb = DfuServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
.val_handle = nullptr,
},
{
- .uuid = (ble_uuid_t*) &controlPointCharacteristicUuid,
+ .uuid = &controlPointCharacteristicUuid.u,
.access_cb = DfuServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY,
.val_handle = nullptr,
},
{
- .uuid = (ble_uuid_t*) &revisionCharacteristicUuid,
+ .uuid = &revisionCharacteristicUuid.u,
.access_cb = DfuServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
@@ -60,7 +60,7 @@ DfuService::DfuService(Pinetime::System::SystemTask& systemTask,
serviceDefinition {
{/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid = (ble_uuid_t*) &serviceUuid,
+ .uuid = &serviceUuid.u,
.characteristics = characteristicDefinition},
{0},
} {
@@ -81,9 +81,9 @@ int DfuService::OnServiceData(uint16_t connectionHandle, uint16_t attributeHandl
xTimerStart(timeoutTimer, 0);
}
- ble_gatts_find_chr((ble_uuid_t*) &serviceUuid, (ble_uuid_t*) &packetCharacteristicUuid, nullptr, &packetCharacteristicHandle);
- ble_gatts_find_chr((ble_uuid_t*) &serviceUuid, (ble_uuid_t*) &controlPointCharacteristicUuid, nullptr, &controlPointCharacteristicHandle);
- ble_gatts_find_chr((ble_uuid_t*) &serviceUuid, (ble_uuid_t*) &revisionCharacteristicUuid, nullptr, &revisionCharacteristicHandle);
+ ble_gatts_find_chr(&serviceUuid.u, &packetCharacteristicUuid.u, nullptr, &packetCharacteristicHandle);
+ ble_gatts_find_chr(&serviceUuid.u, &controlPointCharacteristicUuid.u, nullptr, &controlPointCharacteristicHandle);
+ ble_gatts_find_chr(&serviceUuid.u, &revisionCharacteristicUuid.u, nullptr, &revisionCharacteristicHandle);
if (attributeHandle == packetCharacteristicHandle) {
if (context->op == BLE_GATT_ACCESS_OP_WRITE_CHR)
@@ -164,10 +164,10 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf* om) {
if ((nbPacketReceived % nbPacketsToNotify) == 0 && bytesReceived != applicationSize) {
uint8_t data[5] {static_cast<uint8_t>(Opcodes::PacketReceiptNotification),
- (uint8_t) (bytesReceived & 0x000000FFu),
- (uint8_t) (bytesReceived >> 8u),
- (uint8_t) (bytesReceived >> 16u),
- (uint8_t) (bytesReceived >> 24u)};
+ (uint8_t)(bytesReceived & 0x000000FFu),
+ (uint8_t)(bytesReceived >> 8u),
+ (uint8_t)(bytesReceived >> 16u),
+ (uint8_t)(bytesReceived >> 24u)};
NRF_LOG_INFO("[DFU] -> Send packet notification: %d bytes received", bytesReceived);
notificationManager.Send(connectionHandle, controlPointCharacteristicHandle, data, 5);
}
@@ -422,9 +422,9 @@ uint16_t DfuService::DfuImage::ComputeCrc(uint8_t const* p_data, uint32_t size,
uint16_t crc = (p_crc == NULL) ? 0xFFFF : *p_crc;
for (uint32_t i = 0; i < size; i++) {
- crc = (uint8_t) (crc >> 8) | (crc << 8);
+ crc = (uint8_t)(crc >> 8) | (crc << 8);
crc ^= p_data[i];
- crc ^= (uint8_t) (crc & 0xFF) >> 4;
+ crc ^= (uint8_t)(crc & 0xFF) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xFF) << 4) << 1;
}
diff --git a/src/components/ble/HeartRateService.cpp b/src/components/ble/HeartRateService.cpp
index c556566b..5b00f492 100644
--- a/src/components/ble/HeartRateService.cpp
+++ b/src/components/ble/HeartRateService.cpp
@@ -18,7 +18,7 @@ namespace {
HeartRateService::HeartRateService(Pinetime::System::SystemTask& system, Controllers::HeartRateController& heartRateController)
: system {system},
heartRateController {heartRateController},
- characteristicDefinition {{.uuid = (ble_uuid_t*) &heartRateMeasurementUuid,
+ characteristicDefinition {{.uuid = &heartRateMeasurementUuid.u,
.access_cb = HeartRateServiceServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
@@ -27,7 +27,7 @@ HeartRateService::HeartRateService(Pinetime::System::SystemTask& system, Control
serviceDefinition {
{/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid = (ble_uuid_t*) &heartRateServiceUuid,
+ .uuid = &heartRateServiceUuid.u,
.characteristics = characteristicDefinition},
{0},
} {
diff --git a/src/components/ble/ImmediateAlertService.cpp b/src/components/ble/ImmediateAlertService.cpp
index 820d3b6e..17ed1a96 100644
--- a/src/components/ble/ImmediateAlertService.cpp
+++ b/src/components/ble/ImmediateAlertService.cpp
@@ -32,7 +32,7 @@ ImmediateAlertService::ImmediateAlertService(Pinetime::System::SystemTask& syste
Pinetime::Controllers::NotificationManager& notificationManager)
: systemTask {systemTask},
notificationManager {notificationManager},
- characteristicDefinition {{.uuid = (ble_uuid_t*) &alertLevelUuid,
+ characteristicDefinition {{.uuid = &alertLevelUuid.u,
.access_cb = AlertLevelCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
@@ -41,7 +41,7 @@ ImmediateAlertService::ImmediateAlertService(Pinetime::System::SystemTask& syste
serviceDefinition {
{/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid = (ble_uuid_t*) &immediateAlertServiceUuid,
+ .uuid = &immediateAlertServiceUuid.u,
.characteristics = characteristicDefinition},
{0},
} {
@@ -72,4 +72,4 @@ int ImmediateAlertService::OnAlertLevelChanged(uint16_t connectionHandle, uint16
}
return 0;
-} \ No newline at end of file
+}
diff --git a/src/components/ble/NavigationService.cpp b/src/components/ble/NavigationService.cpp
index 4bbe8b45..b49148d2 100644
--- a/src/components/ble/NavigationService.cpp
+++ b/src/components/ble/NavigationService.cpp
@@ -47,24 +47,18 @@ namespace {
Pinetime::Controllers::NavigationService::NavigationService(Pinetime::System::SystemTask& system) : m_system(system) {
characteristicDefinition[0] = {
- .uuid = (ble_uuid_t*) (&navFlagCharUuid), .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
-
- characteristicDefinition[1] = {.uuid = (ble_uuid_t*) (&navNarrativeCharUuid),
- .access_cb = NAVCallback,
- .arg = this,
- .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
- characteristicDefinition[2] = {.uuid = (ble_uuid_t*) (&navManDistCharUuid),
- .access_cb = NAVCallback,
- .arg = this,
- .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
- characteristicDefinition[3] = {.uuid = (ble_uuid_t*) (&navProgressCharUuid),
- .access_cb = NAVCallback,
- .arg = this,
- .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
+ .uuid = &navFlagCharUuid.u, .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
+
+ characteristicDefinition[1] = {
+ .uuid = &navNarrativeCharUuid.u, .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
+ characteristicDefinition[2] = {
+ .uuid = &navManDistCharUuid.u, .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
+ characteristicDefinition[3] = {
+ .uuid = &navProgressCharUuid.u, .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
characteristicDefinition[4] = {0};
- serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = (ble_uuid_t*) &navUuid, .characteristics = characteristicDefinition};
+ serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &navUuid.u, .characteristics = characteristicDefinition};
serviceDefinition[1] = {0};
m_progress = 0;
@@ -87,13 +81,13 @@ int Pinetime::Controllers::NavigationService::OnCommand(uint16_t conn_handle, ui
data[notifSize] = '\0';
os_mbuf_copydata(ctxt->om, 0, notifSize, data);
char* s = (char*) &data[0];
- if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &navFlagCharUuid) == 0) {
+ if (ble_uuid_cmp(ctxt->chr->uuid, &navFlagCharUuid.u) == 0) {
m_flag = s;
- } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &navNarrativeCharUuid) == 0) {
+ } else if (ble_uuid_cmp(ctxt->chr->uuid, &navNarrativeCharUuid.u) == 0) {
m_narrative = s;
- } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &navManDistCharUuid) == 0) {
+ } else if (ble_uuid_cmp(ctxt->chr->uuid, &navManDistCharUuid.u) == 0) {
m_manDist = s;
- } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &navProgressCharUuid) == 0) {
+ } else if (ble_uuid_cmp(ctxt->chr->uuid, &navProgressCharUuid.u) == 0) {
m_progress = data[0];
}
}
diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp
index 5eb227bf..879421e7 100644
--- a/src/components/ble/NimbleController.cpp
+++ b/src/components/ble/NimbleController.cpp
@@ -42,6 +42,19 @@ 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);
@@ -51,6 +64,10 @@ 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();
@@ -64,28 +81,31 @@ void NimbleController::Init() {
batteryInformationService.Init();
immediateAlertService.Init();
heartRateService.Init();
- 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);
+
+ 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);
Pinetime::Controllers::Ble::BleAddress address;
- res = ble_hs_id_copy_addr(addrType, address.data(), nullptr);
- ASSERT(res == 0);
+ rc = ble_hs_id_copy_addr(addrType, address.data(), nullptr);
+ ASSERT(rc == 0);
bleController.AddressType((addrType == 0) ? Ble::AddressTypes::Public : Ble::AddressTypes::Random);
bleController.Address(std::move(address));
- res = ble_gatts_start();
- ASSERT(res == 0);
+ rc = ble_gatts_start();
+ ASSERT(rc == 0);
+
+ if (!ble_gap_adv_active() && !bleController.IsConnected())
+ StartAdvertising();
}
void NimbleController::StartAdvertising() {
- if (bleController.IsConnected() || ble_gap_conn_active() || ble_gap_adv_active())
- return;
-
- ble_svc_gap_device_name_set(deviceName);
+ int rc;
/* set adv parameters */
struct ble_gap_adv_params adv_params;
@@ -102,11 +122,17 @@ 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;
@@ -116,28 +142,25 @@ void NimbleController::StartAdvertising() {
rsp_fields.name_len = strlen(deviceName);
rsp_fields.name_is_complete = 1;
- ble_gap_adv_set_fields(&fields);
- // ASSERT(res == 0); // TODO this one sometimes fails with error 22 (notsync)
+ rc = ble_gap_adv_set_fields(&fields);
+ ASSERT(rc == 0);
- ble_gap_adv_rsp_set_fields(&rsp_fields);
- // ASSERT(res == 0);
+ rc = ble_gap_adv_rsp_set_fields(&rsp_fields);
+ 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.
+ rc = ble_gap_adv_start(addrType, NULL, 2000, &adv_params, GAPEventCallback, this);
+ ASSERT(rc == 0);
}
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("advertise complete; reason=%dn status=%d", event->adv_complete.reason, event->connect.status);
+ NRF_LOG_INFO("reason=%d; status=%d", event->adv_complete.reason, event->connect.status);
+ StartAdvertising();
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. */
@@ -145,35 +168,44 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
if (event->connect.status != 0) {
/* Connection failed; resume advertising. */
- StartAdvertising();
+ currentTimeClient.Reset();
+ alertNotificationClient.Reset();
+ connectionHandle = BLE_HS_CONN_HANDLE_NONE;
bleController.Disconnect();
+ fastAdvCount = 0;
+ StartAdvertising();
} else {
+ connectionHandle = event->connect.conn_handle;
bleController.Connect();
systemTask.PushMessage(Pinetime::System::Messages::BleConnected);
- connectionHandle = event->connect.conn_handle;
- // Service discovery is deffered via systemtask
+ // Service discovery is deferred 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("connection updated; status=%d ", event->conn_update.status);
+ NRF_LOG_INFO("update 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);
- return 0;
+ break;
+
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",
@@ -183,10 +215,12 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
event->subscribe.prev_notify,
event->subscribe.cur_notify,
event->subscribe.prev_indicate);
- return 0;
+ break;
+
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);
- return 0;
+ 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;
case BLE_GAP_EVENT_REPEAT_PAIRING: {
/* We already have a bond with the peer, but it is attempting to
@@ -217,8 +251,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
notifSize);
alertNotificationClient.OnNotification(event);
- return 0;
- }
+ } break;
/* Attribute data is contained in event->notify_rx.attr_data. */
default:
@@ -229,7 +262,9 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
}
void NimbleController::StartDiscovery() {
- serviceDiscovery.StartDiscovery(connectionHandle);
+ if (connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
+ serviceDiscovery.StartDiscovery(connectionHandle);
+ }
}
uint16_t NimbleController::connHandle() {
@@ -237,7 +272,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 0cfe983c..473bb1af 100644
--- a/src/components/ble/NimbleController.h
+++ b/src/components/ble/NimbleController.h
@@ -72,6 +72,10 @@ 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;
@@ -94,6 +98,7 @@ 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},
@@ -101,5 +106,7 @@ namespace Pinetime {
ServiceDiscovery serviceDiscovery;
};
+
+ static NimbleController* nptr;
}
}
diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp
index b1b0e6b2..7ffed300 100644
--- a/src/components/ble/NotificationManager.cpp
+++ b/src/components/ble/NotificationManager.cpp
@@ -79,14 +79,6 @@ bool NotificationManager::AreNewNotificationsAvailable() {
return newNotification;
}
-bool NotificationManager::IsVibrationEnabled() {
- return vibrationEnabled;
-}
-
-void NotificationManager::ToggleVibrations() {
- vibrationEnabled = !vibrationEnabled;
-}
-
bool NotificationManager::ClearNewNotificationFlag() {
return newNotification.exchange(false);
}
diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h
index d4072cc2..40f174ea 100644
--- a/src/components/ble/NotificationManager.h
+++ b/src/components/ble/NotificationManager.h
@@ -44,8 +44,6 @@ namespace Pinetime {
Notification GetPrevious(Notification::Id id);
bool ClearNewNotificationFlag();
bool AreNewNotificationsAvailable();
- bool IsVibrationEnabled();
- void ToggleVibrations();
static constexpr size_t MaximumMessageSize() {
return MessageSize;
@@ -60,7 +58,6 @@ namespace Pinetime {
uint8_t writeIndex = 0;
bool empty = true;
std::atomic<bool> newNotification {false};
- bool vibrationEnabled = true;
};
}
-} \ No newline at end of file
+}
diff --git a/src/components/brightness/BrightnessController.cpp b/src/components/brightness/BrightnessController.cpp
index 8ad987d1..6c524679 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(pinLcdBacklight1);
- nrf_gpio_cfg_output(pinLcdBacklight2);
- nrf_gpio_cfg_output(pinLcdBacklight3);
+ nrf_gpio_cfg_output(PinMap::LcdBacklightLow);
+ nrf_gpio_cfg_output(PinMap::LcdBacklightMedium);
+ nrf_gpio_cfg_output(PinMap::LcdBacklightHigh);
Set(level);
}
@@ -16,24 +16,24 @@ void BrightnessController::Set(BrightnessController::Levels level) {
switch (level) {
default:
case Levels::High:
- nrf_gpio_pin_clear(pinLcdBacklight1);
- nrf_gpio_pin_clear(pinLcdBacklight2);
- nrf_gpio_pin_clear(pinLcdBacklight3);
+ nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
+ nrf_gpio_pin_clear(PinMap::LcdBacklightMedium);
+ nrf_gpio_pin_clear(PinMap::LcdBacklightHigh);
break;
case Levels::Medium:
- nrf_gpio_pin_clear(pinLcdBacklight1);
- nrf_gpio_pin_clear(pinLcdBacklight2);
- nrf_gpio_pin_set(pinLcdBacklight3);
+ nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
+ nrf_gpio_pin_clear(PinMap::LcdBacklightMedium);
+ nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
break;
case Levels::Low:
- nrf_gpio_pin_clear(pinLcdBacklight1);
- nrf_gpio_pin_set(pinLcdBacklight2);
- nrf_gpio_pin_set(pinLcdBacklight3);
+ nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
+ nrf_gpio_pin_set(PinMap::LcdBacklightMedium);
+ nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
break;
case Levels::Off:
- nrf_gpio_pin_set(pinLcdBacklight1);
- nrf_gpio_pin_set(pinLcdBacklight2);
- nrf_gpio_pin_set(pinLcdBacklight3);
+ nrf_gpio_pin_set(PinMap::LcdBacklightLow);
+ nrf_gpio_pin_set(PinMap::LcdBacklightMedium);
+ nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
break;
}
}
diff --git a/src/components/brightness/BrightnessController.h b/src/components/brightness/BrightnessController.h
index c47158a9..0d7ac2ff 100644
--- a/src/components/brightness/BrightnessController.h
+++ b/src/components/brightness/BrightnessController.h
@@ -22,9 +22,6 @@ 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/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp
index 28a70abc..0756d38d 100644
--- a/src/components/datetime/DateTimeController.cpp
+++ b/src/components/datetime/DateTimeController.cpp
@@ -5,6 +5,11 @@
using namespace Pinetime::Controllers;
+void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) {
+ this->currentDateTime = t;
+ UpdateTime(previousSystickCounter); // Update internal state without updating the time
+}
+
void DateTime::SetTime(
uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter) {
std::tm tm = {
@@ -55,9 +60,9 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
auto time = date::make_time(currentDateTime - dp);
auto yearMonthDay = date::year_month_day(dp);
- year = (int) yearMonthDay.year();
- month = static_cast<Months>((unsigned) yearMonthDay.month());
- day = (unsigned) yearMonthDay.day();
+ year = static_cast<int>(yearMonthDay.year());
+ month = static_cast<Months>(static_cast<unsigned>(yearMonthDay.month()));
+ day = static_cast<unsigned>(yearMonthDay.day());
dayOfWeek = static_cast<Days>(date::weekday(yearMonthDay).iso_encoding());
hour = time.hours().count();
@@ -67,7 +72,7 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
// Notify new day to SystemTask
if (hour == 0 and not isMidnightAlreadyNotified) {
isMidnightAlreadyNotified = true;
- if(systemTask != nullptr)
+ if (systemTask != nullptr)
systemTask->PushMessage(System::Messages::OnNewDay);
} else if (hour != 0) {
isMidnightAlreadyNotified = false;
@@ -75,31 +80,31 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
}
const char* DateTime::MonthShortToString() {
- return DateTime::MonthsString[(uint8_t) month];
+ return DateTime::MonthsString[static_cast<uint8_t>(month)];
}
const char* DateTime::MonthShortToStringLow() {
- return DateTime::MonthsStringLow[(uint8_t) month];
+ return DateTime::MonthsStringLow[static_cast<uint8_t>(month)];
}
const char* DateTime::MonthsToStringLow() {
- return DateTime::MonthsLow[(uint8_t) month];
+ return DateTime::MonthsLow[static_cast<uint8_t>(month)];
}
const char* DateTime::DayOfWeekToString() {
- return DateTime::DaysString[(uint8_t) dayOfWeek];
+ return DateTime::DaysString[static_cast<uint8_t>(dayOfWeek)];
}
const char* DateTime::DayOfWeekShortToString() {
- return DateTime::DaysStringShort[(uint8_t) dayOfWeek];
+ return DateTime::DaysStringShort[static_cast<uint8_t>(dayOfWeek)];
}
const char* DateTime::DayOfWeekToStringLow() {
- return DateTime::DaysStringLow[(uint8_t) dayOfWeek];
+ return DateTime::DaysStringLow[static_cast<uint8_t>(dayOfWeek)];
}
const char* DateTime::DayOfWeekShortToStringLow() {
- return DateTime::DaysStringShortLow[(uint8_t) dayOfWeek];
+ return DateTime::DaysStringShortLow[static_cast<uint8_t>(dayOfWeek)];
}
void DateTime::Register(Pinetime::System::SystemTask* systemTask) {
diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h
index 265d6e9d..061c303f 100644
--- a/src/components/datetime/DateTimeController.h
+++ b/src/components/datetime/DateTimeController.h
@@ -74,6 +74,7 @@ namespace Pinetime {
}
void Register(System::SystemTask* systemTask);
+ void SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t);
private:
uint16_t year = 0;
diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h
index 1f2eb7e0..75ba16c8 100644
--- a/src/components/fs/FS.h
+++ b/src/components/fs/FS.h
@@ -53,7 +53,7 @@ namespace Pinetime {
*
*/
static constexpr size_t startAddress = 0x0B4000;
- static constexpr size_t size = 0x3C0000;
+ static constexpr size_t size = 0x34C000;
static constexpr size_t blockSize = 4096;
bool resourcesValid = false;
diff --git a/src/components/motor/MotorController.cpp b/src/components/motor/MotorController.cpp
index 3afa0ced..f596c718 100644
--- a/src/components/motor/MotorController.cpp
+++ b/src/components/motor/MotorController.cpp
@@ -2,30 +2,42 @@
#include <hal/nrf_gpio.h>
#include "systemtask/SystemTask.h"
#include "app_timer.h"
+#include "drivers/PinMap.h"
-APP_TIMER_DEF(vibTimer);
+APP_TIMER_DEF(shortVibTimer);
+APP_TIMER_DEF(longVibTimer);
using namespace Pinetime::Controllers;
-MotorController::MotorController(Controllers::Settings& settingsController) : settingsController {settingsController} {
+void MotorController::Init() {
+ nrf_gpio_cfg_output(PinMap::Motor);
+ nrf_gpio_pin_set(PinMap::Motor);
+ app_timer_init();
+
+ app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor);
+ app_timer_create(&longVibTimer, APP_TIMER_MODE_REPEATED, Ring);
}
-void MotorController::Init() {
- nrf_gpio_cfg_output(pinMotor);
- nrf_gpio_pin_set(pinMotor);
- app_timer_create(&vibTimer, APP_TIMER_MODE_SINGLE_SHOT, vibrate);
+void MotorController::Ring(void* p_context) {
+ auto* motorController = static_cast<MotorController*>(p_context);
+ motorController->RunForDuration(50);
}
-void MotorController::SetDuration(uint8_t motorDuration) {
+void MotorController::RunForDuration(uint8_t motorDuration) {
+ nrf_gpio_pin_clear(PinMap::Motor);
+ app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr);
+}
- if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF)
- return;
+void MotorController::StartRinging() {
+ Ring(this);
+ app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this);
+}
- nrf_gpio_pin_clear(pinMotor);
- /* Start timer for motorDuration miliseconds and timer triggers vibrate() when it finishes*/
- app_timer_start(vibTimer, APP_TIMER_TICKS(motorDuration), NULL);
+void MotorController::StopRinging() {
+ app_timer_stop(longVibTimer);
+ nrf_gpio_pin_set(PinMap::Motor);
}
-void MotorController::vibrate(void* p_context) {
- nrf_gpio_pin_set(pinMotor);
-} \ No newline at end of file
+void MotorController::StopMotor(void* p_context) {
+ nrf_gpio_pin_set(PinMap::Motor);
+}
diff --git a/src/components/motor/MotorController.h b/src/components/motor/MotorController.h
index df61af78..c9326d57 100644
--- a/src/components/motor/MotorController.h
+++ b/src/components/motor/MotorController.h
@@ -1,22 +1,22 @@
#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:
- MotorController(Controllers::Settings& settingsController);
+ MotorController() = default;
+
void Init();
- void SetDuration(uint8_t motorDuration);
+ void RunForDuration(uint8_t motorDuration);
+ void StartRinging();
+ static void StopRinging();
private:
- Controllers::Settings& settingsController;
- static void vibrate(void* p_context);
+ static void Ring(void* p_context);
+ static void StopMotor(void* p_context);
};
}
}
diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h
index 93d6d217..871ff3b6 100644
--- a/src/components/settings/Settings.h
+++ b/src/components/settings/Settings.h
@@ -11,12 +11,20 @@ namespace Pinetime {
class Settings {
public:
enum class ClockType : uint8_t { H24, H12 };
- enum class Vibration : uint8_t { ON, OFF };
+ enum class Notification : uint8_t { ON, OFF };
enum class WakeUpMode : uint8_t {
SingleTap = 0,
DoubleTap = 1,
RaiseWrist = 2,
};
+ enum class Colors : uint8_t {
+ White, Silver, Gray, Black, Red, Maroon, Yellow, Olive, Lime, Green, Cyan, Teal, Blue, Navy, Magenta, Purple, Orange
+ };
+ struct PineTimeStyle {
+ Colors ColorTime = Colors::Teal;
+ Colors ColorBar = Colors::Teal;
+ Colors ColorBG = Colors::Black;
+ };
Settings(Pinetime::Controllers::FS& fs);
@@ -33,10 +41,38 @@ namespace Pinetime {
return settings.clockFace;
};
+ void SetPTSColorTime(Colors colorTime) {
+ if (colorTime != settings.PTS.ColorTime)
+ settingsChanged = true;
+ settings.PTS.ColorTime = colorTime;
+ };
+ Colors GetPTSColorTime() const {
+ return settings.PTS.ColorTime;
+ };
+
+ void SetPTSColorBar(Colors colorBar) {
+ if (colorBar != settings.PTS.ColorBar)
+ settingsChanged = true;
+ settings.PTS.ColorBar = colorBar;
+ };
+ Colors GetPTSColorBar() const {
+ return settings.PTS.ColorBar;
+ };
+
+ void SetPTSColorBG(Colors colorBG) {
+ if (colorBG != settings.PTS.ColorBG)
+ settingsChanged = true;
+ settings.PTS.ColorBG = colorBG;
+ };
+ Colors GetPTSColorBG() const {
+ return settings.PTS.ColorBG;
+ };
+
void SetAppMenu(uint8_t menu) {
appMenu = menu;
};
- uint8_t GetAppMenu() {
+
+ uint8_t GetAppMenu() const {
return appMenu;
};
@@ -57,14 +93,14 @@ namespace Pinetime {
return settings.clockType;
};
- void SetVibrationStatus(Vibration status) {
- if (status != settings.vibrationStatus) {
+ void SetNotificationStatus(Notification status) {
+ if (status != settings.notificationStatus) {
settingsChanged = true;
}
- settings.vibrationStatus = status;
+ settings.notificationStatus = status;
};
- Vibration GetVibrationStatus() const {
- return settings.vibrationStatus;
+ Notification GetNotificationStatus() const {
+ return settings.notificationStatus;
};
void SetScreenTimeOut(uint32_t timeout) {
@@ -78,7 +114,7 @@ namespace Pinetime {
};
void setWakeUpMode(WakeUpMode wakeUp, bool enabled) {
- if (!isWakeUpModeOn(wakeUp)) {
+ if (enabled != isWakeUpModeOn(wakeUp)) {
settingsChanged = true;
}
settings.wakeUpMode.set(static_cast<size_t>(wakeUp), enabled);
@@ -127,18 +163,19 @@ namespace Pinetime {
private:
Pinetime::Controllers::FS& fs;
- static constexpr uint32_t settingsVersion = 0x0001;
+ static constexpr uint32_t settingsVersion = 0x0002;
struct SettingsData {
-
uint32_t version = settingsVersion;
uint32_t stepsGoal = 10000;
uint32_t screenTimeOut = 15000;
ClockType clockType = ClockType::H24;
- Vibration vibrationStatus = Vibration::ON;
+ Notification notificationStatus = Notification::ON;
uint8_t clockFace = 0;
+ PineTimeStyle PTS;
+
std::bitset<3> wakeUpMode {0};
Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium;