From 0164078583b37a70b4aa093e2b17900a7e9ef231 Mon Sep 17 00:00:00 2001
From: "jamesimmanuel.magsino@stud.h-da.de" <James Immanuel Magsino>
Date: Tue, 3 Dec 2024 15:12:23 +0100
Subject: [PATCH] Glorious functioning code

---
 src/main.c | 54 ++++++++++++++++++++++++------------------------------
 1 file changed, 24 insertions(+), 30 deletions(-)

diff --git a/src/main.c b/src/main.c
index 658461d..d565f3c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,7 +4,7 @@
 #include <unistd.h>
 #include <hardware/pwm.h>
 
-const uint8_t init[4] = {0x21, 0xa0, 0xe1, 0x81};
+const uint8_t init[4] = {0x21, 0xa0, 0xef, 0x81};
 const uint8_t zeile[8] = {0x00, 0x02, 0x04, 0x06, 0x08, 0x10, 0x12, 0x14};
 const uint8_t fill[17] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
 
@@ -125,9 +125,14 @@ void callback(uint gpio, uint32_t events)
 
     if (events & GPIO_IRQ_EDGE_FALL)
     {
-        volatile uint16_t duration = pwm_get_counter(slice_num) * 42;
-        volatile uint16_t test = pwm_get_counter(slice_num);
-        volatile float distance = (test * 314) / (20000);
+
+        volatile uint16_t duration = pwm_get_counter(slice_num);
+
+        volatile float distance = (duration * 340) / (20000);
+        if (distance <= 2)
+        {
+            distance = 0;
+        }
         if (distance > 20)
         {
             distance = 20;
@@ -144,34 +149,27 @@ void callback(uint gpio, uint32_t events)
     {
         pwm_set_counter(slice_num, 0);
     }
-
-    //    volatile uint16_t test = pwm_get_counter(another_slice_num);
-    //    volatile float distance = (test * 314) / (20000);
-    //    if (distance > 20)
-    //    {
-    //       distance = 20;
-    //   }
-
-    // distance = 20 - distance;
-
-    //    volatile int percentage = (distance / 10) * 100;
-
-    //  set_progress(percentage);
-    // pwm_set_counter(another_slice_num, 0);
 }
 
 void init_pwm()
 {
+    gpio_init(28);
+    gpio_set_dir(28, GPIO_OUT);
+
     gpio_set_function(28, GPIO_FUNC_PWM);
 
     volatile uint slice_num = pwm_gpio_to_slice_num(28);
     pwm_set_clkdiv(slice_num, 125);
+    //
     pwm_set_wrap(slice_num, 0xFFFF);
     pwm_set_chan_level(slice_num, PWM_CHAN_A, 10);
     pwm_set_enabled(slice_num, true);
 }
 void pwm_echo_init()
 {
+    gpio_init(27);
+    gpio_set_dir(27, GPIO_IN);
+
     volatile uint slice_num = pwm_gpio_to_slice_num(27);
     /*
     // Count once for every 100 cycles the PWM B input is high
@@ -183,17 +181,17 @@ void pwm_echo_init()
     */
     // count once for every 42 cycles the PWM B input is high
     // one cycle approximately equal to 20 ms
-    pwm_set_clkdiv(slice_num, 42);
+    pwm_set_clkdiv(slice_num, 125);
     pwm_set_wrap(slice_num, 0xFFFF);
-    pwm_set_clkdiv_mode(slice_num, PWM_DIV_B_HIGH);
+    pwm_set_clkdiv_mode(slice_num, PWM_DIV_FREE_RUNNING);
     pwm_set_enabled(slice_num, true);
 }
 int a = 0;
 
 int main()
 {
-    // clock enable: 100000 baud (bit/s)
-    i2c_init(i2c_default, 100000);
+    // clock enable: 100.000 baud (bit/s)
+    i2c_init(i2c_default, 1000000);
     // Pin 0 and 1 are given I2C functionality (they're the ones connected to the led matrix)
     gpio_set_function(0, GPIO_FUNC_I2C);
     gpio_set_function(1, GPIO_FUNC_I2C);
@@ -212,18 +210,11 @@ int main()
     i2c_write_blocking(i2c_default, 0x70, &init[2], 1, false);
     i2c_write_blocking(i2c_default, 0x70, &init[3], 1, false);
 
-    i2c_write_blocking(i2c_default, 0x70, zeile, 4, false);
-
-    i2c_write_blocking(i2c_default, 0x70, empty, 17, false);
-
     init_pwm();
     pwm_echo_init();
 
-    gpio_set_irq_enabled_with_callback(27, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &callback);
+    gpio_set_irq_enabled_with_callback(27, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, callback);
 
-    while (1)
-    {
-    }
     /*
     timer_hw->dbgpause = 0;
     gpio_init(27);
@@ -257,6 +248,9 @@ int main()
         asm("BKPT #0");
     }
     */
+    while (1)
+    {
+    }
     a = 3;
     return 0;
 }
\ No newline at end of file
-- 
GitLab