伪代码;
1-引导(红色 LED)
广播数据包,发送完成后(绿灯)
- 接收数据包,解码数据包并相应地设置led为(设置蓝色)
- 编码后广播数据包,发送完成(设置为绿色)
- 接收数据包,解码并相应地设置led为(设置7(红+绿+蓝))
在此阶段之后,mote 1 将投注 LED.set(7),mote 2 将投注 LED.set(2) [mote 1 已开始传输],直到它工作得很好。但是当我重新启动 mote 1 时,它一直工作到 2 个伪代码步骤,并且 mote 2 无法接收数据包)
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;
}