]> www.fi.muni.cz Git - openparking.git/blobdiff - firmware/firmware.c
Make running averaging optional.
[openparking.git] / firmware / firmware.c
index eb064cd8b79f3b97511ab9d2030eeae6002fc715..5400714f48b10645dc196673493eb88d7a321871 100644 (file)
@@ -5,8 +5,9 @@
 #include "clock.h"
 #include "modbus.h"
 
-#define ECHO_TIMEOUT           (CLOCK_HZ/10)   // 10 ms
-#define MEASUREMENT_WAIT       (CLOCK_HZ/3)    // three triggers per second
+#define ECHO_TIMEOUT           (CLOCK_HZ/10)   // 100 ms
+#define MEASUREMENT_WAIT       (2*ECHO_TIMEOUT)
+#define MEASUREMENT_SHIFT      0               // running avg (1 << M_SHIFT)
 
 #define N_TRIGGERS 3
 #define N_SENSORS 12
@@ -23,6 +24,7 @@
 #define distances      (hold_regs+MB_N_HOLD_REGS_EEPROM+1)
 #define free_bitmap    (hold_regs[MB_N_HOLD_REGS_EEPROM+13])
 #define err_bitmap     (hold_regs[MB_N_HOLD_REGS_EEPROM+14])
+#define max_distances  (hold_regs+MB_N_HOLD_REGS_EEPROM+21)
 
 static void pull_trigger(uint8_t trig)
 {
@@ -70,10 +72,33 @@ static void do_measurement(unsigned char trig)
                                to_start &= ~mask;
                                to_measure |= mask;
                        } else if ((to_measure & mask) && !(bits & mask)) {
+#if MEASUREMENT_SHIFT > 0
+                               uint16_t old_d;
+#endif
+                               uint16_t new_d;
+                               uint8_t idx = trig*N_TRIG_SENSORS+i;
                                // echo end
                                to_measure &= ~mask;
-                               distances[trig*N_TRIG_SENSORS + i]
-                                       = now - starttimes[i];
+                               new_d = now - starttimes[i];
+                               if (new_d > max_distances[idx])
+                                       max_distances[idx] = new_d;
+
+#if MEASUREMENT_SHIFT > 0
+                               old_d = distances[idx];
+
+                               if (old_d == 0
+                                       || old_d == -1) {
+                                       distances[idx] = new_d;
+                               } else {
+                                       distances[idx] = (
+                                               (old_d << MEASUREMENT_SHIFT)
+                                               + new_d
+                                               - old_d
+                                               ) >> MEASUREMENT_SHIFT;
+                               }
+#else
+                               distances[idx] = new_d;
+#endif
                        }
                }
        }
@@ -88,24 +113,63 @@ static void do_measurement(unsigned char trig)
 static void led_set(uint8_t led, uint8_t state)
 {
        if (led == 0) {
-               if (state) {
-                       PORTC |= _BV(PC5);
-                       led_bitmap |= 1;
-               } else {
-                       PORTC &= ~_BV(PC5);
+               switch (state) {
+               case 0:
                        led_bitmap &= ~1;
+                       led_bitmap &= ~2;
+                       break;
+               case 1:
+                       led_bitmap |= 1;
+                       led_bitmap &= ~2;
+                       break;
+               default: // error
+                       led_bitmap |= 2;
+                       break;
                }
        } else {
-               if (state) {
-                       PORTB |= _BV(PB5);
-                       led_bitmap |= 2;
-               } else {
-                       PORTB &= ~_BV(PB5);
-                       led_bitmap &= ~2;
+               switch (state) {
+               case 0:
+                       led_bitmap &= ~4;
+                       led_bitmap &= ~8;
+                       break;
+               case 1:
+                       led_bitmap |= 4;
+                       led_bitmap &= ~8;
+                       break;
+               default:
+                       led_bitmap |= 8;
+                       break;
                }
        }
 }
 
+static void leds_update()
+{
+       if (led_bitmap & 1) {
+               PORTC |= _BV(PC5);
+       } else {
+               PORTC &= ~_BV(PC5);
+       }
+
+       if (led_bitmap & 2) {
+               DDRC &= ~_BV(PC5);
+       } else {
+               DDRC |= _BV(PC5);
+       }
+
+       if (led_bitmap & 4) {
+               PORTB |= _BV(PB5);
+       } else {
+               PORTB &= ~_BV(PB5);
+       }
+
+       if (led_bitmap & 8) {
+               DDRB |= _BV(PB5);
+       } else {
+               DDRB &= ~_BV(PB5);
+       }
+}
+
 static void eval_bitmaps()
 {
        uint16_t free_b = 0, err_b = 0, mask;
@@ -127,7 +191,9 @@ static void eval_bitmaps()
        err_bitmap  = err_b;
 
        if (led1_sensors) {
-               if (led1_sensors & free_bitmap) {
+               if (led1_sensors & err_bitmap) {
+                       led_set(0, 2);
+               } else if (led1_sensors & free_bitmap) {
                        led_set(0, 1);
                } else {
                        led_set(0, 0);
@@ -135,7 +201,9 @@ static void eval_bitmaps()
        }
 
        if (led2_sensors) {
-               if (led2_sensors & free_bitmap) {
+               if (led2_sensors & err_bitmap) {
+                       led_set(1, 2);
+               } else if (led2_sensors & free_bitmap) {
                        led_set(1, 1);
                } else {
                        led_set(1, 0);
@@ -178,6 +246,7 @@ int main()
                }
 
                eval_bitmaps();
+               leds_update(); // might be written from modbus
 //             led_set(0,
 //                     distances[4] > 100 || distances[11] > 100);
        }