2 * Copyright Droids Corporation (2009)
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.6 2009-11-08 17:25:00 zer0 Exp $
25 #include <aversive/list.h>
26 #include <aversive/error.h>
32 #include <clock_time.h>
37 #include <control_system_manager.h>
38 #include <blocking_detection_manager.h>
42 #include <parse_string.h>
43 #include <parse_num.h>
45 #include "../common/i2c_commands.h"
50 void i2c_protocol_init(void)
55 void i2c_led_control(uint8_t l, uint8_t state)
59 state? LED1_ON():LED1_OFF();
62 state? LED2_ON():LED2_OFF();
65 state? LED3_ON():LED3_OFF();
68 state? LED4_ON():LED4_OFF();
76 static void i2c_test(uint16_t val)
78 static uint16_t prev=0;
80 if ( (val-prev) != 1 ) {
81 WARNING(E_USER_I2C_PROTO, "Dupplicated msg %d", val);
88 static void i2c_send_status(void)
90 struct i2c_ans_cobboard_status ans;
92 ans.hdr.cmd = I2C_ANS_COBBOARD_STATUS;
95 ans.mode = state_get_mode();
96 ans.status = state_get_status();
98 ans.left_cobroller_speed = cobboard.left_cobroller_speed;
99 ans.right_cobroller_speed = cobboard.right_cobroller_speed;
101 ans.cob_count = state_get_cob_count();
103 i2c_send(I2C_ADD_MASTER, (uint8_t *) &ans,
104 sizeof(ans), I2C_CTRL_GENERIC);
107 void i2c_recvevent(uint8_t * buf, int8_t size)
109 void *void_cmd = buf;
111 static uint8_t a = 0;
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_COBBOARD_SET_MODE:
135 struct i2c_cmd_cobboard_set_mode *cmd = void_cmd;
136 if (size != sizeof(struct i2c_cmd_cobboard_set_mode))
138 if (!state_get_i2c_ignore())
139 state_set_mode(cmd->mode);
143 case I2C_CMD_GENERIC_SET_COLOR:
145 struct i2c_cmd_generic_color *cmd = void_cmd;
146 if (size != sizeof (*cmd))
148 cobboard.our_color = cmd->color;
153 case I2C_CMD_EXTENSION_TEST:
155 struct i2c_cmd_extension_test *cmd = void_cmd;
156 if (size != sizeof (*cmd))
163 /* Add other commands here ...*/
165 case I2C_REQ_COBBOARD_STATUS:
167 struct i2c_req_cobboard_status *cmd = void_cmd;
168 if (size != sizeof (struct i2c_req_cobboard_status))
172 if (state_get_status() != I2C_COBBOARD_STATUS_OFF &&
173 !state_get_i2c_ignore()) {
174 state_set_spickle(I2C_LEFT_SIDE, cmd->lspickle);
175 state_set_spickle(I2C_RIGHT_SIDE, cmd->rspickle);
186 /* log error on a led ? */
190 void i2c_recvbyteevent(__attribute__((unused)) uint8_t hwstatus,
191 __attribute__((unused)) uint8_t i,
192 __attribute__((unused)) int8_t c)
196 void i2c_sendevent(__attribute__((unused)) int8_t size)