uart: fix compilation on recent avr-gcc
[aversive.git] / modules / comm / uart / test / main.c
1 /*
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
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,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: main.c,v 1.15.10.5 2008-12-27 16:29:08 zer0 Exp $
19  *
20  */
21 #include <aversive/wait.h>
22 #include <aversive/pgmspace.h>
23
24 #include <uart.h>
25
26 #include <stdio.h>
27
28 /* sending "pop" on cmdline uart resets the robot */
29 void emergency(char c)
30 {
31         static uint8_t i = 0;
32
33         printf("%c\n", c);
34
35         if ((i == 0 && c == 'p') ||
36             (i == 1 && c == 'o') ||
37             (i == 2 && c == 'p'))
38                 i++;
39         else if ( !(i == 1 && c == 'p') )
40                 i = 0;
41         if (i == 3) {
42 #ifdef HOST_VERSION
43                 hostsim_uart_exit();
44 #endif
45                 reset();
46         }
47 }
48
49 /*
50  * This code sends a counter value to uart.
51  */
52 int main(void)
53 {
54         int i;
55
56 #ifdef HOST_VERSION
57         hostsim_uart_init();
58         hostsim_ittimer_enable(100000);
59 #endif
60
61         /* initialize uart with the default parameters ( see
62          * uart_config.h ) */
63         uart_init();
64         uart_register_rx_event(0, emergency);
65
66         /* enable interrupts */
67         sei();
68
69         /* send some single characters */
70         for (i=0; i<10; i++) {
71                 uart_send(0, 'x');
72                 uart_send(0, '0' + i);
73                 wait_ms(100);
74         }
75         uart_send(0, '\n');
76
77 #ifndef HOST_VERSION
78         /* now we want to do a printf : we must register the
79          * uart0_send as stdout. Here no receive function is
80          * specified. */
81         fdevopen(uart0_dev_send, NULL);
82 #endif
83
84         /** ready to do a nice printf on the uart */
85         printf("Uart is cool !!\n");
86
87         /* one drawback of the previous printf is that the format
88          * chain is stored in RAM and this can take a huge size if
89          * there are many printf. To avoid this problem, please use
90          * printf_P together with PSTR, like in the next example. */
91         while (1) {
92                 printf_P(PSTR("This format string takes no RAM "
93                               "space. %i\n"), i++);
94                 wait_ms(1000);
95         }
96
97         return 0;
98 }