#include #include //=====================// // FUNCTION PROTOTYPES // //=====================// // keypad related functions char get_keypad_char( void ); char wait_keypad_letter( void ); int wait_keypad_number( void ); // LCD related functions void write_lcd( char *, int, int, int ); void write_menu( int ); // main menu functions void main_loop( void ); // for programmable directions capability void input_directions( void ); void execute_directions( void ); //=========// // GLOBALS // //=========// int cur_mode; char command_chars[10]; int command_nums[10]; int num_commands; int main( void ) { cur_mode = MODE_STOP; while( 1 ) main_loop(); // loop in main menu forever... return 0; } char wait_keypad_letter( void ) { char the_char = '0'; while( the_char == '0' ) { if( T2 && P1_7 ) the_char = 'A'; else if( T2_EX && P1_7 ) the_char = 'B'; else if( P1_2 && P1_7 ) the_char = 'C'; else if( P1_3 && P1_7 ) the_char = 'D'; else if( P1_3 && P1_6 ) the_char = USER_DONE; } // loop until the key is released while( P1 != 0 ) ; return the_char; } int wait_keypad_number( void ) { int the_num = 10; while( the_num == 10 ) { if( T2 && P1_4 ) the_num = 1; else if( T2 && P1_5 ) the_num = 2; else if( T2 && P1_6 ) the_num = 3; else if( T2_EX && P1_4 ) the_num = 4; else if( T2_EX && P1_5 ) the_num = 5; else if( T2_EX && P1_6 ) the_num = 6; else if( P1_2 && P1_4 ) the_num = 7; else if( P1_2 && P1_5 ) the_num = 8; else if( P1_2 && P1_6 ) the_num = 9; else if( P1_3 && P1_5 ) the_num = 0; else if( P1_3 && P1_6 ) // # key represents user finished programming directions the_num = USER_DONE; } // loop until the key is released while( P1 != 0 ) ; return the_num; } char get_keypad_char( void ) { char the_char='\0'; if( T2 && P1_4 ) the_char = '1'; else if( T2 && P1_5 ) the_char = '2'; else if( T2 && P1_6 ) the_char = '3'; else if( T2 && P1_7 ) the_char = 'A'; else if( T2_EX && P1_4 ) the_char = '4'; else if( T2_EX && P1_5 ) the_char = '5'; else if( T2_EX && P1_6 ) the_char = '6'; else if( T2_EX && P1_7 ) the_char = 'B'; else if( P1_2 && P1_4 ) the_char = '7'; else if( P1_2 && P1_5 ) the_char = '8'; else if( P1_2 && P1_6 ) the_char = '9'; else if( P1_2 && P1_7 ) the_char = 'C'; else if( P1_3 && P1_4 ) the_char = '*'; else if( P1_3 && P1_5 ) the_char = '0'; else if( P1_3 && P1_6 ) the_char = '#'; else if( P1_3 && P1_7 ) the_char = 'D'; // wait for the key to be released if it was pressed while( P1 != 0 ) ; return the_char; } void main_loop( void ) { int cur_line = 0; int the_char; write_menu( cur_line ); while( 1 ) { the_char = get_keypad_char(); switch( the_char ) { case 'A': cur_line++; if( cur_line > 7 ) cur_line = 0; write_menu( cur_line ); break; case 'B': if( cur_line <= 0 ) cur_line = 7; else cur_line--; write_menu( cur_line ); break; case '1': // input and execute the directions input_directions(); execute_directions(); // return to main menu cur_line = 0; write_menu( cur_line ); break; case '2': // RANDOM MOVEMENT break; case '3': // REMOTE CONTROL break; case '4': // FOLLOW PATH break; case '5': // STOP break; case '6': // ABOUT break; default: // no key pressed or invalid key break; } } } void write_menu( int line_no ) { switch( line_no ) { case 0: write_lcd( MENU1, 16, LINE1, CLEAR_LCD ); write_lcd( MENU2, 16, LINE1, DONT_CLEAR_LCD ); break; case 1: write_lcd( MENU2, 16, LINE1, CLEAR_LCD ); write_lcd( MENU3, 16, LINE1, DONT_CLEAR_LCD ); break; case 2: write_lcd( MENU3, 16, LINE1, CLEAR_LCD ); write_lcd( MENU4, 16, LINE1, DONT_CLEAR_LCD ); break; case 3: write_lcd( MENU4, 16, LINE1, CLEAR_LCD ); write_lcd( MENU5, 16, LINE1, DONT_CLEAR_LCD ); break; case 4: write_lcd( MENU5, 16, LINE1, CLEAR_LCD ); write_lcd( MENU6, 16, LINE1, DONT_CLEAR_LCD ); break; case 5: write_lcd( MENU6, 16, LINE1, CLEAR_LCD ); write_lcd( MENU7, 16, LINE1, DONT_CLEAR_LCD ); break; case 6: write_lcd( MENU7, 16, LINE1, CLEAR_LCD ); write_lcd( MENU8, 16, LINE1, DONT_CLEAR_LCD ); break; case 7: write_lcd( MENU8, 16, LINE1, CLEAR_LCD ); write_lcd( MENU1, 16, LINE1, DONT_CLEAR_LCD ); break; default: write_lcd( "Invalid Menu", 12, LINE1, CLEAR_LCD ); break; } } void input_directions( void ) { char com_str[16] = "X Seconds "; int command_num; int the_int; char the_char; write_lcd( PROG1, 16, LINE1, CLEAR_LCD ); for( command_num=0; command_num < 10; command_num++ ) { // wait for the user to press a letter // each letter corresponds to a directions the_char = wait_keypad_letter(); if( the_char == USER_DONE ) return; switch( the_char ) { case DIR_FORWARD: write_lcd( PROG2, 16, LINE1, CLEAR_LCD ); break; case DIR_REVERSE: write_lcd( PROG3, 16, LINE1, CLEAR_LCD ); break; case DIR_LEFT: write_lcd( PROG4, 16, LINE1, CLEAR_LCD ); break; case DIR_RIGHT: write_lcd( PROG5, 16, LINE1, CLEAR_LCD ); break; default: write_lcd( "Invalid Op", 10, LINE1, CLEAR_LCD ); break; } // wait for the user to press a single digit number the_int = wait_keypad_number(); if( the_int == USER_DONE ) return; // make the string say the right number com_str[0] = '0' + the_int; write_lcd( com_str, 16, LINE2, DONT_CLEAR_LCD ); command_chars[command_num] = the_char; command_nums[command_num] = the_int; num_commands = command_num; } } void execute_directions( void ) { int i; int command_len; char command_dir; for( i = 0; i < num_commands; i++ ) { command_len = command_nums[i]; command_dir = command_chars[i]; // INCOMPLETE. decided to axe the programmability mode at this point } } void write_lcd( char *string, int str_length, int line_no, int clear_lcd ) { int i; P2 = string[0]; // send one character at a time for( i=0; i