/* * Read arbitrary number of chars from FIFO. Must conform to * function definition that is passed to the function * sbp_process(). * Returns the number of characters successfully read. */ u32 fifo_read(u8 *buff, u32 n, void *context) { int i; for (i=0; i<n; i++) if (!fifo_read_char((char *)(buff + i))) break; return i; }
// get's a y or n response, writes non-0 to target if yes // returns OK/ABORT/TIMEOUT // abort only occurs if ESC is hit, in which case target is not modified static enum input_result_enum getyn(char* target) { enum input_result_enum success; char tempchar; char finished=FALSE; do { //wait for input or timeout time=INPUT_PROMPT_TIME/TIMER_TICK; while(time && (console_fifo_tx.empty == TRUE)) main_fly(); if(console_fifo_tx.empty == TRUE) { success=INPUT_RESULT_TIMEOUT; finished=TRUE; } else { tempchar=fifo_read_char(&console_fifo_tx); if(tempchar=='y' || tempchar=='Y') { if(console_echo) TEXTOUT_CHAR(tempchar); *target=TRUE; finished=TRUE; success=INPUT_RESULT_OK; } else if(tempchar=='n' || tempchar=='N') { if(console_echo) TEXTOUT_CHAR(tempchar); *target=FALSE; finished=TRUE; success=INPUT_RESULT_OK; } else if(tempchar==0x1B) { finished=TRUE; success=INPUT_RESULT_ABORTED; }; }; }while(!finished); TEXTOUT_P(PSTR("\r\n")); return success; }
// get's a line from input, terminates at CR, ignores outside 0x20-0x7E, writes it to *buffer // returns OK/ABORT/TIMEOUT, does NOT return INPUT_RESULT_NIL, as an empty line is a valid entry. // abort only occurs if ESC is hit, in which case buffer is terminated string of length 0 // a line input length of 0 is a valid entry (returns OK) // maxlen includes terminator, must be non0 // attempting to enter text longer than maxlen-1 will simply return a truncated string once CR is received // if echo is enabled chars will not be echo'd beyond maxlen-1 static enum input_result_enum getline(char* buffer, int maxlen) { enum input_result_enum success; int index=0; //indexes terminator in buffer char tempchar; char finished=FALSE; buffer[index]=0; do { //wait for input or timeout time=(INPUT_PROMPT_TIME/TIMER_TICK); while( time && (console_fifo_tx.empty == TRUE) ) main_fly(); if(console_fifo_tx.empty == TRUE) { buffer[0]=0; success=INPUT_RESULT_TIMEOUT; finished=TRUE; } else { tempchar=fifo_read_char(&console_fifo_tx); //abort (esc) if(tempchar==0x1B) { buffer[0]=0; success=INPUT_RESULT_ABORTED; finished=TRUE; } //backspace else if(tempchar==0x08 && index) { if(console_echo) TEXTOUT_P(PSTR("\x08 \x08")); buffer[--index]=0; } //text else if(0x1F < tempchar && tempchar < 0x7F) { if(index<maxlen-1) { if(console_echo) TEXTOUT_CHAR(tempchar); buffer[index++]=tempchar; buffer[index]=0; }; } //CR else if(tempchar=='\r') { finished=TRUE; success=INPUT_RESULT_OK; }; }; } while(!finished); if(console_echo) { TEXTOUT_P(PSTR("\r\n")); }; return success; }