// int dec2int(char c) // // Convert a character to its decimal equivalent. // // Inputs: // char c: a character to decode // // Return = the decimal value of character c // = -1 if the character is not a decimal character // int dec2int(char c) { if(c >= '0' && c <= '9') { // This is a valid character return(c-'0'); }else return(-1); }; // int get_dec(uint8_t num_digits) // // Read num_digits decimal digits from serial input (at most 4 digits). Returns after that // number of digits or after a non-decimal character has been read. Always eats // the one character following the number // // Inputs: // uint8_t num_digits: the maximum number of digits to read // // Return = the value that is read from the serial input // = 0 if the first character read from the serial input is non-decimal. // uint16_t get_dec(uint8_t num_digits) { char c; uint16_t out = 0; int val; uint8_t counter; // Only accept at most 4 digits if(num_digits > 4) num_digits = 4; // Loop until we receive the max digits for(counter = 0; counter < num_digits; ++counter) { c = getchar(); val = dec2int(c); if(val == -1) { // We have hit the end of the integer: return our value return(out); } out = (out * 10) + val; }; // We have read num_digits digits // Eat next char c = getchar(); // Return parsed value return(out); };