altos: Make micropeak 'serial' interface work
[fw/altos] / src / micropeak / ao_micropeak.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_micropeak.h>
20 #include <ao_ms5607.h>
21 #include <ao_log_micro.h>
22 #include <ao_async.h>
23
24 static struct ao_ms5607_sample  sample;
25 static struct ao_ms5607_value   value;
26
27 uint32_t        pa;
28 uint32_t        pa_avg;
29 uint32_t        pa_ground;
30 uint32_t        pa_min;
31 alt_t           ground_alt, max_alt;
32 alt_t           ao_max_height;
33
34 static uint32_t pa_sum;
35
36 static void
37 ao_pa_get(void)
38 {
39         ao_ms5607_sample(&sample);
40         ao_ms5607_convert(&sample, &value);
41         pa = value.pres;
42 }
43
44 static void
45 ao_compute_height(void)
46 {
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;
50 }
51
52 int
53 main(void)
54 {
55         int16_t         sample_count;
56         uint16_t        time;
57         uint32_t        pa_interval_min, pa_interval_max;
58         int32_t         pa_diff;
59
60         ao_led_init(LEDS_AVAILABLE);
61         ao_timer_init();
62
63         /* Init external hardware */
64         ao_spi_init();
65         ao_ms5607_init();
66         ao_ms5607_setup();
67
68         /* Give the person a second to get their finger out of the way */
69         ao_delay(AO_MS_TO_TICKS(1000));
70
71         ao_log_micro_restore();
72         ao_compute_height();
73         ao_report_altitude();
74         ao_log_micro_dump();
75         
76         ao_delay(BOOST_DELAY);
77         /* Wait for motion, averaging values to get ground pressure */
78         time = ao_time();
79         ao_pa_get();
80         pa_avg = pa_ground = pa << FILTER_SHIFT;
81         sample_count = 0;
82         for (;;) {
83                 time += SAMPLE_SLEEP;
84                 if (sample_count == 0)
85                         ao_led_on(AO_LED_REPORT);
86                 ao_delay_until(time);
87                 ao_pa_get();
88                 if (sample_count == 0)
89                         ao_led_off(AO_LED_REPORT);
90                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
91                 pa_diff = pa_ground - pa_avg;
92
93                 /* Check for a significant pressure change */
94                 if (pa_diff > (BOOST_DETECT << FILTER_SHIFT))
95                         break;
96
97                 if (sample_count < GROUND_AVG * 2) {
98                         if (sample_count < GROUND_AVG)
99                                 pa_sum += pa;
100                         ++sample_count;
101                 } else {
102                         pa_ground = pa_sum >> (GROUND_AVG_SHIFT - FILTER_SHIFT);
103                         pa_sum = 0;
104                         sample_count = 0;
105                 }
106         }
107
108         pa_ground >>= FILTER_SHIFT;
109
110         /* Now sit around until the pressure is stable again and record the max */
111
112         sample_count = 0;
113         pa_min = pa_avg;
114         pa_interval_min = pa_avg;
115         pa_interval_max = pa_avg;
116         for (;;) {
117                 time += SAMPLE_SLEEP;
118                 ao_delay_until(time);
119                 if ((sample_count & 3) == 0)
120                         ao_led_on(AO_LED_REPORT);
121                 ao_pa_get();
122                 if ((sample_count & 3) == 0)
123                         ao_led_off(AO_LED_REPORT);
124                 if (sample_count & 1)
125                         ao_log_micro_data();
126                 pa_avg = pa_avg - (pa_avg >> FILTER_SHIFT) + pa;
127                 if (pa_avg < pa_min)
128                         pa_min = pa_avg;
129
130                 if (sample_count == (GROUND_AVG - 1)) {
131                         pa_diff = pa_interval_max - pa_interval_min;
132
133                         /* Check to see if the pressure is now stable */
134                         if (pa_diff < (LAND_DETECT << FILTER_SHIFT))
135                                 break;
136                         sample_count = 0;
137                         pa_interval_min = pa_avg;
138                         pa_interval_max = pa_avg;
139                 } else {
140                         if (pa_avg < pa_interval_min)
141                                 pa_interval_min = pa_avg;
142                         if (pa_avg > pa_interval_max)
143                                 pa_interval_max = pa_avg;
144                         ++sample_count;
145                 }
146         }
147         pa_min >>= FILTER_SHIFT;
148         ao_log_micro_save();
149         ao_compute_height();
150         ao_report_altitude();
151         for (;;) {
152                 cli();
153                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
154                 sleep_mode();
155         }
156 }