From f6f28a95f6405c6d99670a5bc25ad0b22d2cdd05 Mon Sep 17 00:00:00 2001 From: Avamander Date: Mon, 17 May 2021 02:08:12 +0300 Subject: Used a macro for UUID generation, switched from C-style casts to reinterpret_cast, renamed callback --- src/components/ble/MusicService.cpp | 194 +++++++++++++----------------------- src/components/ble/MusicService.h | 54 ++++------ 2 files changed, 93 insertions(+), 155 deletions(-) (limited to 'src/components') diff --git a/src/components/ble/MusicService.cpp b/src/components/ble/MusicService.cpp index 36bf2709..30dd7274 100644 --- a/src/components/ble/MusicService.cpp +++ b/src/components/ble/MusicService.cpp @@ -18,117 +18,68 @@ #include "MusicService.h" #include "systemtask/SystemTask.h" -int MSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { - auto musicService = static_cast(arg); - return musicService->OnCommand(conn_handle, attr_handle, ctxt); +int MusicCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + return static_cast(arg)->OnCommand(conn_handle, attr_handle, ctxt); } Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask& system) : m_system(system) { - msUuid.value[14] = msId[0]; - msUuid.value[15] = msId[1]; - - msEventCharUuid.value[12] = msEventCharId[0]; - msEventCharUuid.value[13] = msEventCharId[1]; - msEventCharUuid.value[14] = msId[0]; - msEventCharUuid.value[15] = msId[1]; - - msStatusCharUuid.value[12] = msStatusCharId[0]; - msStatusCharUuid.value[13] = msStatusCharId[1]; - msStatusCharUuid.value[14] = msId[0]; - msStatusCharUuid.value[15] = msId[1]; - - msTrackCharUuid.value[12] = msTrackCharId[0]; - msTrackCharUuid.value[13] = msTrackCharId[1]; - msTrackCharUuid.value[14] = msId[0]; - msTrackCharUuid.value[15] = msId[1]; - - msArtistCharUuid.value[12] = msArtistCharId[0]; - msArtistCharUuid.value[13] = msArtistCharId[1]; - msArtistCharUuid.value[14] = msId[0]; - msArtistCharUuid.value[15] = msId[1]; - - msAlbumCharUuid.value[12] = msAlbumCharId[0]; - msAlbumCharUuid.value[13] = msAlbumCharId[1]; - msAlbumCharUuid.value[14] = msId[0]; - msAlbumCharUuid.value[15] = msId[1]; - - msPositionCharUuid.value[12] = msPositionCharId[0]; - msPositionCharUuid.value[13] = msPositionCharId[1]; - msPositionCharUuid.value[14] = msId[0]; - msPositionCharUuid.value[15] = msId[1]; - - msTotalLengthCharUuid.value[12] = msTotalLengthCharId[0]; - msTotalLengthCharUuid.value[13] = msTotalLengthCharId[1]; - msTotalLengthCharUuid.value[14] = msId[0]; - msTotalLengthCharUuid.value[15] = msId[1]; - - msTrackNumberCharUuid.value[12] = msTrackNumberCharId[0]; - msTrackNumberCharUuid.value[13] = msTrackNumberCharId[1]; - msTrackNumberCharUuid.value[14] = msId[0]; - msTrackNumberCharUuid.value[15] = msId[1]; - - msTrackTotalCharUuid.value[12] = msTrackTotalCharId[0]; - msTrackTotalCharUuid.value[13] = msTrackTotalCharId[1]; - msTrackTotalCharUuid.value[14] = msId[0]; - msTrackTotalCharUuid.value[15] = msId[1]; - - msPlaybackSpeedCharUuid.value[12] = msPlaybackSpeedCharId[0]; - msPlaybackSpeedCharUuid.value[13] = msPlaybackSpeedCharId[1]; - msPlaybackSpeedCharUuid.value[14] = msId[0]; - msPlaybackSpeedCharUuid.value[15] = msId[1]; - - msRepeatCharUuid.value[12] = msRepeatCharId[0]; - msRepeatCharUuid.value[13] = msRepeatCharId[1]; - msRepeatCharUuid.value[14] = msId[0]; - msRepeatCharUuid.value[15] = msId[1]; - - msShuffleCharUuid.value[12] = msShuffleCharId[0]; - msShuffleCharUuid.value[13] = msShuffleCharId[1]; - msShuffleCharUuid.value[14] = msId[0]; - msShuffleCharUuid.value[15] = msId[1]; - - characteristicDefinition[0] = {.uuid = (ble_uuid_t*) (&msEventCharUuid), - .access_cb = MSCallback, + characteristicDefinition[0] = {.uuid = reinterpret_cast(&msEventCharUuid), + .access_cb = MusicCallback, .arg = this, .flags = BLE_GATT_CHR_F_NOTIFY, .val_handle = &eventHandle}; - characteristicDefinition[1] = { - .uuid = (ble_uuid_t*) (&msStatusCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[2] = { - .uuid = (ble_uuid_t*) (&msTrackCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[3] = { - .uuid = (ble_uuid_t*) (&msArtistCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[4] = { - .uuid = (ble_uuid_t*) (&msAlbumCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[5] = { - .uuid = (ble_uuid_t*) (&msPositionCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[6] = {.uuid = (ble_uuid_t*) (&msTotalLengthCharUuid), - .access_cb = MSCallback, + characteristicDefinition[1] = {.uuid = reinterpret_cast(&msStatusCharUuid), + .access_cb = MusicCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[7] = {.uuid = (ble_uuid_t*) (&msTotalLengthCharUuid), - .access_cb = MSCallback, + characteristicDefinition[2] = {.uuid = reinterpret_cast(&msTrackCharUuid), + .access_cb = MusicCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[8] = {.uuid = (ble_uuid_t*) (&msTrackNumberCharUuid), - .access_cb = MSCallback, + characteristicDefinition[3] = {.uuid = reinterpret_cast(&msArtistCharUuid), + .access_cb = MusicCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[9] = {.uuid = (ble_uuid_t*) (&msTrackTotalCharUuid), - .access_cb = MSCallback, + characteristicDefinition[4] = {.uuid = reinterpret_cast(&msAlbumCharUuid), + .access_cb = MusicCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[10] = {.uuid = (ble_uuid_t*) (&msPlaybackSpeedCharUuid), - .access_cb = MSCallback, + characteristicDefinition[5] = {.uuid = reinterpret_cast(&msPositionCharUuid), + .access_cb = MusicCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[6] = {.uuid = reinterpret_cast(&msTotalLengthCharUuid), + .access_cb = MusicCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[7] = {.uuid = reinterpret_cast(&msTotalLengthCharUuid), + .access_cb = MusicCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[8] = {.uuid = reinterpret_cast(&msTrackNumberCharUuid), + .access_cb = MusicCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[9] = {.uuid = reinterpret_cast(&msTrackTotalCharUuid), + .access_cb = MusicCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[10] = {.uuid = reinterpret_cast(&msPlaybackSpeedCharUuid), + .access_cb = MusicCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[11] = {.uuid = reinterpret_cast(&msRepeatCharUuid), + .access_cb = MusicCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[12] = {.uuid = reinterpret_cast(&msShuffleCharUuid), + .access_cb = MusicCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[11] = { - .uuid = (ble_uuid_t*) (&msRepeatCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; - characteristicDefinition[12] = { - .uuid = (ble_uuid_t*) (&msShuffleCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; characteristicDefinition[13] = {0}; - serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = (ble_uuid_t*) &msUuid, .characteristics = characteristicDefinition}; + serviceDefinition[0] = { + .type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = reinterpret_cast(&msUuid), .characteristics = characteristicDefinition}; serviceDefinition[1] = {0}; artistName = "Waiting for"; @@ -143,7 +94,7 @@ Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask& } void Pinetime::Controllers::MusicService::Init() { - int res = 0; + uint8_t res = 0; res = ble_gatts_count_cfg(serviceDefinition); ASSERT(res == 0); @@ -152,60 +103,67 @@ void Pinetime::Controllers::MusicService::Init() { } int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { - if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { size_t notifSize = OS_MBUF_PKTLEN(ctxt->om); - uint8_t data[notifSize + 1]; + char data[notifSize + 1]; 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*) &msArtistCharUuid) == 0) { + char* s = &data[0]; + if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msArtistCharUuid)) == 0) { artistName = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msTrackCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msTrackCharUuid)) == 0) { trackName = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msAlbumCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msAlbumCharUuid)) == 0) { albumName = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msStatusCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msStatusCharUuid)) == 0) { playing = s[0]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msRepeatCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msRepeatCharUuid)) == 0) { repeat = s[0]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msShuffleCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msShuffleCharUuid)) == 0) { shuffle = s[0]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msPositionCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msPositionCharUuid)) == 0) { trackProgress = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msTotalLengthCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msTotalLengthCharUuid)) == 0) { trackLength = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msTrackNumberCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msTrackNumberCharUuid)) == 0) { trackNumber = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msTrackTotalCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msTrackTotalCharUuid)) == 0) { tracksTotal = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msPlaybackSpeedCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, reinterpret_cast(&msPlaybackSpeedCharUuid)) == 0) { playbackSpeed = static_cast(((s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3])) / 100.0f; } } return 0; } -std::string Pinetime::Controllers::MusicService::getAlbum() { +std::string Pinetime::Controllers::MusicService::getAlbum() const { return albumName; } -std::string Pinetime::Controllers::MusicService::getArtist() { +std::string Pinetime::Controllers::MusicService::getArtist() const { return artistName; } -std::string Pinetime::Controllers::MusicService::getTrack() { +std::string Pinetime::Controllers::MusicService::getTrack() const { return trackName; } -bool Pinetime::Controllers::MusicService::isPlaying() { +bool Pinetime::Controllers::MusicService::isPlaying() const { return playing; } -float Pinetime::Controllers::MusicService::getPlaybackSpeed() { +float Pinetime::Controllers::MusicService::getPlaybackSpeed() const { return playbackSpeed; } +int Pinetime::Controllers::MusicService::getProgress() const { + return trackProgress; +} + +int Pinetime::Controllers::MusicService::getTrackLength() const { + return trackLength; +} + void Pinetime::Controllers::MusicService::event(char event) { auto* om = ble_hs_mbuf_from_flat(&event, 1); @@ -216,12 +174,4 @@ void Pinetime::Controllers::MusicService::event(char event) { } ble_gattc_notify_custom(connectionHandle, eventHandle, om); -} - -int Pinetime::Controllers::MusicService::getProgress() { - return trackProgress; -} - -int Pinetime::Controllers::MusicService::getTrackLength() { - return trackLength; -} +} \ No newline at end of file diff --git a/src/components/ble/MusicService.h b/src/components/ble/MusicService.h index 5f5343e4..4314db0f 100644 --- a/src/components/ble/MusicService.h +++ b/src/components/ble/MusicService.h @@ -29,6 +29,8 @@ // 00000000-78fc-48fe-8e23-433b3a1942d0 #define MUSIC_SERVICE_UUID_BASE \ { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x00, 0x00 } +#define MUSIC_SERVICE_CHAR_UUID(x, y) \ + { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x00, 0x00 } namespace Pinetime { namespace System { @@ -46,19 +48,19 @@ namespace Pinetime { void event(char event); - std::string getArtist(); + std::string getArtist() const; - std::string getTrack(); + std::string getTrack() const; - std::string getAlbum(); + std::string getAlbum() const; - int getProgress(); + int getProgress() const; - int getTrackLength(); + int getTrackLength() const; - float getPlaybackSpeed(); + float getPlaybackSpeed() const; - bool isPlaying(); + bool isPlaying() const; static const char EVENT_MUSIC_OPEN = 0xe0; static const char EVENT_MUSIC_PLAY = 0x00; @@ -71,34 +73,20 @@ namespace Pinetime { enum MusicStatus { NotPlaying = 0x00, Playing = 0x01 }; private: - static constexpr uint8_t msId[2] = {0x00, 0x00}; - static constexpr uint8_t msEventCharId[2] = {0x01, 0x00}; - static constexpr uint8_t msStatusCharId[2] = {0x02, 0x00}; - static constexpr uint8_t msArtistCharId[2] = {0x03, 0x00}; - static constexpr uint8_t msTrackCharId[2] = {0x04, 0x00}; - static constexpr uint8_t msAlbumCharId[2] = {0x05, 0x00}; - static constexpr uint8_t msPositionCharId[2] = {0x06, 0x00}; - static constexpr uint8_t msTotalLengthCharId[2] = {0x07, 0x00}; - static constexpr uint8_t msTrackNumberCharId[2] = {0x08, 0x00}; - static constexpr uint8_t msTrackTotalCharId[2] = {0x09, 0x00}; - static constexpr uint8_t msPlaybackSpeedCharId[2] = {0x0a, 0x00}; - static constexpr uint8_t msRepeatCharId[2] = {0x0b, 0x00}; - static constexpr uint8_t msShuffleCharId[2] = {0x0c, 0x00}; - ble_uuid128_t msUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msEventCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msStatusCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msArtistCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msTrackCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msAlbumCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msPositionCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msTotalLengthCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msTrackNumberCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msTrackTotalCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msPlaybackSpeedCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msRepeatCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; - ble_uuid128_t msShuffleCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msEventCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x01, 0x00)}; + ble_uuid128_t msStatusCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x02, 0x00)}; + ble_uuid128_t msArtistCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x03, 0x00)}; + ble_uuid128_t msTrackCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x04, 0x00)}; + ble_uuid128_t msAlbumCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x05, 0x00)}; + ble_uuid128_t msPositionCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x06, 0x00)}; + ble_uuid128_t msTotalLengthCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x07, 0x00)}; + ble_uuid128_t msTrackNumberCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x08, 0x00)}; + ble_uuid128_t msTrackTotalCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x09, 0x00)}; + ble_uuid128_t msPlaybackSpeedCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x0a, 0x00)}; + ble_uuid128_t msRepeatCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x0b, 0x00)}; + ble_uuid128_t msShuffleCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_CHAR_UUID(0x0c, 0x00)}; struct ble_gatt_chr_def characteristicDefinition[14]; struct ble_gatt_svc_def serviceDefinition[2]; -- cgit v1.2.3-70-g09d2 From 8568b5f31b1b3f6b5232ba0ce6e3e6a7064fe787 Mon Sep 17 00:00:00 2001 From: Avamander Date: Mon, 17 May 2021 02:16:50 +0300 Subject: Updated license header year --- src/components/ble/MusicService.cpp | 2 +- src/components/ble/MusicService.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/components') diff --git a/src/components/ble/MusicService.cpp b/src/components/ble/MusicService.cpp index 30dd7274..c420a576 100644 --- a/src/components/ble/MusicService.cpp +++ b/src/components/ble/MusicService.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 JF, Adam Pigg, Avamander +/* Copyright (C) 2020-2021 JF, Adam Pigg, Avamander This file is part of InfiniTime. diff --git a/src/components/ble/MusicService.h b/src/components/ble/MusicService.h index 4314db0f..0b5a7464 100644 --- a/src/components/ble/MusicService.h +++ b/src/components/ble/MusicService.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 JF, Adam Pigg, Avamander +/* Copyright (C) 2020-2021 JF, Adam Pigg, Avamander This file is part of InfiniTime. -- cgit v1.2.3-70-g09d2 From 7f9cc51b050e1034b573e37484f7afe29c370d81 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Sun, 6 Jun 2021 15:56:03 +0200 Subject: Initialize SystemTask, DisplayApp and HeartRateTask as global static variable instead of variables on the heap. We don't need them on the heap as we know their size at build time, it'll reduce memory fragmentation and it'll make memory analysis easier. --- src/CMakeLists.txt | 4 +- src/FreeRTOSConfig.h | 2 +- src/components/ble/AlertNotificationClient.cpp | 2 +- src/components/ble/AlertNotificationService.cpp | 2 +- src/components/ble/DfuService.cpp | 6 +- src/components/ble/ImmediateAlertService.cpp | 2 +- src/components/ble/NimbleController.cpp | 2 +- src/components/datetime/DateTimeController.cpp | 10 +-- src/components/datetime/DateTimeController.h | 6 +- src/components/heartrate/HeartRateController.cpp | 3 - src/components/heartrate/HeartRateController.h | 4 +- src/components/heartrate/Ppg.cpp | 11 +--- src/components/heartrate/Ppg.h | 3 +- src/components/timer/TimerController.cpp | 29 +++++---- src/components/timer/TimerController.h | 13 ++-- src/displayapp/DisplayApp.cpp | 32 ++++++---- src/displayapp/DisplayApp.h | 7 ++- src/displayapp/screens/FlashLight.cpp | 4 +- src/displayapp/screens/HeartRate.cpp | 8 +-- src/displayapp/screens/ScreenList.h | 13 ++-- src/displayapp/screens/settings/QuickSettings.cpp | 4 +- src/drivers/Bma421.cpp | 2 - src/drivers/SpiMaster.h | 1 - src/heartratetask/HeartRateTask.cpp | 2 +- src/main.cpp | 69 +++++++++++++++------ src/systemtask/Messages.h | 26 ++++++++ src/systemtask/SystemTask.cpp | 75 +++++++++++------------ src/systemtask/SystemTask.h | 53 +++++++--------- 28 files changed, 224 insertions(+), 171 deletions(-) create mode 100644 src/systemtask/Messages.h (limited to 'src/components') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cd729921..c72f7c9c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -750,8 +750,8 @@ add_definitions(-DNIMBLE_CFG_CONTROLLER) add_definitions(-DOS_CPUTIME_FREQ) add_definitions(-DNRF52 -DNRF52832 -DNRF52832_XXAA -DNRF52_PAN_74 -DNRF52_PAN_64 -DNRF52_PAN_12 -DNRF52_PAN_58 -DNRF52_PAN_54 -DNRF52_PAN_31 -DNRF52_PAN_51 -DNRF52_PAN_36 -DNRF52_PAN_15 -DNRF52_PAN_20 -DNRF52_PAN_55 -DBOARD_PCA10040) add_definitions(-DFREERTOS) -add_definitions(-D__STACK_SIZE=8192) -add_definitions(-D__HEAP_SIZE=8192) +add_definitions(-D__STACK_SIZE=1024) +add_definitions(-D__HEAP_SIZE=4096) # NOTE : Add the following defines to enable debug mode of the NRF SDK: #add_definitions(-DDEBUG) diff --git a/src/FreeRTOSConfig.h b/src/FreeRTOSConfig.h index 15185766..07c152dc 100644 --- a/src/FreeRTOSConfig.h +++ b/src/FreeRTOSConfig.h @@ -62,7 +62,7 @@ #define configTICK_RATE_HZ 1024 #define configMAX_PRIORITIES (3) #define configMINIMAL_STACK_SIZE (120) -#define configTOTAL_HEAP_SIZE (1024 * 16) +#define configTOTAL_HEAP_SIZE (1024 * 17) #define configMAX_TASK_NAME_LEN (4) #define configUSE_16_BIT_TICKS 0 #define configIDLE_SHOULD_YIELD 1 diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp index 6043a129..c3d1d69a 100644 --- a/src/components/ble/AlertNotificationClient.cpp +++ b/src/components/ble/AlertNotificationClient.cpp @@ -159,7 +159,7 @@ void AlertNotificationClient::OnNotification(ble_gap_event* event) { notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; notificationManager.Push(std::move(notif)); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); + systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification); } } diff --git a/src/components/ble/AlertNotificationService.cpp b/src/components/ble/AlertNotificationService.cpp index e9f5941e..d5fc7f65 100644 --- a/src/components/ble/AlertNotificationService.cpp +++ b/src/components/ble/AlertNotificationService.cpp @@ -79,7 +79,7 @@ int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle break; } - auto event = Pinetime::System::SystemTask::Messages::OnNewNotification; + auto event = Pinetime::System::Messages::OnNewNotification; notificationManager.Push(std::move(notif)); systemTask.PushMessage(event); } diff --git a/src/components/ble/DfuService.cpp b/src/components/ble/DfuService.cpp index 2031668e..cec194cc 100644 --- a/src/components/ble/DfuService.cpp +++ b/src/components/ble/DfuService.cpp @@ -205,7 +205,7 @@ int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf* om) { bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Running); bleController.FirmwareUpdateTotalBytes(0xffffffffu); bleController.FirmwareUpdateCurrentBytes(0); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::BleFirmwareUpdateStarted); + systemTask.PushMessage(Pinetime::System::Messages::BleFirmwareUpdateStarted); return 0; } else { NRF_LOG_INFO("[DFU] -> Start DFU, mode %d not supported!", imageType); @@ -279,7 +279,7 @@ int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf* om) { } NRF_LOG_INFO("[DFU] -> Activate image and reset!"); bleController.StopFirmwareUpdate(); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::BleFirmwareUpdateFinished); + systemTask.PushMessage(Pinetime::System::Messages::BleFirmwareUpdateFinished); Reset(); bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated); return 0; @@ -304,7 +304,7 @@ void DfuService::Reset() { notificationManager.Reset(); bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Error); bleController.StopFirmwareUpdate(); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::BleFirmwareUpdateFinished); + systemTask.PushMessage(Pinetime::System::Messages::BleFirmwareUpdateFinished); } DfuService::NotificationManager::NotificationManager() { diff --git a/src/components/ble/ImmediateAlertService.cpp b/src/components/ble/ImmediateAlertService.cpp index fd6430af..820d3b6e 100644 --- a/src/components/ble/ImmediateAlertService.cpp +++ b/src/components/ble/ImmediateAlertService.cpp @@ -67,7 +67,7 @@ int ImmediateAlertService::OnAlertLevelChanged(uint16_t connectionHandle, uint16 notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; notificationManager.Push(std::move(notif)); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); + systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification); } } diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 67a6d691..2c1d0f99 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -149,7 +149,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { bleController.Disconnect(); } else { bleController.Connect(); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::BleConnected); + systemTask.PushMessage(Pinetime::System::Messages::BleConnected); connectionHandle = event->connect.conn_handle; // Service discovery is deffered via systemtask } diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 4f9302eb..28a70abc 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -5,9 +5,6 @@ using namespace Pinetime::Controllers; -DateTime::DateTime(System::SystemTask& systemTask) : systemTask {systemTask} { -} - 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 = { @@ -70,7 +67,8 @@ void DateTime::UpdateTime(uint32_t systickCounter) { // Notify new day to SystemTask if (hour == 0 and not isMidnightAlreadyNotified) { isMidnightAlreadyNotified = true; - systemTask.PushMessage(System::SystemTask::Messages::OnNewDay); + if(systemTask != nullptr) + systemTask->PushMessage(System::Messages::OnNewDay); } else if (hour != 0) { isMidnightAlreadyNotified = false; } @@ -104,6 +102,10 @@ const char* DateTime::DayOfWeekShortToStringLow() { return DateTime::DaysStringShortLow[(uint8_t) dayOfWeek]; } +void DateTime::Register(Pinetime::System::SystemTask* systemTask) { + this->systemTask = systemTask; +} + char const* DateTime::DaysStringLow[] = {"--", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; char const* DateTime::DaysStringShortLow[] = {"--", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index d0ae727e..265d6e9d 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -27,8 +27,6 @@ namespace Pinetime { December }; - DateTime(System::SystemTask& systemTask); - void SetTime(uint16_t year, uint8_t month, uint8_t day, @@ -75,8 +73,9 @@ namespace Pinetime { return uptime; } + void Register(System::SystemTask* systemTask); + private: - System::SystemTask& systemTask; uint16_t year = 0; Months month = Months::Unknown; uint8_t day = 0; @@ -90,6 +89,7 @@ namespace Pinetime { std::chrono::seconds uptime {0}; bool isMidnightAlreadyNotified = false; + System::SystemTask* systemTask = nullptr; static char const* DaysString[]; static char const* DaysStringShort[]; diff --git a/src/components/heartrate/HeartRateController.cpp b/src/components/heartrate/HeartRateController.cpp index e84d665a..716813b3 100644 --- a/src/components/heartrate/HeartRateController.cpp +++ b/src/components/heartrate/HeartRateController.cpp @@ -4,9 +4,6 @@ using namespace Pinetime::Controllers; -HeartRateController::HeartRateController(Pinetime::System::SystemTask& systemTask) : systemTask {systemTask} { -} - void HeartRateController::Update(HeartRateController::States newState, uint8_t heartRate) { this->state = newState; if (this->heartRate != heartRate) { diff --git a/src/components/heartrate/HeartRateController.h b/src/components/heartrate/HeartRateController.h index d3a8460d..a63f1a70 100644 --- a/src/components/heartrate/HeartRateController.h +++ b/src/components/heartrate/HeartRateController.h @@ -15,8 +15,7 @@ namespace Pinetime { public: enum class States { Stopped, NotEnoughData, NoTouch, Running }; - explicit HeartRateController(System::SystemTask& systemTask); - + HeartRateController() = default; void Start(); void Stop(); void Update(States newState, uint8_t heartRate); @@ -32,7 +31,6 @@ namespace Pinetime { void SetService(Pinetime::Controllers::HeartRateService* service); private: - System::SystemTask& systemTask; Applications::HeartRateTask* task = nullptr; States state = States::Stopped; uint8_t heartRate = 0; diff --git a/src/components/heartrate/Ppg.cpp b/src/components/heartrate/Ppg.cpp index da0789e0..fcba3815 100644 --- a/src/components/heartrate/Ppg.cpp +++ b/src/components/heartrate/Ppg.cpp @@ -38,9 +38,8 @@ namespace { } } -Ppg::Ppg(float spl) - : offset {spl}, - hpf {0.87033078, -1.74066156, 0.87033078, -1.72377617, 0.75754694}, +Ppg::Ppg() + : hpf {0.87033078, -1.74066156, 0.87033078, -1.72377617, 0.75754694}, agc {20, 0.971, 2}, lpf {0.11595249, 0.23190498, 0.11595249, -0.72168143, 0.18549138} { } @@ -67,13 +66,7 @@ float Ppg::HeartRate() { dataIndex = 0; return hr; } - -int cccount = 0; float Ppg::ProcessHeartRate() { - - if (cccount > 2) - asm("nop"); - cccount++; auto t0 = Trough(data.data(), dataIndex, 7, 48); if (t0 < 0) return 0; diff --git a/src/components/heartrate/Ppg.h b/src/components/heartrate/Ppg.h index ee07dfcf..00014162 100644 --- a/src/components/heartrate/Ppg.h +++ b/src/components/heartrate/Ppg.h @@ -8,8 +8,7 @@ namespace Pinetime { namespace Controllers { class Ppg { public: - explicit Ppg(float spl); - + Ppg(); int8_t Preprocess(float spl); float HeartRate(); diff --git a/src/components/timer/TimerController.cpp b/src/components/timer/TimerController.cpp index 3b25901c..8d5f5c33 100644 --- a/src/components/timer/TimerController.cpp +++ b/src/components/timer/TimerController.cpp @@ -12,14 +12,17 @@ using namespace Pinetime::Controllers; APP_TIMER_DEF(timerAppTimer); - -TimerController::TimerController(System::SystemTask& systemTask) : systemTask{systemTask} { +namespace { + void TimerEnd(void* p_context) { + auto* controller = static_cast (p_context); + if(controller != nullptr) + controller->OnTimerEnd(); + } } void TimerController::Init() { - app_timer_create(&timerAppTimer, APP_TIMER_MODE_SINGLE_SHOT, timerEnd); - + app_timer_create(&timerAppTimer, APP_TIMER_MODE_SINGLE_SHOT, TimerEnd); } void TimerController::StartTimer(uint32_t duration) { @@ -47,13 +50,6 @@ uint32_t TimerController::GetTimeRemaining() { return (static_cast(deltaTicks) / static_cast(configTICK_RATE_HZ)) * 1000; } -void TimerController::timerEnd(void* p_context) { - - auto* controller = static_cast (p_context); - controller->timerRunning = false; - controller->systemTask.PushMessage(System::SystemTask::Messages::OnTimerDone); -} - void TimerController::StopTimer() { app_timer_stop(timerAppTimer); timerRunning = false; @@ -61,4 +57,13 @@ void TimerController::StopTimer() { bool TimerController::IsRunning() { return timerRunning; -} \ No newline at end of file +} +void TimerController::OnTimerEnd() { + timerRunning = false; + if(systemTask != nullptr) + systemTask->PushMessage(System::Messages::OnTimerDone); +} + +void TimerController::Register(Pinetime::System::SystemTask* systemTask) { + this->systemTask = systemTask; +} diff --git a/src/components/timer/TimerController.h b/src/components/timer/TimerController.h index 5a0b293e..fa7bc90d 100644 --- a/src/components/timer/TimerController.h +++ b/src/components/timer/TimerController.h @@ -12,7 +12,7 @@ namespace Pinetime { class TimerController { public: - TimerController(Pinetime::System::SystemTask& systemTask); + TimerController() = default; void Init(); @@ -23,12 +23,13 @@ namespace Pinetime { uint32_t GetTimeRemaining(); bool IsRunning(); - + + void OnTimerEnd(); + + void Register(System::SystemTask* systemTask); + private: - System::SystemTask& systemTask; - - static void timerEnd(void* p_context); - + System::SystemTask* systemTask = nullptr; TickType_t endTicks; bool timerRunning = false; }; diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 7b03d569..ba6dfbd2 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -32,6 +32,7 @@ #include "drivers/St7789.h" #include "drivers/Watchdog.h" #include "systemtask/SystemTask.h" +#include "systemtask/Messages.h" #include "displayapp/screens/settings/QuickSettings.h" #include "displayapp/screens/settings/Settings.h" @@ -51,7 +52,6 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, Controllers::Ble& bleController, Controllers::DateTime& dateTimeController, Drivers::WatchdogView& watchdog, - System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::HeartRateController& heartRateController, Controllers::Settings& settingsController, @@ -65,7 +65,6 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, bleController {bleController}, dateTimeController {dateTimeController}, watchdog {watchdog}, - systemTask {systemTask}, notificationManager {notificationManager}, heartRateController {heartRateController}, settingsController {settingsController}, @@ -130,7 +129,7 @@ void DisplayApp::Refresh() { vTaskDelay(100); } lcd.DisplayOff(); - systemTask.PushMessage(System::SystemTask::Messages::OnDisplayTaskSleeping); + PushMessageToSystemTask(Pinetime::System::Messages::OnDisplayTaskSleeping); state = States::Idle; break; case Messages::GoToRunning: @@ -139,7 +138,7 @@ void DisplayApp::Refresh() { state = States::Running; break; case Messages::UpdateTimeOut: - systemTask.PushMessage(System::SystemTask::Messages::UpdateTimeOut); + PushMessageToSystemTask(System::Messages::UpdateTimeOut); break; case Messages::UpdateBleConnection: // clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected : @@ -176,7 +175,7 @@ void DisplayApp::Refresh() { LoadApp(Apps::QuickSettings, DisplayApp::FullRefreshDirections::RightAnim); break; case TouchEvents::DoubleTap: - systemTask.PushMessage(System::SystemTask::Messages::GoToSleep); + PushMessageToSystemTask(System::Messages::GoToSleep); break; default: break; @@ -188,7 +187,7 @@ void DisplayApp::Refresh() { } break; case Messages::ButtonPushed: if (currentApp == Apps::Clock) { - systemTask.PushMessage(System::SystemTask::Messages::GoToSleep); + PushMessageToSystemTask(System::Messages::GoToSleep); } else { if (!currentScreen->OnButtonPushed()) { LoadApp(returnToApp, returnDirection); @@ -267,12 +266,12 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) case Apps::Notifications: currentScreen = std::make_unique( - this, notificationManager, systemTask.nimble().alertService(), Screens::Notifications::Modes::Normal); + this, notificationManager, systemTask->nimble().alertService(), Screens::Notifications::Modes::Normal); ReturnApp(Apps::Clock, FullRefreshDirections::Up, TouchEvents::SwipeUp); break; case Apps::NotificationsPreview: currentScreen = std::make_unique( - this, notificationManager, systemTask.nimble().alertService(), Screens::Notifications::Modes::Preview); + this, notificationManager, systemTask->nimble().alertService(), Screens::Notifications::Modes::Preview); ReturnApp(Apps::Clock, FullRefreshDirections::Up, TouchEvents::SwipeUp); break; case Apps::Timer: @@ -321,7 +320,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) // case Apps::FlashLight: - currentScreen = std::make_unique(this, systemTask, brightnessController); + currentScreen = std::make_unique(this, *systemTask, brightnessController); ReturnApp(Apps::Clock, FullRefreshDirections::Down, TouchEvents::None); break; case Apps::StopWatch: @@ -337,13 +336,13 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) currentScreen = std::make_unique(this, lvgl); break; case Apps::Music: - currentScreen = std::make_unique(this, systemTask.nimble().music()); + currentScreen = std::make_unique(this, systemTask->nimble().music()); break; case Apps::Navigation: - currentScreen = std::make_unique(this, systemTask.nimble().navigation()); + currentScreen = std::make_unique(this, systemTask->nimble().navigation()); break; case Apps::HeartRate: - currentScreen = std::make_unique(this, heartRateController, systemTask); + currentScreen = std::make_unique(this, heartRateController, *systemTask); break; case Apps::Motion: currentScreen = std::make_unique(this, motionController); @@ -425,3 +424,12 @@ void DisplayApp::SetFullRefresh(DisplayApp::FullRefreshDirections direction) { void DisplayApp::SetTouchMode(DisplayApp::TouchModes mode) { touchMode = mode; } + +void DisplayApp::PushMessageToSystemTask(Pinetime::System::Messages message) { + if(systemTask != nullptr) + systemTask->PushMessage(message); +} + +void DisplayApp::Register(Pinetime::System::SystemTask* systemTask) { + this->systemTask = systemTask; +} diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 0c7bd216..65abd41a 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "Apps.h" #include "LittleVgl.h" #include "TouchEvents.h" @@ -49,7 +50,6 @@ namespace Pinetime { Controllers::Ble& bleController, Controllers::DateTime& dateTimeController, Drivers::WatchdogView& watchdog, - System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::HeartRateController& heartRateController, Controllers::Settings& settingsController, @@ -64,6 +64,8 @@ namespace Pinetime { void SetFullRefresh(FullRefreshDirections direction); void SetTouchMode(TouchModes mode); + void Register(Pinetime::System::SystemTask* systemTask); + private: Pinetime::Drivers::St7789& lcd; Pinetime::Components::LittleVgl& lvgl; @@ -72,7 +74,7 @@ namespace Pinetime { Pinetime::Controllers::Ble& bleController; Pinetime::Controllers::DateTime& dateTimeController; Pinetime::Drivers::WatchdogView& watchdog; - Pinetime::System::SystemTask& systemTask; + Pinetime::System::SystemTask* systemTask = nullptr; Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::HeartRateController& heartRateController; Pinetime::Controllers::Settings& settingsController; @@ -108,6 +110,7 @@ namespace Pinetime { void Refresh(); void ReturnApp(Apps app, DisplayApp::FullRefreshDirections direction, TouchEvents touchEvent); void LoadApp(Apps app, DisplayApp::FullRefreshDirections direction); + void PushMessageToSystemTask(Pinetime::System::Messages message); }; } } diff --git a/src/displayapp/screens/FlashLight.cpp b/src/displayapp/screens/FlashLight.cpp index 4568db40..7db2c6c8 100644 --- a/src/displayapp/screens/FlashLight.cpp +++ b/src/displayapp/screens/FlashLight.cpp @@ -39,14 +39,14 @@ FlashLight::FlashLight(Pinetime::Applications::DisplayApp* app, backgroundAction->user_data = this; lv_obj_set_event_cb(backgroundAction, event_handler); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::DisableSleeping); + systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping); } FlashLight::~FlashLight() { lv_obj_clean(lv_scr_act()); lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000)); brightness.Restore(); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::EnableSleeping); + systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping); } void FlashLight::OnClickEvent(lv_obj_t* obj, lv_event_t event) { diff --git a/src/displayapp/screens/HeartRate.cpp b/src/displayapp/screens/HeartRate.cpp index 90f6bc26..5689b63e 100644 --- a/src/displayapp/screens/HeartRate.cpp +++ b/src/displayapp/screens/HeartRate.cpp @@ -63,12 +63,12 @@ HeartRate::HeartRate(Pinetime::Applications::DisplayApp* app, label_startStop = lv_label_create(btn_startStop, nullptr); UpdateStartStopButton(isHrRunning); if (isHrRunning) - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::DisableSleeping); + systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping); } HeartRate::~HeartRate() { lv_obj_clean(lv_scr_act()); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::EnableSleeping); + systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping); } bool HeartRate::Refresh() { @@ -95,12 +95,12 @@ void HeartRate::OnStartStopEvent(lv_event_t event) { if (heartRateController.State() == Controllers::HeartRateController::States::Stopped) { heartRateController.Start(); UpdateStartStopButton(heartRateController.State() != Controllers::HeartRateController::States::Stopped); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::DisableSleeping); + systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping); lv_obj_set_style_local_text_color(label_hr, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN); } else { heartRateController.Stop(); UpdateStartStopButton(heartRateController.State() != Controllers::HeartRateController::States::Stopped); - systemTask.PushMessage(Pinetime::System::SystemTask::Messages::EnableSleeping); + systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping); lv_obj_set_style_local_text_color(label_hr, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); } } diff --git a/src/displayapp/screens/ScreenList.h b/src/displayapp/screens/ScreenList.h index 73ea4610..ea66bfb2 100644 --- a/src/displayapp/screens/ScreenList.h +++ b/src/displayapp/screens/ScreenList.h @@ -15,12 +15,17 @@ namespace Pinetime { public: ScreenList(DisplayApp* app, uint8_t initScreen, - std::array()>, N>&& screens, + const std::array()>, N>&& screens, ScreenListModes mode) - : Screen(app), initScreen {initScreen}, screens {std::move(screens)}, mode {mode}, current {this->screens[initScreen]()} { - screenIndex = initScreen; + : Screen(app), initScreen {initScreen}, screens {std::move(screens)}, mode {mode}, screenIndex{initScreen}, current {this->screens[initScreen]()} { + } + ScreenList(const ScreenList&) = delete; + ScreenList& operator=(const ScreenList&) = delete; + ScreenList(ScreenList&&) = delete; + ScreenList& operator=(ScreenList&&) = delete; + ~ScreenList() override { lv_obj_clean(lv_scr_act()); } @@ -97,7 +102,7 @@ namespace Pinetime { private: uint8_t initScreen = 0; - std::array()>, N> screens; + const std::array()>, N> screens; ScreenListModes mode = ScreenListModes::UpDown; uint8_t screenIndex = 0; diff --git a/src/displayapp/screens/settings/QuickSettings.cpp b/src/displayapp/screens/settings/QuickSettings.cpp index 3994794d..5db7468c 100644 --- a/src/displayapp/screens/settings/QuickSettings.cpp +++ b/src/displayapp/screens/settings/QuickSettings.cpp @@ -7,12 +7,12 @@ using namespace Pinetime::Applications::Screens; namespace { static void ButtonEventHandler(lv_obj_t* obj, lv_event_t event) { - QuickSettings* screen = static_cast(obj->user_data); + auto* screen = static_cast(obj->user_data); screen->OnButtonEvent(obj, event); } static void lv_update_task(struct _lv_task_t* task) { - auto user_data = static_cast(task->user_data); + auto* user_data = static_cast(task->user_data); user_data->UpdateScreen(); } } diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp index 925b66c7..35b2c105 100644 --- a/src/drivers/Bma421.cpp +++ b/src/drivers/Bma421.cpp @@ -103,8 +103,6 @@ Bma421::Values Bma421::Process() { uint8_t activity = 0; bma423_activity_output(&activity, &bma); - NRF_LOG_INFO("MOTION : %d - %d/%d/%d", steps, data.x, data.y, data.z); - // X and Y axis are swapped because of the way the sensor is mounted in the PineTime return {steps, data.y, data.x, data.z}; } diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index dfc195b7..5045369a 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -10,7 +10,6 @@ namespace Pinetime { namespace Drivers { class SpiMaster { public: - ; enum class SpiModule : uint8_t { SPI0, SPI1 }; enum class BitOrder : uint8_t { Msb_Lsb, Lsb_Msb }; enum class Modes : uint8_t { Mode0, Mode1, Mode2, Mode3 }; diff --git a/src/heartratetask/HeartRateTask.cpp b/src/heartratetask/HeartRateTask.cpp index 36c8cc18..1c21db71 100644 --- a/src/heartratetask/HeartRateTask.cpp +++ b/src/heartratetask/HeartRateTask.cpp @@ -6,7 +6,7 @@ using namespace Pinetime::Applications; HeartRateTask::HeartRateTask(Drivers::Hrs3300& heartRateSensor, Controllers::HeartRateController& controller) - : heartRateSensor {heartRateSensor}, controller {controller}, ppg {static_cast(heartRateSensor.ReadHrs())} { + : heartRateSensor {heartRateSensor}, controller {controller}, ppg{} { messageQueue = xQueueCreate(10, 1); controller.SetHeartRateTask(this); } diff --git a/src/main.cpp b/src/main.cpp index 61194b95..60ed058b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,6 +34,7 @@ #include "components/motor/MotorController.h" #include "components/datetime/DateTimeController.h" #include "components/settings/Settings.h" +#include "components/heartrate/HeartRateController.h" #include "drivers/Spi.h" #include "drivers/SpiMaster.h" #include "drivers/SpiNorFlash.h" @@ -50,8 +51,6 @@ Pinetime::Logging::NrfLogger logger; Pinetime::Logging::DummyLogger logger; #endif -#include - static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiMosi = 3; static constexpr uint8_t pinSpiMiso = 4; @@ -108,15 +107,59 @@ void ble_manager_set_ble_connection_callback(void (*connection)()); void ble_manager_set_ble_disconnection_callback(void (*disconnection)()); static constexpr uint8_t pinTouchIrq = 28; static constexpr uint8_t pinPowerPresentIrq = 19; -std::unique_ptr systemTask; Pinetime::Controllers::Settings settingsController {spiNorFlash}; Pinetime::Controllers::MotorController motorController {settingsController}; +Pinetime::Controllers::HeartRateController heartRateController; +Pinetime::Applications::HeartRateTask heartRateApp(heartRateSensor, heartRateController); + +Pinetime::Controllers::DateTime dateTimeController; +Pinetime::Drivers::Watchdog watchdog; +Pinetime::Drivers::WatchdogView watchdogView(watchdog); +Pinetime::Controllers::NotificationManager notificationManager; +Pinetime::Controllers::MotionController motionController; +Pinetime::Controllers::TimerController timerController; + +Pinetime::Applications::DisplayApp displayApp(lcd, + lvgl, + touchPanel, + batteryController, + bleController, + dateTimeController, + watchdogView, + notificationManager, + heartRateController, + settingsController, + motorController, + motionController, + timerController); + +Pinetime::System::SystemTask systemTask(spi, + lcd, + spiNorFlash, + twiMaster, + touchPanel, + lvgl, + batteryController, + bleController, + dateTimeController, + timerController, + watchdog, + notificationManager, + motorController, + heartRateSensor, + motionController, + motionSensor, + settingsController, + heartRateController, + displayApp, + heartRateApp); + void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { if (pin == pinTouchIrq) { - systemTask->OnTouchEvent(); + systemTask.OnTouchEvent(); return; } @@ -141,12 +184,12 @@ void vApplicationIdleHook(void) { void DebounceTimerChargeCallback(TimerHandle_t xTimer) { xTimerStop(xTimer, 0); - systemTask->PushMessage(Pinetime::System::SystemTask::Messages::OnChargingEvent); + systemTask.PushMessage(Pinetime::System::Messages::OnChargingEvent); } void DebounceTimerCallback(TimerHandle_t xTimer) { xTimerStop(xTimer, 0); - systemTask->OnButtonPushed(); + systemTask.OnButtonPushed(); } void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) { @@ -264,19 +307,7 @@ int main(void) { debounceTimer = xTimerCreate("debounceTimer", 200, pdFALSE, (void*) 0, DebounceTimerCallback); debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, (void*) 0, DebounceTimerChargeCallback); - systemTask = std::make_unique(spi, - lcd, - spiNorFlash, - twiMaster, - touchPanel, - lvgl, - batteryController, - bleController, - motorController, - heartRateSensor, - motionSensor, - settingsController); - systemTask->Start(); + systemTask.Start(); nimble_port_init(); vTaskStartScheduler(); diff --git a/src/systemtask/Messages.h b/src/systemtask/Messages.h new file mode 100644 index 00000000..3a195e2d --- /dev/null +++ b/src/systemtask/Messages.h @@ -0,0 +1,26 @@ +#pragma once + +namespace Pinetime { + namespace System { + enum class Messages { + GoToSleep, + GoToRunning, + TouchWakeUp, + OnNewTime, + OnNewNotification, + OnTimerDone, + OnNewCall, + BleConnected, + UpdateTimeOut, + BleFirmwareUpdateStarted, + BleFirmwareUpdateFinished, + OnTouchEvent, + OnButtonEvent, + OnDisplayTaskSleeping, + EnableSleeping, + DisableSleeping, + OnNewDay, + OnChargingEvent + }; + } +} diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 58377764..be484bb4 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -42,10 +42,18 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, Components::LittleVgl& lvgl, Controllers::Battery& batteryController, Controllers::Ble& bleController, + Controllers::DateTime& dateTimeController, + Controllers::TimerController& timerController, + Drivers::Watchdog& watchdog, + Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, + Pinetime::Controllers::MotionController& motionController, Pinetime::Drivers::Bma421& motionSensor, - Controllers::Settings& settingsController) + Controllers::Settings& settingsController, + Pinetime::Controllers::HeartRateController& heartRateController, + Pinetime::Applications::DisplayApp& displayApp, + Pinetime::Applications::HeartRateTask& heartRateApp) : spi {spi}, lcd {lcd}, spiNorFlash {spiNorFlash}, @@ -53,17 +61,20 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, touchPanel {touchPanel}, lvgl {lvgl}, batteryController {batteryController}, - heartRateController {*this}, bleController {bleController}, - dateTimeController {*this}, - timerController {*this}, - watchdog {}, - watchdogView {watchdog}, + dateTimeController {dateTimeController}, + timerController {timerController}, + watchdog {watchdog}, + notificationManager{notificationManager}, motorController {motorController}, heartRateSensor {heartRateSensor}, motionSensor {motionSensor}, settingsController {settingsController}, - nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController) { + heartRateController{heartRateController}, + nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController), + motionController{motionController}, + displayApp{displayApp}, + heartRateApp(heartRateApp) { systemTasksMsgQueue = xQueueCreate(10, 1); } @@ -96,9 +107,11 @@ void SystemTask::Work() { twiMaster.Init(); touchPanel.Init(); + dateTimeController.Register(this); batteryController.Init(); motorController.Init(); motionSensor.SoftReset(); + timerController.Register(this); timerController.Init(); // Reset the TWI device because the motion sensor chip most probably crashed it... @@ -108,28 +121,14 @@ void SystemTask::Work() { motionSensor.Init(); settingsController.Init(); - displayApp = std::make_unique(lcd, - lvgl, - touchPanel, - batteryController, - bleController, - dateTimeController, - watchdogView, - *this, - notificationManager, - heartRateController, - settingsController, - motorController, - motionController, - timerController); - displayApp->Start(); - - displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel); + displayApp.Register(this); + displayApp.Start(); + + displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel); heartRateSensor.Init(); heartRateSensor.Disable(); - heartRateApp = std::make_unique(heartRateSensor, heartRateController); - heartRateApp->Start(); + heartRateApp.Start(); nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_High); nrf_gpio_cfg_output(15); @@ -208,9 +207,9 @@ void SystemTask::Work() { spiNorFlash.Wakeup(); lcd.Wakeup(); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::GoToRunning); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel); - heartRateApp->PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToRunning); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel); + heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp); isSleeping = false; isWakingUp = false; @@ -230,26 +229,26 @@ void SystemTask::Work() { isGoingToSleep = true; NRF_LOG_INFO("[systemtask] Going to sleep"); xTimerStop(idleTimer, 0); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::GoToSleep); - heartRateApp->PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToSleep); + heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep); break; case Messages::OnNewTime: ReloadIdleTimer(); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateDateTime); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateDateTime); break; case Messages::OnNewNotification: if (isSleeping && !isWakingUp) { GoToRunning(); } motorController.SetDuration(35); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::NewNotification); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::NewNotification); break; case Messages::OnTimerDone: if (isSleeping && !isWakingUp) { GoToRunning(); } motorController.SetDuration(35); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::TimerDone); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::TimerDone); break; case Messages::BleConnected: ReloadIdleTimer(); @@ -260,7 +259,7 @@ void SystemTask::Work() { doNotGoToSleep = true; if (isSleeping && !isWakingUp) GoToRunning(); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::BleFirmwareUpdateStarted); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::BleFirmwareUpdateStarted); break; case Messages::BleFirmwareUpdateFinished: doNotGoToSleep = false; @@ -359,7 +358,7 @@ void SystemTask::OnButtonPushed() { if (!isSleeping) { NRF_LOG_INFO("[systemtask] Button pushed"); PushMessage(Messages::OnButtonEvent); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed); } else { if (!isWakingUp) { NRF_LOG_INFO("[systemtask] Button pushed, waking up"); @@ -380,7 +379,7 @@ void SystemTask::OnTouchEvent() { return; if (!isSleeping) { PushMessage(Messages::OnTouchEvent); - displayApp->PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); } else if (!isWakingUp) { if (settingsController.getWakeUpMode() == Pinetime::Controllers::Settings::WakeUpMode::None or settingsController.getWakeUpMode() == Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) @@ -389,7 +388,7 @@ void SystemTask::OnTouchEvent() { } } -void SystemTask::PushMessage(SystemTask::Messages msg) { +void SystemTask::PushMessage(System::Messages msg) { if (msg == Messages::GoToSleep) { isGoingToSleep = true; } diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index ea41a69d..fa6a949c 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -27,6 +26,7 @@ #endif #include "drivers/Watchdog.h" +#include "Messages.h" namespace Pinetime { namespace Drivers { @@ -40,27 +40,6 @@ namespace Pinetime { namespace System { class SystemTask { public: - enum class Messages { - GoToSleep, - GoToRunning, - TouchWakeUp, - OnNewTime, - OnNewNotification, - OnTimerDone, - OnNewCall, - BleConnected, - UpdateTimeOut, - BleFirmwareUpdateStarted, - BleFirmwareUpdateFinished, - OnTouchEvent, - OnButtonEvent, - OnDisplayTaskSleeping, - EnableSleeping, - DisableSleeping, - OnNewDay, - OnChargingEvent - }; - SystemTask(Drivers::SpiMaster& spi, Drivers::St7789& lcd, Pinetime::Drivers::SpiNorFlash& spiNorFlash, @@ -69,10 +48,18 @@ namespace Pinetime { Components::LittleVgl& lvgl, Controllers::Battery& batteryController, Controllers::Ble& bleController, + Controllers::DateTime& dateTimeController, + Controllers::TimerController& timerController, + Drivers::Watchdog& watchdog, + Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, + Pinetime::Controllers::MotionController& motionController, Pinetime::Drivers::Bma421& motionSensor, - Controllers::Settings& settingsController); + Controllers::Settings& settingsController, + Pinetime::Controllers::HeartRateController& heartRateController, + Pinetime::Applications::DisplayApp& displayApp, + Pinetime::Applications::HeartRateTask& heartRateApp); void Start(); void PushMessage(Messages msg); @@ -96,27 +83,29 @@ namespace Pinetime { Pinetime::Drivers::Cst816S& touchPanel; Pinetime::Components::LittleVgl& lvgl; Pinetime::Controllers::Battery& batteryController; - std::unique_ptr displayApp; - Pinetime::Controllers::HeartRateController heartRateController; - std::unique_ptr heartRateApp; + Pinetime::Controllers::Ble& bleController; - Pinetime::Controllers::DateTime dateTimeController; - Pinetime::Controllers::TimerController timerController; + Pinetime::Controllers::DateTime& dateTimeController; + Pinetime::Controllers::TimerController& timerController; QueueHandle_t systemTasksMsgQueue; std::atomic isSleeping {false}; std::atomic isGoingToSleep {false}; std::atomic isWakingUp {false}; - Pinetime::Drivers::Watchdog watchdog; - Pinetime::Drivers::WatchdogView watchdogView; - Pinetime::Controllers::NotificationManager notificationManager; + Pinetime::Drivers::Watchdog& watchdog; + Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::MotorController& motorController; Pinetime::Drivers::Hrs3300& heartRateSensor; Pinetime::Drivers::Bma421& motionSensor; Pinetime::Controllers::Settings& settingsController; + Pinetime::Controllers::HeartRateController& heartRateController; Pinetime::Controllers::NimbleController nimbleController; Controllers::BrightnessController brightnessController; - Pinetime::Controllers::MotionController motionController; + Pinetime::Controllers::MotionController& motionController; + + Pinetime::Applications::DisplayApp& displayApp; + Pinetime::Applications::HeartRateTask& heartRateApp; + static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiMosi = 3; -- cgit v1.2.3-70-g09d2 From e90e8c7426f9a44e696b208a5b393a60476a3d43 Mon Sep 17 00:00:00 2001 From: Jonathan Vander Mey Date: Mon, 24 May 2021 18:17:56 -0400 Subject: Fix signed/unsigned comparison warning Changed type of encodedBufferIndex to size_t to eliminate warning. It is fine as a size_t as its only ever compared to another size_t and used as an array index. --- src/components/rle/RleDecoder.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/components') diff --git a/src/components/rle/RleDecoder.h b/src/components/rle/RleDecoder.h index 0f607fb8..31c1ed10 100644 --- a/src/components/rle/RleDecoder.h +++ b/src/components/rle/RleDecoder.h @@ -21,7 +21,7 @@ namespace Pinetime { const uint8_t* buffer; size_t size; - int encodedBufferIndex = 0; + size_t encodedBufferIndex = 0; int y = 0; uint16_t bp = 0; uint16_t foregroundColor = 0xffff; -- cgit v1.2.3-70-g09d2