add ballboard commands on mainboard
[aversive.git] / projects / microb2009 / tests / spi_test / main.c
1 /*  
2  *  Copyright Droids Corporation
3  *  Olivier Matz <zer0@droids-corp.org>
4  * 
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Revision : $Id: main.c,v 1.1 2009-01-30 20:42:17 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive.h>
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29
30 #include <uart.h>
31 #include <parse.h>
32 #include <rdline.h>
33 #include <timer.h>
34 #include <scheduler.h>
35 #include <spi.h>
36
37 #include "main.h"
38
39 /* for cmdline interface */
40 struct rdline rdl;
41 char prompt[RDLINE_PROMPT_SIZE];
42 extern parse_pgm_ctx_t main_ctx[];
43
44 /******** For cmdline. See in commands.c for the list of commands. */
45 static void write_char(char c) 
46 {
47         uart_send(0, c);
48 }
49
50 static void 
51 valid_buffer(const char * buf, uint8_t size) 
52 {
53         int8_t ret;
54         ret = parse(main_ctx, buf);
55         if (ret == PARSE_AMBIGUOUS)
56                 printf_P(PSTR("Ambiguous command\r\n"));
57         else if (ret == PARSE_NOMATCH)
58                 printf_P(PSTR("Command not found\r\n"));
59         else if (ret == PARSE_BAD_ARGS)
60                 printf_P(PSTR("Bad arguments\r\n"));
61 }
62
63 static int8_t 
64 complete_buffer(const char * buf, char * dstbuf, uint8_t dstsize,
65                 int16_t * state)
66 {
67         return complete(main_ctx, buf, state, dstbuf, dstsize);
68 }
69
70 /***********************/
71
72 void do_led_blink(void * dummy)
73 {
74 #if 1 /* simple blink */
75         static uint8_t a=0;
76
77         if(a)
78                 LED1_ON();
79         else
80                 LED1_OFF();
81         
82         a = !a;
83 #endif
84 }
85
86 static void main_timer_interrupt(void)
87 {
88         static uint8_t cpt = 0;
89
90         cpt++;
91         sei();
92
93         if ((cpt & 0x3) == 0)
94                 scheduler_interrupt();
95 }
96
97 /* sending "pop" on uart0 resets the robot */
98 static void emergency(char c) {
99         static uint8_t i = 0;
100         
101         if( (i == 0 && c == 'p') ||
102             (i == 1 && c == 'o') ||
103             (i == 2 && c == 'p') )
104                 i++;
105         else if ( !(i == 1 && c == 'p') )
106                 i = 0;
107         if(i == 3)
108                 reset();
109 }
110
111 int main(void)
112 {
113         int c;
114         const char * history;
115         int8_t ret;
116
117         /* SPI */
118         spi_init(SPI_MODE_MASTER, SPI_FORMAT_2, SPI_CLK_RATE_16);
119         spi_set_data_order(SPI_MSB_FIRST);
120         spi_register_ss_line(&SS_PORT, SS_BIT);
121
122         /* UART */
123         uart_init();
124         fdevopen(uart0_dev_send, uart0_dev_recv);
125         uart_register_rx_event(0, emergency);
126
127         /* TIMER */
128         timer_init();
129         timer0_register_OV_intr(main_timer_interrupt);
130
131         /* SCHEDULER */
132         scheduler_init();
133         scheduler_add_periodical_event_priority(do_led_blink, NULL, 
134                                                 100000L / SCHEDULER_UNIT, 
135                                                 LED_PRIO);
136         sei();
137
138         printf_P(PSTR("Coucou\r\n"));
139         
140         rdline_init(&rdl, write_char, valid_buffer, complete_buffer);
141         snprintf(prompt, sizeof(prompt), "ax12 > ");    
142         rdline_newline(&rdl, prompt);
143
144         while (1) {
145                 c = uart_recv_nowait(0);
146                 if (c == -1) 
147                         continue;
148                 ret = rdline_char_in(&rdl, c);
149                 if (ret != 2 && ret != 0) {
150                         history = rdline_get_buffer(&rdl);
151                         if (strlen(history) > 1)
152                                 rdline_add_history(&rdl, history);
153                         rdline_newline(&rdl, prompt);
154                 }
155         }
156
157         return 0;
158 }