伪代码;
1-boot(红色led)
广播数据包(绿灯)
implementation{
bool busy = FALSE;
message_t pkt;
message_t i_pkt;
event void Boot.booted() {
call Leds.set(1);
call AMControl.start();
printf("Booted %d\n",1);
call CC2420Packet.setPower(&pkt, 20);
call CC2420Packet.setPower(&i_pkt, 2);
}
event void Timer0.fired() {
if (!busy) {
FindRoute_t *btrpkt = (FindRoute_t*)(call Packet.getPayload(&pkt, sizeof (FindRoute_t)));
btrpkt->counter = 4;
if (call firstsender.send(AM_BROADCAST_ADDR, &pkt, sizeof(FindRoute_t)) == SUCCESS) {
busy = TRUE;
}
}
}
event void AMControl.startDone(error_t err) {
if (err == SUCCESS) {
call Timer0.startOneShot(250);
}
else {
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err) {
}
event void firstsender.sendDone(message_t *msg, error_t error) {
if (&pkt == msg) {
call Leds.set(2);
busy = FALSE;
}
}
event message_t* firstreceiver.receive(message_t* msg, void* payload, uint8_t len) {
if (len == sizeof(FindRoute_t)) {
FindRoute_t* btrpkt = (FindRoute_t*)payload;
call Leds.set(btrpkt->counter);
call Timer1.startOneShot(3000);
}
return msg;
}
event void Timer1.fired(){
if (!busy) {
FindRoute_t *btrpkt = (FindRoute_t*)(call Packet.getPayload(&i_pkt, sizeof (FindRoute_t)));
btrpkt->counter = 7;
if (call nextsender.send(AM_BROADCAST_ADDR, &i_pkt, sizeof(FindRoute_t)) == SUCCESS) {
busy = TRUE;
}
}
}
event void nextsender.sendDone(message_t *msg, error_t error) {
if (&i_pkt == msg) {
call Leds.set(2);
busy = FALSE;
}
}
event message_t* nextreceiver.receive(message_t* msg, void* payload, uint8_t len) {
if (len == sizeof(FindRoute_t)) {
FindRoute_t* btrpkt = (FindRoute_t*)payload;
call Leds.set(btrpkt->counter);
}
return msg;
}
答案 0 :(得分:1)
你有两个接收器。它们是否连接到两个不同的接收模块?如果没有,收到的数据包将触发它们的接收事件,这可能会导致一些问题。