Latest
Dependencies: serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice
Diff: main.cpp
- Revision:
- 28:7fccaef8fa72
- Parent:
- 27:bb8d4c883e1b
- Child:
- 29:806e9281af2f
diff -r bb8d4c883e1b -r 7fccaef8fa72 main.cpp --- a/main.cpp Fri Dec 21 09:59:24 2018 +0000 +++ b/main.cpp Sat Dec 22 21:15:49 2018 +0000 @@ -19,13 +19,14 @@ //Global variables -unsigned int newestIndex = 0; //First time it is incremented, it will be 0 -unsigned int oldestIndex = 0; +unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0 +unsigned int oldestIndex = BUFFERSIZE-1; FILE* fp; FATFileSystem* fs; //Shared mutable variables VOLATILE bool sd_init; //is it? +bool logging = false; float sample_rate = 15; //is it? struct tm* timeData; char cmdBuffer[256]; @@ -34,7 +35,7 @@ //Thread synchronisation primatives Semaphore spaceAvailable(BUFFERSIZE); -Semaphore samplesInBuffer(0); +Semaphore samplesInBuffer(0, BUFFERSIZE); Mutex bufferLock; //Queues @@ -81,7 +82,10 @@ //TEST FOR BUFFER CHANGES -unsigned int saveIndex = 0; +unsigned int saveIndex = BUFFERSIZE-1; + +int32_t Nspaces = BUFFERSIZE; +int32_t Nsamples; int main() { @@ -105,9 +109,9 @@ //TEST FOR SD CARD SDmount_thread.start(SDmount); - + //Attach ISRs - sample.attach(&sampleISR, sample_rate); + sample.attach(&sampleISR, sample_rate); //Allow sampling to start pc->attach(serialISR, Serial::RxIrq); //TEST FOR SD CARD MOUNT AND UNMOUNT @@ -143,8 +147,9 @@ cmdBuffer[i] = pc->getc(); if (cmdBuffer[i] == '\r') { - i = 0; - serialqueue.call(serialterm); + cmdBuffer[i+1]==NULL; + serialqueue.call(serialterm); + i = 0; } else i++; } @@ -158,33 +163,42 @@ { //High priority thread Thread::signal_wait(TAKE_SAMPLE); - //int32_t Nspaces = spaceAvailable.wait(); //Blocking if space is not available //Dont check if there is space available becuase we are overwriting + Nspaces = spaceAvailable.wait(0); //Non-blocking + bufferLock.lock(); + //Update buffer + newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR - bufferLock.lock(); - //Update buffer + if (newestIndex == oldestIndex) + { + oldestIndex = (oldestIndex+1) % BUFFERSIZE; + } + buffer[newestIndex].updatetemp(sensor.getTemperature()); buffer[newestIndex].updatepress(sensor.getPressure()); buffer[newestIndex].updatelight(adcIn.read()); buffer[newestIndex].updateTime(); - newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR //bufferLock.unlock(); //normally here, moved due to updating queues. + + if (Nspaces != 0) + { + Nspaces--; + } + samplesInBuffer.release(); - - if (newestIndex == oldestIndex) - { - oldestIndex = (oldestIndex+1) % BUFFERSIZE; - } //Pass onto queues - LCDqueue.call(LCD_display, buffer[newestIndex-1].gettemp(),buffer[newestIndex-1].getpress(),buffer[newestIndex-1].getlight()); + LCDqueue.call(LCD_display, buffer[newestIndex].gettemp(),buffer[newestIndex].getpress(),buffer[newestIndex].getlight()); + + if(logging) + { + printlock.lock(); + pc->printf("%s: Sample placed in buffer at position %d\r\n", buffer[newestIndex].getTime(), newestIndex); + pc->printf("Number of spaces available in buffer:%d\r\n\n",Nspaces); + printlock.unlock(); + } + bufferLock.unlock(); - if (newestIndex == ((saveIndex + 60) % BUFFERSIZE)) //correct? - { - //save to SD card - consumer_thread.signal_set(STORE_DATA); - - } } } @@ -192,53 +206,61 @@ { while(true) { - - static time_t seconds; + static time_t seconds; //possibly move into if(sd_init) + //write to the SD card from oldestindex up to newestIndex. - //write to the SD card from oldestindex up to newestIndex. - Thread::signal_wait(STORE_DATA); - redLED = 1; + Nsamples = samplesInBuffer.wait(); //Block if no samples to take - acquires - char fileDate[30]; - timeLock.lock(); - seconds = time(NULL); - timeData = localtime(&seconds); - set_time(mktime(timeData)); - strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData); - timeLock.unlock(); - fp = fopen(fileDate,"a"); - if (fp == NULL) - { - printlock.lock(); - pc->printf("WARNING: SD card could not be updated\n\r"); - printlock.unlock(); - } + if (sd_init) + { + char fileDate[30]; + timeLock.lock(); + seconds = time(NULL); + timeData = localtime(&seconds); + + //set_time(mktime(timeData)); + + strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData); + timeLock.unlock(); + + fp = fopen(fileDate,"a"); + if (fp == NULL) + { + printlock.lock(); + pc->printf("WARNING: SD card could not be updated\r\n\n"); + sd_init = false; + printlock.unlock(); + samplesInBuffer.release(); + } + else + { + //Nested locks probably a bad idea! + bufferLock.lock(); + SDlock.lock(); + oldestIndex = (oldestIndex+1) % BUFFERSIZE; + fprintf(fp,"%s,%5.2f,%5.2f,%5.2f\r", buffer[oldestIndex].getTime(), buffer[oldestIndex].gettemp(), buffer[oldestIndex].getpress(), buffer[oldestIndex].getlight()); + SDlock.unlock(); + + if(logging) + { + printlock.lock(); + pc->printf("Log file %s updated with sample from position %d in buffer\r\n",fileDate,oldestIndex); + pc->printf("newestIndex position %d\r\n",newestIndex); + pc->printf("oldestIndex position %d\r\n",oldestIndex); + printlock.unlock(); + } + + bufferLock.unlock(); + fclose(fp); + } + redLED = 0; + } else { - //Nested locks probably a bad idea! - bufferLock.lock(); - SDlock.lock(); - //read first then update oldestIndex? - int i = 0; - - while (saveIndex != newestIndex) - { - fprintf(fp,"%s,%5.2f,%5.2f,%5.2f\n\r", buffer[saveIndex].getTime(), buffer[saveIndex].gettemp(), buffer[saveIndex].getpress(), buffer[saveIndex].getlight()); - saveIndex = (saveIndex+1) % BUFFERSIZE; - i++; - } - SDlock.unlock(); - bufferLock.unlock(); - - printlock.lock(); - pc->printf("SD card updated with %d samples\r\n", i); - pc->printf("oldestIndex : %d\n\r", oldestIndex); - pc->printf("newestIndex : %d\n\r", newestIndex); - pc->printf("saveIndex : %d\n\r", saveIndex); - printlock.unlock(); - } - fclose(fp); - redLED = 0; + samplesInBuffer.release(); + } + + } }