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