Преглед изворни кода

update leds on ui tick instead of seperate thread

Sanjay Govind пре 10 месеци
родитељ
комит
289b514619
4 измењених фајлова са 26 додато и 30 уклоњено
  1. 1 3
      portal_of_flipper.c
  2. 1 2
      test.py
  3. 23 24
      virtual_portal.c
  4. 1 1
      virtual_portal.h

+ 1 - 3
portal_of_flipper.c

@@ -17,9 +17,7 @@ static void pof_app_tick_event_callback(void* context) {
     furi_assert(context);
     PoFApp* app = context;
     scene_manager_handle_tick_event(app->scene_manager);
-    if (app->virtual_portal) {
-        virtual_portal_tick(app->virtual_portal);
-    }
+    virtual_portal_tick();
 }
 
 void pof_show_loading_popup(void* context, bool show) {

+ 1 - 2
test.py

@@ -1,6 +1,5 @@
 import usb.core
 import usb.util
-import time
 
 # Find our device
 dev = usb.core.find(idVendor=0x1430, idProduct=0x0150)
@@ -11,5 +10,5 @@ try:
 except:
     pass
 dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"C\xff\x00\x00", timeout=None)
-time.sleep(1)
+# time.sleep(1)
 dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"J\x02\x00\xff\x00\x00\x10", timeout=None)

+ 23 - 24
virtual_portal.c

@@ -32,29 +32,9 @@ uint32_t elapsed = 0;
 uint32_t duration = 0;
 bool running = false;
 bool two_phase = false;
-bool received_led_command = false;
 int current_phase = 0;
 float t_phase;
-void virtual_portal_tick(VirtualPortal* virtual_portal) {
-    if (received_led_command) {
-        received_led_command = false;
-        start_time = furi_get_tick();
-        last_r = target_r;
-        last_g = target_g;
-        last_b = target_b;
-        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;
-        }
-    }
+void virtual_portal_tick() {
     elapsed = furi_get_tick() - start_time;
     if (elapsed < duration) {
         t_phase = fminf((float)elapsed / (float)duration, 1);
@@ -94,6 +74,25 @@ void virtual_portal_tick(VirtualPortal* virtual_portal) {
     }
 }
 
+void queue_led_command(VirtualPortal* virtual_portal) {
+    start_time = furi_get_tick();
+    last_r = target_r;
+    last_g = target_g;
+    last_b = target_b;
+    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;
+    }
+}
+
 VirtualPortal* virtual_portal_alloc(NotificationApp* notifications) {
     VirtualPortal* virtual_portal = malloc(sizeof(VirtualPortal));
     virtual_portal->notifications = notifications;
@@ -315,7 +314,7 @@ int virtual_portal_l(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* r
             virtual_portal->left.g = message[3];
             virtual_portal->left.b = message[4];
             virtual_portal->left.delay = 0;
-            received_led_command = true;
+            queue_led_command(virtual_portal);
             break;
         case 1:
             brightness = message[2];
@@ -368,7 +367,7 @@ int virtual_portal_j(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* r
             virtual_portal->left.g = g;
             virtual_portal->left.b = b;
             virtual_portal->left.delay = delay;
-            received_led_command = true;
+            queue_led_command(virtual_portal);
             break;
     }
 
@@ -456,7 +455,7 @@ int virtual_portal_process_message(
             virtual_portal->left.g = message[2];
             virtual_portal->left.b = message[3];
             virtual_portal->left.delay = 0;
-            received_led_command = true;
+            queue_led_command(virtual_portal);
             return 0;
         case 'J':
             // https://github.com/flyandi/flipper_zero_rgb_led

+ 1 - 1
virtual_portal.h

@@ -48,7 +48,7 @@ VirtualPortal* virtual_portal_alloc(NotificationApp* notifications);
 void virtual_portal_free(VirtualPortal* virtual_portal);
 void virtual_portal_cleanup(VirtualPortal* virtual_portal);
 void virtual_portal_load_token(VirtualPortal* virtual_portal, PoFToken* pof_token);
-void virtual_portal_tick(VirtualPortal* virtual_portal);
+void virtual_portal_tick();
 
 int virtual_portal_process_message(
     VirtualPortal* virtual_portal,