beacon from 2009
[aversive.git] / projects / microb2010 / ballboard / i2c_protocol.c
1 /*
2  *  Copyright Droids Corporation (2010)
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: i2c_protocol.c,v 1.3 2009-05-27 20:04:07 zer0 Exp $
19  *
20  */
21
22 #include <string.h>
23
24 #include <aversive.h>
25 #include <aversive/list.h>
26 #include <aversive/error.h>
27
28 #include <scheduler.h>
29
30 #include <i2c.h>
31 #include <ax12.h>
32 #include <uart.h>
33 #include <pwm_ng.h>
34 #include <clock_time.h>
35 #include <spi.h>
36
37 #include <pid.h>
38 #include <quadramp.h>
39 #include <control_system_manager.h>
40 #include <blocking_detection_manager.h>
41
42 #include <rdline.h>
43 #include <parse.h>
44 #include <parse_string.h>
45 #include <parse_num.h>
46
47 #include "../common/i2c_commands.h"
48 #include "main.h"
49 #include "state.h"
50 #include "sensor.h"
51 #include "beacon.h"
52 #include "actuator.h"
53
54 void i2c_protocol_init(void)
55 {
56 }
57
58 /*** LED CONTROL ***/
59 void i2c_led_control(uint8_t l, uint8_t state)
60 {
61         switch(l) {
62         case 1:
63                 state? LED1_ON():LED1_OFF();
64                 break;
65         case 2:
66                 state? LED2_ON():LED2_OFF();
67                 break;
68         default:
69                 break;
70         }
71 }
72
73 void i2c_send_status(void)
74 {
75         struct i2c_ans_ballboard_status ans;
76         i2c_flush();
77         ans.hdr.cmd =  I2C_ANS_BALLBOARD_STATUS;
78         ans.status = state_get_status();
79         ans.ball_count = state_get_ball_count();
80         ans.lcob = cob_detect_left();
81         ans.rcob = cob_detect_right();
82         ans.opponent_x = beacon.opponent_x;
83         ans.opponent_y = beacon.opponent_y;
84         ans.opponent_a = beacon.opponent_angle;
85         ans.opponent_d = beacon.opponent_dist;
86
87         i2c_send(I2C_ADD_MASTER, (uint8_t *) &ans,
88                  sizeof(ans), I2C_CTRL_GENERIC);
89 }
90
91 static int8_t i2c_set_mode(struct i2c_cmd_ballboard_set_mode *cmd)
92 {
93         state_set_mode(cmd->mode);
94         return 0;
95 }
96
97 void i2c_recvevent(uint8_t * buf, int8_t size)
98 {
99         void *void_cmd = buf;
100         static uint8_t a=0;
101         a=!a;
102         if (a)
103                 LED2_ON();
104         else
105                 LED2_OFF();
106
107         if (size <= 0) {
108                 goto error;
109         }
110
111         switch (buf[0]) {
112
113         /* Commands (no answer needed) */
114         case I2C_CMD_GENERIC_LED_CONTROL:
115                 {
116                         struct i2c_cmd_led_control *cmd = void_cmd;
117                         if (size != sizeof (*cmd))
118                                 goto error;
119                         i2c_led_control(cmd->led_num, cmd->state);
120                         break;
121                 }
122
123         case I2C_CMD_GENERIC_SET_COLOR:
124                 {
125                         struct i2c_cmd_generic_color *cmd = void_cmd;
126                         if (size != sizeof (*cmd))
127                                 goto error;
128                         ballboard.our_color = cmd->color;
129                         break;
130                 }
131
132         case I2C_CMD_BALLBOARD_SET_BEACON:
133                 {
134                         struct i2c_cmd_ballboard_start_beacon *cmd = void_cmd;
135                         if (size != sizeof (*cmd))
136                                 goto error;
137
138                         if (cmd->enable)
139                                 beacon_start();
140                         else
141                                 beacon_stop();
142                         break;
143                 }
144
145         case I2C_CMD_BALLBOARD_SET_MODE:
146                 {
147                         struct i2c_cmd_ballboard_set_mode *cmd = void_cmd;
148                         if (size != sizeof(struct i2c_cmd_ballboard_set_mode))
149                                 goto error;
150                         i2c_set_mode(cmd);
151                         break;
152                 }
153
154
155         /* Add other commands here ...*/
156
157
158         case I2C_REQ_BALLBOARD_STATUS:
159                 {
160                         struct i2c_req_ballboard_status *cmd = void_cmd;
161                         if (size != sizeof (*cmd))
162                                 goto error;
163
164                         i2c_send_status();
165                         break;
166                 }
167
168         default:
169                 goto error;
170         }
171
172  error:
173         /* log error on a led ? */
174         return;
175 }
176
177 void i2c_recvbyteevent(uint8_t hwstatus, uint8_t i, uint8_t c)
178 {
179 }
180
181 void i2c_sendevent(int8_t size)
182 {
183 }
184
185