Skip to content

Commit

Permalink
format code, add blinking on click
Browse files Browse the repository at this point in the history
  • Loading branch information
gebeto committed Jan 16, 2023
1 parent 7080c29 commit 9d4a46c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 62 deletions.
34 changes: 18 additions & 16 deletions views/xbox_controller_view.c
@@ -1,13 +1,8 @@
#include "xbox_controller_view.h"
#include <furi.h>
#include <gui/canvas_i.h>
#include <gui/elements.h>
#include <xc_icons.h>

#include <infrared_transmit.h>

struct XboxControllerView {
View* view;
NotificationApp* notifications;
};

typedef struct {
Expand Down Expand Up @@ -100,12 +95,20 @@ static void xbox_controller_view_draw_callback(Canvas* canvas, void* context) {
canvas, model->back_pressed, 0, buttons_post + 19, "B", &I_Pin_back_arrow_10x8);
}

void send_xbox_ir(uint32_t command) {
const NotificationSequence sequence_blink_purple_50 = {
&message_red_255,
&message_blue_255,
&message_delay_50,
NULL,
};

void send_xbox_ir(uint32_t command, NotificationApp* notifications) {
InfraredMessage* message = malloc(sizeof(InfraredMessage));
message->protocol = InfraredProtocolNECext;
message->address = 0xD880;
message->command = command;
message->repeat = false;
notification_message(notifications, &sequence_blink_purple_50);
infrared_send(message, 2);
free(message);
}
Expand All @@ -119,22 +122,22 @@ static void
if(event->type == InputTypePress) {
if(event->key == InputKeyUp) {
model->up_pressed = true;
send_xbox_ir(0xE11E);
send_xbox_ir(0xE11E, xbox_controller_view->notifications);
} else if(event->key == InputKeyDown) {
model->down_pressed = true;
send_xbox_ir(0xE01F);
send_xbox_ir(0xE01F, xbox_controller_view->notifications);
} else if(event->key == InputKeyLeft) {
model->left_pressed = true;
send_xbox_ir(0xDF20);
send_xbox_ir(0xDF20, xbox_controller_view->notifications);
} else if(event->key == InputKeyRight) {
model->right_pressed = true;
send_xbox_ir(0xDE21);
send_xbox_ir(0xDE21, xbox_controller_view->notifications);
} else if(event->key == InputKeyOk) {
model->ok_pressed = true;
send_xbox_ir(0x9966);
send_xbox_ir(0x9966, xbox_controller_view->notifications);
} else if(event->key == InputKeyBack) {
model->back_pressed = true;
send_xbox_ir(0x9A65);
send_xbox_ir(0x9A65, xbox_controller_view->notifications);
}
} else if(event->type == InputTypeRelease) {
if(event->key == InputKeyUp) {
Expand Down Expand Up @@ -167,8 +170,6 @@ static bool xbox_controller_view_input_callback(InputEvent* event, void* context
XboxControllerView* xbox_controller_view = context;
bool consumed = false;

FURI_LOG_I("XBOX_CONTROLLER", "TYPE: %d, KEY: %d", event->type, event->key);

if(event->type == InputTypeLong && event->key == InputKeyBack) {
// LONG KEY BACK PRESS HANDLER
} else {
Expand All @@ -179,9 +180,10 @@ static bool xbox_controller_view_input_callback(InputEvent* event, void* context
return consumed;
}

XboxControllerView* xbox_controller_view_alloc() {
XboxControllerView* xbox_controller_view_alloc(NotificationApp* notifications) {
XboxControllerView* xbox_controller_view = malloc(sizeof(XboxControllerView));
xbox_controller_view->view = view_alloc();
xbox_controller_view->notifications = notifications;
view_set_orientation(xbox_controller_view->view, ViewOrientationVertical);
view_set_context(xbox_controller_view->view, xbox_controller_view);
view_allocate_model(
Expand Down
8 changes: 8 additions & 0 deletions views/xbox_controller_view.h
@@ -1,6 +1,14 @@
#pragma once

#include <gui/view.h>
#include <furi.h>
#include <gui/canvas_i.h>
#include <gui/elements.h>
#include <xc_icons.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>

#include <infrared_transmit.h>

typedef struct XboxControllerView XboxControllerView;

Expand Down
55 changes: 19 additions & 36 deletions xbox_controller.c
@@ -1,38 +1,30 @@
#include "xbox_controller.h"
#include "xc_icons.h"

#define TAG "XboxControllerApp"

enum XboxControllerSubmenuIndex
{
enum XboxControllerSubmenuIndex {
XboxControllerSubmenuIndexXboxOne,
XboxControllerSubmenuIndexPower
};

uint32_t usb_hid_exit_confirm_view(void *context)
{
uint32_t usb_hid_exit_confirm_view(void* context) {
UNUSED(context);
return UsbHidViewSubmenu;
}

uint32_t usb_hid_exit(void *context)
{
uint32_t usb_hid_exit(void* context) {
UNUSED(context);
return VIEW_NONE;
}

void usb_hid_submenu_callback(void *context, uint32_t index)
{
void usb_hid_submenu_callback(void* context, uint32_t index) {
furi_assert(context);
XboxController *app = context;
if (index == XboxControllerSubmenuIndexXboxOne)
{
XboxController* app = context;
if(index == XboxControllerSubmenuIndexXboxOne) {
app->view_id = UsbHidViewXboxController;
view_dispatcher_switch_to_view(app->view_dispatcher, UsbHidViewXboxController);
}
else if (index == XboxControllerSubmenuIndexPower)
{
InfraredMessage *message = malloc(sizeof(InfraredMessage));
} else if(index == XboxControllerSubmenuIndexPower) {
InfraredMessage* message = malloc(sizeof(InfraredMessage));
message->protocol = InfraredProtocolNECext;
message->address = 0xD880;
message->command = 0xD02F;
Expand All @@ -42,27 +34,20 @@ void usb_hid_submenu_callback(void *context, uint32_t index)
}
}

void usb_hid_dialog_callback(DialogExResult result, void *context)
{
void usb_hid_dialog_callback(DialogExResult result, void* context) {
furi_assert(context);
XboxController *app = context;
if (result == DialogExResultLeft)
{
XboxController* app = context;
if(result == DialogExResultLeft) {
view_dispatcher_stop(app->view_dispatcher);
}
else if (result == DialogExResultRight)
{
} else if(result == DialogExResultRight) {
view_dispatcher_switch_to_view(app->view_dispatcher, app->view_id); // Show last view
}
else if (result == DialogExResultCenter)
{
} else if(result == DialogExResultCenter) {
view_dispatcher_switch_to_view(app->view_dispatcher, UsbHidViewSubmenu);
}
}

XboxController *xbox_controller_app_alloc()
{
XboxController *app = malloc(sizeof(XboxController));
XboxController* xbox_controller_app_alloc() {
XboxController* app = malloc(sizeof(XboxController));

app->gui = furi_record_open(RECORD_GUI);
app->notifications = furi_record_open(RECORD_NOTIFICATION);
Expand All @@ -84,7 +69,7 @@ XboxController *xbox_controller_app_alloc()
app->view_dispatcher, UsbHidViewSubmenu, submenu_get_view(app->submenu));

// Xbox Controller View
app->xbox_controller_view = xbox_controller_view_alloc();
app->xbox_controller_view = xbox_controller_view_alloc(app->notifications);
view_set_previous_callback(
xbox_controller_view_get_view(app->xbox_controller_view), usb_hid_exit_confirm_view);
view_dispatcher_add_view(
Expand All @@ -99,8 +84,7 @@ XboxController *xbox_controller_app_alloc()
return app;
}

void xbox_controller_app_free(XboxController *app)
{
void xbox_controller_app_free(XboxController* app) {
furi_assert(app);

// Reset notification
Expand All @@ -123,11 +107,10 @@ void xbox_controller_app_free(XboxController *app)
free(app);
}

int32_t xbox_controller_app(void *p)
{
int32_t xbox_controller_app(void* p) {
UNUSED(p);

XboxController *app = xbox_controller_app_alloc();
XboxController* app = xbox_controller_app_alloc();
view_dispatcher_run(app->view_dispatcher);

xbox_controller_app_free(app);
Expand Down
20 changes: 10 additions & 10 deletions xbox_controller.h
Expand Up @@ -11,23 +11,23 @@
#include <infrared_worker.h>
#include <infrared_transmit.h>
#include <gui/modules/dialog_ex.h>

#include "views/xbox_controller_view.h"
#include "xc_icons.h"

// this should be used as global state
// we can store different things here
typedef struct
{
Gui *gui;
NotificationApp *notifications;
ViewDispatcher *view_dispatcher;
Submenu *submenu;
DialogEx *dialog;
XboxControllerView *xbox_controller_view;
typedef struct {
Gui* gui;
NotificationApp* notifications;
ViewDispatcher* view_dispatcher;
Submenu* submenu;
DialogEx* dialog;
XboxControllerView* xbox_controller_view;
uint32_t view_id;
} XboxController;

typedef enum
{
typedef enum {
UsbHidViewSubmenu,
UsbHidViewXboxController,
UsbHidViewExitConfirm,
Expand Down

0 comments on commit 9d4a46c

Please sign in to comment.