// TerminalMenuManager // Control routine for operating the menu. // // Parameters: None // Generate menu // output menu // prompt for valid selection // do action of selection // until done. int Menu::TerminalMenuManager() { char* pMenuBuffer = NULL; boolean bDone = false; int iMenuResponse, iActionID; while (!bDone) { pMenuBuffer = (char*) malloc(150); GenerateMenu(pMenuBuffer, 150,true); Serial << endl << pMenuBuffer; free(pMenuBuffer); gBuffer[0] = GetMenuResponse( GetBuffer_P(STI_MENU_PROMPT_1000, gBuffer, ENTRY_FIELD_SIZE) ,9); gBuffer[1] = '\0'; iMenuResponse = atoi(gBuffer); iActionID = paMenuActions[iMenuResponse]; PerformAction(iActionID); if (iCurrentMenu < MENU_MAIN ) { bDone = true; } } }
void WiFlySerial::reboot() { char szCommand[SMALL_COMMAND_BUFFER_SIZE]; DEBUG_LOG(1, "Entered softwareReboot"); for (int retryCount = 0; retryCount < SOFTWARE_REBOOT_RETRY_ATTEMPTS; retryCount++) { // TODO: Have the post-boot delay here rather than in enterCommandMode()? if (!enterCommandMode(isAfterBoot)) { return false; // If the included retries have failed we give up } uart->println("reboot"); // For some reason the full "*Reboot*" message doesn't always // seem to be received so we look for the later "*READY*" message instead. // TODO: Extract information from boot? e.g. version and MAC address if (findInResponse("*READY*", 2000)) { return true; } } return false; GetBuffer_P(STI_WIFLYDEVICE_REBOOT, szCommand, SMALL_COMMAND_BUFFER_SIZE); // DebugPrint(szCommand); if (!SendCommandSimple( szCommand , WiFlyFixedPrompts[WIFLY_MSG_AOK] )) { DebugPrint( GetBuffer_P(STI_WIFLYDEVICE_ERR_START_FAIL, szCommand, SMALL_COMMAND_BUFFER_SIZE)); while (1) {}; // Hang. TODO: Handle differently? } }
// GenerateMenu // Forms a menu for display on terminal or web at CurrentMenu level. // // Menu format: // Menu Title // 0. Return to prior menu // 1. Menu Item 1. // // Parameters: // pMenuBuffer Buffer for menu // iBufSize size of buffer // bTerminal true = terminal, false = html // // Returns true on success, false on failure boolean Menu::GenerateMenu(char* pMenuBuffer, int iBufSize, boolean bTerminal) { int iMenuCounter = 0; byte aCurrentMenuProperties[N_MENU_ELEMENTS]; int iMenuRecord = 0; boolean bDone = false; boolean bCollecting = false; memset (pMenuBuffer, '\0', iBufSize); while ( (iMenuRecord < N_MENU_RECORDS) && (bDone == false) ) { GetMenuProperties( iMenuRecord, aCurrentMenuProperties ); // gone past last entry for desired menu? if ( bCollecting && (aCurrentMenuProperties[MENU_ID] != iCurrentMenu) ) { bDone = true; } if ( aCurrentMenuProperties[MENU_ID] == iCurrentMenu ) { // Get menu text GetBuffer_P( aCurrentMenuProperties[MENU_STRING_IDX], gBuffer, ENTRY_FIELD_SIZE); // check for Menu Title if ( aCurrentMenuProperties[MENU_ACTION] == MENU_ACTION_NONE ) { strcat ( pMenuBuffer, " "); } else { // if web add html etc itoa( iMenuCounter, strchr(pMenuBuffer, '\0'), 10); strcat (pMenuBuffer, ".\t"); paMenuActions[iMenuCounter] = aCurrentMenuProperties[MENU_ACTION]; iMenuCounter++; } // add text strcat ( pMenuBuffer, gBuffer ); strcat ( pMenuBuffer, "\r\n"); bCollecting = true; } // if a menu we want iMenuRecord++; } // while return true; }
// GetMenuResponse // Prompt for menu response: a number less than maxOptions. // waits for single character, checks for validity. char Menu::GetMenuResponse( char* Prompt, const byte maxOptions) { boolean bDone = false; char Response; char inChar[6]; // Show prompt Serial.print(Prompt); memset(inChar,'\0',6); // read a single character line while (!bDone) { if (Serial.available()) { inChar[0] = (char)Serial.read(); Serial.print(inChar); // Accept only digits at this time if (inChar[0] <= (char) ( maxOptions + (byte) '0') && inChar[0] >='0' ) { Response = inChar[0]; bDone = true; } else { //beep Serial.println(GetBuffer_P(STI_MESSAGE_1000, gBuffer, ENTRY_FIELD_SIZE)); } } else { delay (1000); } } // if not done Serial.println(); // clear out anything else Serial.flush(); return Response; }
// PerformAction // Executes action sequence for given iActionID // Add menu actions into this 'big switch'. // // // // Parameters: // iActionID ActionID as listed above // // SIDE EFFECTS: // Globals affected // iCurrentMenu Current menu level // // Returns: // true on success, false on failures. boolean Menu::PerformAction( int iActionID) { boolean bOk = false; switch ( iActionID ) { case MENU_ACTION_NONE: // do nothing break; case MENU_ACTION_EXIT: // return to prior menu iCurrentMenu--; break; case MENU_ACTION_MEMORY_REPORT: // Show available system memory Serial << "Memory Available: " << freeMemory() << endl; break; case MENU_ACTION_SUBMENU_ALPHA: // Alpha sub-menu iCurrentMenu = MENU_ALPHA; break; case MENU_ACTION_SUBMENU_BETA: // Beta sub-menu iCurrentMenu = MENU_BETA; break; case MENU_ACTION_SAMPLE_I: // Sample action I Serial << "Sample Menu Action I" << endl; break; case MENU_ACTION_SAMPLE_II: // Sample action I Serial << "Presenting Sample Menu Action II" << endl; break; case MENU_ACTION_EXIT_ALPHA_MENU: // Return to main menu... or another. iCurrentMenu = MENU_MAIN; break; case MENU_ACTION_EXIT_BETA_MENU: // Return to main menu, or some other menu. iCurrentMenu = MENU_MAIN; break; case MENU_ACTION_CHG_SETTING: // Change a setting. GetBuffer_P(STI_MENU_RESPONSE_PROMPT_1000, gPrompt, ENTRY_FIELD_SIZE); strcat( gPrompt, mySetting); strcat( gPrompt, "]"); GetPromptResponse( gPrompt, gBuffer, ENTRY_FIELD_SIZE); if ( strlen(gBuffer) > 0 ) { strncpy( mySetting, gBuffer, ENTRY_FIELD_SIZE); } break; case MENU_ACTION_STRING_ENTRY: // Change a setting. strcpy( gPrompt, "Enter a string:"); GetPromptResponse( gPrompt, gBuffer, ENTRY_FIELD_SIZE); Serial << "Thanks for the string " << gBuffer << endl; break; default: Serial << "???" << endl; break; } return bOk; }