Spring 2015 Program Code in RobotC
////////////////////////////////////////////////////////////////////////////////
//
// Menu System Primitives
//
////////////////////////////////////////////////////////////////////////////////
bool robotPaused;
#define MAX_MENU_ITEMS 30
typedef enum ProgramMenuItem { RED_FUNCTION, ORANGE_FUNCTION, YELLOW_FUNCTION, GREEN_FUNCTION, BLUE_FUNCTION, INDIGO_FUNCTION, VIOLET_FUNCTION, ULTRAVIOLET_FUNCTION, TERMINATOR_FUNCTION };
typedef struct genericMenuItem_struct {
string prompt;
ProgramMenuItem value;
} genericMenuItem;
typedef struct genericMenu_struct {
genericMenuItem items[MAX_MENU_ITEMS];
string name;
string header;
int lastMenuItem;
int scrollTopMenuItem;
int cursorAtMenuItem;
int selectedItem;
bool returnNow;
bool terminateNow;
} menuObj;
menuObj menu;
void newMenu(string *promptStr) {
menu.lastMenuItem=0;
menu.scrollTopMenuItem = 0;
menu.cursorAtMenuItem = 0;
menu.selectedItem = 0;
menu.name = *promptStr;
menu.header = "-- Missions --";
menu.returnNow = false;
menu.terminateNow = false;
}
void addMenuItem(string *promptStr, ProgramMenuItem retVal) {
menu.items[menu.lastMenuItem].prompt = *promptStr;
menu.items[menu.lastMenuItem].value = retVal;
menu.lastMenuItem++;
}
void showMenu() {
eraseDisplay();
menu.returnNow = false; // This has to be reset so that once a selection is made, the robot will execute it
robotPaused = false;
displayTextLine(0, "%s", menu.name);
displayTextLine(1, "%s", menu.header);
int cdLine = 0;
for(cdLine = 0; cdLine < 6; cdLine++) {
displayTextLine((cdLine+2), " %s", menu.items[cdLine+menu.scrollTopMenuItem].prompt);
}
}
void showCursor(int menuItemNum) {
int putOnLine = (menuItemNum - menu.scrollTopMenuItem) + 2;
displayTextLine(putOnLine, ">%s", menu.items[menuItemNum].prompt);
}
void adjustMenuItem(const bool bIncrement) {
if(bIncrement)
{
menu.cursorAtMenuItem = ++menu.cursorAtMenuItem % menu.lastMenuItem;
menu.scrollTopMenuItem = (menu.cursorAtMenuItem >= menu.scrollTopMenuItem+6) ? ++menu.scrollTopMenuItem: ((menu.cursorAtMenuItem==0)? 0: menu.scrollTopMenuItem);
}
else
{
menu.cursorAtMenuItem = --menu.cursorAtMenuItem % menu.lastMenuItem;
menu.cursorAtMenuItem = (menu.cursorAtMenuItem < 0) ? (menu.lastMenuItem-1): menu.cursorAtMenuItem;
menu.scrollTopMenuItem = (menu.cursorAtMenuItem < menu.scrollTopMenuItem) ? ((menu.scrollTopMenuItem>0)?--menu.scrollTopMenuItem : 0) : ((menu.cursorAtMenuItem >= menu.scrollTopMenuItem+6) ? menu.cursorAtMenuItem-5: menu.scrollTopMenuItem);
}
showMenu();
string tempString = "";
switch(menu.cursorAtMenuItem)
{
case 0: tempString = "Red.rso"; break;
case 1: tempString = "Orange.rso"; break;
case 2: tempString = "Yellow.rso"; break;
case 3: tempString = "Sorry.rso"; break;
}
playSoundFile(tempString);
showCursor(menu.cursorAtMenuItem);
sleep(200);
}
int startShowingTheMenu(int menuStartsAt)
{
playSoundFile("Hello.rso");
SensorType[S3] = sensorLightInactive; // Make sure you turn the light 'off'
SensorType[S2] = sensorLightInactive; // Make sure you turn the light 'off'
menu.cursorAtMenuItem = menuStartsAt;
showCursor(menu.cursorAtMenuItem);
sleep(1000);
robotPaused = true;
while(nNxtButtonPressed != kEnterButton) { // Wait Here
switch(nNxtButtonPressed)
{
case kLeftButton: adjustMenuItem(false); break;
case kEnterButton: displayCenteredBigTextLine(4, "Enter"); break;
case kRightButton: adjustMenuItem(true); break;
case kExitButton: displayCenteredBigTextLine(4, "Exit"); break;
}
sleep(100);
}
robotPaused = false;
SensorType[S3] = sensorLightActive;
SensorType[S2] = sensorLightActive;
playSoundFile("! Blips 07.rso");
sleep(1000);
SensorType[S3] = sensorLightInactive; // Make sure you turn the light 'off'
SensorType[S2] = sensorLightInactive; // Make sure you turn the light 'off'
eraseDisplay();
menu.returnNow = false; // This has to be reset so that once a selection is made, the robot will execute it
return menu.cursorAtMenuItem;
}
int startMenu() {
return startShowingTheMenu(0);
}
int startMenu(int menuStartsAt) {
return startShowingTheMenu(menuStartsAt - 1); // This is a zero offset parameter
}
////////////////////////////////////////////////////////////////////////////////
//
// Menu System Usage
//
////////////////////////////////////////////////////////////////////////////////
/*----
//The menu is stored in an array structure. The syntax for creating menu items is as follows:
menu[lastMenuItem] = addMenuItem("Calibrate Light", FUNCTION_1);
menu[lastMenuItem] = addMenuItem("Round the Track", FUNCTION_6);
menu[lastMenuItem] = addMenuItem("Drive To Center", FUNCTION_2);
showMenu(); // The menu is shown
selectedItem = startMenu(); // An item is selected
switch (menu[selectedItem]) // Now switch based on the menu value of the selected item
{
case FUNCTION_1: calibrate(); break; // With a break, only this function will be executed.
case FUNCTION_6: roundTrack(); // Without a break, every subsequent function will be executed.
case FUNCTION_2: toCenter();
case FUNCTION_3: locateOtherRobot();
case FUNCTION_4: crash();
}
-----*/