root / branches / slam / code / projects / slam / computer / data_requests.c @ 525
History | View | Annotate | Download (2.25 KB)
1 |
#include "data_requests.h" |
---|---|
2 |
#include <stdlib.h> |
3 |
#include <stdio.h> |
4 |
#include "../../libwireless/lib/wireless.h" |
5 |
#include <string.h> |
6 |
|
7 |
PacketGroupHandler pgh; |
8 |
|
9 |
void receive_handle(char type, int source, unsigned char* packet, int length); |
10 |
|
11 |
void (*bom_handle)(short id, BomNode** head); |
12 |
void (*IR_handle)(short id, short** data); |
13 |
void (*encoder_handle)(short id, unsigned char** data); |
14 |
|
15 |
void data_requests_init(void(*bom_data_handler)(short id, BomNode** head), |
16 |
void(*IR_data_handler)(short id, short** data), |
17 |
void(*encoder_data_handler)(short id, unsigned char** data)){ |
18 |
bom_handle = bom_data_handler; |
19 |
IR_handle = IR_data_handler; |
20 |
encoder_handle = encoder_data_handler; |
21 |
|
22 |
pgh.groupCode = DATA_REQUEST_GROUP; |
23 |
pgh.timeout_handler = NULL;
|
24 |
pgh.handle_response = NULL;
|
25 |
pgh.handle_receive = receive_handle; |
26 |
pgh.unregister = NULL;
|
27 |
|
28 |
wl_register_packet_group(&pgh); |
29 |
} |
30 |
|
31 |
|
32 |
void request_bom_data(int robot_id){ |
33 |
wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,BOM_TYPE,NULL,0,robot_id,0); |
34 |
} |
35 |
|
36 |
void request_IR_data(int robot_id){ |
37 |
wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,NULL,0,robot_id,0); |
38 |
} |
39 |
|
40 |
void request_encoder_data(int robot_id){ |
41 |
wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,ENCODER_TYPE,NULL,0,robot_id,0); |
42 |
} |
43 |
|
44 |
void receive_handle(char type, int source, unsigned char* packet, int length){ |
45 |
int i=0; |
46 |
BomNode* head; |
47 |
switch(type){
|
48 |
|
49 |
case BOM_TYPE:
|
50 |
head = malloc(sizeof(BomNode));
|
51 |
BomNode* iter = head; |
52 |
|
53 |
while(i<length){
|
54 |
iter->robot_id = ((short)packet[i]<<8) + ((short)packet[i+1]); |
55 |
i+=2;
|
56 |
iter->value = packet[i++]; |
57 |
if(i<length){
|
58 |
iter->next = malloc(sizeof(BomNode));
|
59 |
iter = iter->next; |
60 |
} |
61 |
} |
62 |
bom_handle(source,&head); |
63 |
break;
|
64 |
|
65 |
case IR_TYPE:
|
66 |
/*An IR packet consists of just the 10 bytes representing the IR
|
67 |
values as shorts, in order.*/
|
68 |
if(length != 10) break; |
69 |
|
70 |
short* data = malloc(5*sizeof(short)); |
71 |
for(i=0;i<5;i++){ |
72 |
data[i]= ((((short)packet[2*i])<<8)&0xff00 ) | ( (short)(packet[2*i+1])&255); |
73 |
} |
74 |
|
75 |
IR_handle(source, &data); |
76 |
break;
|
77 |
|
78 |
case ENCODER_TYPE:
|
79 |
/*I have no idea yet, unsupported.*/
|
80 |
break;
|
81 |
} |
82 |
} |
83 |
|