Initial code for music app screena and service

This commit is contained in:
Adam Pigg 2020-07-13 12:40:39 +01:00
parent 789e06fdb7
commit 306aa25aad
4 changed files with 362 additions and 0 deletions

View File

@ -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<Pinetime::Controllers::MusicService*>(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);
}

View File

@ -0,0 +1,81 @@
#pragma once
#include <cstdint>
#include <array>
#include <host/ble_gap.h>
#include <host/ble_uuid.h>
#include <string>
//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;
};
}
}

View File

@ -0,0 +1,111 @@
#include <libs/lvgl/lvgl.h>
#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<Music *>(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);
}
}
}

View File

@ -0,0 +1,47 @@
#pragma once
#include <cstdint>
#include <chrono>
#include <Components/Gfx/Gfx.h>
#include "Screen.h"
#include <bits/unique_ptr.h>
#include <libs/lvgl/src/lv_core/lv_style.h>
#include <libs/lvgl/src/lv_core/lv_obj.h>
#include <Components/Battery/BatteryController.h>
#include <Components/Ble/BleController.h>
#include "../../Version.h"
#include <Components/Ble/MusicService.h>
#include <string>
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;
};
}
}
}