Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.14 KB)

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

    
11
#include <dragonfly_lib.h>
12
#include "colonet_dragonfly.h"
13

    
14
typedef struct {
15
  unsigned char msgId; //is this necessary?
16
  void (*handler)(void);
17
} UserHandler;
18

    
19
/* Globals (internal) */
20
static UserHandler user_handlers[USER_DEFINED_MSG_TOTAL];
21

    
22
/* Internal function prototypes */
23
static void packet_string_to_struct(ColonetPacket* dest_pkt, char* pkt_buf);
24
static unsigned int two_bytes_to_int(char high, char low);
25

    
26
/* Public functions */
27
int colonet_handle_message(unsigned char robot_id, char* pkt_buf)
28
{
29
  ColonetPacket pkt;
30
  unsigned char* args; //up to 7 char args
31
  unsigned int int_args[3]; //up to 3 int (2-byte) args
32

    
33
  packet_string_to_struct(&pkt, pkt_buf);
34

    
35
  if(pkt.msg_dest != GLOBAL_DEST && pkt.msg_dest != robot_id){ 
36
    //message not intended for us
37
    return 1;
38
  }
39

    
40
  args = pkt.data;
41
  
42
  int_args[0] = two_bytes_to_int(args[0], args[1]);
43
  int_args[1] = two_bytes_to_int(args[2], args[3]);
44
  int_args[2] = two_bytes_to_int(args[4], args[5]);
45

    
46
  if(pkt.msg_type == COLONET_REQUEST){
47
    /* TODO - send back data! */
48

    
49
    switch(pkt.msg_code){
50
      //Sharp
51
    case READ_DISTANCE:
52
      break;
53
    case LINEARIZE_DISTANCE:
54
      break;
55
    case LOG_DISTANCE:
56
      break;
57

    
58

    
59
      //Analog
60
    case CALL_ANALOG8:
61
      break;
62
    case CALL_ANALOG10:
63
      break;
64
    case WHEEL:
65
      break;
66
    case BATTERY:
67
      break;
68

    
69
      //BOM
70
    case GETMAXBOM:
71
      break;
72

    
73
      //Dio
74
    case DIGITAL_INPUT:
75
      break;
76
    case BUTTON1_READ:
77
      break;
78
    case BUTTON2_READ:
79
      break;
80

    
81
      //Bumper
82
    case DETECT_BUMP:
83
      break;
84
    }
85
  }else if(pkt.msg_type == COLONET_COMMAND){
86
    if(pkt.msg_code >= USER_DEFINED_MSG_ID_START && 
87
       pkt.msg_code <= USER_DEFINED_MSG_ID_END){
88
      if (user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler) {
89
        /* Call the user's handler function if it the function's address
90
         * is non-zero (has been set) */
91
        user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler();
92
      }
93

    
94
      return 0;
95
    }
96

    
97
    switch(pkt.msg_code){
98
    default:
99
      printf("%s: Error - message code %d not implemented\n", __FUNCTION__,
100
             pkt.msg_code);
101
      return -1;
102
      break;
103

    
104
      //Buzzer
105
    case BUZZER_INIT:
106
      buzzer_init();
107
      break;
108
    case BUZZER_SET_VAL:
109
      buzzer_set_val(args[0]);
110
      break;
111
    case BUZZER_SET_FREQ:
112
      buzzer_set_freq(args[0]);
113
      break;
114
    case BUZZER_CHIRP:
115
      buzzer_chirp(int_args[0], int_args[1]);
116
      break;
117
    case BUZZER_OFF:
118
      buzzer_off();
119
      break;
120
      
121
      //LCD
122
    case LCD_INIT:
123
      lcd_init();
124
      break;
125
    case LCD_CLEAR_SCREEN:
126
      lcd_clear_screen();
127
      break;
128
    case LCD_PUTBYTE:
129
      lcd_putbyte(args[0]);
130
      break;
131
    case LCD_PUTCHAR:
132
      lcd_putchar((char)args[0]);
133
      break;
134
    case LCD_PUTSTR:
135
      lcd_putstr((char*)args);
136
      break;
137
    case LCD_GOTOXY: 
138
      lcd_gotoxy(int_args[0], int_args[1]);
139
      break;
140
    case LCD_PUTINT: 
141
      lcd_putint(int_args[0]);
142
      break;
143
    case ORB_INIT:
144
      orb_init();
145
      break;
146
      //Orb
147
    case ORB_SET:
148
      orb_set(args[0], args[1], args[2]);
149
      break;
150
    case ORB_SET_COLOR:
151
      orb_set_color(int_args[0]);
152
      break;
153
    case ORB_DISABLE:
154
      orb_disable();
155
      break;
156
    case ORB_ENABLE:
157
      orb_enable();
158
      break;
159
    case LED_INIT:
160
      break;
161
    case LED_USER:
162
      break;
163
    case ORB_SEND:
164
      //orb_send();
165
      break;
166
      //Motors
167
    case MOTORS_INIT:
168
      motors_init();
169
      break;
170
    case MOTOR1_SET:
171
      motor1_set(int_args[0], int_args[1]);
172
      break;
173
    case MOTOR2_SET:
174
      motor2_set(int_args[0], int_args[1]);
175
      break;
176
    case MOTORS_OFF:
177
      motors_off();
178
      break;
179

    
180
      /*
181
    case MOVE:
182
      
183
      break;
184
      */
185

    
186
    case XBEE_INIT:
187
      xbee_init();
188
      break;
189

    
190
    case XBEE_PUTC:
191
      xbee_putc((char)args[0]);
192
      break;
193

    
194
    case USB_INIT:
195
      usb_init();
196
      break;
197

    
198
    case USB_PUTC:
199
      usb_putc((char)args[0]);
200
      break;
201

    
202
    case PRINTF:
203
      printf("%s", pkt.data);
204
      break;
205
    case KILL_ROBOT:
206
      motors_off();
207
      buzzer_off();
208
      exit(0); //kill the robot
209
      break;
210
      //Time
211
    case DELAY_MS:
212
      delay_ms(int_args[0]);
213
      break;
214

    
215
      //Analog
216
    case ANALOG_INIT:
217
      analog_init();
218
      break;
219

    
220
      //BOM
221
    case BOM_ON:
222
      bom_on();
223
      break;
224
    case BOM_OFF:
225
      bom_off();
226
      break;
227
    case OUTPUT_HIGH:
228
      output_high(int_args[0]);
229
      break;
230
    case OUTPUT_LOW:
231
      output_low(int_args[0]);
232
      break;
233

    
234
      //Dio
235
    case DIGITAL_OUTPUT:
236
      digital_output(int_args[0], int_args[1]);
237
      break;
238
    }
239
  }else{
240
    printf("%s: Error: Invalid colonet message type", __FUNCTION__);
241
    return -1;
242
  }
243

    
244
  return 0;
245
}
246

    
247
/* colonet_add_message
248
 * Adds a user-defined message
249
 */
250
int colonet_add_message(unsigned char msgId, void (*handler)(void))
251
{
252
  if(msgId < USER_DEFINED_MSG_ID_START || msgId > USER_DEFINED_MSG_ID_END){
253
    return -1;
254
  }
255

    
256
  /* Register this function in the array */
257
  user_handlers[msgId-USER_DEFINED_MSG_ID_START].msgId = msgId;
258
  user_handlers[msgId-USER_DEFINED_MSG_ID_START].handler = handler;
259

    
260
  return 0;
261
}
262

    
263
/* Private functions */
264
static void packet_string_to_struct(ColonetPacket* dest_pkt, char* pkt_buf)
265
{
266
  int i;
267
  
268
  printf("\npacket:");
269
  for(i = 0; i < 16; i++){
270
    printf("%d ", pkt_buf[i]);
271
  }
272

    
273
  printf("\n");
274

    
275
  dest_pkt->prefix[0] = pkt_buf[0];
276
  dest_pkt->prefix[1] = pkt_buf[1];
277
  dest_pkt->token_src = pkt_buf[2];
278
  dest_pkt->token_dest = pkt_buf[3];
279
  dest_pkt->msg_dest = pkt_buf[4];
280
  dest_pkt->msg_type = pkt_buf[5];
281
  dest_pkt->msg_code = pkt_buf[6];
282
  
283
  for(i = 0; i < PACKET_DATA_LEN; i++){
284
    dest_pkt->data[i] = pkt_buf[7+i];
285
  }
286

    
287
  dest_pkt->num_robots = pkt_buf[PACKET_SIZE-2];
288
  dest_pkt->checksum = pkt_buf[PACKET_SIZE-1];
289
}
290

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