Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / lib / colonet_dragonfly / colonet_dragonfly.c @ 175

History | View | Annotate | Download (6.86 KB)

1
/** @file colonet.c
2
 * @brief Colonet library for DRAGONFLY colony robots
3
 *
4
 * @author Eugene Marinelli
5
 * @date 10/10/07
6
 *
7
 * @bug Handler registration not tested
8
 * @bug Request reponding not implemented - only accepts commands.
9
 */
10

    
11
#include <assert.h>
12
#include <dragonfly_lib.h>
13
#include <string.h>
14
#include <wireless.h>
15

    
16
#include "colonet_dragonfly.h"
17

    
18
typedef struct {
19
  unsigned char msgId; //is this necessary?
20
  void (*handler)(void);
21
} UserHandler;
22

    
23
/* Globals (internal) */
24
static UserHandler user_handlers[USER_DEFINED_MSG_TOTAL];
25

    
26
/* Internal function prototypes */
27
static void packet_string_to_struct(ColonetRobotServerPacket* dest_pkt,
28
                                    char* pkt_buf);
29
static unsigned int two_bytes_to_int(char high, char low);
30
static void colonet_handle_receive(char type, int source,
31
                                   unsigned char* packet, int length);
32

    
33
static PacketGroupHandler colonet_pgh;
34

    
35
/* Public functions */
36
int colonet_init() {
37
  colonet_pgh.groupCode = COLONET_PACKET_GROUP_ID;
38
  colonet_pgh.timeout_handler = NULL;
39
  colonet_pgh.handle_response = NULL;
40
  colonet_pgh.handle_receive = colonet_handle_receive;
41
  colonet_pgh.unregister = NULL;
42

    
43
  // TODO this should return an error if wl_init has not been called yet.
44
  wl_register_packet_group(&colonet_pgh);
45

    
46
  return 0;
47
}
48

    
49
/* Private functions */
50

    
51
/** @brief Handles colonet packets.  Should be called by parse_buffer
52
 * when it is determined that a colonet message has been received.
53
 *
54
 * @param robot_id The robot id
55
 * @param pkt_buf The packet buffer (e.g. wl_buf)
56
 *
57
 * @return -1 on error (invalid msgId), 0 on success
58
 */
59
void colonet_handle_receive(char type, int source, unsigned char* packet,
60
                            int length) {
61
  ColonetRobotServerPacket pkt;
62
  unsigned char* args; //up to 7 char args
63
  unsigned int int_args[3]; //up to 3 int (2-byte) args
64

    
65
  usb_puts("Packet received.\n");
66
  char buf[40];
67
  sprintf(buf, "length=%d\n", length);
68

    
69
  int i;
70
  for (i = 0; i < length; i++) {
71
    sprintf(buf, "%d: %d ", i, packet[i]);
72
    usb_puts(buf);
73
  }
74
  usb_puts("\n");
75

    
76
  //printf("received message from %d of length %d\n", source, length);
77

    
78
  ///assert(length == sizeof(ColonetRobotServerPacket));
79
  packet_string_to_struct(&pkt, packet);
80

    
81
  char buf2[40];
82
  sprintf(buf2, "client_id:%d, msg_code:%d\n", pkt.client_id, pkt.msg_code);
83
  usb_puts(buf2);
84

    
85
  args = pkt.data;
86

    
87
  int_args[0] = two_bytes_to_int(args[0], args[1]);
88
  int_args[1] = two_bytes_to_int(args[2], args[3]);
89
  int_args[2] = two_bytes_to_int(args[4], args[5]);
90

    
91
  if (type == COLONET_REQUEST) {
92
    usb_puts("type is colonet request\n");
93

    
94
    /* TODO - send back data! */
95

    
96
    switch (pkt.msg_code) {
97
      //Sharp
98
    case READ_DISTANCE:
99
      break;
100
    case LINEARIZE_DISTANCE:
101
      break;
102
    case LOG_DISTANCE:
103
      break;
104

    
105

    
106
      //Analog
107
    case CALL_ANALOG8:
108
      break;
109
    case CALL_ANALOG10:
110
      break;
111
    case WHEEL:
112
      break;
113
    case BATTERY:
114
      break;
115

    
116
      //BOM
117
    case GETMAXBOM:
118
      break;
119

    
120
      //Dio
121
    case DIGITAL_INPUT:
122
      break;
123
    case BUTTON1_READ:
124
      break;
125
    case BUTTON2_READ:
126
      break;
127

    
128
      //Bumper
129
    case DETECT_BUMP:
130
      break;
131
    }
132
  } else if (type == COLONET_COMMAND) {
133
    usb_puts("type is colonet command...\n");
134

    
135
/* TODO uncomment this stuff */
136
/*     if (pkt.msg_code >= USER_DEFINED_MSG_ID_START && */
137
/*         pkt.msg_code <= USER_DEFINED_MSG_ID_END) { */
138
/*       if (user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler) { */
139
/*         /\* Call the user's handler function if it the function's address */
140
/*          * is non-zero (has been set) *\/ */
141
/*         user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler(); */
142
/*       } */
143
/*     } */
144

    
145
    switch (pkt.msg_code) {
146
/*     default: */
147
/*       printf("%s: Error - message code %d not implemented\n", __FUNCTION__, */
148
/*              pkt.msg_code); */
149
/*       break; */
150

    
151
      //Buzzer
152
    case BUZZER_INIT:
153
      buzzer_init();
154
      break;
155
    case BUZZER_SET_VAL:
156
      buzzer_set_val(args[0]);
157
      break;
158
    case BUZZER_SET_FREQ:
159
      buzzer_set_freq(args[0]);
160
      break;
161
    case BUZZER_CHIRP:
162
      buzzer_chirp(int_args[0], int_args[1]);
163
      break;
164
    case BUZZER_OFF:
165
      buzzer_off();
166
      break;
167

    
168
    case ORB_INIT:
169
      orb_init();
170
      break;
171
      //Orb
172
    case ORB_SET:
173
      orb_set(args[0], args[1], args[2]);
174
      break;
175
    case ORB_SET_COLOR:
176
      orb_set_color(int_args[0]);
177
      break;
178
    case ORB_DISABLE:
179
      orb_disable();
180
      break;
181
    case ORB_ENABLE:
182
      orb_enable();
183
      break;
184
    case LED_INIT:
185
      break;
186
    case LED_USER:
187
      break;
188
    case ORB_SEND:
189
      //orb_send();
190
      break;
191
      //Motors
192
    case MOTORS_INIT:
193
      usb_puts("calling motors_init\n");
194
      motors_init();
195
      break;
196
    case MOTOR1_SET:
197
      usb_puts("calling motor1_set\n");
198
      motor1_set(int_args[0], int_args[1]);
199
      break;
200
    case MOTOR2_SET:
201
      usb_puts("calling motor2_set\n");
202
      motor2_set(int_args[0], int_args[1]);
203
      break;
204
    case MOTORS_OFF:
205
      usb_puts("calling motors_off\n");
206
      motors_off();
207
      break;
208
    case MOVE:
209
      buf[40];
210
      sprintf(buf, "calling move with: %d, %d\n", int_args[0], int_args[1]);
211
      usb_puts(buf);
212
      move(args[0], args[1]);
213
      break;
214
    case XBEE_INIT:
215
      xbee_init();
216
      break;
217

    
218
    case XBEE_PUTC:
219
      xbee_putc((char)args[0]);
220
      break;
221

    
222
    case USB_INIT:
223
      usb_init();
224
      break;
225

    
226
    case USB_PUTC:
227
      usb_putc((char)args[0]);
228
      break;
229

    
230
    case PRINTF:
231
      printf("%s", pkt.data);
232
      break;
233
    case KILL_ROBOT:
234
      motors_off();
235
      buzzer_off();
236
      exit(0); //kill the robot
237
      break;
238
      //Time
239
    case DELAY_MS:
240
      delay_ms(int_args[0]);
241
      break;
242

    
243
      //Analog
244
    case ANALOG_INIT:
245
      analog_init();
246
      break;
247

    
248
      //BOM
249
    case BOM_ON:
250
      bom_on();
251
      break;
252
    case BOM_OFF:
253
      bom_off();
254
      break;
255

    
256
      //Dio
257
    case DIGITAL_OUTPUT:
258
      digital_output(int_args[0], int_args[1]);
259
      break;
260
    }
261
  } else {
262
    char buf[80];
263
    sprintf(buf, "%s: Error: Invalid colonet message type", __FUNCTION__);
264
    usb_puts(buf);
265
  }
266
}
267

    
268
/* colonet_add_message
269
 * Adds a user-defined message
270
 */
271
int colonet_add_message(unsigned char msgId, void (*handler)(void)) {
272
  if(msgId < USER_DEFINED_MSG_ID_START || msgId > USER_DEFINED_MSG_ID_END){
273
    return -1;
274
  }
275

    
276
  /* Register this function in the array */
277
  user_handlers[msgId-USER_DEFINED_MSG_ID_START].msgId = msgId;
278
  user_handlers[msgId-USER_DEFINED_MSG_ID_START].handler = handler;
279

    
280
  return 0;
281
}
282

    
283
static void packet_string_to_struct(ColonetRobotServerPacket* dest_pkt,
284
                                    char* pkt_buf) {
285
  memcpy(dest_pkt, pkt_buf, sizeof(ColonetRobotServerPacket));
286
}
287

    
288
/* two_bytes_to_int(char a, char b)
289
 * Returns int of form [high][low]
290
 */
291
static unsigned int two_bytes_to_int(char high, char low) {
292
  return (((unsigned int)high)<<8) + (unsigned int)low;
293
}