Explorar o código

Engine: Fix inputs in Lefty mode (#2)

* Engine: Fix inputs in Lefty mode
* IMU: Lefty mode support

---------

Co-authored-by: SG <who.just.the.doctor@gmail.com>
Silent hai 1 ano
pai
achega
1444d665d8
Modificáronse 2 ficheiros con 34 adicións e 10 borrados
  1. 29 7
      game_engine.c
  2. 5 3
      sensors/imu.c

+ 29 - 7
game_engine.c

@@ -1,5 +1,6 @@
 #include "game_engine.h"
 #include <furi.h>
+#include <furi_hal_rtc.h>
 #include <gui/gui.h>
 #include <input/input.h>
 #include <notification/notification_messages.h>
@@ -7,6 +8,11 @@
 
 typedef _Atomic uint32_t AtomicUint32;
 
+typedef struct {
+    bool lefty;
+    AtomicUint32 state;
+} InputHolder;
+
 GameEngineSettings game_engine_settings_init() {
     GameEngineSettings settings;
     settings.target_fps = 30.0f;
@@ -61,7 +67,7 @@ static void clock_timer_callback(void* context) {
     furi_thread_flags_set(engine->thread_id, GameThreadFlagUpdate);
 }
 
-static const GameKey keys[] = {
+static const GameKey keys_right_hand[] = {
     [InputKeyUp] = GameKeyUp,
     [InputKeyDown] = GameKeyDown,
     [InputKeyRight] = GameKeyRight,
@@ -70,19 +76,32 @@ static const GameKey keys[] = {
     [InputKeyBack] = GameKeyBack,
 };
 
-static const size_t keys_count = sizeof(keys) / sizeof(keys[0]);
+static const GameKey keys_left_hand[] = {
+    [InputKeyUp] = GameKeyDown,
+    [InputKeyDown] = GameKeyUp,
+    [InputKeyRight] = GameKeyLeft,
+    [InputKeyLeft] = GameKeyRight,
+    [InputKeyOk] = GameKeyOk,
+    [InputKeyBack] = GameKeyBack,
+};
+static_assert(
+    sizeof(keys_right_hand) == sizeof(keys_left_hand),
+    "keys_right_hand and keys_left_hand do not match!");
+
+static const size_t keys_count = sizeof(keys_right_hand) / sizeof(keys_right_hand[0]);
 
 static void input_events_callback(const void* value, void* context) {
-    AtomicUint32* input_state = context;
+    InputHolder* holder = context;
     const InputEvent* event = value;
+    const GameKey* keys = holder->lefty ? keys_left_hand : keys_right_hand;
 
     if(event->key < keys_count) {
         switch(event->type) {
         case InputTypePress:
-            *input_state |= (keys[event->key]);
+            holder->state |= (keys[event->key]);
             break;
         case InputTypeRelease:
-            *input_state &= ~(keys[event->key]);
+            holder->state &= ~(keys[event->key]);
             break;
         default:
             break;
@@ -92,7 +111,10 @@ static void input_events_callback(const void* value, void* context) {
 
 void game_engine_run(GameEngine* engine) {
     // input state
-    AtomicUint32 input_state = 0;
+    InputHolder input_state = {
+        .lefty = furi_hal_rtc_is_flag_set(FuriHalRtcFlagHandOrient),
+        .state = 0,
+    };
     uint32_t input_prev_state = 0;
 
     // set backlight if needed
@@ -130,7 +152,7 @@ void game_engine_run(GameEngine* engine) {
             time_start = time_end;
 
             // update input state
-            uint32_t input_current_state = input_state;
+            uint32_t input_current_state = input_state.state;
             InputState input = {
                 .held = input_current_state,
                 .pressed = input_current_state & ~input_prev_state,

+ 5 - 3
sensors/imu.c

@@ -37,6 +37,7 @@ typedef struct {
     FuriThread* thread;
     ICM42688P* icm42688p;
     ImuProcessedData processed_data;
+    bool lefty;
 } ImuThread;
 
 static void imu_madgwick_filter(
@@ -168,7 +169,7 @@ ImuThread* imu_start(ICM42688P* icm42688p) {
     ImuThread* imu = malloc(sizeof(ImuThread));
     imu->icm42688p = icm42688p;
     imu->thread = furi_thread_alloc_ex("ImuThread", 4096, imu_thread, imu);
-
+    imu->lefty = furi_hal_rtc_is_flag_set(FuriHalRtcFlagHandOrient);
     furi_thread_start(imu->thread);
 
     return imu;
@@ -312,7 +313,8 @@ bool imu_present(Imu* imu) {
 
 float imu_pitch_get(Imu* imu) {
     // we pretend that reading a float is an atomic operation
-    return imu->thread->processed_data.pitch;
+    return imu->thread->lefty ? -imu->thread->processed_data.pitch :
+                                imu->thread->processed_data.pitch;
 }
 
 float imu_roll_get(Imu* imu) {
@@ -322,5 +324,5 @@ float imu_roll_get(Imu* imu) {
 
 float imu_yaw_get(Imu* imu) {
     // we pretend that reading a float is an atomic operation
-    return imu->thread->processed_data.yaw;
+    return imu->thread->lefty ? -imu->thread->processed_data.yaw : imu->thread->processed_data.yaw;
 }