2 * Copyright © 2011 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.
20 static struct ao_task ao_log_task;
22 uint8_t ao_log_running;
24 uint32_t ao_log_start_pos;
25 uint32_t ao_log_end_pos;
26 uint32_t ao_log_current_pos;
28 #define AO_LOG_TELESCIENCE_START ((uint8_t) 's')
29 #define AO_LOG_TELESCIENCE_DATA ((uint8_t) 'd')
31 struct ao_log_telescience {
37 uint16_t adc[NUM_ADC];
41 static struct ao_log_telescience log;
42 static uint8_t ao_log_adc_pos;
45 ao_log_csum(__xdata uint8_t *b) __reentrant
50 for (i = 0; i < sizeof (struct ao_log_telescience); i++)
56 ao_log_telescience_data(struct ao_log_telescience *log)
61 log->csum = ao_log_csum((__xdata uint8_t *) log);
62 ao_mutex_get(&ao_log_mutex); {
63 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
67 ao_storage_write(ao_log_current_pos,
69 sizeof (struct ao_log_telescience));
70 ao_log_current_pos += sizeof (struct ao_log_telescience);
72 } ao_mutex_put(&ao_log_mutex);
77 ao_log_valid(struct ao_log_telescience *log)
82 for (i = 0; i < sizeof (struct ao_log_telescience); i++)
91 printf("Log goes from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
93 ao_wakeup(&ao_log_running);
99 printf ("Log stopped at %ld\n", ao_log_current_pos);
101 ao_wakeup((void *) &ao_adc_head);
105 ao_log_check_pin(void)
107 if (PINB & (1 << PINB0))
114 ao_log_telescience(void)
120 /* Find end of data */
121 while (ao_log_start_pos < ao_log_end_pos) {
122 if (!(ao_storage_read(ao_log_start_pos, &log, sizeof (struct ao_log_telescience))))
124 if (!ao_log_valid(&log))
127 ao_log_current_pos = ao_log_start_pos;
128 ao_log_end_pos = ao_storage_config;
130 while (!ao_log_running)
131 ao_sleep(&ao_log_running);
133 memset(&log, '\0', sizeof (struct ao_log_telescience));
134 log.type = AO_LOG_TELESCIENCE_START;
135 log.tick = ao_time();
136 ao_log_telescience_data(&log);
137 /* Write the whole contents of the ring to the log
140 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
141 log.type = AO_LOG_TELESCIENCE_DATA;
142 while (ao_log_running) {
143 /* Write samples to EEPROM */
144 while (ao_log_adc_pos != ao_adc_head) {
145 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
146 memcpy(&log.u.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
147 NUM_ADC * sizeof (uint16_t));
148 ao_log_telescience_data(&log);
149 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
151 /* Wait for more ADC data to arrive */
152 ao_sleep((void *) &ao_adc_head);
161 if (ao_cmd_status == ao_cmd_success) {
176 for (pos = 0; pos < ao_storage_config; pos += sizeof (struct ao_log_telescience)) {
177 if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
179 if (!ao_log_valid(&log) || log.type == AO_LOG_TELESCIENCE_START) {
181 printf("flight %d start %x end %x\n",
183 (uint16_t) (start >> 8),
184 (uint16_t) ((pos + 0xff) >> 8));
186 if (!ao_log_valid(&log))
201 if (ao_cmd_status != ao_cmd_success)
203 if (ao_cmd_lex_i != 1) {
204 ao_cmd_status = ao_cmd_syntax_error;
205 printf("No such flight: %d\n", ao_cmd_lex_i);
208 for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
209 if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
211 if (!ao_log_valid(&log))
213 ao_storage_erase(pos);
216 printf("No such flight: %d\n", ao_cmd_lex_i);
221 const struct ao_cmds ao_log_cmds[] = {
222 { ao_log_set, "L <0 off, 1 on>\0Set logging mode" },
223 { ao_log_list, "l\0List stored flight logs" },
224 { ao_log_delete, "d 1\0Delete all stored flights" },
238 PCMSK0 |= (1 << PCINT0); /* Enable PCINT0 pin change */
240 PCICR |= (1 << PCIE0); /* Enable pin change interrupt */
242 PORTB &= ~(1 << PORTB0); /* Pull input down; always log if NC */
244 ao_cmd_register(&ao_log_cmds[0]);
246 ao_add_task(&ao_log_task, ao_log_telescience, "log");