#include #include #include #include #include #include "Input String.h" #include "Arduino2HAL.h" #define TOKEN_WHITE_SPACE ":/\\_- \t" #define INPUT_STR_SIZE 64 #define RGB_STR_SIZE 32 typedef enum { no_input = 0, help_input, valid_input, address_input, mode_input, RGB_input, flame_input, flame_high_input_min, flame_high_input_max, flame_low_input_min, flame_low_input_max, hold_input, hold_high_input_min, hold_high_input_max, hold_low_input_min, hold_low_input_max, base_green_input, G2R_input, brightness_input, numColor_input, random_input, rotate_input, fade_input, fade2b_input, hr_input, min_input, sec_input } input_t; static char * StoreRBG(lamp_t * lamp, char * input_string, char * last_char); static void Store_next_num(lamp_t * lamp, char * tokenptr, input_t * last_input_type); static void Print_input_header(lamp_t * lamp); static void Print_help(void); int32_t Check4USB_message(lamp_t * lamp) { char input_str[INPUT_STR_SIZE + 1] = {' '}; //Number of max bytes in user input string + 1 for '\0' char * tokenptr; char * last_char; uint32_t input_str_lng; input_t last_input_type = no_input; if (NULL == fgets(input_str, INPUT_STR_SIZE + 1, stdin)) //Check for possible errors if (EOF != ferror(stdin)) //Check for error (note: if EOF due to no data strtok will return null and the while loop will not run) { printf("Input error:%d\n", ferror(stdin)); return -1; //return -1 for no new inputs } input_str_lng = strlen(input_str); if (INPUT_STR_SIZE <= input_str_lng) { printf("Too many inputs, max %d\n", INPUT_STR_SIZE); while (EOF != fgetc(stdin) ); //Flush input stream return -1; //return -1 for no new inputs } last_char = input_str + input_str_lng; tokenptr = strtok(input_str, TOKEN_WHITE_SPACE); //Tokenize string to separate out white space if (NULL == tokenptr) //if no data is in the input string return -1; //return -1 for no new inputs while (NULL != tokenptr) { //Loop until all tokens are gone if (',' == *tokenptr) tokenptr ++; switch (*tokenptr) //compare first char of the token to set where to store the data { case '(': //For RGB inputs //Get all of the data between '(' and ')' tokenptr = StoreRBG(lamp, tokenptr, last_char); //Finds the ')' and stores all of the num between tokenptr = strtok(tokenptr - 1, TOKEN_WHITE_SPACE); //Reset strtok() to start after ')' since all tokens between ( and ) have been entered last_input_type = RGB_input; break; case 'A': case 'a': last_input_type = address_input; break; case 'B': case 'b': last_input_type = brightness_input; break; case 'C': case 'c': last_input_type = hr_input; break; case 'D': case 'd': Set_default(); last_input_type = no_input; break; case 'F': case 'f': last_input_type = fade_input; break; case 'G': case 'g': last_input_type = G2R_input; break; case 'H': case 'h': Print_help(); strtok(last_char, ""); //Set strtok to be null on next call so help ends all inputs last_input_type = help_input; break; case 'K': case 'k': last_input_type = fade2b_input; case 'L': case 'l': last_input_type = flame_input; break; case 'M': case 'm': last_input_type = mode_input; break; case 'N': case 'n': last_input_type = numColor_input; break; case 'R': case 'r': last_input_type = random_input; break; case 'S': case 's': last_input_type = rotate_input; break; case 'T': case 't': last_input_type = hold_input; break; case 'V': case 'v': last_input_type = base_green_input; break; case ' ': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': //Do nothing for numbers break; default: printf("'%s'Invalid input, (H) for help.\n", tokenptr); last_input_type = no_input; break; } //Store the next number after the tokenptr char Store_next_num(lamp, tokenptr, &last_input_type); tokenptr = strtok(NULL, TOKEN_WHITE_SPACE); //Get the next token } Print_input_header(lamp); return lamp->addr; } void Store_next_num(lamp_t * lamp, char * tokenptr, input_t * last_input_type) { char * last_char; uint32_t new_num; new_num = strtol(tokenptr, &last_char, 10); //Get data if (last_char == tokenptr) //If no int was returned from strtol { new_num = strtol(tokenptr + 1, &last_char, 10); //check for data after defining char if (last_char == tokenptr + 1) //If no int was returned from strtol { //printf("'%s'Invalid input, (H) for help.\n", tokenptr); return; //Return without storing any data or changing input type } } switch (*last_input_type) { //Store valid lamp data and set last input case address_input: lamp->addr = new_num; *last_input_type = valid_input; break; case mode_input: lamp->mode = (lamp_mode_t) new_num; *last_input_type = valid_input; break; case flame_input: lamp->brightness_hi_min = new_num; *last_input_type = flame_high_input_min; break; case flame_high_input_min: lamp->brightness_hi_max = new_num; *last_input_type = flame_high_input_max; break; case flame_high_input_max: lamp->brightness_lo_min = new_num; *last_input_type = flame_low_input_min; break; case flame_low_input_min: lamp->brightness_lo_max = new_num; *last_input_type = valid_input; break; case hold_input: lamp->hold_hi_min = new_num; *last_input_type = hold_high_input_min; break; case hold_high_input_min: lamp->hold_hi_max = new_num; *last_input_type = hold_high_input_max; break; case hold_high_input_max: lamp->hold_lo_min = new_num; *last_input_type = hold_low_input_min; break; case hold_low_input_min: lamp->hold_lo_max = new_num; *last_input_type = valid_input; break; case base_green_input: lamp->base_green_value = new_num; *last_input_type = valid_input; case G2R_input: lamp->G2R_slope = new_num; *last_input_type = valid_input; break; case brightness_input: lamp->brightness = new_num; *last_input_type = valid_input; case numColor_input: lamp->num_of_colors = new_num; *last_input_type = valid_input; break; case random_input: lamp->randomize = new_num; *last_input_type = valid_input; break; case rotate_input: lamp->rotate_speed = new_num; *last_input_type = valid_input; break; case fade_input: lamp->fade_speed = new_num; *last_input_type = valid_input; break; case fade2b_input: lamp->fade_2_black = new_num; *last_input_type = valid_input; break; case hr_input: Set_hr(new_num); *last_input_type = min_input; break; case min_input: Set_min(new_num); *last_input_type = sec_input; break; case sec_input: Set_sec(new_num); *last_input_type = valid_input; break; case no_input: //do nothing case help_input: case RGB_input: break; case valid_input: printf("Missing identifier for number\n"); break; default: printf("Invalid input type"); break; } } char * StoreRBG(lamp_t *lamp, char * input_string, char * last_char) { char RGB_string[RGB_STR_SIZE]; char * tokenptr; char * num_check; char * RGB_end_char; uint32_t string_leng, new_leng, num_of_bytes = 0; uint32_t bytes[4]; //assert(input_string[0] == '('); RGB_end_char = (char *)memchr(input_string, ')', last_char - input_string); //Search entire input_string for end of RGB char ')' if (NULL == RGB_end_char) //If ')' was not found { //Send error message and exit printf("Missing ')', (H) for help.\n"); return last_char; //Return end of input string as end of RGB input } //Create a new string so the original doesnt get touched by strtok string_leng = RGB_end_char - input_string; //Set the length of the RGB string if (RGB_STR_SIZE <= string_leng) //If there are too many chars in input string { printf("Too many inputs, (H) for help\n"); return last_char; //Return end of input string as end of RGB input } memcpy(RGB_string, input_string, string_leng); //Copy to new string RGB_string[string_leng] = '\0'; //Add an ending char to the string new_leng = strlen(RGB_string); //Get length of new string if (new_leng != string_leng) //Check that the new string has no addtional '\0' chars from previous strtok calls RGB_string[new_leng] = ' '; //If there is a end of string char replace it with a space tokenptr = strtok(RGB_string, "()\t , :\\."); //Tokenize string to separate out white space and chars //Separate the string out and convert to int while (NULL != tokenptr) { if (4 == num_of_bytes) //If too many tokens are found { //Send error message and exit printf("Too many inputs, (H) for help\n"); return RGB_end_char; //Return end of input string as end of RGB input } bytes[num_of_bytes] = strtol(tokenptr, &num_check, 10); //Get data if (num_check == tokenptr) //If no int was returned from strtol { //Send error message and exit printf("Invalid input, (H) for help\n"); return last_char; //Return end of input string as end of RGB input } else num_of_bytes ++; tokenptr = strtok(NULL, "()\t , :\\."); //Tokenize string to separate out white space and chars } if (2 == num_of_bytes) //Too few arg { //Send error message printf("Too few inputs, (H) for help\n"); } else if (3 == num_of_bytes) //Store RGB data in array 0 { lamp->red[0] = bytes[0]; lamp->green[0] = bytes[1]; lamp->blue[0] = bytes[2]; } else if (4 == num_of_bytes) //Store RGB data and array info { lamp->red[bytes[0]] = bytes[1]; lamp->green[bytes[0]] = bytes[2]; lamp->blue[bytes[0]] = bytes[3]; } return RGB_end_char; } void Print_input_header(lamp_t * lamp) { uint32_t hour, min, sec; hour = Get_hr(); min = Get_min(); sec = Get_sec(); printf("Current Time:(C) %02d:%02d:%02d\t", hour, min, sec); printf("Current Values:(H) for help (D)to set default\n"); printf("Addr:(A)%03d\tMode:(M)%03d\t", lamp->addr, lamp->mode); switch (lamp->mode) { case lamp_solid: printf("Red, Green, Blue:(%03d,%03d,%03d)\n", lamp->red[0], lamp->green[0], lamp->blue[0]); break; case lamp_flicker: printf("Flame High Min,Max, Low Min, Max:(L)%03d,%03d, %03d,%03d\t", lamp->brightness_hi_min, lamp->brightness_hi_max, lamp->brightness_lo_min, lamp->brightness_lo_max); printf("Hold time High Min,Max, Low Min, Max:(T)%03d,%03d, %03d,%03d\t", lamp->hold_hi_min, lamp->hold_hi_max, lamp->hold_lo_min, lamp->hold_lo_max); printf("G2R Slope:(G)%03d\t", lamp->G2R_slope); printf("Base green value:(V)%03d\t", lamp->base_green_value); printf("Brightness:(B)%03d\n", lamp->brightness); break; case lamp_rotate: printf("Num of Colors:(N)%03d\t", lamp->num_of_colors); printf("Random:(R)%d\t", lamp->randomize); printf("Rotate Speed:(S)%03d\t", lamp->rotate_speed); printf("Fade Speed:(F)%03d\t", lamp->fade_speed); printf("Fade 2 Black:(K)%d\n", lamp->fade_2_black); for (uint32_t i = 0; i < lamp->num_of_colors; i++) printf("(%03d,%03d,%03d,%03d)\n", i, lamp->red[i], lamp->green[i], lamp->blue[i]); break; case lamp_off: default: printf("\n"); break; } } void Print_help(void) { printf("I'm sorry, Dave. I'm afraid I can't do that.\n\n"); }