/** * decodeAndRunPending * @param cmd - command that will be executed * RETURN: none * INFO: Once a command is to be processed it is passed into this function and it is executed. * A switch statement is used in a similar function to the old version of the command parser * to call the command specified by the opcode. */ void decodeAndRunPending(seq_command_t cmd){ int16_t reformatResult; BYTE reformatMode = 1; journal_t journalTemp; switch(cmd.opcode){ case OP_START_SEQUENCE: //start sequence dprintf("Start sequence\r\n"); break; case OP_LOAD_RADIO_CONFIGURATION: //load radio config //store away a copy of the journal structure Journal_GetStruct(&journalTemp); journalTemp.radioConfigs = Global->csSequence.configs; Journal_SetStruct(&journalTemp); //Send a pointer to the radio configuration structure to Radio Code. radioConfig(&Global->csSequence.configs); dprintf("Radio Configuration\r\n"); break; case OP_RELOAD_RADIO_CONFIGURATION: //reload default radio configs //Send null to radio, indicating to load defualt values dprintf("Reload Radio Configuration\r\n"); Journal_GetStruct(&journalTemp); radioConfig(&journalTemp.radioConfigs); break; case OP_SET_SWITCH://set swith setPCASwitch(cmd.params.set_switch.pca_id,cmd.params.set_switch.config); dprintf("PCA %u configured as %u\r\n",cmd.params.set_switch.pca_id,cmd.params.set_switch.config); //cmd.params.set_switch.id; break; case OP_PROCESSOR_MODE: //processor mode setProcMode(cmd.params.set_proc_mode.mode); dprintf("Processor mode has been set.\r\n"); break; case OP_CHECK_SD_CARD: //check SD checkSDCard(); dprintf("SD card check complete.\r\n"); break; case OP_REFORMAT_SD: //reformat SD reformatResult = FSformat(reformatMode,0,NULL); if(!reformatResult){ dprintf("SD card reformatted successfully\r\n"); if (!Storage_Init()) { dprintf("Unable to init SD card. Error: %d\n", FSerror()); } } else{ dprintf("SD card reformat had an error: %d\n", FSerror()); } break; case OP_END_SEQUENCE://end sequence dprintf("End Sequence\r\n"); beaconMsgUpdateSingle(SOFTWARE_STATE,'C'); break; default: //uh oh dprintf("UNKNOWN PENDING COMMAND IN SEQUENCE\r\n"); break; } }
/* ************************************************************************* * FUNCTION NAME : voLoggerTask * * DESCRIPTION : Handles the temperatur logging to sd card. * * INPUT : None * * OUTPUT : None * * NOTE : * ************************************************************************* */ void voLoggerTask(void) { static enLoggerStateType senLoggerState= INIT; static enLoggerStateType senLoggerPrevState= INIT; static FSFILE *sLogFilePtr = NULL; static s8 ss8CurrentTMP=0; CETYPE errorCode; switch (senLoggerState) { /* * Init the sd card, goes to wait for card if no card is in socket. * if error ocured goes to error state, this is not implemented yet! */ case INIT: if (CARD_AVAILIBLE) { if (FSInit()) senLoggerState = DELAYINIT; else senLoggerState = ERROR; } else { senLoggerPrevState = senLoggerState; senLoggerState = WAIT_FOR_CARD; } break; /* * Load delay and goes to wait1sec state. */ case DELAYINIT: DISABLE_INTERRUPTS(mu16LoggerDelay = ONE_SEC_DELAY ); senLoggerState = WAIT1S; break; /* * delay for 1 sec. */ case WAIT1S: if (!mu16LoggerDelay) senLoggerState = STARTAD; break; /* * Start ADC sampling */ case STARTAD: AD1CHS = TMP_S; AD1CON1bits.SAMP = 1; senLoggerState = BUSYAD; break; /* * Wait until sampling is completed. */ case BUSYAD: if (AD1CON1bits.DONE) senLoggerState = STORE; break; /* * Rechecks that the sd card is in the socket, if so the adc value is * first run thrue a recursive filther then converted to degrees, * and added to sd card. */ case STORE: if (CARD_AVAILIBLE) { ss8CurrentTMP = ((u32)330*u16RecursiveFilther() - 51200)/1024; sLogFilePtr = FSfopen(LOG_FILENAME,APPEND); if (sLogFilePtr != NULL) { FSfprintf(sLogFilePtr,"%x-%x-%x %x:%x:%x %d \r\n", munTime.u8Year, munTime.u8Month, munTime.u8Day, munTime.u8Hr, munTime.u8Min, munTime.u8Sec, ss8CurrentTMP); if (FSfclose(sLogFilePtr) < 0) senLoggerState = ERROR; else senLoggerState = DELAYINIT; } else senLoggerState = ERROR; } else { senLoggerPrevState = senLoggerState; senLoggerState = WAIT_FOR_CARD; } break; /* * error handling should be inplemented here #todo */ case ERROR: errorCode = FSerror(); senLoggerState = INIT; break; case WAIT_FOR_CARD: if (CARD_AVAILIBLE) senLoggerState = INIT; break; default: senLoggerState = INIT; break; } }
//************************************************************************************** //*** //*** //*** //************************************************************************************** int ffferror(FILE *fin) { // Just returns the result of the last operation, // not related to <fin>... return FSerror(); }
int ErrorCheck(void) { int e; e = FSerror(); if(e < 1 || e > 33) return e; return ErrorThrow(ErrorMap[e]); }
/****************************************************************************** * Main Program Loop ******************************************************************************/ void main(void) { // SD Card variables FSFILE *csv_file; // The file to write to SearchRec rec; // The data structure to store the search results UINT8 i=0; char filename[] = "ACCEL00.CSV"; char file_number[2]; char attr0[] = "w"; char attr1[] = "a"; char headers[] = "X,Y,Z\n"; char rangeString[] = "range=00\n"; char buf[6] = ""; ADXL345_AXIS_DATA *temp; UINT8 range = 0x03; // The range to use the accelerometer with (initialize with a mask for the pins) boolean selfTest = FALSE; // Selftest enable flag for the accelerometer (initialize with false) /* * Initialize the program */ // TODO: Fix function prototype warning // Initialize the processor and peripherals config_proc(); Delay10KTCYx(4); i2c_init(); printf("\n\n\rProcessor Configuration Complete\n\r"); printf("Delaying for 5..."); Delay10KTCYx(FOSC); printf("4..."); Delay10KTCYx(FOSC); printf("3..."); Delay10KTCYx(FOSC); printf("2..."); Delay10KTCYx(FOSC); printf("1..."); Delay10KTCYx(FOSC); printf("0...\n\r"); // Initialize the SD Card and set a default clock printf("Initializing SD Card\n\r"); while (!FSInit()) { printf("Error Initializing SD Card: %d\n\r", FSerror()); } if (SetClockVars (2013, 1, 1, 12, 0, 00)) printf("Clock not set\n\r"); // It didn't work // Read the accelerometer range from the pins and make a string to say what it is range = range & ~( ( PORTAbits.RA0 << 1) | PORTAbits.RA1 ); printf("Determining range setting\n\r"); switch (range) { case ADXL345_RANGE_2G: rangeString[6] = '0'; rangeString[7] = '2'; break; case ADXL345_RANGE_4G: rangeString[6] = '0'; rangeString[7] = '4'; break; case ADXL345_RANGE_8G: rangeString[6] = '0'; rangeString[7] = '8'; break; case ADXL345_RANGE_16G: rangeString[6] = '1'; rangeString[7] = '6'; break; default: // Just leave the range as 00 to show an error condition has occured break; } printf("%s\r", rangeString); /* * Determine the filename to use for the new file. * The filename should be "accelx" where x is the next available integer. */ // This code section causes the PIC to crash for some reason, // It was used previosuly so I have no idea what is going on /* printf("Searching for files\n\r"); if (FindFirstpgm("accel*.csv", ATTR_MASK, &rec) == -1) { // No file was found so leave it at the default filename printf("No files found\n\r"); } else { printf("Files found\n\r"); // Files were found, so find the next unused number do{ file_number[0] = rec.filename[5]; file_number[1] = rec.filename[6]; } while (FindNext(&rec) == 0); if(file_number[1] == '9') { file_number[0]++; file_number[1] = '0'; } else { file_number[1]++; } filename[5] = file_number[0]; filename[6] = file_number[1]; } */ // Open the file using the above name printf("Opening file %s\n\r",filename); csv_file = FSfopen(filename, attr0); if (csv_file == NULL) printf("File not opened, error: %u\n\r",FSerror()); // Write a header to the file saying the current measurement range if (FSfwrite((void*)rangeString , 1, 9, csv_file) != 9) printf("Unable to write measurement range to the file\n\r"); // Write the column headers into the file if (FSfwrite((void *)headers, 1, 6, csv_file) != 6) printf("Unable to write headers to the file\n\r"); // Close the file if (FSfclose(csv_file)) printf("Unable to close file\n\r"); /* * Perform configuration on the accelerometer */ // Enable the selftest if neccessary if (PORTAbits.RA2 == 0) { selfTest = TRUE; } config_accel(range); printf("Accelerometer Configuration Complete\n\r"); // Main program loop while(1) { #ifdef PRINT_DEBUGS printf("Waiting for watermark\n\r"); #endif // Loop and do nothing while waiting for the watermark to be triggered while (ADXL345_INT1_PORT == 0){}; LATCbits.LATC1 = 1; // Turn on the LED to show we are processing data #ifdef PRINT_DEBUGS printf("Watermark Received\n\r"); #endif // Read in the data for(i=0; i<12; i++) { // Read the data adxl345_data_read(temp); #ifdef PRINT_DEBUGS printf("%i, %i, %i\n\r", temp->x, temp->y, temp->z); #endif // Convert the x-axis data into text padded_itoa(buf, temp->x); sendBuffer[(18*i)+0] = buf[0]; sendBuffer[(18*i)+1] = buf[1]; sendBuffer[(18*i)+2] = buf[2]; sendBuffer[(18*i)+3] = buf[3]; sendBuffer[(18*i)+4] = buf[4]; sendBuffer[(18*i)+5] = ','; // Convert the y-axis data into text padded_itoa(buf, temp->y); sendBuffer[(18*i)+6] = buf[0]; sendBuffer[(18*i)+7] = buf[1]; sendBuffer[(18*i)+8] = buf[2]; sendBuffer[(18*i)+9] = buf[3]; sendBuffer[(18*i)+10] = buf[4]; sendBuffer[(18*i)+11] = ','; // Convert the z-axis data into text padded_itoa(buf, temp->z); sendBuffer[(18*i)+12] = buf[0]; sendBuffer[(18*i)+13] = buf[1]; sendBuffer[(18*i)+14] = buf[2]; sendBuffer[(18*i)+15] = buf[3]; sendBuffer[(18*i)+16] = buf[4]; sendBuffer[(18*i)+17] = '\n'; } csv_file = FSfopen(filename, attr1); if (csv_file == NULL) printf("File not opened, error: %u\n\r",FSerror()); FSfwrite((void *) sendBuffer, 1, 216, csv_file); FSfclose(csv_file); LATCbits.LATC1 = 0; } }
/********************************************************************* * Transfer files to the client ********************************************************************/ static BOOL HTTP_SendFile(void) { ptrdiff_t read; size_t write; char *buffer; // First handle static file data, which is simply pushed through // with minimal processing if(!HTTP.dynamic) { write = TCPIsPutReady(HTTP.socket); while(write) { read = FSfreadbuf((void **) &buffer, write, HTTP.file); write -= TCPPutArray(HTTP.socket, (BYTE *) buffer, read); if(FSerror() != CE_GOOD) return true; } return false; } // Transfer data from the file to the socket while looking for escape // characters and substituting variable values while(read = FSfreadbuf((void **) &buffer, TCPIsPutReady(HTTP.socket), HTTP.file), read) { // Reserved output buffer space for (escaped) variable values enum { VARIABLE_SPACE = HTTP_MAX_DATA_LEN * 2 }; char *escape; // Look for the escape characters within the buffer while(escape = memchr((const char *) buffer, '%', read), escape) { char ch; // First flush all data up to the escape character read -= TCPPutArray(HTTP.socket, (BYTE *) buffer, escape - buffer); // Abort if there isn't enough output buffer space available if(TCPIsPutReady(HTTP.socket) < VARIABLE_SPACE) { FSfseek(HTTP.file, -read, SEEK_CUR); return false; } // Eat the escape character itself --read; buffer = escape + 1; // Read the variable name into a buffer HTTP_Buffer.write = HTTP_Data; do { if(read) --read; else if(!FSfreadbuf((void **) &buffer, 1, HTTP.file)) return true; ch = *buffer++; if(ch == '%') ch = '\0'; HTTP_PutBuf(ch); } while(ch); // Two escape characters in a row are used to emit the '%' // character itself if(HTTP_Buffer.write == &HTTP_Data[1]) TCPPut(HTTP.socket, '%'); // Write the variable's value with appropriate escape characters else { Cfg_SaveOne(Microchip_Settings, HTTP_Data, HTTP_WriteSocket); HTTP_WriteSocket(); } } // Write the remainder and check for EOF TCPPutArray(HTTP.socket, (BYTE *) buffer, read); if(FSerror() != CE_GOOD) return true; } // We're not done yet.. return false; }