f9960eb8f17530796faa857294ebbc58a4e41545
[fw/altos] / src / product / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_micropeak.h>
21 #include <ao_ms5607.h>
22 #include <ao_log_micro.h>
23 #include <ao_async.h>
24
25 static struct ao_ms5607_sample  sample;
26 static struct ao_ms5607_value   value;
27
28 alt_t           ground_alt, max_alt;
29 alt_t           ao_max_height;
30
31 void
32 ao_pa_get(void)
33 {
34         ao_ms5607_sample(&sample);
35         ao_ms5607_convert(&sample, &value);
36         pa = value.pres;
37 }
38
39 static void
40 ao_compute_height(void)
41 {
42         ground_alt = ao_pa_to_altitude(pa_ground);
43         max_alt = ao_pa_to_altitude(pa_min);
44         ao_max_height = max_alt - ground_alt;
45 }
46
47 static void
48 ao_pips(void)
49 {
50         uint8_t i;
51         for (i = 0; i < 10; i++) {
52                 ao_led_toggle(AO_LED_REPORT);
53                 ao_delay(AO_MS_TO_TICKS(80));
54         }
55         ao_delay(AO_MS_TO_TICKS(200));
56 }
57
58 int
59 main(void)
60 {
61         ao_led_init(LEDS_AVAILABLE);
62         ao_timer_init();
63
64         /* Init external hardware */
65         ao_spi_init();
66         ao_ms5607_init();
67         ao_ms5607_setup();
68
69         /* Give the person a second to get their finger out of the way */
70         ao_delay(AO_MS_TO_TICKS(1000));
71
72         ao_log_micro_restore();
73         ao_compute_height();
74         ao_report_altitude();
75         ao_pips();
76         ao_log_micro_dump();
77 #if BOOST_DELAY 
78         ao_delay(BOOST_DELAY);
79 #endif
80
81         ao_microflight();
82
83         ao_log_micro_save();
84         ao_compute_height();
85         ao_report_altitude();
86         for (;;) {
87                 cli();
88                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
89                 sleep_mode();
90         }
91 }