python display hostsim
[aversive.git] / projects / microb2010 / mechboard / i2c_protocol.c
1 /*
2  *  Copyright Droids Corporation (2009)
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.6 2009-11-08 17:25:00 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 <i2c.h>
29 #include <ax12.h>
30 #include <uart.h>
31 #include <pwm_ng.h>
32 #include <time.h>
33 #include <spi.h>
34
35 #include <pid.h>
36 #include <quadramp.h>
37 #include <control_system_manager.h>
38 #include <blocking_detection_manager.h>
39
40 #include <rdline.h>
41 #include <parse.h>
42 #include <parse_string.h>
43 #include <parse_num.h>
44
45 #include "../common/i2c_commands.h"
46 #include "main.h"
47 #include "sensor.h"
48 #include "state.h"
49
50 void i2c_protocol_init(void)
51 {
52 }
53
54 /*** LED CONTROL ***/
55 void i2c_led_control(uint8_t l, uint8_t state)
56 {
57         switch(l) {
58         case 1:
59                 state? LED1_ON():LED1_OFF();
60                 break;
61         case 2:
62                 state? LED2_ON():LED2_OFF();
63                 break;
64         case 3:
65                 state? LED3_ON():LED3_OFF();
66                 break;
67         case 4:
68                 state? LED4_ON():LED4_OFF();
69                 break;
70         default:
71                 break;
72         }
73 }
74
75 #ifdef notyet
76 static void i2c_test(uint16_t val)
77 {
78         static uint16_t prev=0;
79
80         if ( (val-prev) != 1 ) {
81                 WARNING(E_USER_I2C_PROTO, "Dupplicated msg %d", val);
82         }
83         prev = val;
84         ext.nb_test_cmd ++;
85 }
86 #endif
87
88 static void i2c_send_status(void)
89 {
90         struct i2c_ans_mechboard_status ans;
91         i2c_flush();
92         ans.hdr.cmd =  I2C_ANS_MECHBOARD_STATUS;
93
94         /* status */
95         ans.mode = state_get_mode();
96         ans.status = mechboard.status;
97
98         ans.lintel_count = mechboard.lintel_count;
99         ans.column_flags = mechboard.column_flags;
100         /* pumps pwm */
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;
105         /* pumps current */
106         ans.pump_right1_current = sensor_get_adc(ADC_CSENSE3);
107         ans.pump_right2_current = sensor_get_adc(ADC_CSENSE4);
108         /* servos */
109         ans.servo_lintel_left = mechboard.servo_lintel_left;
110         ans.servo_lintel_right = mechboard.servo_lintel_right;
111
112         i2c_send(I2C_ADD_MASTER, (uint8_t *) &ans,
113                  sizeof(ans), I2C_CTRL_GENERIC);
114 }
115
116 static int8_t i2c_set_mode(struct i2c_cmd_mechboard_set_mode *cmd)
117 {
118         state_set_mode(cmd);
119         return 0;
120 }
121
122 void i2c_recvevent(uint8_t * buf, int8_t size)
123 {
124         void *void_cmd = buf;
125         
126         static uint8_t a = 0;
127         
128         a++;
129         if (a & 0x10)
130                 LED2_TOGGLE();
131         
132         if (size <= 0) {
133                 goto error;
134         }
135         
136         switch (buf[0]) {
137
138         /* Commands (no answer needed) */
139         case I2C_CMD_GENERIC_LED_CONTROL: 
140                 {
141                         struct i2c_cmd_led_control *cmd = void_cmd;
142                         if (size != sizeof (*cmd))
143                                 goto error;
144                         i2c_led_control(cmd->led_num, cmd->state);
145                         break;
146                 }
147                 
148         case I2C_CMD_MECHBOARD_SET_MODE:
149                 {
150                         struct i2c_cmd_mechboard_set_mode *cmd = void_cmd;
151                         if (size != sizeof(struct i2c_cmd_mechboard_set_mode))
152                                 goto error;
153                         i2c_set_mode(cmd);
154                         break;
155                 }
156
157         case I2C_CMD_GENERIC_SET_COLOR:
158                 {
159                         struct i2c_cmd_generic_color *cmd = void_cmd;
160                         if (size != sizeof (*cmd))
161                                 goto error;
162                         mechboard.our_color = cmd->color;
163                         break;
164                 }
165
166 #ifdef notyet
167         case I2C_CMD_EXTENSION_TEST: 
168                 {
169                         struct i2c_cmd_extension_test *cmd = void_cmd;
170                         if (size != sizeof (*cmd))
171                                 goto error;
172                         i2c_test(cmd->val);
173                         break;
174                 }
175 #endif
176                 
177         /* Add other commands here ...*/
178
179
180         case I2C_REQ_MECHBOARD_STATUS:
181                 {
182                         struct i2c_req_mechboard_status *cmd = void_cmd;
183                         if (size != sizeof (struct i2c_req_mechboard_status))
184                                 goto error;
185                         
186                         mechboard.pump_left1_current = cmd->pump_left1_current;
187                         mechboard.pump_left2_current = cmd->pump_left2_current;
188                         i2c_send_status();
189                         break;
190                 }
191
192         default:
193                 goto error;
194         }
195
196  error:
197         /* log error on a led ? */
198         return;
199 }
200
201 void i2c_recvbyteevent(__attribute__((unused)) uint8_t hwstatus,
202                        __attribute__((unused)) uint8_t i, 
203                        __attribute__((unused)) int8_t c)
204 {
205 }
206
207 void i2c_sendevent(__attribute__((unused)) int8_t size)
208 {
209 }
210
211