2 * Copyright Droids Corporation (2007)
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.
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.
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
18 * Revision : $Id: i2c_protocol.c,v 1.3 2009-05-27 20:04:07 zer0 Exp $
25 #include <aversive/list.h>
26 #include <aversive/error.h>
28 #include <scheduler.h>
39 #include <control_system_manager.h>
40 #include <blocking_detection_manager.h>
44 #include <parse_string.h>
45 #include <parse_num.h>
47 #include "../common/i2c_commands.h"
53 void i2c_protocol_init(void)
58 void i2c_led_control(uint8_t l, uint8_t state)
62 state? LED1_ON():LED1_OFF();
65 state? LED2_ON():LED2_OFF();
72 void i2c_send_status(void)
74 struct i2c_ans_sensorboard_status ans;
76 ans.hdr.cmd = I2C_ANS_SENSORBOARD_STATUS;
77 ans.status = 0x55; /* XXX */
78 ans.opponent_x = beacon.opponent_x;
79 ans.opponent_y = beacon.opponent_y;
80 ans.opponent_a = beacon.opponent_angle;
81 ans.opponent_d = beacon.opponent_dist;
84 ans.scan_status |= scan_params.working ? 0 : I2C_SCAN_DONE;
85 ans.scan_status |= scan_params.max_column_detected ? I2C_SCAN_MAX_COLUMN : 0;
88 ans.dropzone_x = scan_params.dropzone_x;
89 ans.dropzone_y = scan_params.dropzone_y;
90 ans.dropzone_h = scan_params.dropzone_h;
92 i2c_send(I2C_ADD_MASTER, (uint8_t *) &ans,
93 sizeof(ans), I2C_CTRL_GENERIC);
97 void i2c_scanner_calibre_laser(void* dummy)
99 scanner_calibre_laser();
102 void i2c_scanner_end_process(void* dummy)
104 scanner_end_process();
107 void i2c_recvevent(uint8_t * buf, int8_t size)
109 void *void_cmd = buf;
123 /* Commands (no answer needed) */
124 case I2C_CMD_GENERIC_LED_CONTROL:
126 struct i2c_cmd_led_control *cmd = void_cmd;
127 if (size != sizeof (*cmd))
129 i2c_led_control(cmd->led_num, cmd->state);
133 case I2C_CMD_GENERIC_SET_COLOR:
135 struct i2c_cmd_generic_color *cmd = void_cmd;
136 if (size != sizeof (*cmd))
138 sensorboard.our_color = cmd->color;
142 case I2C_CMD_SENSORBOARD_SET_BEACON:
144 struct i2c_cmd_sensorboard_start_beacon *cmd = void_cmd;
145 if (size != sizeof (*cmd))
156 case I2C_CMD_SENSORBOARD_SET_SCANNER:
158 struct i2c_cmd_sensorboard_scanner *cmd = void_cmd;
159 if (size != sizeof (*cmd))
162 scanner_set_mode(cmd->mode);
168 case I2C_CMD_SENSORBOARD_SCANNER_ALGO:
170 struct i2c_cmd_sensorboard_scanner_algo *cmd = void_cmd;
171 if (size != sizeof (*cmd))
174 scan_params.algo = cmd->algo;
176 if (cmd->algo == I2C_SCANNER_ALGO_COLUMN_DROPZONE){
177 scan_params.drop_zone.working_zone = cmd->drop_zone.working_zone;
178 scan_params.drop_zone.center_x = cmd->drop_zone.center_x;
179 scan_params.drop_zone.center_y = cmd->drop_zone.center_y;
181 else if (cmd->algo == I2C_SCANNER_ALGO_CHECK_TEMPLE) {
182 scan_params.check_temple.level = cmd->check_temple.level;
183 scan_params.check_temple.temple_x = cmd->check_temple.temple_x;
184 scan_params.check_temple.temple_y = cmd->check_temple.temple_y;
186 else if (cmd->algo == I2C_SCANNER_ALGO_TEMPLE_DROPZONE){
187 scan_params.drop_zone.working_zone = cmd->drop_zone.working_zone;
188 scan_params.drop_zone.center_x = cmd->drop_zone.center_x;
189 scan_params.drop_zone.center_y = cmd->drop_zone.center_y;
195 scan_params.working = 1;
196 scheduler_add_single_event_priority(i2c_scanner_end_process, NULL,
203 case I2C_CMD_SENSORBOARD_CALIB_SCANNER:
205 struct i2c_cmd_sensorboard_calib_scanner *cmd = void_cmd;
206 if (size != sizeof (*cmd))
209 scheduler_add_single_event_priority(i2c_scanner_calibre_laser, NULL,
217 /* Add other commands here ...*/
220 case I2C_REQ_SENSORBOARD_STATUS:
222 struct i2c_req_sensorboard_status *cmd = void_cmd;
223 if (size != sizeof (*cmd))
226 beacon.robot_x = cmd->x;
227 beacon.robot_y = cmd->y;
228 beacon.robot_angle = cmd->a;
230 if (cmd->enable_pickup_wheels)
244 /* log error on a led ? */
248 void i2c_recvbyteevent(uint8_t hwstatus, uint8_t i, uint8_t c)
252 void i2c_sendevent(int8_t size)