2022-11-28 16:27:16 +00:00
|
|
|
#include "hid_media.h"
|
2021-12-15 17:39:06 +00:00
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_bt_hid.h>
|
|
|
|
#include <furi_hal_usb_hid.h>
|
2021-12-15 17:39:06 +00:00
|
|
|
#include <gui/elements.h>
|
2022-11-28 16:27:16 +00:00
|
|
|
#include "../hid.h"
|
2021-12-15 17:39:06 +00:00
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
#include "hid_icons.h"
|
2022-10-12 15:48:13 +00:00
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
#define TAG "HidMedia"
|
|
|
|
|
|
|
|
struct HidMedia {
|
2021-12-15 17:39:06 +00:00
|
|
|
View* view;
|
2022-11-28 16:27:16 +00:00
|
|
|
Hid* hid;
|
2021-12-15 17:39:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
bool left_pressed;
|
|
|
|
bool up_pressed;
|
|
|
|
bool right_pressed;
|
|
|
|
bool down_pressed;
|
|
|
|
bool ok_pressed;
|
|
|
|
bool connected;
|
2022-12-27 08:03:56 +00:00
|
|
|
HidTransport transport;
|
2022-11-28 16:27:16 +00:00
|
|
|
} HidMediaModel;
|
2021-12-15 17:39:06 +00:00
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
static void hid_media_draw_arrow(Canvas* canvas, uint8_t x, uint8_t y, CanvasDirection dir) {
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_draw_triangle(canvas, x, y, 5, 3, dir);
|
|
|
|
if(dir == CanvasDirectionBottomToTop) {
|
|
|
|
canvas_draw_dot(canvas, x, y - 1);
|
|
|
|
} else if(dir == CanvasDirectionTopToBottom) {
|
|
|
|
canvas_draw_dot(canvas, x, y + 1);
|
|
|
|
} else if(dir == CanvasDirectionRightToLeft) {
|
|
|
|
canvas_draw_dot(canvas, x - 1, y);
|
|
|
|
} else if(dir == CanvasDirectionLeftToRight) {
|
|
|
|
canvas_draw_dot(canvas, x + 1, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
static void hid_media_draw_callback(Canvas* canvas, void* context) {
|
2021-12-15 17:39:06 +00:00
|
|
|
furi_assert(context);
|
2022-11-28 16:27:16 +00:00
|
|
|
HidMediaModel* model = context;
|
2021-12-15 17:39:06 +00:00
|
|
|
|
|
|
|
// Header
|
2022-12-27 08:03:56 +00:00
|
|
|
if(model->transport == HidTransportBle) {
|
|
|
|
if(model->connected) {
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Ble_connected_15x15);
|
|
|
|
} else {
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Ble_disconnected_15x15);
|
|
|
|
}
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
2022-12-27 08:03:56 +00:00
|
|
|
|
2022-07-08 12:36:34 +00:00
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
elements_multiline_text_aligned(canvas, 17, 3, AlignLeft, AlignTop, "Media");
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
2021-12-15 17:39:06 +00:00
|
|
|
|
|
|
|
// Keypad circles
|
|
|
|
canvas_draw_icon(canvas, 76, 8, &I_Circles_47x47);
|
|
|
|
|
|
|
|
// Up
|
|
|
|
if(model->up_pressed) {
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_draw_icon(canvas, 93, 9, &I_Pressed_Button_13x13);
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
|
|
|
canvas_draw_icon(canvas, 96, 12, &I_Volup_8x6);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Down
|
|
|
|
if(model->down_pressed) {
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_draw_icon(canvas, 93, 41, &I_Pressed_Button_13x13);
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
|
|
|
canvas_draw_icon(canvas, 96, 45, &I_Voldwn_6x6);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Left
|
|
|
|
if(model->left_pressed) {
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_draw_icon(canvas, 77, 25, &I_Pressed_Button_13x13);
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_media_draw_arrow(canvas, 82, 31, CanvasDirectionRightToLeft);
|
|
|
|
hid_media_draw_arrow(canvas, 86, 31, CanvasDirectionRightToLeft);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Right
|
|
|
|
if(model->right_pressed) {
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_draw_icon(canvas, 109, 25, &I_Pressed_Button_13x13);
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_media_draw_arrow(canvas, 112, 31, CanvasDirectionLeftToRight);
|
|
|
|
hid_media_draw_arrow(canvas, 116, 31, CanvasDirectionLeftToRight);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Ok
|
|
|
|
if(model->ok_pressed) {
|
|
|
|
canvas_draw_icon(canvas, 93, 25, &I_Pressed_Button_13x13);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_media_draw_arrow(canvas, 96, 31, CanvasDirectionLeftToRight);
|
2021-12-15 17:39:06 +00:00
|
|
|
canvas_draw_line(canvas, 100, 29, 100, 33);
|
|
|
|
canvas_draw_line(canvas, 102, 29, 102, 33);
|
2022-08-22 16:41:41 +00:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Exit
|
|
|
|
canvas_draw_icon(canvas, 0, 54, &I_Pin_back_arrow_10x8);
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
elements_multiline_text_aligned(canvas, 13, 62, AlignLeft, AlignBottom, "Hold to exit");
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
static void hid_media_process_press(HidMedia* hid_media, InputEvent* event) {
|
2021-12-15 17:39:06 +00:00
|
|
|
with_view_model(
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_media->view,
|
|
|
|
HidMediaModel * model,
|
2022-10-08 17:38:29 +00:00
|
|
|
{
|
2021-12-15 17:39:06 +00:00
|
|
|
if(event->key == InputKeyUp) {
|
|
|
|
model->up_pressed = true;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_press(hid_media->hid, HID_CONSUMER_VOLUME_INCREMENT);
|
2021-12-15 17:39:06 +00:00
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
model->down_pressed = true;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_press(hid_media->hid, HID_CONSUMER_VOLUME_DECREMENT);
|
2021-12-15 17:39:06 +00:00
|
|
|
} else if(event->key == InputKeyLeft) {
|
|
|
|
model->left_pressed = true;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_press(hid_media->hid, HID_CONSUMER_SCAN_PREVIOUS_TRACK);
|
2021-12-15 17:39:06 +00:00
|
|
|
} else if(event->key == InputKeyRight) {
|
|
|
|
model->right_pressed = true;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_press(hid_media->hid, HID_CONSUMER_SCAN_NEXT_TRACK);
|
2021-12-15 17:39:06 +00:00
|
|
|
} else if(event->key == InputKeyOk) {
|
|
|
|
model->ok_pressed = true;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_press(hid_media->hid, HID_CONSUMER_PLAY_PAUSE);
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
static void hid_media_process_release(HidMedia* hid_media, InputEvent* event) {
|
2021-12-15 17:39:06 +00:00
|
|
|
with_view_model(
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_media->view,
|
|
|
|
HidMediaModel * model,
|
2022-10-08 17:38:29 +00:00
|
|
|
{
|
2021-12-15 17:39:06 +00:00
|
|
|
if(event->key == InputKeyUp) {
|
|
|
|
model->up_pressed = false;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_release(hid_media->hid, HID_CONSUMER_VOLUME_INCREMENT);
|
2021-12-15 17:39:06 +00:00
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
model->down_pressed = false;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_release(hid_media->hid, HID_CONSUMER_VOLUME_DECREMENT);
|
2021-12-15 17:39:06 +00:00
|
|
|
} else if(event->key == InputKeyLeft) {
|
|
|
|
model->left_pressed = false;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_release(hid_media->hid, HID_CONSUMER_SCAN_PREVIOUS_TRACK);
|
2021-12-15 17:39:06 +00:00
|
|
|
} else if(event->key == InputKeyRight) {
|
|
|
|
model->right_pressed = false;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_release(hid_media->hid, HID_CONSUMER_SCAN_NEXT_TRACK);
|
2021-12-15 17:39:06 +00:00
|
|
|
} else if(event->key == InputKeyOk) {
|
|
|
|
model->ok_pressed = false;
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_release(hid_media->hid, HID_CONSUMER_PLAY_PAUSE);
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
static bool hid_media_input_callback(InputEvent* event, void* context) {
|
2021-12-15 17:39:06 +00:00
|
|
|
furi_assert(context);
|
2022-11-28 16:27:16 +00:00
|
|
|
HidMedia* hid_media = context;
|
2021-12-15 17:39:06 +00:00
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
if(event->type == InputTypePress) {
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_media_process_press(hid_media, event);
|
2021-12-15 17:39:06 +00:00
|
|
|
consumed = true;
|
|
|
|
} else if(event->type == InputTypeRelease) {
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_media_process_release(hid_media, event);
|
2021-12-15 17:39:06 +00:00
|
|
|
consumed = true;
|
|
|
|
} else if(event->type == InputTypeShort) {
|
|
|
|
if(event->key == InputKeyBack) {
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_hal_consumer_key_release_all(hid_media->hid);
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
HidMedia* hid_media_alloc(Hid* hid) {
|
|
|
|
HidMedia* hid_media = malloc(sizeof(HidMedia));
|
|
|
|
hid_media->view = view_alloc();
|
|
|
|
hid_media->hid = hid;
|
|
|
|
view_set_context(hid_media->view, hid_media);
|
|
|
|
view_allocate_model(hid_media->view, ViewModelTypeLocking, sizeof(HidMediaModel));
|
|
|
|
view_set_draw_callback(hid_media->view, hid_media_draw_callback);
|
|
|
|
view_set_input_callback(hid_media->view, hid_media_input_callback);
|
2021-12-15 17:39:06 +00:00
|
|
|
|
2022-12-27 08:03:56 +00:00
|
|
|
with_view_model(
|
|
|
|
hid_media->view, HidMediaModel * model, { model->transport = hid->transport; }, true);
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
return hid_media;
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
void hid_media_free(HidMedia* hid_media) {
|
|
|
|
furi_assert(hid_media);
|
|
|
|
view_free(hid_media->view);
|
|
|
|
free(hid_media);
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
View* hid_media_get_view(HidMedia* hid_media) {
|
|
|
|
furi_assert(hid_media);
|
|
|
|
return hid_media->view;
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:16 +00:00
|
|
|
void hid_media_set_connected_status(HidMedia* hid_media, bool connected) {
|
|
|
|
furi_assert(hid_media);
|
2021-12-15 17:39:06 +00:00
|
|
|
with_view_model(
|
2022-11-28 16:27:16 +00:00
|
|
|
hid_media->view, HidMediaModel * model, { model->connected = connected; }, true);
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|