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>
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_mechboard_status ans;
92 ans.hdr.cmd = I2C_ANS_MECHBOARD_STATUS;
95 ans.mode = state_get_mode();
96 ans.status = mechboard.status;
98 ans.lintel_count = mechboard.lintel_count;
99 ans.column_flags = mechboard.column_flags;
101 ans.pump_left1 = mechboard.pump_left1;
102 ans.pump_right1 = mechboard.pump_right1;
103 ans.pump_left2 = mechboard.pump_left2;
104 ans.pump_right2 = mechboard.pump_right2;
106 ans.pump_right1_current = sensor_get_adc(ADC_CSENSE3);
107 ans.pump_right2_current = sensor_get_adc(ADC_CSENSE4);
109 ans.servo_lintel_left = mechboard.servo_lintel_left;
110 ans.servo_lintel_right = mechboard.servo_lintel_right;
112 i2c_send(I2C_ADD_MASTER, (uint8_t *) &ans,
113 sizeof(ans), I2C_CTRL_GENERIC);
116 static int8_t i2c_set_mode(struct i2c_cmd_mechboard_set_mode *cmd)
122 void i2c_recvevent(uint8_t * buf, int8_t size)
124 void *void_cmd = buf;
126 static uint8_t a = 0;
138 /* Commands (no answer needed) */
139 case I2C_CMD_GENERIC_LED_CONTROL:
141 struct i2c_cmd_led_control *cmd = void_cmd;
142 if (size != sizeof (*cmd))
144 i2c_led_control(cmd->led_num, cmd->state);
148 case I2C_CMD_MECHBOARD_SET_MODE:
150 struct i2c_cmd_mechboard_set_mode *cmd = void_cmd;
151 if (size != sizeof(struct i2c_cmd_mechboard_set_mode))
157 case I2C_CMD_GENERIC_SET_COLOR:
159 struct i2c_cmd_generic_color *cmd = void_cmd;
160 if (size != sizeof (*cmd))
162 mechboard.our_color = cmd->color;
167 case I2C_CMD_EXTENSION_TEST:
169 struct i2c_cmd_extension_test *cmd = void_cmd;
170 if (size != sizeof (*cmd))
177 /* Add other commands here ...*/
180 case I2C_REQ_MECHBOARD_STATUS:
182 struct i2c_req_mechboard_status *cmd = void_cmd;
183 if (size != sizeof (struct i2c_req_mechboard_status))
186 mechboard.pump_left1_current = cmd->pump_left1_current;
187 mechboard.pump_left2_current = cmd->pump_left2_current;
197 /* log error on a led ? */
201 void i2c_recvbyteevent(__attribute__((unused)) uint8_t hwstatus,
202 __attribute__((unused)) uint8_t i,
203 __attribute__((unused)) int8_t c)
207 void i2c_sendevent(__attribute__((unused)) int8_t size)