Browse Source

added hysteresis

stefan 1 year ago
parent
commit
c5bf1d7492
1 changed files with 26 additions and 20 deletions
  1. 26 20
      software/src/main.c

+ 26 - 20
software/src/main.c

@@ -1,5 +1,6 @@
 
 #include <string.h>
+#include <stdbool.h>
 #include "light_ws2812.h"
 #include <avr/io.h>
 
@@ -132,10 +133,23 @@ const int8_t currentSteps[LED_NUM_HEIGTH] = {
     120,
 };
 
+static void setLedCurrentDisplay(uint8_t ledOffset, bool reverse, uint8_t steps)
+{
+    for(uint32_t i = 0u; i < steps; i ++)
+    {
+        ledState[ledOffset - i].g = 0;
+        ledState[ledOffset - i].b = 0;
+        ledState[ledOffset - i].r = 255;
+    }
+}
+
 int main(void)
 {
     int32_t adc_result_ch1 = 0u;
     int32_t adc_result_ch2 = 0u;
+    uint8_t ch1_last_step = 0u;
+    uint8_t ch2_last_step = 0u;
+    uint8_t step;
     uint8_t led_num;
     DDRA = (uint8_t)0x00u;
     ADCSRA = (uint8_t)0b10000111u;
@@ -173,21 +187,16 @@ int main(void)
         // ch1 left
         for(uint32_t i = 0u; i < LED_NUM_HEIGTH; i ++)
         {
-            led_num = LED_NUM_HEIGTH + LED_NUM_WIDTH - 1 - i;
-
             if (adc_result_ch1 >= currentSteps[i])
             {
-                ledState[led_num].g = 0;
-                ledState[led_num].b = 0;
-                ledState[led_num].r = 255;
-            }
-            else if (i > 0 && adc_result_ch1 >= currentSteps[i - 1])
-            {
-                ledState[led_num].g = 0;
-                ledState[led_num].b = 0;
-                ledState[led_num].r = adc_result_ch1 - currentSteps[i - 1];
+                step = i;
             }
         }
+        if (step > (ch1_last_step + 1) || step < ch1_last_step)
+        {
+            ch1_last_step = step;
+        }
+        setLedCurrentDisplay(LED_NUM_HEIGTH + LED_NUM_WIDTH - 1u, true, ch1_last_step);
 
         // ch2 right
         for(uint32_t i = 0u; i < LED_NUM_HEIGTH; i ++)
@@ -196,17 +205,14 @@ int main(void)
 
             if (adc_result_ch2 >= currentSteps[i])
             {
-                ledState[led_num].g = 0;
-                ledState[led_num].b = 0;
-                ledState[led_num].r = 255;
-            }
-            else if (i > 0 && adc_result_ch2 >= currentSteps[i - 1])
-            {
-                ledState[led_num].g = 0;
-                ledState[led_num].b = 0;
-                ledState[led_num].r = adc_result_ch2 - currentSteps[i - 1];
+                step = i;
             }
         }
+        if (step > (ch2_last_step + 1) || step < ch2_last_step)
+        {
+            ch2_last_step = step;
+        }
+        setLedCurrentDisplay(LED_NUM_HEIGTH + (2 * LED_NUM_WIDTH), false, ch2_last_step);
 
         ws2812_setleds(ledState, 102);
     }