Skip to content

Commit 9f52b88

Browse files
committed
Motion: Expose acceleration FIFO via BLE
The motion BLE service now broadcasts the entire FIFO buffer, in order to expose a higher frequency measurement. A high enough MTU has to be negotiated to fit all max. 192 bytes. The format is backwards-compatible.
1 parent e474afd commit 9f52b88

5 files changed

Lines changed: 28 additions & 35 deletions

File tree

src/components/ble/MotionService.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ using namespace Pinetime::Controllers;
88
namespace {
99
// 0003yyxx-78fc-48fe-8e23-433b3a1942d0
1010
constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) {
11-
return ble_uuid128_t{
12-
.u = {.type = BLE_UUID_TYPE_128},
13-
.value = { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x03, 0x00 }
14-
};
11+
return ble_uuid128_t {.u = {.type = BLE_UUID_TYPE_128},
12+
.value = {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x03, 0x00}};
1513
}
1614

1715
// 00030000-78fc-48fe-8e23-433b3a1942d0
@@ -45,11 +43,7 @@ MotionService::MotionService(Pinetime::System::SystemTask& system, Controllers::
4543
.val_handle = &motionValuesHandle},
4644
{0}},
4745
serviceDefinition {
48-
{
49-
.type = BLE_GATT_SVC_TYPE_PRIMARY,
50-
.uuid = &motionServiceUuid.u,
51-
.characteristics = characteristicDefinition
52-
},
46+
{.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &motionServiceUuid.u, .characteristics = characteristicDefinition},
5347
{0},
5448
} {
5549
// TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service)
@@ -72,8 +66,8 @@ int MotionService::OnStepCountRequested(uint16_t connectionHandle, uint16_t attr
7266

7367
int res = os_mbuf_append(context->om, &buffer, 4);
7468
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
75-
} else if(attributeHandle == motionValuesHandle) {
76-
int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() };
69+
} else if (attributeHandle == motionValuesHandle) {
70+
int16_t buffer[3] = {motionController.X(), motionController.Y(), motionController.Z()};
7771

7872
int res = os_mbuf_append(context->om, buffer, 3 * sizeof(int16_t));
7973
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
@@ -82,7 +76,8 @@ int MotionService::OnStepCountRequested(uint16_t connectionHandle, uint16_t attr
8276
}
8377

8478
void MotionService::OnNewStepCountValue(uint32_t stepCount) {
85-
if(!stepCountNoficationEnabled) return;
79+
if (!stepCountNoficationEnabled)
80+
return;
8681

8782
uint32_t buffer = stepCount;
8883
auto* om = ble_hs_mbuf_from_flat(&buffer, 4);
@@ -95,31 +90,33 @@ void MotionService::OnNewStepCountValue(uint32_t stepCount) {
9590

9691
ble_gattc_notify_custom(connectionHandle, stepCountHandle, om);
9792
}
98-
void MotionService::OnNewMotionValues(int16_t x, int16_t y, int16_t z) {
99-
if(!motionValuesNoficationEnabled) return;
93+
void MotionService::OnNewMotionValues(int16_t* samples, uint16_t samples_length) {
94+
if (!motionValuesNoficationEnabled)
95+
return;
10096

101-
int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() };
102-
auto* om = ble_hs_mbuf_from_flat(buffer, 3 * sizeof(int16_t));
97+
if (samples_length > 0 && samples != nullptr) {
98+
auto* om = ble_hs_mbuf_from_flat(samples, samples_length * 3 * sizeof(int16_t));
10399

104-
uint16_t connectionHandle = system.nimble().connHandle();
100+
uint16_t connectionHandle = system.nimble().connHandle();
105101

106-
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
107-
return;
108-
}
102+
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
103+
return;
104+
}
109105

110-
ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om);
106+
ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om);
107+
}
111108
}
112109

113110
void MotionService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
114-
if(attributeHandle == stepCountHandle)
111+
if (attributeHandle == stepCountHandle)
115112
stepCountNoficationEnabled = true;
116-
else if(attributeHandle == motionValuesHandle)
113+
else if (attributeHandle == motionValuesHandle)
117114
motionValuesNoficationEnabled = true;
118115
}
119116

120117
void MotionService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
121-
if(attributeHandle == stepCountHandle)
118+
if (attributeHandle == stepCountHandle)
122119
stepCountNoficationEnabled = false;
123-
else if(attributeHandle == motionValuesHandle)
120+
else if (attributeHandle == motionValuesHandle)
124121
motionValuesNoficationEnabled = false;
125122
}

src/components/ble/MotionService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Pinetime {
1818
void Init();
1919
int OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
2020
void OnNewStepCountValue(uint32_t stepCount);
21-
void OnNewMotionValues(int16_t x, int16_t y, int16_t z);
21+
void OnNewMotionValues(int16_t* samples, uint16_t samples_length);
2222

2323
void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
2424
void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);

src/components/motion/MotionController.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#include "os/os_cputime.h"
33
using namespace Pinetime::Controllers;
44

5-
void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
5+
void MotionController::Update(uint32_t nbSteps, int16_t x, int16_t y, int16_t z, int16_t* samples, uint16_t samples_length) {
66
if (this->nbSteps != nbSteps && service != nullptr) {
77
service->OnNewStepCountValue(nbSteps);
88
}
99

1010
if (service != nullptr && (this->x != x || this->y != y || this->z != z)) {
11-
service->OnNewMotionValues(x, y, z);
11+
service->OnNewMotionValues(samples, samples_length);
1212
}
1313

1414
this->x = x;

src/components/motion/MotionController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Pinetime {
88
namespace Controllers {
99
class MotionController {
1010
public:
11-
void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps);
11+
void Update(uint32_t nbSteps, int16_t x, int16_t y, int16_t z, int16_t* samples, uint16_t samples_length);
1212

1313
int16_t X() const {
1414
return x;

src/systemtask/SystemTask.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,6 @@ void SystemTask::UpdateMotion() {
480480
return;
481481
}
482482

483-
if (state == SystemTaskState::Sleeping && !(settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) ||
484-
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake))) {
485-
return;
486-
}
487-
488483
if (stepCounterMustBeReset) {
489484
motionSensor.ResetStepCounter();
490485
stepCounterMustBeReset = false;
@@ -493,7 +488,8 @@ void SystemTask::UpdateMotion() {
493488
auto motionValues = motionSensor.Process();
494489

495490
motionController.IsSensorOk(motionSensor.IsInitialized());
496-
motionController.Update(motionValues.x, motionValues.y, motionValues.z, motionValues.steps);
491+
motionController.Update(
492+
motionValues.steps, motionValues.x, motionValues.y, motionValues.z, motionValues.samples, motionValues.samples_length);
497493

498494
if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) &&
499495
motionController.Should_RaiseWake(state == SystemTaskState::Sleeping)) {

0 commit comments

Comments
 (0)