e7f2c2191ae9508826a2824502e82da6fb479896
[fw/altos] / src / vidtime / ao_vidtime.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_exti.h>
20
21 static uint8_t  sensor_value;
22 static uint8_t  vidtime_monitor;
23
24 static void
25 vidtime(void)
26 {
27         ao_exti_enable(AO_SENSOR_PORT, AO_SENSOR_PIN);
28         for (;;) {
29                 while (!vidtime_monitor)
30                         ao_sleep(&vidtime_monitor);
31                 ao_sleep(&sensor_value);
32                 printf("%d\n", sensor_value);
33                 flush();
34         }
35 }
36
37 static void
38 sensor_interrupt(void)
39 {
40         uint8_t new = ao_gpio_get(AO_SENSOR_PORT, AO_SENSOR_PIN, foo);
41
42 #if 0
43         if (new)
44                 ao_exti_set_mode(AO_SENSOR_PORT, AO_SENSOR_PIN,
45                                  AO_EXTI_MODE_FALLING);
46         else
47                 ao_exti_set_mode(AO_SENSOR_PORT, AO_SENSOR_PIN,
48                                  AO_EXTI_MODE_RISING);
49 #endif
50         if (new != sensor_value) {
51                 sensor_value = new;
52                 ao_wakeup(&sensor_value);
53         }
54 }
55
56 static struct ao_task   vidtime_task;
57
58 static void
59 ao_init_vidtime(void)
60 {
61         ao_enable_port(AO_SENSOR_PORT);
62         ao_exti_setup(AO_SENSOR_PORT, AO_SENSOR_PIN,
63                       AO_EXTI_MODE_RISING|
64                       AO_EXTI_MODE_FALLING|
65                       AO_EXTI_MODE_PULL_NONE|
66                       AO_EXTI_PRIORITY_MED,
67                       sensor_interrupt);
68         ao_add_task(&vidtime_task, vidtime, "vidtime");
69 }
70
71 static void
72 ao_set_vidtime(void)
73 {
74         ao_cmd_decimal();
75         if (ao_cmd_status == ao_cmd_success) {
76                 vidtime_monitor = ao_cmd_lex_i != 0;
77                 ao_wakeup(&vidtime_monitor);
78         }
79 }
80
81 const struct ao_cmds    ao_vidtime_cmds[] = {
82         { ao_set_vidtime, "V <0 off, 1 on>\0Enable/disable timing monitor" },
83         { 0, NULL }
84 };
85
86 void main(void)
87 {
88         ao_clock_init();
89
90         ao_task_init();
91
92         ao_timer_init();
93
94         ao_dma_init();
95
96         ao_init_vidtime();
97
98         ao_usb_init();
99
100         ao_cmd_init();
101
102         ao_cmd_register(&ao_vidtime_cmds[0]);
103
104         ao_start_scheduler();
105 }