Merge remote-tracking branch 'upstream/develop' into pts-settings
commit
18e3cc7038
@ -0,0 +1,17 @@
|
||||
# Motion Service
|
||||
## Introduction
|
||||
The motion service exposes step count and raw X/Y/Z motion value as READ and NOTIFY characteristics.
|
||||
|
||||
## Service
|
||||
The service UUID is **00020000-78fc-48fe-8e23-433b3a1942d0**
|
||||
|
||||
## Characteristics
|
||||
### Step count (UUID 00020001-78fc-48fe-8e23-433b3a1942d0)
|
||||
The current number of steps represented as a single `uint32_t` (4 bytes) value.
|
||||
|
||||
### Raw motion values (UUID 00020002-78fc-48fe-8e23-433b3a1942d0)
|
||||
The current raw motion values. This is a 3 `int16_t` array:
|
||||
|
||||
- [0] : X
|
||||
- [1] : Y
|
||||
- [2] : Z
|
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace Pinetime {
|
||||
namespace Controllers {
|
||||
enum class ButtonActions { None, Click, DoubleClick, LongPress, LongerPress };
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
#include "ButtonHandler.h"
|
||||
|
||||
using namespace Pinetime::Controllers;
|
||||
|
||||
void ButtonTimerCallback(TimerHandle_t xTimer) {
|
||||
auto* sysTask = static_cast<Pinetime::System::SystemTask*>(pvTimerGetTimerID(xTimer));
|
||||
sysTask->PushMessage(Pinetime::System::Messages::HandleButtonTimerEvent);
|
||||
}
|
||||
|
||||
void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) {
|
||||
buttonTimer = xTimerCreate("buttonTimer", 0, pdFALSE, systemTask, ButtonTimerCallback);
|
||||
}
|
||||
|
||||
ButtonActions ButtonHandler::HandleEvent(Events event) {
|
||||
static constexpr TickType_t doubleClickTime = pdMS_TO_TICKS(200);
|
||||
static constexpr TickType_t longPressTime = pdMS_TO_TICKS(400);
|
||||
static constexpr TickType_t longerPressTime = pdMS_TO_TICKS(2000);
|
||||
|
||||
if (event == Events::Press) {
|
||||
buttonPressed = true;
|
||||
} else if (event == Events::Release) {
|
||||
releaseTime = xTaskGetTickCount();
|
||||
buttonPressed = false;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case States::Idle:
|
||||
if (event == Events::Press) {
|
||||
xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
|
||||
xTimerStart(buttonTimer, 0);
|
||||
state = States::Pressed;
|
||||
}
|
||||
break;
|
||||
case States::Pressed:
|
||||
if (event == Events::Press) {
|
||||
if (xTaskGetTickCount() - releaseTime < doubleClickTime) {
|
||||
xTimerStop(buttonTimer, 0);
|
||||
state = States::Idle;
|
||||
return ButtonActions::DoubleClick;
|
||||
}
|
||||
} else if (event == Events::Release) {
|
||||
xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
|
||||
xTimerStart(buttonTimer, 0);
|
||||
} else if (event == Events::Timer) {
|
||||
if (buttonPressed) {
|
||||
xTimerChangePeriod(buttonTimer, longPressTime - doubleClickTime, 0);
|
||||
xTimerStart(buttonTimer, 0);
|
||||
state = States::Holding;
|
||||
} else {
|
||||
state = States::Idle;
|
||||
return ButtonActions::Click;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case States::Holding:
|
||||
if (event == Events::Release) {
|
||||
xTimerStop(buttonTimer, 0);
|
||||
state = States::Idle;
|
||||
return ButtonActions::Click;
|
||||
} else if (event == Events::Timer) {
|
||||
xTimerChangePeriod(buttonTimer, longerPressTime - longPressTime - doubleClickTime, 0);
|
||||
xTimerStart(buttonTimer, 0);
|
||||
state = States::LongHeld;
|
||||
return ButtonActions::LongPress;
|
||||
}
|
||||
break;
|
||||
case States::LongHeld:
|
||||
if (event == Events::Release) {
|
||||
xTimerStop(buttonTimer, 0);
|
||||
state = States::Idle;
|
||||
} else if (event == Events::Timer) {
|
||||
state = States::Idle;
|
||||
return ButtonActions::LongerPress;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return ButtonActions::None;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "ButtonActions.h"
|
||||
#include "systemtask/SystemTask.h"
|
||||
#include <FreeRTOS.h>
|
||||
#include <timers.h>
|
||||
|
||||
namespace Pinetime {
|
||||
namespace Controllers {
|
||||
class ButtonHandler {
|
||||
public:
|
||||
enum class Events : uint8_t { Press, Release, Timer };
|
||||
void Init(Pinetime::System::SystemTask* systemTask);
|
||||
ButtonActions HandleEvent(Events event);
|
||||
|
||||
private:
|
||||
enum class States : uint8_t { Idle, Pressed, Holding, LongHeld };
|
||||
TickType_t releaseTime = 0;
|
||||
TimerHandle_t buttonTimer;
|
||||
bool buttonPressed = false;
|
||||
States state = States::Idle;
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
#include "MotionService.h"
|
||||
#include "components/motion//MotionController.h"
|
||||
#include "systemtask/SystemTask.h"
|
||||
|
||||
using namespace Pinetime::Controllers;
|
||||
|
||||
namespace {
|
||||
// 0002yyxx-78fc-48fe-8e23-433b3a1942d0
|
||||
constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) {
|
||||
return ble_uuid128_t{
|
||||
.u = {.type = BLE_UUID_TYPE_128},
|
||||
.value = { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x03, 0x00 }
|
||||
};
|
||||
}
|
||||
|
||||
// 00020000-78fc-48fe-8e23-433b3a1942d0
|
||||
constexpr ble_uuid128_t BaseUuid() {
|
||||
return CharUuid(0x00, 0x00);
|
||||
}
|
||||
|
||||
constexpr ble_uuid128_t motionServiceUuid {BaseUuid()};
|
||||
constexpr ble_uuid128_t stepCountCharUuid {CharUuid(0x01, 0x00)};
|
||||
constexpr ble_uuid128_t motionValuesCharUuid {CharUuid(0x02, 0x00)};
|
||||
|
||||
int MotionServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
||||
auto* motionService = static_cast<MotionService*>(arg);
|
||||
return motionService->OnStepCountRequested(conn_handle, attr_handle, ctxt);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Refactoring - remove dependency to SystemTask
|
||||
MotionService::MotionService(Pinetime::System::SystemTask& system, Controllers::MotionController& motionController)
|
||||
: system {system},
|
||||
motionController {motionController},
|
||||
characteristicDefinition {{.uuid = &stepCountCharUuid.u,
|
||||
.access_cb = MotionServiceCallback,
|
||||
.arg = this,
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
||||
.val_handle = &stepCountHandle},
|
||||
{.uuid = &motionValuesCharUuid.u,
|
||||
.access_cb = MotionServiceCallback,
|
||||
.arg = this,
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
||||
.val_handle = &motionValuesHandle},
|
||||
{0}},
|
||||
serviceDefinition {
|
||||
{
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = &motionServiceUuid.u,
|
||||
.characteristics = characteristicDefinition
|
||||
},
|
||||
{0},
|
||||
} {
|
||||
// TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service)
|
||||
motionController.SetService(this);
|
||||
}
|
||||
|
||||
void MotionService::Init() {
|
||||
int res = 0;
|
||||
res = ble_gatts_count_cfg(serviceDefinition);
|
||||
ASSERT(res == 0);
|
||||
|
||||
res = ble_gatts_add_svcs(serviceDefinition);
|
||||
ASSERT(res == 0);
|
||||
}
|
||||
|
||||
int MotionService::OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
|
||||
if (attributeHandle == stepCountHandle) {
|
||||
NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle);
|
||||
uint32_t buffer = motionController.NbSteps();
|
||||
|
||||
int res = os_mbuf_append(context->om, &buffer, 4);
|
||||
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
} else if(attributeHandle == motionValuesHandle) {
|
||||
int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() };
|
||||
|
||||
int res = os_mbuf_append(context->om, buffer, 3 * sizeof(int16_t));
|
||||
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MotionService::OnNewStepCountValue(uint32_t stepCount) {
|
||||
if(!stepCountNoficationEnabled) return;
|
||||
|
||||
uint32_t buffer = stepCount;
|
||||
auto* om = ble_hs_mbuf_from_flat(&buffer, 4);
|
||||
|
||||
uint16_t connectionHandle = system.nimble().connHandle();
|
||||
|
||||
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
ble_gattc_notify_custom(connectionHandle, stepCountHandle, om);
|
||||
}
|
||||
void MotionService::OnNewMotionValues(int16_t x, int16_t y, int16_t z) {
|
||||
if(!motionValuesNoficationEnabled) return;
|
||||
|
||||
int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() };
|
||||
auto* om = ble_hs_mbuf_from_flat(buffer, 3 * sizeof(int16_t));
|
||||
|
||||
uint16_t connectionHandle = system.nimble().connHandle();
|
||||
|
||||
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om);
|
||||
}
|
||||
|
||||
void MotionService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
|
||||
if(attributeHandle == stepCountHandle)
|
||||
stepCountNoficationEnabled = true;
|
||||
else if(attributeHandle == motionValuesHandle)
|
||||
motionValuesNoficationEnabled = true;
|
||||
}
|
||||
|
||||
void MotionService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
|
||||
if(attributeHandle == stepCountHandle)
|
||||
stepCountNoficationEnabled = false;
|
||||
else if(attributeHandle == motionValuesHandle)
|
||||
motionValuesNoficationEnabled = false;
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#define min // workaround: nimble's min/max macros conflict with libstdc++
|
||||
#define max
|
||||
#include <host/ble_gap.h>
|
||||
#include <atomic>
|
||||
#undef max
|
||||
#undef min
|
||||
|
||||
namespace Pinetime {
|
||||
namespace System {
|
||||
class SystemTask;
|
||||
}
|
||||
namespace Controllers {
|
||||
class MotionController;
|
||||
class MotionService {
|
||||
public:
|
||||
MotionService(Pinetime::System::SystemTask& system, Controllers::MotionController& motionController);
|
||||
void Init();
|
||||
int OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
|
||||
void OnNewStepCountValue(uint32_t stepCount);
|
||||
void OnNewMotionValues(int16_t x, int16_t y, int16_t z);
|
||||
|
||||
void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
|
||||
void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
|
||||
|
||||
private:
|
||||
Pinetime::System::SystemTask& system;
|
||||
Controllers::MotionController& motionController;
|
||||
|
||||
struct ble_gatt_chr_def characteristicDefinition[3];
|
||||
struct ble_gatt_svc_def serviceDefinition[2];
|
||||
|
||||
uint16_t stepCountHandle;
|
||||
uint16_t motionValuesHandle;
|
||||
std::atomic_bool stepCountNoficationEnabled {false};
|
||||
std::atomic_bool motionValuesNoficationEnabled {false};
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue