2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_micropeak.h>
20 #include <ao_ms5607.h>
21 #include <ao_log_micro.h>
24 static struct ao_ms5607_sample sample;
25 static struct ao_ms5607_value value;
31 alt_t ground_alt, max_alt;
34 static uint32_t pa_sum;
39 ao_ms5607_sample(&sample);
40 ao_ms5607_convert(&sample, &value);
45 ao_compute_height(void)
47 ground_alt = ao_pa_to_altitude(pa_ground);
48 max_alt = ao_pa_to_altitude(pa_min);
49 ao_max_height = max_alt - ground_alt;
56 for (i = 0; i < 10; i++) {
57 ao_led_toggle(AO_LED_REPORT);
58 ao_delay(AO_MS_TO_TICKS(80));
60 ao_delay(AO_MS_TO_TICKS(200));
68 uint32_t pa_interval_min, pa_interval_max;
71 ao_led_init(LEDS_AVAILABLE);
74 /* Init external hardware */
79 /* Give the person a second to get their finger out of the way */
80 ao_delay(AO_MS_TO_TICKS(1000));
82 ao_log_micro_restore();
88 ao_delay(BOOST_DELAY);
89 /* Wait for motion, averaging values to get ground pressure */
92 pa_avg = pa_ground = pa << FILTER_SHIFT;
96 if (sample_count == 0)
97 ao_led_on(AO_LED_REPORT);
100 if (sample_count == 0)
101 ao_led_off(AO_LED_REPORT);
102 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
103 pa_diff = pa_ground - pa_avg;
105 /* Check for a significant pressure change */
106 if (pa_diff > (BOOST_DETECT << FILTER_SHIFT))
109 if (sample_count < GROUND_AVG * 2) {
110 if (sample_count < GROUND_AVG)
114 pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
120 pa_ground >>= FILTER_SHIFT;
122 /* Now sit around until the pressure is stable again and record the max */
126 pa_interval_min = pa_avg;
127 pa_interval_max = pa_avg;
129 time += SAMPLE_SLEEP;
130 ao_delay_until(time);
131 if ((sample_count & 3) == 0)
132 ao_led_on(AO_LED_REPORT);
134 if ((sample_count & 3) == 0)
135 ao_led_off(AO_LED_REPORT);
136 if (sample_count & 1)
138 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
142 if (sample_count == (GROUND_AVG - 1)) {
143 pa_diff = pa_interval_max - pa_interval_min;
145 /* Check to see if the pressure is now stable */
146 if (pa_diff < (LAND_DETECT << FILTER_SHIFT))
149 pa_interval_min = pa_avg;
150 pa_interval_max = pa_avg;
152 if (pa_avg < pa_interval_min)
153 pa_interval_min = pa_avg;
154 if (pa_avg > pa_interval_max)
155 pa_interval_max = pa_avg;
159 pa_min >>= FILTER_SHIFT;
162 ao_report_altitude();
165 set_sleep_mode(SLEEP_MODE_PWR_DOWN);