InfiniTime.git

commit 5713eac1045394928de19e76fd00a172f63bffa7

Author: Adam Pigg <adam@piggz.co.uk>

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 ++-


diff --git a/src/Components/Ble/MusicService.cpp b/src/Components/Ble/MusicService.cpp
index 080a814cecdc42df08e02630e0d3ed967d33c80a..5ec766972986a400bca30d8f16b1d968699f6549 100644
--- a/src/Components/Ble/MusicService.cpp
+++ b/src/Components/Ble/MusicService.cpp
@@ -1,3 +1,4 @@
+#include <SystemTask/SystemTask.h>
 #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 @@   auto musicService = static_cast(arg);
   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 @@         } 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;
+        } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msStatusCharUuid) == 0) {
+            m_status = s[0];
         }
   }
   return 0;
@@ -106,18 +109,19 @@ {
     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 ba872358c7dcdc13686045fa63b46fb85a2293b5..ab6db572b183980efec46bd4942f729b4762d672 100644
--- a/src/Components/Ble/MusicService.h
+++ b/src/Components/Ble/MusicService.h
@@ -1,4 +1,5 @@
 #pragma once
+
 #include <cstdint>
 #include <array>
 #include <host/ble_gap.h>
@@ -9,10 +10,14 @@ //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 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 @@
         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 @@         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;
-
+        static const char STATUS_MUSIC_PAUSED = 0x00;
+        static const char STATUS_MUSIC_PLAYING = 0x01;
 
       private:
         static constexpr uint8_t msId[2] = {0x00, 0x01};
@@ -67,12 +75,16 @@         };
 
         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 53458676f939e964968381bfc32848eda17f740b..d7bbd8be1669359bf04136cfbc7aecf752501c9c 100644
--- a/src/Components/Ble/NimbleController.cpp
+++ b/src/Components/Ble/NimbleController.cpp
@@ -6,6 +6,7 @@ #include 
 #include <hal/nrf_rtc.h>
 
 #include "NimbleController.h"
+#include "MusicService.h"
 #include <services/gatt/ble_svc_gatt.h>
 #include <services/gap/ble_svc_gap.h>
 #include <host/util/util.h>
@@ -35,7 +36,8 @@         dfuService{systemTask, bleController, spiNorFlash},
         currentTimeClient{dateTimeController},
         anService{systemTask, notificationManager},
         alertNotificationClient{systemTask, notificationManager},
-        currentTimeService{dateTimeController} {
+        currentTimeService{dateTimeController},
+        musicService{systemTask} {
 
 }
 
@@ -327,5 +329,7 @@   ble_gattc_disc_all_svcs(connectionHandle, OnAllSvrDisco, this);
 }
 
 
-
+uint16_t NimbleController::connHandle() {
+    return connectionHandle;
+}
 




diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h
index fab3df2b2a114c762a31be38b9b4c784aded678d..dff93c8784f08fa16d3895882421465b9fbf20ac 100644
--- a/src/Components/Ble/NimbleController.h
+++ b/src/Components/Ble/NimbleController.h
@@ -16,6 +16,7 @@     class SpiNorFlash;
   }
   namespace Controllers {
     class DateTime;
+
     class NimbleController {
 
       public:
@@ -39,6 +40,8 @@         void StartDiscovery();
 
         Pinetime::Controllers::MusicService& music() {return musicService;};
 
+        uint16_t connHandle();
+
       private:
         static constexpr char* deviceName = "Pinetime-JF";
         Pinetime::System::SystemTask& systemTask;
@@ -56,7 +59,7 @@         CurrentTimeService currentTimeService;
         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 5b54c49ce53b19b44356eb3018db8a4aace74116..9b7d198bcde58788e36f92454b28b30daee61e92 100644
--- a/src/DisplayApp/Screens/Music.cpp
+++ b/src/DisplayApp/Screens/Music.cpp
@@ -41,8 +41,8 @@     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, ">");
+    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 @@     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);
+
+    musicService.event(Controllers::MusicService::EVENT_MUSIC_OPEN);
 }
 
 Music::~Music() {
@@ -89,6 +91,14 @@     }
     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 538e1daf3c0d843285f8e4b0806cfc9ea9e11722..95cac0f0a434d7d206bb9721ccb348afd21532df 100644
--- a/src/DisplayApp/Screens/Music.h
+++ b/src/DisplayApp/Screens/Music.h
@@ -35,12 +35,14 @@           lv_obj_t * btnVolDown;
           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 39e9751b9aeb20ae8a3b504b43f6677586c4d027..a17808f1a62938838a4a3456833c25c678edaba1 100644
--- a/src/SystemTask/SystemTask.cpp
+++ b/src/SystemTask/SystemTask.cpp
@@ -12,6 +12,7 @@ #include 
 #include <host/util/util.h>
 #include <drivers/InternalFlash.h>
 #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 5a51a7769024f809d86006f32a67f6c786db921e..0d8b87f6619404858191942ef8bcdc6989571506 100644
--- a/src/SystemTask/SystemTask.h
+++ b/src/SystemTask/SystemTask.h
@@ -8,9 +8,10 @@ #include 
 #include <Components/Battery/BatteryController.h>
 #include <DisplayApp/DisplayApp.h>
 #include <drivers/Watchdog.h>
-#include <Components/Ble/NimbleController.h>
 #include <drivers/SpiNorFlash.h>
 #include "SystemMonitor.h"
+#include "Components/Ble/NimbleController.h"
+#include "timers.h"
 
 namespace Pinetime {
   namespace System {