void FormatTxMessage() { BYTE inputChar; inputChar = CONSOLE_Get(); CONSOLE_Put(inputChar); if(inputChar == 0x0D) { CONSOLE_PutString((char *) "\r\n$$"); messagePending = true; transmitPending = true; } else if(inputChar == 0x08) { TxMessageSize--; } else { if(TxMessageSize < MAX_MESSAGE_LEN) { TxMessage[TxMessageSize] = inputChar; TxMessageSize++; tickPrevious = MiWi_TickGet(); messagePending = true; } } }
/********************************************************************* * Function: void RangeDemo(void) * * PreCondition: none * * Input: none * * Output: none * * Side Effects: none * * Overview: Following routine * * * Note: **********************************************************************/ void RangeDemo(void) { BOOL Run_Demo = TRUE; BOOL Tx_Packet = TRUE; BYTE rssi = 0; BYTE Pkt_Loss_Cnt = 0; MIWI_TICK tick1, tick2; BYTE switch_val; /*******************************************************************/ // Dispaly Range Demo Splach Screen /*******************************************************************/ LCDBacklightON(); LCDDisplay((char *)" Microchip Range Demo ", 0, TRUE); LCDBacklightOFF(); /*******************************************************************/ // Read Start tickcount /*******************************************************************/ tick1 = MiWi_TickGet(); while(Run_Demo) { /*******************************************************************/ // Read current tickcount /*******************************************************************/ tick2 = MiWi_TickGet(); // Send a Message if((MiWi_TickGetDiff(tick2,tick1) > (ONE_SECOND * TX_PKT_INTERVAL))) { LCDErase(); if(Tx_Packet) { LCDDisplay((char *)"Checking Signal Strength...", 0, TRUE); MiApp_FlushTx(); MiApp_WriteData(RANGE_PKT); MiApp_WriteData(0x4D); MiApp_WriteData(0x69); MiApp_WriteData(0x57); MiApp_WriteData(0x69); MiApp_WriteData(0x20); MiApp_WriteData(0x52); MiApp_WriteData(0x6F); MiApp_WriteData(0x63); MiApp_WriteData(0x6B); MiApp_WriteData(0x73); MiApp_WriteData(0x21); if( MiApp_UnicastConnection(0, FALSE) == FALSE ) Pkt_Loss_Cnt++; else Pkt_Loss_Cnt = 0; Tx_Packet = FALSE; } else { if(Pkt_Loss_Cnt < 1) { if(rssi > 120) { sprintf((char *)&LCDText, (far rom char*)"Strength: High "); LED0 = 1; LED1 = 0; LED2 = 0; } else if(rssi < 121 && rssi > 60) { sprintf((char *)&LCDText, (far rom char*)"Strength: Medium"); LED0 = 1; LED1 = 1; LED2 = 0; } else if(rssi < 61) { sprintf((char *)&LCDText, (far rom char*)"Strength: Low"); LED0 = 0; LED1 = 1; LED2 = 0; } // Convert to dB //rssi = RSSIlookupTable[rssi]; sprintf((char *)&(LCDText[16]), (far rom char*)"Rcv RSSI: %03d", rssi); } else { LCDDisplay((char *)"No Device Found or Out of Range ", 0, TRUE); LED0 = 0; LED1 = 0; LED2 = 1; } LCDUpdate(); Tx_Packet = TRUE; } /*******************************************************************/ // Read New Start tickcount /*******************************************************************/ tick1 = MiWi_TickGet(); } // Check if Message Available if(MiApp_MessageAvailable()) { if(rxMessage.Payload[0] == EXIT_PKT) { MiApp_DiscardMessage(); MiApp_FlushTx(); MiApp_WriteData(ACK_PKT); MiApp_UnicastConnection(0, FALSE); Run_Demo = FALSE; LCDBacklightON(); LCDDisplay((char *)" Exiting.... Range Demo ", 0, TRUE); LCDBacklightOFF(); } else if(rxMessage.Payload[0] == RANGE_PKT) { // Get RSSI value from Recieved Packet rssi = rxMessage.PacketRSSI; // Disguard the Packet so can recieve next MiApp_DiscardMessage(); } else MiApp_DiscardMessage(); } // Check if Switch Pressed switch_val = ButtonPressed(); if((switch_val == SW0) || (switch_val == SW1)) { /*******************************************************************/ // Send Exit Demo Request Packet and exit Range Demo /*******************************************************************/ MiApp_FlushTx(); MiApp_WriteData(EXIT_PKT); MiApp_UnicastConnection(0, FALSE); LCDBacklightON(); LCDDisplay((char *)" Exiting.... Range Demo ", 0, TRUE); LCDBacklightOFF(); tick1 = MiWi_TickGet(); // Wait for ACK Packet while(Run_Demo) { if(MiApp_MessageAvailable()) { if(rxMessage.Payload[0] == ACK_PKT) Run_Demo = FALSE; MiApp_DiscardMessage(); } if ((MiWi_TickGetDiff(tick2,tick1) > (ONE_SECOND * EXIT_DEMO_TIMEOUT))) Run_Demo = FALSE; tick2 = MiWi_TickGet(); } } } }
void main (void) { uint8_t i; SYSTEM_Initialize(); CONSOLE_Initialize(); /*******************************************************************/ // Initialize the system /*******************************************************************/ /*******************************************************************/ // Following block display demo information on LCD of Explore 16 or // PIC18 Explorer demo board. /*******************************************************************/ LCDDisplay((char *)"Chat Demo", 0, true); // Clear the screen (VT100) Printf("\x1b[2J"); // Send the cursor home (VT100) Printf("\x1b[H"); Printf("\r\nChat Demo"); #if defined(MRF24J40) Printf("\r\nRF Transceiver: MRF24J40"); #elif defined(MRF49XA) Printf("\r\nRF Transceiver: MRF49XA"); #elif defined(MRF89XA) Printf("\r\nRF Transceiver: MRF89XA"); #endif Printf("\r\n\r\nDemo Instruction:"); Printf("\r\nUse Console to Chat with the Peer Device"); Printf("\r\n"); LED_1 = 0; LED_2 = 0; /******************************************************************/ // Read the MAC address from the MAC EEPROM on the PICTail Card ReadMacAddress(); // ..and display on terminal Printf("\r\n\r\nMy MAC Address: 0x"); for(i = 0; i < MY_ADDRESS_LENGTH; i++) { CONSOLE_PrintHex(myLongAddress[MY_ADDRESS_LENGTH-1-i]); } /*******************************************************************/ // Initialize Microchip proprietary protocol. Which protocol to use // depends on the configuration in ConfigApp.h /*******************************************************************/ /*******************************************************************/ // Function MiApp_ProtocolInit initialize the protocol stack. The // only input parameter indicates if previous network configuration // should be restored. In this simple example, we assume that the // network starts from scratch. /*******************************************************************/ MiApp_ProtocolInit(false); // Set default channel if(MiApp_SetChannel(myChannel) == false) { Printf("\r\nERROR: Unable to program the channel\r\n"); Printf("\r\nPress MCLR to start again\r\n"); LCDDisplay((char *)"Error: Unable to Program Channel ", 0, TRUE); while(1); //Display error message on LCD and Console } /*******************************************************************/ // Function MiApp_ConnectionMode defines the connection mode. The // possible connection modes are: // ENABLE_ALL_CONN: Enable all kinds of connection // ENABLE_PREV_CONN: Only allow connection already exists in // connection table // ENABL_ACTIVE_SCAN_RSP: Allow response to Active scan // DISABLE_ALL_CONN: Disable all connections. /*******************************************************************/ MiApp_ConnectionMode(ENABLE_ALL_CONN); /*******************************************************************/ // Display current opertion on LCD of demo board, if applicable /*******************************************************************/ LCDDisplay((char *)"Connecting Peer on Channel %d ", myChannel, true); Printf("\r\n\r\nConnecting to Peer...\r\n"); /*******************************************************************/ // Function MiApp_StartConnection will enable a node to start operating // in a variety of ways. Usually, this fucntion is called by the // PAN Coordinator who is the first in the PAN. // // The first parameter defines the mode to start the PAN in // // The second parameter defines the scan duration (if energy/carrier // sense scan is enabled). 0 if START_CONN_DIRECT used. // // The third parameter is a bit map of of the channels to perform the // noise scan on. 0 if START_CONN_DIRECT used. /*******************************************************************/ MiApp_StartConnection(START_CONN_DIRECT, 0, 0) ; /*******************************************************************/ // Function MiApp_EstablishConnection try to establish a new // connection with peer device. // The first parameter is the index to the active scan result, // which is acquired by discovery process (active scan). If // the value of the index is 0xFF, try to establish a // connection with any peer. // The second parameter is the mode to establish connection, // either direct or indirect. Direct mode means connection // within the radio range; indirect mode means connection // may or may not in the radio range. /*******************************************************************/ #ifdef ENABLE_HAND_SHAKE i = 0xFF; while(i == 0xFF) { i = MiApp_EstablishConnection(0xFF, CONN_MODE_DIRECT); } #endif /*******************************************************************/ // Display current opertion on LCD of demo board, if applicable /*******************************************************************/ LCDDisplay((char *)"Joined Network Successfully..", 0, true); /*******************************************************************/ // Function DumpConnection is used to print out the content of the // Connection Entry on the hyperterminal. It may be useful in // the debugging phase. // The only parameter of this function is the index of the // Connection Entry. The value of 0xFF means to print out all // valid Connection Entry; otherwise, the Connection Entry // of the input index will be printed out. /*******************************************************************/ #ifdef ENABLE_DUMP DumpConnection(0xFF); #endif #ifndef ENABLE_POWERSAVE // Turn on LED 1 to indicate P2P connection established LED_1 = 1; #endif DELAY_ms(100); LCDBacklightON(); LCD_Erase(); sprintf((char *)LCDText, (char *)"MyAddr: %02x%02x%02x", myLongAddress[2], myLongAddress[1], myLongAddress[0]); sprintf((char *) &(LCDText[16]), (char *)"PeerAddr: %02x%02x%02x", ConnectionTable[i].Address[2], ConnectionTable[i].Address[1], ConnectionTable[i].Address[0]); LCD_Update(); /*******************************************************************/ // Following block display demo instructions on LCD based on the // demo board used. /*******************************************************************/ Printf("-------------------------------------------------------\r\n"); Printf("Chat Window: \r\n"); Printf("-------------------------------------------------------\r\n"); Printf("$$"); while(1) { if(MiApp_MessageAvailable()) { ProcessRxMessage(); } if(CONSOLE_IsGetReady()) { FormatTxMessage(); } if(messagePending) { tickCurrent = MiWi_TickGet(); if ( (MiWi_TickGetDiff(tickCurrent, tickPrevious) > (ONE_SECOND * 30)) || (TxMessageSize >= MAX_MESSAGE_LEN) || (transmitPending == true) ) { TransmitMessage(); } } // Display connection table if RB0 is pressed if(PUSH_BUTTON_1 == 0) { while(PUSH_BUTTON_1 == 0); Printf("\r\n\r\nDumping Connection Table...\r\n"); DumpConnection(0xFF); Printf("-------------------------------------------------------\r\n"); Printf("Chat Window: \r\n"); Printf("-------------------------------------------------------\r\n"); Printf("$$ "); } } //Enable device to foward the received packet information to the console } //end of main
void run_star_demo(void) { #if defined(PROTOCOL_STAR) t1 = MiWi_TickGet(); LCDDisplay((char *)"Sleeping!!", 0, false); while(1) { t2 = MiWi_TickGet(); // Calculate the Time for Sleeping device if((MiWi_TickGetDiff(t2,t1) > (ONE_SECOND * 20))) { awake = true; #if !defined(EIGHT_BIT_WIRELESS_BOARD) LCD_BacklightON(); #endif MiApp_TransceiverPowerState(POWER_STATE_WAKEUP_DR); LCDDisplay((char *)"Woke Up!!!", 0, false); DELAY_ms(1000); STAR_DEMO_OPTIONS_MESSAGE (false); tt1 = MiWi_TickGet(); } while(awake) { tt2 = MiWi_TickGet(); if((MiWi_TickGetDiff(tt2,tt1) > (ONE_SECOND * 10))) { // The Indirect Message for a Sleeping RFD is stored in PAN CO // We periodically send Data Requests for the Indirect Message to // not loose a message. CheckForData(); tt1 = MiWi_TickGet(); } /*******************************************************************/ // Function MiApp_MessageAvailable returns a boolean to indicate if // a packet has been received by the transceiver. If a packet has // been received, all information will be stored in the rxFrame, // structure of RECEIVED_MESSAGE. /*******************************************************************/ if( MiApp_MessageAvailable()) { /*******************************************************************/ // If a packet has been received, update the information available // in rxMessage. /*******************************************************************/ DemoOutput_UpdateTxRx(TxNum, ++RxNum); DELAY_ms(2000); // Toggle LED2 to indicate receiving a packet. LED_2 ^= 1; /*******************************************************************/ // Function MiApp_DiscardMessage is used to release the current // received packet. // After calling this function, the stack can start to process the // next received frame /*******************************************************************/ MiApp_DiscardMessage(); /****************************************/ } else { /*******************************************************************/ // If no packet received, now we can check if we want to send out // any information. // Function ButtonPressed will return if any of the two buttons // has been pushed. /*******************************************************************/ uint8_t PressedButton = ButtonPressed(); if ( PressedButton == 1 || PressedButton == 2) { uint8_t select_ed =0; bool update_ed = true; while(update_ed == true) { //User Selected Change end device LCD_Erase(); if (myConnectionIndex_in_PanCo == select_ed) { // if END_device displays itself , "me" is added in display to denote itself sprintf((char *)LCDText, (char*)"RB0:%02d-%02x%02x%02x-me",END_DEVICES_Short_Address[select_ed].connection_slot,END_DEVICES_Short_Address[select_ed].Address[0], END_DEVICES_Short_Address[select_ed].Address[1],END_DEVICES_Short_Address[select_ed].Address[2] ); } else { sprintf((char *)LCDText, (char*)"RB0:%02d-%02x%02x%02x",END_DEVICES_Short_Address[select_ed].connection_slot,END_DEVICES_Short_Address[select_ed].Address[0], END_DEVICES_Short_Address[select_ed].Address[1],END_DEVICES_Short_Address[select_ed].Address[2] ); } sprintf((char *)LCDText, (char*)"RB0:%02d-%02x%02x%02x",END_DEVICES_Short_Address[select_ed].connection_slot,END_DEVICES_Short_Address[select_ed].Address[0], END_DEVICES_Short_Address[select_ed].Address[1],END_DEVICES_Short_Address[select_ed].Address[2] ); sprintf((char *)&(LCDText[16]), (char*)"RB2: Change node"); LCD_Update(); chk_sel_status = true; bool sw_layer_ack_status , mac_ack_status; while(chk_sel_status) { uint8_t switch_val = ButtonPressed(); if(switch_val == 1) { update_ed = false; chk_sel_status = false; update_ed = false; // Star_User_Data is defined in star_demo.c // Its user data , in case of Star Network 60 bytes of // Data can be sent at a time from one END_DEVICE_TO_ANOTHER // Edx --> Pan CO --> EDy if (myConnectionIndex_in_PanCo == select_ed) { MiApp_FlushTx(); for (i = 0 ; i < 21 ; i++) { MiApp_WriteData(MiWi[(TxSynCount%6)][i]); } // IF on the demo , a END_Device displays its own Connection Detail // We unicast data packet to just PAN COR , No forwarding #if defined(ENABLE_SECURITY) mac_ack_status = MiApp_UnicastConnection (0, true); #else mac_ack_status = MiApp_UnicastConnection (0, false); #endif TxNum++; } else { // Data can be sent at a time from one END_DEVICE_TO_ANOTHER // Edx --> Pan CO --> EDy // To forward a Packet from one ED to another ED , the first 4 bytes should holding // a CMD and end dest device short address (3 bytes) MiApp_FlushTx(); MiApp_WriteData(CMD_FORWRD_PACKET); MiApp_WriteData(END_DEVICES_Short_Address[select_ed].Address[0]);// sending the first byte payload as the destination nodes MiApp_WriteData(END_DEVICES_Short_Address[select_ed].Address[1]);// sending the first byte payload as the destination nodes MiApp_WriteData(END_DEVICES_Short_Address[select_ed].Address[2]);// sending the first byte payload as the destination nodes for (i = 4 ; i < 25 ; i++) { MiApp_WriteData(MiWi[(TxSynCount%6)][i-4]); } #if defined(ENABLE_SECURITY) sw_layer_ack_status = MiApp_UnicastStar (true); #else sw_layer_ack_status = MiApp_UnicastStar (false); #endif #if defined(ENABLE_APP_LAYER_ACK) if (sw_layer_ack_status) { TxNum++; // Tx was successful } else { LCDDisplay((char *)"Data_Sending_Fail!!", 0, false); } #else TxNum++; #endif } } // end of switch_val == 1 else if(switch_val == 2) { if (select_ed > end_nodes-1) { select_ed = 0; } else { select_ed = select_ed+1; } chk_sel_status = false; } // end of switch_val == 2 } // end of chk_sel_status } // end of updating the LCD info STAR_DEMO_OPTIONS_MESSAGE (false); } // end of actions on button press } // end of check for user inputs (button press check) t3 = MiWi_TickGet(); if((MiWi_TickGetDiff(t3,t2) > (ONE_SECOND * 40))) { awake = false; #if !defined(EIGHT_BIT_WIRELESS_BOARD) LCD_BacklightOFF(); #endif MiApp_TransceiverPowerState(POWER_STATE_SLEEP); LCDDisplay((char *)"Sleeping!!!", 0, false); DELAY_ms(1000); t1 = t3; } if (lost_connection && !role) { MiApp_EstablishConnection(0xFF, CONN_MODE_DIRECT); lost_connection = false; } } // end of actions performed while node is awake } // end of while(1) #endif }