#use this interface in our module signature declaration use interface CC2420Packet; #add this following into your module implementation part #you can use this packet variable to encapsulate the radio power value #as well as the light reading value, ...please look at the sample code of #the following file message_t packet; uint16_t txpower = 0; call CC2420Packet.setPower(&packet, txpower); /** * TranxPower.nc * @author Jun Yi, University of Notre Dame * @date May 10 2010 */ #include "Timer.h" #include "TranxPower.h" module TranxPowerC @safe() { uses { interface Leds; interface Boot; interface Receive; interface AMSend; interface Timer as MilliTimer; interface SplitControl as AMControl; interface CC2420Packet; interface Packet; } } implementation { message_t packet; bool locked; uint16_t counter = 0; uint16_t txpower = 0; uint16_t txpower_tries = 0; event void Boot.booted() { call AMControl.start(); } event void AMControl.startDone(error_t err) { if (err == SUCCESS) { call MilliTimer.startPeriodic(1000); } else { call AMControl.start(); } } event void AMControl.stopDone(error_t err) { // do nothing } event void MilliTimer.fired() { counter++; dbg("TranxPowerC", "TranxPowerC: timer fired, counter is %hu.\n", counter); if (locked) { return; } else { tranx_power_msg_t* txpowerMsg = (tranx_power_msg_t*)call Packet.getPayload(&packet, sizeof(tranx_power_msg_t)); if (txpowerMsg == NULL) { return; } txpowerMsg->counter = counter; txpowerMsg->txpower = txpower; call CC2420Packet.setPower(&packet, txpower); if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(tranx_power_msg_t)) == SUCCESS) { dbg("TranxPowerC", "TranxPowerC: packet sent.\n", counter, txpower); locked = TRUE; if (txpowerMsg->counter & 0x1) { call Leds.led0On(); } else { call Leds.led0Off(); } if (txpowerMsg->counter & 0x2) { call Leds.led1On(); } else { call Leds.led1Off(); } if (txpowerMsg->counter & 0x4) { call Leds.led2On(); } else { call Leds.led2Off(); } } txpower_tries++; if(txpower_tries > 10){ txpower_tries = 0; txpower++; txpower = txpower%32; } } } event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len) { dbg("TranxPowerC", "Received packet of length %hhu.\n", len); if (len != sizeof(tranx_power_msg_t)) {return bufPtr;} else { tranx_power_msg_t* txpowerMsg = (tranx_power_msg_t*)payload; if (txpowerMsg->counter & 0x1) { call Leds.led0On(); } else { call Leds.led0Off(); } if (txpowerMsg->counter & 0x2) { call Leds.led1On(); } else { call Leds.led1Off(); } if (txpowerMsg->counter & 0x4) { call Leds.led2On(); } else { call Leds.led2Off(); } return bufPtr; } } event void AMSend.sendDone(message_t* bufPtr, error_t error) { if (&packet == bufPtr) { locked = FALSE; } } } /** * * * * @author Jun Yi, University of Notre Dame * *@date May 10 2010, TranxPower.h * */ #ifndef TRANX_POWER_H #define TRANX_POWER_H typedef nx_struct tranx_power_msg{ nx_uint16_t counter; nx_uint16_t txpower; }tranx_power_msg_t; enum { AM_TRANX_POWER_MSG = 6, };