altos: Make sure pa to altitude conversion is done with 32 bits
authorKeith Packard <keithp@keithp.com>
Mon, 10 Dec 2012 02:27:49 +0000 (18:27 -0800)
committerKeith Packard <keithp@keithp.com>
Mon, 10 Dec 2012 02:27:49 +0000 (18:27 -0800)
We need 32 bits to hold intermediate values, even if the final
altitude is reported in only 16 bits.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/core/ao_convert_pa.c
src/micropeak/ao_pins.h

index 55fe6e7..fe6e0ef 100644 (file)
@@ -43,13 +43,13 @@ ao_pa_to_altitude(int32_t pa)
 
        if (pa < 0)
                pa = 0;
-       if (pa > 120000)
-               pa = 120000;
+       if (pa > 120000L)
+               pa = 120000L;
        o = pa >> ALT_SHIFT;
        part = pa & ALT_MASK;
 
-       low = (alt_t) FETCH_ALT(o) * (ALT_SCALE - part);
-       high = (alt_t) FETCH_ALT(o+1) * part + (ALT_SCALE >> 1);
+       low = (int32_t) FETCH_ALT(o) * (ALT_SCALE - part);
+       high = (int32_t) FETCH_ALT(o+1) * part + (ALT_SCALE >> 1);
        return (low + high) >> ALT_SHIFT;
 }
 
index 187b254..63e9cb1 100644 (file)
@@ -60,6 +60,6 @@
 typedef int32_t alt_t;
 #define FETCH_ALT(o)           ((alt_t) pgm_read_dword(&altitude_table[o]))
 
-#define AO_ALT_VALUE(x)                ((x) * 10)
+#define AO_ALT_VALUE(x)                ((x) * (alt_t) 10)
 
 #endif /* _AO_PINS_H_ */