altos: Log baro readings for MicroPeak
[fw/altos] / src / micropeak / ao_log_micro.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_log_micro.h>
21 #include <ao_async.h>
22
23 static uint16_t ao_log_offset = STARTING_LOG_OFFSET;
24
25 void
26 ao_log_micro_save(void)
27 {
28         uint16_t        n_samples = (ao_log_offset - STARTING_LOG_OFFSET) / sizeof (uint16_t);
29         ao_eeprom_write(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
30         ao_eeprom_write(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
31         ao_eeprom_write(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
32 }
33
34 void
35 ao_log_micro_restore(void)
36 {
37         ao_eeprom_read(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
38         ao_eeprom_read(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
39 }
40
41 void
42 ao_log_micro_data(void)
43 {
44         uint16_t        low_bits = pa;
45
46         if (ao_log_offset < MAX_LOG_OFFSET) {
47                 ao_eeprom_write(ao_log_offset, &low_bits, sizeof (low_bits));
48                 ao_log_offset += sizeof (low_bits);
49         }
50 }
51
52 void
53 ao_log_micro_dump(void)
54 {
55         uint16_t        n_samples;
56         uint16_t        nbytes;
57         uint8_t         byte;
58         uint16_t        b;
59
60         ao_eeprom_read(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
61         nbytes = STARTING_LOG_OFFSET + sizeof (uint16_t) * n_samples;
62         ao_async_byte('M');
63         ao_async_byte('P');
64         for (b = 0; b < nbytes; b++) {
65                 ao_eeprom_read(b, &byte, 1);
66                 ao_async_byte(byte);
67         }
68 }