From 7a1e6e6e5bf187846bd533f04ee58e04798f0035 Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Sat, 11 Jul 2020 21:37:28 +0100 Subject: [PATCH 1/4] Add start of music appliction --- src/CMakeLists.txt | 2 ++ src/Components/Ble/NimbleController.cpp | 1 + src/Components/Ble/NimbleController.h | 5 +++++ src/DisplayApp/DisplayApp.cpp | 2 ++ src/DisplayApp/DisplayApp.h | 2 +- src/DisplayApp/Screens/Gauge.cpp | 1 + src/DisplayApp/Screens/Tile.cpp | 4 ++-- src/SystemTask/SystemTask.h | 4 +++- 8 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a30fb568..8c702b8e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -336,6 +336,7 @@ list(APPEND SOURCE_FILES DisplayApp/Screens/ScreenList.cpp DisplayApp/Screens/Label.cpp DisplayApp/Screens/FirmwareUpdate.cpp + DisplayApp/Screens/Music.cpp main.cpp drivers/St7789.cpp drivers/SpiNorFlash.cpp @@ -356,6 +357,7 @@ list(APPEND SOURCE_FILES Components/Ble/DfuService.cpp Components/Ble/CurrentTimeService.cpp Components/Ble/AlertNotificationService.cpp + Components/Ble/MusicService.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 561dbce4..53458676 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -80,6 +80,7 @@ void NimbleController::Init() { deviceInformationService.Init(); currentTimeClient.Init(); currentTimeService.Init(); + musicService.Init(); anService.Init(); diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index cf50d78d..fab3df2b 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -7,6 +7,7 @@ #include "CurrentTimeClient.h" #include "DfuService.h" #include "CurrentTimeService.h" +#include "MusicService.h" #include namespace Pinetime { @@ -35,6 +36,9 @@ namespace Pinetime { uint16_t characteristicValueHandle, const ble_gatt_dsc *descriptor); void StartDiscovery(); + + Pinetime::Controllers::MusicService& music() {return musicService;}; + private: static constexpr char* deviceName = "Pinetime-JF"; Pinetime::System::SystemTask& systemTask; @@ -49,6 +53,7 @@ namespace Pinetime { AlertNotificationService anService; AlertNotificationClient alertNotificationClient; CurrentTimeService currentTimeService; + MusicService musicService; uint8_t addrType; // 1 = Random, 0 = PUBLIC uint16_t connectionHandle; diff --git a/src/DisplayApp/DisplayApp.cpp b/src/DisplayApp/DisplayApp.cpp index 8e35ef55..7c822ee0 100644 --- a/src/DisplayApp/DisplayApp.cpp +++ b/src/DisplayApp/DisplayApp.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include "../SystemTask/SystemTask.h" @@ -189,6 +190,7 @@ void DisplayApp::RunningState() { case Apps::Meter: currentScreen.reset(new Screens::Meter(this)); break; case Apps::Gauge: currentScreen.reset(new Screens::Gauge(this)); break; case Apps::Brightness : currentScreen.reset(new Screens::Brightness(this, brightnessController)); break; + case Apps::Music : currentScreen.reset(new Screens::Music(this)); break; } nextApp = Apps::None; } diff --git a/src/DisplayApp/DisplayApp.h b/src/DisplayApp/DisplayApp.h index b45a0bee..671a40d0 100644 --- a/src/DisplayApp/DisplayApp.h +++ b/src/DisplayApp/DisplayApp.h @@ -43,7 +43,7 @@ namespace Pinetime { void Start(); void PushMessage(Messages msg); - enum class Apps {None, Launcher, Clock, SysInfo, Meter, Gauge, Brightness}; + enum class Apps {None, Launcher, Clock, SysInfo, Meter, Gauge, Brightness, Music}; void StartApp(Apps app); void SetFullRefresh(FullRefreshDirections direction); diff --git a/src/DisplayApp/Screens/Gauge.cpp b/src/DisplayApp/Screens/Gauge.cpp index 4c4cccd9..fd905231 100644 --- a/src/DisplayApp/Screens/Gauge.cpp +++ b/src/DisplayApp/Screens/Gauge.cpp @@ -19,6 +19,7 @@ Gauge::Gauge(Pinetime::Applications::DisplayApp *app) : Screen(app) { style.text.color = LV_COLOR_WHITE; style.line.color = LV_COLOR_RED; /*Line color after the critical value*/ + /*Describe the color for the needles*/ needle_colors[0] = LV_COLOR_ORANGE; diff --git a/src/DisplayApp/Screens/Tile.cpp b/src/DisplayApp/Screens/Tile.cpp index 6c225c9d..88e5bff6 100644 --- a/src/DisplayApp/Screens/Tile.cpp +++ b/src/DisplayApp/Screens/Tile.cpp @@ -16,7 +16,7 @@ static void event_handler(lv_obj_t * obj, lv_event_t event) { screen->OnObjectEvent(obj, event, eventData); } -static const char * btnm_map1[] = {"Meter", "Gauge", "Clock", "\n", "Soft\nversion", "App2", "Brightness", ""}; +static const char * btnm_map1[] = {"Meter", "Music", "Clock", "\n", "Soft\nversion", "App2", "Brightness", ""}; Tile::Tile(DisplayApp* app) : Screen(app) { modal.reset(new Modal(app)); @@ -166,7 +166,7 @@ void Tile::StartMeterApp() { } void Tile::StartGaugeApp() { - app->StartApp(DisplayApp::Apps::Gauge); + app->StartApp(DisplayApp::Apps::Music); running = false; } diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index ab5f7010..e2325bd6 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -35,6 +35,8 @@ namespace Pinetime { void OnIdle(); + Pinetime::Controllers::NimbleController& nimble() {return nimbleController;}; + private: TaskHandle_t taskHandle; @@ -74,4 +76,4 @@ namespace Pinetime { void GoToRunning(); }; } -} \ No newline at end of file +} From 306aa25aada3635506c593c6a90bf03218365d02 Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Mon, 13 Jul 2020 12:40:39 +0100 Subject: [PATCH 2/4] Initial code for music app screena and service --- src/Components/Ble/MusicService.cpp | 123 ++++++++++++++++++++++++++++ src/Components/Ble/MusicService.h | 81 ++++++++++++++++++ src/DisplayApp/Screens/Music.cpp | 111 +++++++++++++++++++++++++ src/DisplayApp/Screens/Music.h | 47 +++++++++++ 4 files changed, 362 insertions(+) create mode 100644 src/Components/Ble/MusicService.cpp create mode 100644 src/Components/Ble/MusicService.h create mode 100644 src/DisplayApp/Screens/Music.cpp create mode 100644 src/DisplayApp/Screens/Music.h diff --git a/src/Components/Ble/MusicService.cpp b/src/Components/Ble/MusicService.cpp new file mode 100644 index 00000000..05754e44 --- /dev/null +++ b/src/Components/Ble/MusicService.cpp @@ -0,0 +1,123 @@ +#include "MusicService.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); +} + +Pinetime::Controllers::MusicService::MusicService() +{ + msUuid.value[11] = msId[0]; + msUuid.value[12] = msId[1]; + msEventCharUuid.value[11] = msEventCharId[0]; + msEventCharUuid.value[12] = msEventCharId[1]; + msStatusCharUuid.value[11] = msStatusCharId[0]; + msStatusCharUuid.value[12] = msStatusCharId[1]; + msTrackCharUuid.value[11] = msTrackCharId[0]; + msTrackCharUuid.value[12] = msTrackCharId[1]; + msArtistCharUuid.value[11] = msArtistCharId[0]; + msArtistCharUuid.value[12] = msArtistCharId[1]; + msAlbumCharUuid.value[11] = msAlbumCharId[0]; + msAlbumCharUuid.value[12] = msAlbumCharId[1]; + + characteristicDefinition[0] = {(ble_uuid_t*)(&msEventCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ + }; + characteristicDefinition[1] = {(ble_uuid_t*)(&msStatusCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ + }; + characteristicDefinition[2] = {(ble_uuid_t*)(&msTrackCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ + }; + characteristicDefinition[3] = {(ble_uuid_t*)(&msArtistCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ + }; + characteristicDefinition[4] = {(ble_uuid_t*)(&msAlbumCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ + }; + characteristicDefinition[5] = {0}; + + serviceDefinition[0] = { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &msUuid, + .characteristics = characteristicDefinition + }; + serviceDefinition[1] = {0}; + + m_artist = "Waiting for"; + m_album = ""; + m_track = "track information..."; +} + +void Pinetime::Controllers::MusicService::Init() +{ + int res = 0; + res = ble_gatts_count_cfg(serviceDefinition); + ASSERT(res == 0); + + res = ble_gatts_add_svcs(serviceDefinition); + ASSERT(res == 0); +} + +int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt) { + + connectionHandle = conn_handle; + + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + size_t notifSize = OS_MBUF_PKTLEN(ctxt->om); + uint8_t data[notifSize + 1]; + data[notifSize] = '\0'; + os_mbuf_copydata(ctxt->om, 0, notifSize, data); + char *s = (char *) &data[0]; + NRF_LOG_INFO("DATA : %s", s); + if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msArtistCharUuid) == 0) { + m_artist = s; + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msTrackCharUuid) == 0) { + m_track = s; + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msAlbumCharUuid) == 0) { + m_album = s; + } + } + return 0; +} + +std::string Pinetime::Controllers::MusicService::album() +{ + return m_album; +} + +std::string Pinetime::Controllers::MusicService::artist() +{ + return m_artist; +} + +std::string Pinetime::Controllers::MusicService::track() +{ + return m_track; +} + +void Pinetime::Controllers::MusicService::event(char event) +{ + struct os_mbuf *om; + int ret; + + om = ble_hs_mbuf_from_flat(&event, 1); + + ble_gatts_find_chr((ble_uuid_t *) &msUuid, (ble_uuid_t *) &msEventCharUuid, nullptr, + &eventCharacteristicHandle); + + ret = ble_gattc_notify_custom(connectionHandle, eventCharacteristicHandle, om); + +} + diff --git a/src/Components/Ble/MusicService.h b/src/Components/Ble/MusicService.h new file mode 100644 index 00000000..8139d96e --- /dev/null +++ b/src/Components/Ble/MusicService.h @@ -0,0 +1,81 @@ +#pragma once +#include +#include +#include +#include +#include + +//c7e50000-78fc-48fe-8e23-43b37a1942d0 +#define MUSIC_SERVICE_UUID_BASE {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0xe5, 0xc7} + +namespace Pinetime { + namespace Controllers { + class MusicService { + public: + MusicService(); + void Init(); + int OnCommand(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + std::string artist(); + std::string track(); + std::string album(); + void event(char event); + + static const char EVENT_MUSIC_OPEN = 0xe0; + static const char EVENT_MUSIC_PLAY = 0x00; + static const char EVENT_MUSIC_PAUSE = 0x01; + static const char EVENT_MUSIC_NEXT = 0x03; + static const char EVENT_MUSIC_PREV = 0x04; + static const char EVENT_MUSIC_VOLUP = 0x05; + static const char EVENT_MUSIC_VOLDOWN = 0x06; + + + private: + static constexpr uint8_t msId[2] = {0x00, 0x01}; + static constexpr uint8_t msEventCharId[2] = {0x00, 0x02}; + static constexpr uint8_t msStatusCharId[2] = {0x00, 0x03}; + static constexpr uint8_t msArtistCharId[2] = {0x00, 0x04}; + static constexpr uint8_t msTrackCharId[2] = {0x00, 0x05}; + static constexpr uint8_t msAlbumCharId[2] = {0x00, 0x06}; + + uint16_t connectionHandle = 0; + uint16_t eventCharacteristicHandle = 0; + + 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 + }; + + struct ble_gatt_chr_def characteristicDefinition[6]; + struct ble_gatt_svc_def serviceDefinition[2]; + + std::string m_artist; + std::string m_album; + std::string m_track; + + }; + } +} + diff --git a/src/DisplayApp/Screens/Music.cpp b/src/DisplayApp/Screens/Music.cpp new file mode 100644 index 00000000..5b54c49c --- /dev/null +++ b/src/DisplayApp/Screens/Music.cpp @@ -0,0 +1,111 @@ +#include +#include "Music.h" + +using namespace Pinetime::Applications::Screens; +extern lv_font_t jetbrains_mono_extrabold_compressed; +extern lv_font_t jetbrains_mono_bold_20; + +static void event_handler(lv_obj_t * obj, lv_event_t event) +{ + Music* screen = static_cast(obj->user_data); + screen->OnObjectEvent(obj, event); +} + +Music::Music(Pinetime::Applications::DisplayApp *app, Pinetime::Controllers::MusicService &music) : Screen(app), musicService(music) { + lv_obj_t * label; + + btnVolDown = lv_btn_create(lv_scr_act(), NULL); + btnVolDown->user_data = this; + lv_obj_set_event_cb(btnVolDown, event_handler); + lv_obj_align(btnVolDown, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 10); + label = lv_label_create(btnVolDown, NULL); + lv_label_set_text(label, "v-"); + + btnVolUp = lv_btn_create(lv_scr_act(), NULL); + btnVolUp->user_data = this; + lv_obj_set_event_cb(btnVolUp, event_handler); + lv_obj_align(btnVolUp, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 10); + label = lv_label_create(btnVolUp, NULL); + lv_label_set_text(label, "v+"); + + btnPrev = lv_btn_create(lv_scr_act(), NULL); + btnPrev->user_data = this; + lv_obj_set_event_cb(btnPrev, event_handler); + lv_obj_set_size(btnPrev, LV_HOR_RES / 4, LV_VER_RES / 4); + lv_obj_align(btnPrev, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10,-10); + label = lv_label_create(btnPrev, NULL); + lv_label_set_text(label, "<<"); + + btnPlayPause = lv_btn_create(lv_scr_act(), NULL); + btnPlayPause->user_data = this; + lv_obj_set_event_cb(btnPlayPause, event_handler); + lv_obj_set_size(btnPlayPause, LV_HOR_RES / 4, LV_VER_RES / 4); + lv_obj_align(btnPlayPause, NULL, LV_ALIGN_IN_BOTTOM_MID, 0,-10); + label = lv_label_create(btnPlayPause, NULL); + lv_label_set_text(label, ">"); + + btnNext = lv_btn_create(lv_scr_act(), NULL); + btnNext->user_data = this; + lv_obj_set_event_cb(btnNext, event_handler); + lv_obj_set_size(btnNext, LV_HOR_RES / 4, LV_VER_RES / 4); + lv_obj_align(btnNext, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10,-10); + label = lv_label_create(btnNext, NULL); + lv_label_set_text(label, ">>"); + + txtArtist = lv_label_create(lv_scr_act(), NULL); + lv_label_set_long_mode(txtArtist, LV_LABEL_LONG_SROLL); + lv_obj_align(txtArtist, NULL, LV_ALIGN_IN_LEFT_MID, 0,-20); + lv_label_set_text(txtArtist, "Artist Name"); + lv_label_set_align(txtArtist, LV_LABEL_ALIGN_CENTER); + lv_obj_set_width(txtArtist, LV_HOR_RES); + + txtTrack = lv_label_create(lv_scr_act(), NULL); + lv_label_set_long_mode(txtTrack, LV_LABEL_LONG_DOT); + lv_obj_align(txtTrack, NULL, LV_ALIGN_IN_LEFT_MID, 0,20); + lv_label_set_text(txtTrack, "This is a very long track name"); + lv_label_set_align(txtTrack, LV_LABEL_ALIGN_CENTER); + lv_obj_set_width(txtTrack, LV_HOR_RES); +} + +Music::~Music() { + lv_obj_clean(lv_scr_act()); +} + +bool Music::OnButtonPushed() { + running = false; + return true; +} + +bool Music::Refresh() { + + if (m_artist != musicService.artist()) { + m_artist = musicService.artist(); + lv_label_set_text(txtArtist, m_artist.data()); + } + if (m_track != musicService.track()) { + m_track = musicService.track(); + lv_label_set_text(txtTrack, m_track.data()); + } + if (m_album != musicService.album()) { + m_album = musicService.album(); + } + + return running; +} + +void Music::OnObjectEvent(lv_obj_t* obj, lv_event_t event) +{ + if (event == LV_EVENT_CLICKED) { + if (obj == btnVolDown) { + musicService.event(Controllers::MusicService::EVENT_MUSIC_VOLUP); + } else if (obj == btnVolUp) { + musicService.event(Controllers::MusicService::EVENT_MUSIC_VOLDOWN); + } else if (obj == btnPrev) { + musicService.event(Controllers::MusicService::EVENT_MUSIC_PREV); + } else if (obj == btnPlayPause) { + musicService.event(Controllers::MusicService::EVENT_MUSIC_PLAY); + } else if (obj == btnNext) { + musicService.event(Controllers::MusicService::EVENT_MUSIC_NEXT); + } + } +} diff --git a/src/DisplayApp/Screens/Music.h b/src/DisplayApp/Screens/Music.h new file mode 100644 index 00000000..538e1daf --- /dev/null +++ b/src/DisplayApp/Screens/Music.h @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include "Screen.h" +#include +#include +#include +#include +#include +#include "../../Version.h" +#include +#include + +namespace Pinetime { + namespace Applications { + namespace Screens { + + class Music : public Screen{ + public: + Music(DisplayApp* app, Pinetime::Controllers::MusicService &music); + ~Music() override; + + bool Refresh() override; + bool OnButtonPushed() override; + + void OnObjectEvent(lv_obj_t* obj, lv_event_t event); + + private: + lv_obj_t * btnPrev; + lv_obj_t * btnPlayPause; + lv_obj_t * btnNext; + lv_obj_t * btnVolDown; + lv_obj_t * btnVolUp; + lv_obj_t * txtArtist; + lv_obj_t * txtTrack; + + bool running = true; + Pinetime::Controllers::MusicService &musicService; + std::string m_artist; + std::string m_album; + std::string m_track; + }; + } + } +} From 686e826f4e656546523e989535c74f286a91855b Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Wed, 15 Jul 2020 10:02:01 +0100 Subject: [PATCH 3/4] Rework characteristic handling --- src/Components/Ble/MusicService.cpp | 25 ++++++++++++++----------- src/Components/Ble/MusicService.h | 5 ++--- src/DisplayApp/DisplayApp.cpp | 2 +- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Components/Ble/MusicService.cpp b/src/Components/Ble/MusicService.cpp index 05754e44..080a814c 100644 --- a/src/Components/Ble/MusicService.cpp +++ b/src/Components/Ble/MusicService.cpp @@ -20,27 +20,28 @@ Pinetime::Controllers::MusicService::MusicService() msAlbumCharUuid.value[11] = msAlbumCharId[0]; msAlbumCharUuid.value[12] = msAlbumCharId[1]; - characteristicDefinition[0] = {(ble_uuid_t*)(&msEventCharUuid), + characteristicDefinition[0] = { .uuid = (ble_uuid_t*)(&msEventCharUuid), .access_cb = MSCallback, .arg = this, - .flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ + .flags = BLE_GATT_CHR_F_NOTIFY, + .val_handle = &m_eventHandle }; - characteristicDefinition[1] = {(ble_uuid_t*)(&msStatusCharUuid), + 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] = {(ble_uuid_t*)(&msTrackCharUuid), + 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] = {(ble_uuid_t*)(&msArtistCharUuid), + 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] = {(ble_uuid_t*)(&msAlbumCharUuid), + characteristicDefinition[4] = { .uuid = (ble_uuid_t*)(&msAlbumCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ @@ -72,8 +73,6 @@ void Pinetime::Controllers::MusicService::Init() int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt) { - connectionHandle = conn_handle; - if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { size_t notifSize = OS_MBUF_PKTLEN(ctxt->om); uint8_t data[notifSize + 1]; @@ -112,12 +111,16 @@ void Pinetime::Controllers::MusicService::event(char event) struct os_mbuf *om; int ret; + uint16_t connectionHandle = 0; + om = ble_hs_mbuf_from_flat(&event, 1); - ble_gatts_find_chr((ble_uuid_t *) &msUuid, (ble_uuid_t *) &msEventCharUuid, nullptr, - &eventCharacteristicHandle); + ret = ble_gatts_find_svc((ble_uuid_t *) &msUuid, &connectionHandle); - ret = ble_gattc_notify_custom(connectionHandle, eventCharacteristicHandle, om); + if (connectionHandle == 0) { + return; + } + ret = ble_gattc_notify_custom(connectionHandle, m_eventHandle, om); } diff --git a/src/Components/Ble/MusicService.h b/src/Components/Ble/MusicService.h index 8139d96e..ba872358 100644 --- a/src/Components/Ble/MusicService.h +++ b/src/Components/Ble/MusicService.h @@ -39,9 +39,6 @@ namespace Pinetime { static constexpr uint8_t msTrackCharId[2] = {0x00, 0x05}; static constexpr uint8_t msAlbumCharId[2] = {0x00, 0x06}; - uint16_t connectionHandle = 0; - uint16_t eventCharacteristicHandle = 0; - ble_uuid128_t msUuid { .u = { .type = BLE_UUID_TYPE_128 }, .value = MUSIC_SERVICE_UUID_BASE @@ -70,6 +67,8 @@ namespace Pinetime { struct ble_gatt_chr_def characteristicDefinition[6]; struct ble_gatt_svc_def serviceDefinition[2]; + + uint16_t m_eventHandle; std::string m_artist; std::string m_album; diff --git a/src/DisplayApp/DisplayApp.cpp b/src/DisplayApp/DisplayApp.cpp index 7c822ee0..46a96385 100644 --- a/src/DisplayApp/DisplayApp.cpp +++ b/src/DisplayApp/DisplayApp.cpp @@ -190,7 +190,7 @@ void DisplayApp::RunningState() { case Apps::Meter: currentScreen.reset(new Screens::Meter(this)); break; case Apps::Gauge: currentScreen.reset(new Screens::Gauge(this)); break; case Apps::Brightness : currentScreen.reset(new Screens::Brightness(this, brightnessController)); break; - case Apps::Music : currentScreen.reset(new Screens::Music(this)); break; + case Apps::Music : currentScreen.reset(new Screens::Music(this, systemTask.nimble().music())); break; } nextApp = Apps::None; } From 5713eac1045394928de19e76fd00a172f63bffa7 Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Mon, 20 Jul 2020 21:28:21 +0100 Subject: [PATCH 4/4] Fully implement music app and service SystemTask can return a reference to the nimbleController The nimbleController can return a reference to the musicService The musicService get a connection handle from the nimbleController The musicApp communicated directly with the musicService --- src/Components/Ble/MusicService.cpp | 20 ++++++++++++-------- src/Components/Ble/MusicService.h | 18 +++++++++++++++--- src/Components/Ble/NimbleController.cpp | 8 ++++++-- src/Components/Ble/NimbleController.h | 5 ++++- src/DisplayApp/Screens/Music.cpp | 24 +++++++++++++++++++----- src/DisplayApp/Screens/Music.h | 2 ++ src/SystemTask/SystemTask.cpp | 1 + src/SystemTask/SystemTask.h | 3 ++- 8 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/Components/Ble/MusicService.cpp b/src/Components/Ble/MusicService.cpp index 080a814c..5ec76697 100644 --- a/src/Components/Ble/MusicService.cpp +++ b/src/Components/Ble/MusicService.cpp @@ -1,3 +1,4 @@ +#include #include "MusicService.h" int MSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { @@ -5,7 +6,7 @@ int MSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_acces return musicService->OnCommand(conn_handle, attr_handle, ctxt); } -Pinetime::Controllers::MusicService::MusicService() +Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask &system) : m_system(system) { msUuid.value[11] = msId[0]; msUuid.value[12] = msId[1]; @@ -86,6 +87,8 @@ int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_ m_track = s; } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msAlbumCharUuid) == 0) { m_album = s; + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msStatusCharUuid) == 0) { + m_status = s[0]; } } return 0; @@ -106,18 +109,19 @@ std::string Pinetime::Controllers::MusicService::track() return m_track; } +unsigned char Pinetime::Controllers::MusicService::status() +{ + return m_status; +} + void Pinetime::Controllers::MusicService::event(char event) { - struct os_mbuf *om; + auto *om = ble_hs_mbuf_from_flat(&event, 1); int ret; - uint16_t connectionHandle = 0; + uint16_t connectionHandle = m_system.nimble().connHandle(); - om = ble_hs_mbuf_from_flat(&event, 1); - - ret = ble_gatts_find_svc((ble_uuid_t *) &msUuid, &connectionHandle); - - if (connectionHandle == 0) { + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { return; } diff --git a/src/Components/Ble/MusicService.h b/src/Components/Ble/MusicService.h index ba872358..ab6db572 100644 --- a/src/Components/Ble/MusicService.h +++ b/src/Components/Ble/MusicService.h @@ -1,4 +1,5 @@ #pragma once + #include #include #include @@ -9,10 +10,14 @@ #define MUSIC_SERVICE_UUID_BASE {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0xe5, 0xc7} namespace Pinetime { + namespace System { + class SystemTask; + } namespace Controllers { + class MusicService { public: - MusicService(); + MusicService(Pinetime::System::SystemTask &system); void Init(); int OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt); @@ -20,6 +25,8 @@ namespace Pinetime { std::string artist(); std::string track(); std::string album(); + unsigned char status(); + void event(char event); static const char EVENT_MUSIC_OPEN = 0xe0; @@ -29,7 +36,8 @@ namespace Pinetime { static const char EVENT_MUSIC_PREV = 0x04; static const char EVENT_MUSIC_VOLUP = 0x05; static const char EVENT_MUSIC_VOLDOWN = 0x06; - + static const char STATUS_MUSIC_PAUSED = 0x00; + static const char STATUS_MUSIC_PLAYING = 0x01; private: static constexpr uint8_t msId[2] = {0x00, 0x01}; @@ -67,13 +75,17 @@ namespace Pinetime { struct ble_gatt_chr_def characteristicDefinition[6]; struct ble_gatt_svc_def serviceDefinition[2]; - + uint16_t m_eventHandle; std::string m_artist; std::string m_album; std::string m_track; + unsigned char m_status; + + Pinetime::System::SystemTask& m_system; + }; } } diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 53458676..d7bbd8be 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -6,6 +6,7 @@ #include #include "NimbleController.h" +#include "MusicService.h" #include #include #include @@ -35,7 +36,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, currentTimeClient{dateTimeController}, anService{systemTask, notificationManager}, alertNotificationClient{systemTask, notificationManager}, - currentTimeService{dateTimeController} { + currentTimeService{dateTimeController}, + musicService{systemTask} { } @@ -327,5 +329,7 @@ void NimbleController::StartDiscovery() { } - +uint16_t NimbleController::connHandle() { + return connectionHandle; +} diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index fab3df2b..dff93c87 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -16,6 +16,7 @@ namespace Pinetime { } namespace Controllers { class DateTime; + class NimbleController { public: @@ -39,6 +40,8 @@ namespace Pinetime { Pinetime::Controllers::MusicService& music() {return musicService;}; + uint16_t connHandle(); + private: static constexpr char* deviceName = "Pinetime-JF"; Pinetime::System::SystemTask& systemTask; @@ -56,7 +59,7 @@ namespace Pinetime { MusicService musicService; uint8_t addrType; // 1 = Random, 0 = PUBLIC - uint16_t connectionHandle; + uint16_t connectionHandle = 0; ble_uuid128_t dfuServiceUuid { .u { .type = BLE_UUID_TYPE_128}, diff --git a/src/DisplayApp/Screens/Music.cpp b/src/DisplayApp/Screens/Music.cpp index 5b54c49c..9b7d198b 100644 --- a/src/DisplayApp/Screens/Music.cpp +++ b/src/DisplayApp/Screens/Music.cpp @@ -41,8 +41,8 @@ Music::Music(Pinetime::Applications::DisplayApp *app, Pinetime::Controllers::Mus lv_obj_set_event_cb(btnPlayPause, event_handler); lv_obj_set_size(btnPlayPause, LV_HOR_RES / 4, LV_VER_RES / 4); lv_obj_align(btnPlayPause, NULL, LV_ALIGN_IN_BOTTOM_MID, 0,-10); - label = lv_label_create(btnPlayPause, NULL); - lv_label_set_text(label, ">"); + txtPlayPause = lv_label_create(btnPlayPause, NULL); + lv_label_set_text(txtPlayPause, ">"); btnNext = lv_btn_create(lv_scr_act(), NULL); btnNext->user_data = this; @@ -65,6 +65,8 @@ Music::Music(Pinetime::Applications::DisplayApp *app, Pinetime::Controllers::Mus lv_label_set_text(txtTrack, "This is a very long track name"); lv_label_set_align(txtTrack, LV_LABEL_ALIGN_CENTER); lv_obj_set_width(txtTrack, LV_HOR_RES); + + musicService.event(Controllers::MusicService::EVENT_MUSIC_OPEN); } Music::~Music() { @@ -89,6 +91,14 @@ bool Music::Refresh() { if (m_album != musicService.album()) { m_album = musicService.album(); } + if (m_status != musicService.status()) { + m_status = musicService.status(); + } + if (m_status == Pinetime::Controllers::MusicService::STATUS_MUSIC_PLAYING) { + lv_label_set_text(txtPlayPause, "||"); + } else { + lv_label_set_text(txtPlayPause, ">"); + } return running; } @@ -97,13 +107,17 @@ void Music::OnObjectEvent(lv_obj_t* obj, lv_event_t event) { if (event == LV_EVENT_CLICKED) { if (obj == btnVolDown) { - musicService.event(Controllers::MusicService::EVENT_MUSIC_VOLUP); - } else if (obj == btnVolUp) { musicService.event(Controllers::MusicService::EVENT_MUSIC_VOLDOWN); + } else if (obj == btnVolUp) { + musicService.event(Controllers::MusicService::EVENT_MUSIC_VOLUP); } else if (obj == btnPrev) { musicService.event(Controllers::MusicService::EVENT_MUSIC_PREV); } else if (obj == btnPlayPause) { - musicService.event(Controllers::MusicService::EVENT_MUSIC_PLAY); + if (m_status == Pinetime::Controllers::MusicService::STATUS_MUSIC_PLAYING) { + musicService.event(Controllers::MusicService::EVENT_MUSIC_PAUSE); + } else { + musicService.event(Controllers::MusicService::EVENT_MUSIC_PLAY); + } } else if (obj == btnNext) { musicService.event(Controllers::MusicService::EVENT_MUSIC_NEXT); } diff --git a/src/DisplayApp/Screens/Music.h b/src/DisplayApp/Screens/Music.h index 538e1daf..95cac0f0 100644 --- a/src/DisplayApp/Screens/Music.h +++ b/src/DisplayApp/Screens/Music.h @@ -35,12 +35,14 @@ namespace Pinetime { lv_obj_t * btnVolUp; lv_obj_t * txtArtist; lv_obj_t * txtTrack; + lv_obj_t * txtPlayPause; bool running = true; Pinetime::Controllers::MusicService &musicService; std::string m_artist; std::string m_album; std::string m_track; + unsigned char m_status; }; } } diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 39e9751b..a17808f1 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -12,6 +12,7 @@ #include #include #include "../main.h" +#include "Components/Ble/NimbleController.h" using namespace Pinetime::System; diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index 5a51a776..0d8b87f6 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -8,9 +8,10 @@ #include #include #include -#include #include #include "SystemMonitor.h" +#include "Components/Ble/NimbleController.h" +#include "timers.h" namespace Pinetime { namespace System {