Sanjay Govind 10 месяцев назад
Родитель
Сommit
9698d64311
2 измененных файлов с 45 добавлено и 14 удалено
  1. 2 2
      test.py
  2. 43 12
      virtual_portal.c

+ 2 - 2
test.py

@@ -9,5 +9,5 @@ try:
     dev.detach_kernel_driver(0)
 except:
     pass
-# dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"C\xff\xff\xff", timeout=None)
-dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"J\x00\x20\x20\x20\x00\x10", timeout=None)
+dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"C\xff\x00\x00", timeout=None)
+dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"J\x00\x00\xff\x00\x00\x10", timeout=None)

+ 43 - 12
virtual_portal.c

@@ -33,7 +33,8 @@ static int32_t pof_thread_worker(void* context) {
     uint32_t elapsed = 0;
     uint32_t duration = 0;
     bool running = false;
-    // bool two_phase = false;
+    bool two_phase = false;
+    int current_phase = 0;
     float t_phase;
     uint32_t flags;
     while (true) {
@@ -45,12 +46,18 @@ static int32_t pof_thread_worker(void* context) {
         if (flags) {
             if (flags & EventLed) {
                 start_time = furi_get_tick();
-                elapsed = 0;
                 duration = virtual_portal->left.delay;
                 target_r = virtual_portal->left.r;
                 target_g = virtual_portal->left.g;
                 target_b = virtual_portal->left.b;
                 running = true;
+                bool increasing = target_r > last_r || target_g > last_g || target_b > last_b;
+                bool decreasing = target_r < last_r || target_g < last_g || target_b < last_b;
+                two_phase = increasing && decreasing;
+                current_phase = increasing ? 0 : 1;
+                if (increasing && decreasing) {
+                    duration /= 2;
+                }
             }
             if (flags & EventExit) {
                 FURI_LOG_I(TAG, "exit");
@@ -60,17 +67,41 @@ static int32_t pof_thread_worker(void* context) {
         elapsed = furi_get_tick() - start_time;
         if (elapsed < duration) {
             t_phase = fminf((float)elapsed / (float)duration, 1);
-            furi_hal_light_set(LightRed, lerp(last_r, target_r, t_phase));
-            furi_hal_light_set(LightBlue, lerp(last_g, target_g, t_phase));
-            furi_hal_light_set(LightGreen, lerp(last_b, target_b, t_phase));
+            if (current_phase == 0) {
+                if (last_r > target_r) {
+                    furi_hal_light_set(LightRed, lerp(last_r, target_r, t_phase));
+                }
+                if (last_g > target_g) {
+                    furi_hal_light_set(LightBlue, lerp(last_g, target_g, t_phase));
+                }
+                if (last_b > target_b) {
+                    furi_hal_light_set(LightGreen, lerp(last_b, target_b, t_phase));
+                }
+            } else {
+                if (last_r < target_r) {
+                    furi_hal_light_set(LightRed, lerp(last_r, target_r, t_phase));
+                }
+                if (last_g < target_g) {
+                    furi_hal_light_set(LightBlue, lerp(last_g, target_g, t_phase));
+                }
+                if (last_b < target_b) {
+                    furi_hal_light_set(LightGreen, lerp(last_b, target_b, t_phase));
+                }
+            }
+            
         } else {
-            last_r = target_r;
-            last_g = target_g;
-            last_b = target_b;
-            furi_hal_light_set(LightRed, target_r);
-            furi_hal_light_set(LightBlue, target_g);
-            furi_hal_light_set(LightGreen, target_b);
-            running = false;
+            if (two_phase && current_phase == 0) {
+                start_time = furi_get_tick();
+                current_phase++;
+            } else {
+                last_r = target_r;
+                last_g = target_g;
+                last_b = target_b;
+                furi_hal_light_set(LightRed, target_r);
+                furi_hal_light_set(LightBlue, target_g);
+                furi_hal_light_set(LightGreen, target_b);
+                running = false;
+            }
         }
     }