// Used to get the index of a condition or action for use // in the above arrays int GetArrayIndex( const char **strArray, const char *string ) { const char **i = strArray; while(*i) { if(StringsMatch( string, *i )) return i - strArray; // index ++i; } return NOT_FOUND; }
bool WiFly::WaitForResponse(char* compareBuffer, int timeout){ //Variables char* responseBuffer; boolean isReadComplete = false; boolean isCompareSuccess = false; int bufferPosition = 0; //Reset the buffer responseBuffer = (char*) malloc(RESPONSE_BUFFER_SIZE); memset (responseBuffer, '\0', RESPONSE_BUFFER_SIZE - 1); //Fill the buffer unsigned long startTime = millis(); char chResponse = 0; while(!isReadComplete) { //Start getting values if(uart.available()){ chResponse = uart.read(); if(bufferPosition < RESPONSE_BUFFER_SIZE - 1){ responseBuffer[bufferPosition]=chResponse; bufferPosition++; } else{ Serial.println("Buffer overflow!"); bufferPosition = 0; } } //Check for existence of the comparison string, or if timeout stop if(StringsMatch(responseBuffer,compareBuffer)) { isCompareSuccess = true; isReadComplete = true; } else if((millis()-startTime)>timeout) { isCompareSuccess = false; isReadComplete = true; } } Serial.println(responseBuffer); uart.flush(); return isCompareSuccess; }
// Adds events to the event list from a file void AddEventsFromFile( const char *fileName ) { char buffer[256]; FILE *fp = fopen( fileName, "r" ); Event *e; // Read first token fscanf( fp, "%s%*c", buffer ); if(fp) while(!feof( fp )) { // Start a new event if(StringsMatch( buffer, "Conditions" )) { // Malloc a new event e = (Event *)malloc( sizeof( Event ) ); e->actions = NULL; e->conditions = NULL; // Read in conditions for(;;) { int index; Condition *c; fscanf( fp, "%s%*c", buffer ); // Should never hit end of file while reading conditions if(feof( fp )) assert( NULL ); index = GetArrayIndex( ConditionArray, buffer ); // String in buffer did not match any conditions if(index == NOT_FOUND) break; c = (Condition *)malloc( sizeof( Condition ) ); // Insert condition into event c->next = e->conditions; e->conditions = c; c->cb = ConditionPtrs[index]; // Read parameters for condition ReadCParamPtrs[index]( fp, c ); // Set cleanup function c->clean = ConditionCleanups[index]; } } if(StringsMatch( buffer, "Actions" )) { // Read in actions for(;;) { int index; Action *a; fscanf( fp, "%s%*c", buffer ); // Break if we are at end of file if(feof( fp )) break; index = GetArrayIndex( ActionArray, buffer ); // String in buffer did not match any actions if(index == NOT_FOUND) break; a = (Action *)malloc( sizeof( Action ) ); // Insert action into event a->next = e->actions; e->actions = a; a->cb = ActionPtrs[index]; // Read parameters for action ReadAParamPtrs[index]( fp, a ); // Set cleanup function a->clean = ActionCleanups[index]; } // Insert event into event list EventListAdd( e ); } } }