diff options
Diffstat (limited to 'src/components/ble/MusicService.cpp')
| -rw-r--r-- | src/components/ble/MusicService.cpp | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/src/components/ble/MusicService.cpp b/src/components/ble/MusicService.cpp index c99aa1e..fc7cef0 100644 --- a/src/components/ble/MusicService.cpp +++ b/src/components/ble/MusicService.cpp @@ -22,10 +22,8 @@ namespace { // 0000yyxx-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, 0x00, 0x00 } - }; + 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, 0x00, 0x00}}; } // 00000000-78fc-48fe-8e23-433b3a1942d0 @@ -111,8 +109,7 @@ Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask& .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; characteristicDefinition[13] = {0}; - serviceDefinition[0] = { - .type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &msUuid.u, .characteristics = characteristicDefinition}; + serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &msUuid.u, .characteristics = characteristicDefinition}; serviceDefinition[1] = {0}; } @@ -137,9 +134,9 @@ int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_ os_mbuf_copydata(ctxt->om, 0, bufferSize, data); if (notifSize > bufferSize) { - data[bufferSize-1] = '.'; - data[bufferSize-2] = '.'; - data[bufferSize-3] = '.'; + data[bufferSize - 1] = '.'; + data[bufferSize - 2] = '.'; + data[bufferSize - 3] = '.'; } data[bufferSize] = '\0'; @@ -152,12 +149,21 @@ int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_ albumName = s; } else if (ble_uuid_cmp(ctxt->chr->uuid, &msStatusCharUuid.u) == 0) { playing = s[0]; + // These variables need to be updated, because the progress may not be updated immediately, + // leading to getProgress() returning an incorrect position. + if (playing) { + trackProgressUpdateTime = xTaskGetTickCount(); + } else { + trackProgress += + static_cast<int>((static_cast<float>(xTaskGetTickCount() - trackProgressUpdateTime) / 1024.0f) * getPlaybackSpeed()); + } } else if (ble_uuid_cmp(ctxt->chr->uuid, &msRepeatCharUuid.u) == 0) { repeat = s[0]; } else if (ble_uuid_cmp(ctxt->chr->uuid, &msShuffleCharUuid.u) == 0) { shuffle = s[0]; } else if (ble_uuid_cmp(ctxt->chr->uuid, &msPositionCharUuid.u) == 0) { trackProgress = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; + trackProgressUpdateTime = xTaskGetTickCount(); } else if (ble_uuid_cmp(ctxt->chr->uuid, &msTotalLengthCharUuid.u) == 0) { trackLength = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; } else if (ble_uuid_cmp(ctxt->chr->uuid, &msTrackNumberCharUuid.u) == 0) { @@ -192,6 +198,10 @@ float Pinetime::Controllers::MusicService::getPlaybackSpeed() const { } int Pinetime::Controllers::MusicService::getProgress() const { + if (isPlaying()) { + return trackProgress + + static_cast<int>((static_cast<float>(xTaskGetTickCount() - trackProgressUpdateTime) / 1024.0f) * getPlaybackSpeed()); + } return trackProgress; } |
