Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years ago.
Writing to file with sleep
I am making a temperature monitoring system that displays on a nokia 5110 lcd using a tmp102 temperature sensor. It works well but an issue arises when using sleep along with tickers, specifically when writing a string to a LOG.txt file with sleep enabled it gets stuck just before writing to the log file. It works fine without sleep at the moment.
#include "mbed.h" #include "N5110.h" #include "TMP102.h" #include "RTC.h" #include "FILEWRITE.h" #define TMP102_R_ADD 0x91 #define TMP102_W_ADD 0x90 #define N5110_SCREEN_WIDTH 84 #define N5110_SCREEN_HEIGHT 48 void startLogging(); void stopLogging(); void writeTemperatureToFile(); void displayTemperature(); Ticker writeToFile; Ticker DisplayT; N5110 n5510Lcd(p7,p8,p9,p10,p11,p13,p21); //BusIn buttonInput_bus(p17,p16); DigitalIn A(p17); BusOut ledBus(LED1,LED2,LED3,LED4); TMP102 temperature(TMP102_W_ADD, TMP102_R_ADD); FILEWRITE fileWrite; RTC rtClock(1); bool sleepMode = 0; InterruptIn logSwitch(p18); InterruptIn wakeScreen(p17); int main() { n5510Lcd.init(); n5510Lcd.normalMode(); n5510Lcd.setBrightness(0); temperature.init(); //float temp; logSwitch.rise(&startLogging); logSwitch.fall(&stopLogging); wakeScreen.rise(&displayTemperature); //separate problem //DisplayT.attach(&displayTemperature,7); while(1) { sleep(); } } void startLogging() { n5510Lcd.clear(); n5510Lcd.setBrightness(0.5); writeToFile.attach(&writeTemperatureToFile, 5.0); // THIS IS WHERE THE PROBLEM IS!!! n5510Lcd.clear(); n5510Lcd.printString("LOGGING",0,2); n5510Lcd.printString("ENABLED",42,3); wait(2); n5510Lcd.clear(); n5510Lcd.setBrightness(0); } void stopLogging() { n5510Lcd.clear(); n5510Lcd.setBrightness(0.5); writeToFile.detach(); n5510Lcd.clear(); n5510Lcd.printString("LOGGING",0,2); n5510Lcd.printString("DISABLED",36,3); wait(2); n5510Lcd.clear(); n5510Lcd.setBrightness(0); } void writeTemperatureToFile() { sleepMode = 0; char logTemperatureString[6]; char logTimeString[20]; char logFullString[26]; temperature.getTemperatureString(logTemperatureString, 0); rtClock.getLocalTime(logTimeString); snprintf(logFullString, 26, "%s, %s\n\r", logTimeString, logTemperatureString); fileWrite.writeDataToFile(logFullString); sleepMode = 1; } void displayTemperature() { char temperatureString[6]; char lcdStringBuffer[15]; n5510Lcd.clear(); n5510Lcd.setBrightness(0.5); //Print to lcd temperature in degrees celcius temperature.getTemperatureString(temperatureString,0); sprintf(lcdStringBuffer, "Temp(C): %s", temperatureString); n5510Lcd.printString(lcdStringBuffer,0,0); //Print to lcd temperature in degrees farenheit temperature.getTemperatureString(temperatureString,1); sprintf(lcdStringBuffer, "Temp(F): %s", temperatureString); n5510Lcd.printString(lcdStringBuffer,0,1); wait(2); n5510Lcd.clear(); n5510Lcd.setBrightness(0); }
The filewrite class I created:
#include "FILEWRITE.h" LocalFileSystem local("local"); // create local filesystem FILE *fp; BusOut fileLeds(LED4,LED3,LED2,LED1); FILEWRITE::FILEWRITE() { // fileLeds = 15; // turn on LEDs for feedback // fp = fopen("/local/log.txt", "a"); // open 'log.txt' for appending // if the file doesn't exist it is created, if it exists, data is appended to the end //fprintf(fp,"SESSION START:\n"); // print initial string to file //fclose(fp); // close file //fileLeds = 0; // turn off LEDs to signify file access has finished } void FILEWRITE::writeDataToFile(char fileData[]) { fileLeds = 15; // turn on LEDs for feedback //IT GETS STUCK HERE!!!!!!!!!!!!! fp = fopen("/local/log.txt", "a"); // open 'log.txt' for appending // if the file doesn't exist it is created, if it exists, data is appended to the end fprintf(fp,"%s\n",fileData); // print string to file fclose(fp); // close file fileLeds = 0; // turn off LEDs to signify file access has finished }