Skip to content

Commit

Permalink
upd airmouseofw
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Feb 19, 2024
1 parent 79de917 commit 0c46ae9
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 43 deletions.
106 changes: 78 additions & 28 deletions non_catalog_apps/air_mouse_ofw/air_mouse_app.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_bt_hid.h>
#include <furi_hal_bt.h>
#include <extra_profiles/hid_profile.h>
#include <bt/bt_service/bt.h>

#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/modules/submenu.h>
Expand All @@ -25,6 +27,7 @@ typedef struct {
ICM42688P* icm42688p;
FuriHalUsbInterface* usb_mode_prev;
Bt* bt;
FuriHalBleProfileBase* ble_hid_profile;
} AirMouseApp;

typedef enum {
Expand All @@ -39,19 +42,60 @@ enum StertSubmenuIndex {
StartSubmenuIndexBleReset,
};

static const BleProfileHidParams ble_hid_params = {
.device_name_prefix = "AirMouse",
.mac_xor = 0x0001,
};

static bool usb_hid_mouse_move(void* inst, int8_t dx, int8_t dy) {
UNUSED(inst);
return furi_hal_hid_mouse_move(dx, dy);
}

static bool usb_hid_mouse_key_press(void* inst, uint8_t button) {
UNUSED(inst);
return furi_hal_hid_mouse_press(button);
}

static bool usb_hid_mouse_key_release(void* inst, uint8_t button) {
UNUSED(inst);
return furi_hal_hid_mouse_release(button);
}

static bool usb_hid_mouse_scroll(void* inst, int8_t value) {
UNUSED(inst);
return furi_hal_hid_mouse_scroll(value);
}

static const ImuHidApi hid_api_usb = {
.mouse_move = furi_hal_hid_mouse_move,
.mouse_key_press = furi_hal_hid_mouse_press,
.mouse_key_release = furi_hal_hid_mouse_release,
.mouse_scroll = furi_hal_hid_mouse_scroll,
.mouse_move = usb_hid_mouse_move,
.mouse_key_press = usb_hid_mouse_key_press,
.mouse_key_release = usb_hid_mouse_key_release,
.mouse_scroll = usb_hid_mouse_scroll,
.report_rate_max = 200,
};

static bool ble_hid_mouse_move(void* inst, int8_t dx, int8_t dy) {
return ble_profile_hid_mouse_move(inst, dx, dy);
}

static bool ble_hid_mouse_key_press(void* inst, uint8_t button) {
return ble_profile_hid_mouse_press(inst, button);
}

static bool ble_hid_mouse_key_release(void* inst, uint8_t button) {
return ble_profile_hid_mouse_release(inst, button);
}

static bool ble_hid_mouse_scroll(void* inst, int8_t value) {
return ble_profile_hid_mouse_scroll(inst, value);
}

static const ImuHidApi hid_api_ble = {
.mouse_move = furi_hal_bt_hid_mouse_move,
.mouse_key_press = furi_hal_bt_hid_mouse_press,
.mouse_key_release = furi_hal_bt_hid_mouse_release,
.mouse_scroll = furi_hal_bt_hid_mouse_scroll,
.mouse_move = ble_hid_mouse_move,
.mouse_key_press = ble_hid_mouse_key_press,
.mouse_key_release = ble_hid_mouse_key_release,
.mouse_scroll = ble_hid_mouse_scroll,
.report_rate_max = 30,
};

Expand All @@ -62,14 +106,16 @@ static void ble_hid_remove_pairing(void) {
// Wait 2nd core to update nvm storage
furi_delay_ms(200);

bt_keys_storage_set_storage_path(bt, BLE_HID_KEYS_PATH);
furi_hal_bt_stop_advertising();

bt_keys_storage_set_storage_path(bt, APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME));
bt_forget_bonded_devices(bt);

// Wait 2nd core to update nvm storage
furi_delay_ms(200);
bt_keys_storage_set_default_path(bt);

furi_check(bt_set_profile(bt, BtProfileSerial));
furi_check(bt_profile_restore_default(bt));
furi_record_close(RECORD_BT);
}

Expand All @@ -80,32 +126,36 @@ static void ble_hid_connection_status_callback(BtStatus status, void* context) {
air_mouse_view_set_connected_status(app->air_mouse_view, connected);
}

static Bt* ble_hid_init(AirMouseApp* app) {
Bt* bt = furi_record_open(RECORD_BT);
bt_disconnect(bt);
static FuriHalBleProfileBase* ble_hid_init(AirMouseApp* app) {
app->bt = furi_record_open(RECORD_BT);
bt_disconnect(app->bt);

// Wait 2nd core to update nvm storage
furi_delay_ms(200);

bt_keys_storage_set_storage_path(bt, BLE_HID_KEYS_PATH);
bt_keys_storage_set_storage_path(app->bt, APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME));

furi_check(bt_set_profile(bt, BtProfileHidKeyboard));
FuriHalBleProfileBase* ble_hid_profile =
bt_profile_start(app->bt, ble_profile_hid, (void*)&ble_hid_params);
furi_check(ble_hid_profile);

furi_hal_bt_start_advertising();
bt_set_status_changed_callback(bt, ble_hid_connection_status_callback, app);
return bt;
bt_set_status_changed_callback(app->bt, ble_hid_connection_status_callback, app);

return ble_hid_profile;
}

static void ble_hid_deinit(Bt* bt) {
bt_set_status_changed_callback(bt, NULL, NULL);
bt_disconnect(bt);
static void ble_hid_deinit(AirMouseApp* app) {
bt_set_status_changed_callback(app->bt, NULL, NULL);
bt_disconnect(app->bt);

// Wait 2nd core to update nvm storage
furi_delay_ms(200);
bt_keys_storage_set_default_path(bt);
bt_keys_storage_set_default_path(app->bt);

furi_check(bt_set_profile(bt, BtProfileSerial));
furi_check(bt_profile_restore_default(app->bt));
furi_record_close(RECORD_BT);
app->bt = NULL;
}

static uint32_t air_mouse_exit(void* context) {
Expand All @@ -123,8 +173,8 @@ static void air_mouse_hid_deinit(void* context) {
AirMouseApp* app = context;

if(app->bt) {
ble_hid_deinit(app->bt);
app->bt = NULL;
ble_hid_deinit(app);
app->ble_hid_profile = NULL;
} else if(app->usb_mode_prev) {
furi_hal_usb_set_config(app->usb_mode_prev, NULL);
app->usb_mode_prev = NULL;
Expand All @@ -139,12 +189,12 @@ static void air_mouse_submenu_callback(void* context, uint32_t index) {
furi_hal_usb_unlock();
furi_hal_usb_set_config(&usb_hid, NULL);

air_mouse_view_set_hid_api(app->air_mouse_view, &hid_api_usb, false);
air_mouse_view_set_hid_api(app->air_mouse_view, &hid_api_usb, NULL, false);
view_dispatcher_switch_to_view(app->view_dispatcher, AirMouseViewMain);
} else if(index == StartSubmenuIndexBle) {
app->bt = ble_hid_init(app);
app->ble_hid_profile = ble_hid_init(app);

air_mouse_view_set_hid_api(app->air_mouse_view, &hid_api_ble, true);
air_mouse_view_set_hid_api(app->air_mouse_view, &hid_api_ble, app->ble_hid_profile, true);
view_dispatcher_switch_to_view(app->view_dispatcher, AirMouseViewMain);
} else if(index == StartSubmenuIndexBleReset) {
ble_hid_remove_pairing();
Expand Down
4 changes: 3 additions & 1 deletion non_catalog_apps/air_mouse_ofw/application.fam
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
App(
appid="vgm_air_mouse",
name="[VGM] Air Mouse",
fap_description="Video Game Module Air Mouse",
fap_version="1.1",
fap_description="Turn Flipper Zero with the Video Game Module into an air mouse",
apptype=FlipperAppType.EXTERNAL,
entry_point="air_mouse_app",
stack_size=4 * 1024,
fap_icon="airmouse_10x10.png",
fap_icon_assets="assets",
fap_category="GPIO",
fap_libs=["ble_profile"],
)
19 changes: 11 additions & 8 deletions non_catalog_apps/air_mouse_ofw/imu_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct ImuThread {
FuriThread* thread;
ICM42688P* icm42688p;
const ImuHidApi* hid;
void* hid_inst;
ImuProcessedData processed_data;
};

Expand Down Expand Up @@ -173,16 +174,16 @@ static int32_t imu_thread(void* context) {
}

if(events & ImuMouseRightPress) {
imu->hid->mouse_key_press(HID_MOUSE_BTN_RIGHT);
imu->hid->mouse_key_press(imu->hid_inst, HID_MOUSE_BTN_RIGHT);
}
if(events & ImuMouseRightRelease) {
imu->hid->mouse_key_release(HID_MOUSE_BTN_RIGHT);
imu->hid->mouse_key_release(imu->hid_inst, HID_MOUSE_BTN_RIGHT);
}
if(events & ImuMouseLeftPress) {
imu->hid->mouse_key_press(HID_MOUSE_BTN_LEFT);
imu->hid->mouse_key_press(imu->hid_inst, HID_MOUSE_BTN_LEFT);
}
if(events & ImuMouseLeftRelease) {
imu->hid->mouse_key_release(HID_MOUSE_BTN_LEFT);
imu->hid->mouse_key_release(imu->hid_inst, HID_MOUSE_BTN_LEFT);
}
if(events & ImuMouseScrollOn) {
scroll_pitch = pitch_last;
Expand Down Expand Up @@ -217,7 +218,7 @@ static int32_t imu_thread(void* context) {
SCROLL_SENSITIVITY_K;
scroll_speed = CLAMP(scroll_speed, 127.f, -127.f);

imu->hid->mouse_scroll(scroll_speed);
imu->hid->mouse_scroll(imu->hid_inst, scroll_speed);
}
} else {
diff_x +=
Expand All @@ -235,7 +236,8 @@ static int32_t imu_thread(void* context) {
float mouse_x = CLAMP(diff_x, 127.f, -127.f);
float mouse_y = CLAMP(diff_y, 127.f, -127.f);

imu->hid->mouse_move(mouse_exp_rate(mouse_x), mouse_exp_rate(mouse_y));
imu->hid->mouse_move(
imu->hid_inst, mouse_exp_rate(mouse_x), mouse_exp_rate(mouse_y));

diff_x -= (float)(int8_t)mouse_x;
diff_y -= (float)(int8_t)mouse_y;
Expand All @@ -245,7 +247,7 @@ static int32_t imu_thread(void* context) {
}
}

imu->hid->mouse_key_release(HID_MOUSE_BTN_RIGHT | HID_MOUSE_BTN_LEFT);
imu->hid->mouse_key_release(imu->hid_inst, HID_MOUSE_BTN_RIGHT | HID_MOUSE_BTN_LEFT);

icm42688_fifo_disable(imu->icm42688p);

Expand All @@ -270,10 +272,11 @@ void imu_mouse_scroll_mode(ImuThread* imu, bool enable) {
furi_thread_flags_set(furi_thread_get_id(imu->thread), flag);
}

ImuThread* imu_start(ICM42688P* icm42688p, const ImuHidApi* hid) {
ImuThread* imu_start(ICM42688P* icm42688p, const ImuHidApi* hid, void* hid_inst) {
ImuThread* imu = malloc(sizeof(ImuThread));
imu->icm42688p = icm42688p;
imu->hid = hid;
imu->hid_inst = hid_inst;
imu->thread = furi_thread_alloc_ex("ImuThread", 4096, imu_thread, imu);
furi_thread_start(imu->thread);

Expand Down
10 changes: 5 additions & 5 deletions non_catalog_apps/air_mouse_ofw/imu_mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include "sensors/ICM42688P.h"

typedef struct {
bool (*mouse_move)(int8_t dx, int8_t dy);
bool (*mouse_key_press)(uint8_t button);
bool (*mouse_key_release)(uint8_t button);
bool (*mouse_scroll)(int8_t value);
bool (*mouse_move)(void* inst, int8_t dx, int8_t dy);
bool (*mouse_key_press)(void* inst, uint8_t button);
bool (*mouse_key_release)(void* inst, uint8_t button);
bool (*mouse_scroll)(void* inst, int8_t value);
uint32_t report_rate_max;
} ImuHidApi;

Expand All @@ -17,7 +17,7 @@ typedef enum {

typedef struct ImuThread ImuThread;

ImuThread* imu_start(ICM42688P* icm42688p, const ImuHidApi* hid);
ImuThread* imu_start(ICM42688P* icm42688p, const ImuHidApi* hid, void* hid_inst);

void imu_stop(ImuThread* imu);

Expand Down
5 changes: 4 additions & 1 deletion non_catalog_apps/air_mouse_ofw/views/air_mouse_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct AirMouseView {
void* imu_device;
ImuThread* imu;
const ImuHidApi* hid_api;
void* hid_inst;
AirMouseViewExit exit_callback;
void* context;
};
Expand Down Expand Up @@ -107,7 +108,7 @@ static void air_mouse_view_enter(void* context) {
furi_assert(context);
AirMouseView* air_mouse = context;
furi_assert(air_mouse->imu == NULL);
air_mouse->imu = imu_start(air_mouse->imu_device, air_mouse->hid_api);
air_mouse->imu = imu_start(air_mouse->imu_device, air_mouse->hid_api, air_mouse->hid_inst);
}

static void air_mouse_view_exit(void* context) {
Expand Down Expand Up @@ -157,9 +158,11 @@ void air_mouse_view_set_device(AirMouseView* air_mouse, void* imu_device) {
void air_mouse_view_set_hid_api(
AirMouseView* air_mouse,
const ImuHidApi* hid,
void* hid_inst,
bool is_ble_interface) {
furi_assert(air_mouse);
air_mouse->hid_api = hid;
air_mouse->hid_inst = hid_inst;
with_view_model(
air_mouse->view,
AirMouseModel * model,
Expand Down
1 change: 1 addition & 0 deletions non_catalog_apps/air_mouse_ofw/views/air_mouse_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ View* air_mouse_view_get_view(AirMouseView* air_mouse);
void air_mouse_view_set_hid_api(
AirMouseView* air_mouse,
const ImuHidApi* hid,
void* hid_inst,
bool is_ble_interface);

void air_mouse_view_set_device(AirMouseView* air_mouse, void* imu_device);
Expand Down

0 comments on commit 0c46ae9

Please sign in to comment.