/** test_radio_multi app: This app lets you test the radio_multi library. Each device takes turns being the "leader device", instructing the other devices to flash their LEDs, and then passing leadership to the next device. Assumes that all active devices are numbered sequentially from 1 to N <= 255 at all times. */ #include #include #include #include #include #include #include #define LED_FLASH_TIME 50 // how many milliseconds an LED flash should be #define COMMAND_PAUSE 250 // how long to wait between wireless commands (should be > LED_FLASH_TIME) BIT isLeader = 0; // is this the device in charge of the others? void handleCommunications() { // the different 1-byte commands that the devices can send to each other const uint8 MSG_FLASH_GREEN = 0; const uint8 MSG_FLASH_RED = 1; uint8 sender; // who sent us the current message uint8 i; uint8 message; // this app uses 1-byte messages, although we're not limited to this char a = 0x1; uint8 signals; BIT doneSearching = 0; // set to true when we've reached the maximum device ID that will respond to us uint8 maxIDFound = 1; // we record the highest device ID who has responded to us // the leader in this app is responsible for sending instructions to all other devices if(isLeader) { // instruct each device to flash its green LED individually i = 1; while(!doneSearching && i != 0) // we use != 0 because with enough devices we will wrap around to the broadcast ID, indicating completion { // don't send it to ourselves, because that would be dumb if(i != param_device_id) { // this is a transmission that requires an ACK in response to be considered successful; we // try several times before giving up without receiving an ACK from the other device if(radioMultiTxDataBlocking(i, &MSG_FLASH_GREEN, 1, 5)) { // record a new highest ID if(i > maxIDFound) maxIDFound = i; delayMs(COMMAND_PAUSE); } else { // indicate we're done searching for other devices doneSearching = 1; } } // move to the next device i ++; } // now tell everyone to flash the red LED at the same time (0 is the broadcast ID); does not require an ACK radioMultiTxData(0, &MSG_FLASH_RED, 1); delayMs(COMMAND_PAUSE); } else { // has someone contacted us? if(radioMultiRxAvailable()) { // read one byte of it, which should be all of the message we expect in this app radioMultiRxData(&sender, &message, 1); if (uart1TxAvailable()) { uart1TxSendByte(a); LED_YELLOW(1); delayMs(LED_FLASH_TIME); LED_YELLOW(0); } if(message == MSG_FLASH_GREEN) { LED_GREEN(1); delayMs(LED_FLASH_TIME); LED_GREEN(0); } else if(message == MSG_FLASH_RED) { LED_RED(1); delayMs(LED_FLASH_TIME); LED_RED(0); } } } } void main() { uint8 i; systemInit(); usbInit(); radioMultiInit(); randomSeedFromAdc(); uart1Init(); uart1SetBaudRate(9600); // flash to indicate a reset for(i = 0; i < 3; i ++) { LED_YELLOW(1); delayMs(LED_FLASH_TIME); LED_YELLOW(0); delayMs(LED_FLASH_TIME); } // initially, the device with ID 1 is the leader isLeader = param_device_id == 1; LED_YELLOW(isLeader); while(1) { boardService(); usbComService(); // handles leader and client logic handleCommunications(); } }