Browse Source

more lerping fixes

Sanjay Govind 10 months ago
parent
commit
f32ac61081
3 changed files with 40 additions and 28 deletions
  1. 9 3
      test.py
  2. 28 25
      virtual_portal.c
  3. 3 0
      virtual_portal.h

+ 9 - 3
test.py

@@ -12,7 +12,13 @@ except:
     pass
 dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"C\xff\x00\x00", timeout=None)
 time.sleep(0.001)
-dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"J\x00\x00\xff\x00\x00\x30", timeout=None)
 
-time.sleep(5)
-dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"J\x00\x00\x00\xff\x00\x10", timeout=None)
+while True:
+    dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"J\x00\xff\x00\x00\xD0\x07", timeout=None)
+    time.sleep(3)
+    dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"J\x00\x00\xff\x00\xD0\x07", timeout=None)
+
+    time.sleep(3)
+    dev.ctrl_transfer(0x21, 9, wValue=0x0200, wIndex=0, data_or_wLength=b"J\x00\x00\x00\xff\xD0\x07", timeout=None)
+
+    time.sleep(3)

+ 28 - 25
virtual_portal.c

@@ -37,34 +37,36 @@ void virtual_portal_tick(void* ctx) {
     if (elapsed < led->delay) {
         float t_phase = fminf((float)elapsed / (float)led->delay, 1);
         if (led->current_phase == 0) {
-            if (led->last_r < led->r) {
-                furi_hal_light_set(LightRed, lerp(led->last_r, led->r, t_phase));
+            if (led->last_r < led->target_r) {
+                led->r = lerp(led->last_r, led->target_r, t_phase);
             }
-            if (led->last_g < led->g) {
-                furi_hal_light_set(LightGreen, lerp(led->last_g, led->g, t_phase));
+            if (led->last_g < led->target_g) {
+                led->g = lerp(led->last_g, led->target_g, t_phase);
             }
-            if (led->last_b < led->b) {
-                furi_hal_light_set(LightBlue, lerp(led->last_b, led->b, t_phase));
+            if (led->last_b < led->target_b) {
+                led->b = lerp(led->last_b, led->target_b, t_phase);
             }
         } else {
-            if (led->last_r > led->r) {
-                furi_hal_light_set(LightRed, lerp(led->last_r, led->r, t_phase));
+            if (led->last_r > led->target_r) {
+                led->r = lerp(led->last_r, led->target_r, t_phase);
             }
-            if (led->last_g > led->g) {
-                furi_hal_light_set(LightGreen, lerp(led->last_g, led->g, t_phase));
+            if (led->last_g > led->target_g) {
+                led->g = lerp(led->last_g, led->target_g, t_phase);
             }
-            if (led->last_b > led->b) {
-                furi_hal_light_set(LightBlue, lerp(led->last_b, led->b, t_phase));
+            if (led->last_b > led->target_b) {
+                led->b = lerp(led->last_b, led->target_b, t_phase);
             }
         }
-
+        furi_hal_light_set(LightRed, led->r);
+        furi_hal_light_set(LightGreen, led->g);
+        furi_hal_light_set(LightBlue, led->b);
     } else if (led->two_phase && led->current_phase == 0) {
         led->start_time = furi_get_tick();
         led->current_phase++;
     } else {
-        led->last_r = led->r;
-        led->last_g = led->g;
-        led->last_b = led->b;
+        led->r = led->target_r;
+        led->g = led->target_g;
+        led->b = led->target_b;
         furi_hal_light_set(LightRed, led->r);
         furi_hal_light_set(LightGreen, led->g);
         furi_hal_light_set(LightBlue, led->b);
@@ -85,28 +87,29 @@ void queue_led_command(VirtualPortal* virtual_portal, int side, uint8_t r, uint8
             led = &virtual_portal->left;
             break;
     }
+    led->running = false;
     led->last_r = led->r;
     led->last_g = led->g;
     led->last_b = led->b;
-    led->r = r;
-    led->g = g;
-    led->b = b;
+    led->target_r = r;
+    led->target_g = g;
+    led->target_b = b;
     if (duration) {
         led->start_time = furi_get_tick();
         led->delay = duration;
-        led->running = true;
-        bool increasing = r > led->last_r || g > led->last_g || b > led->last_b;
-        bool decreasing = r < led->last_r || g < led->last_g || b < led->last_b;
+        bool increasing = r > led->r || g > led->g || b > led->b;
+        bool decreasing = r < led->r || g < led->g || b < led->b;
         led->two_phase = increasing && decreasing;
         led->current_phase = increasing ? 0 : 1;
         if (increasing && decreasing) {
             duration /= 2;
         }
+        led->running = true;
     } else {
         if (side == PORTAL_SIDE_RIGHT) {
-            furi_hal_light_set(LightRed, led->r);
-            furi_hal_light_set(LightGreen, led->g);
-            furi_hal_light_set(LightBlue, led->b);
+            furi_hal_light_set(LightRed, r);
+            furi_hal_light_set(LightGreen, g);
+            furi_hal_light_set(LightBlue, b);
         }
         led->running = false;
     }

+ 3 - 0
virtual_portal.h

@@ -29,6 +29,9 @@ typedef struct {
     uint8_t r;
     uint8_t g;
     uint8_t b;
+    uint8_t target_r;
+    uint8_t target_g;
+    uint8_t target_b;
     uint8_t last_r;
     uint8_t last_g;
     uint8_t last_b;