innocent kusinza / Mbed OS mbed-os-example-mbed5-blinkyinnocent

Dependencies:   C12832 LM75B

main.cpp

Committer:
innocentkus
Date:
2020-12-01
Revision:
0:0ac1b1ae4b2f
Child:
1:b5d2f9051fc7

File content as of revision 0:0ac1b1ae4b2f:

#include "mbed.h"
#include <cstdio>
// main() runs in its own thread in the OS
#include "ThisThread.h"
#include "mbed.h"
#include "C12832.h"
#include <stdio.h>
#include "LM75B.h"
#include "platform/mbed_thread.h"
// this solution unsure every thread is accessing the file, the LCD and the LM78B at a diffrent time 
Thread t2;
Thread t1;
DigitalOut myled(LED1);
LocalFileSystem local("local");               // Create the local filesystem under the name "local"
C12832 lcd(p5, p7, p6, p8, p11);
LM75B sensor(p28, p27);
void thread_function(void const *arg){
    int sleepwait=atoi((char*)arg);
    ThisThread::sleep_for(sleepwait*3*1000);
    FILE *fp = fopen("/local/logtemp.txt", "w");  
    lcd.cls();

    float max=0,min=0,avg=0,current;int count=0;
    char buffer[200] ;
    fprintf(fp, "Temperature:\n");
    fclose(fp);
    while(1) {
        
        
        fp = fopen("/local/logtemp.txt", "a");
        printf("Thread ID%d\n",sleepwait);
        if (fp!=NULL){

            if (sensor.open()) {
                printf("Device detected!\n");
                printf("Temp = %.3f\n", sensor.temp());
                fprintf (fp,"%.3f\n",sensor.temp());
                
            } else {
                error("Device not detected!\n");
            }
            fclose (fp);            
          }
        
    
        fp = fopen("/local/logtemp.txt", "r");
        max=0;min=0;avg=0;count=0;
        if(fp) {
          while( fgets(buffer,200,fp) ) {
               if(count==0){
                    printf("reading from file %s\n",buffer);
                    count++;
                    continue;
                }
               // printf("Reading from file %s\n",buffer);
                count++;
                current=atof(buffer);
                if(current>=max)
                    max=current;
                if(current<=min)
                    min=current;
                avg+=current;
            }
            if(count==0)
                count=1;
            avg=avg/count;
            printf("Minimum Temp = %.3f\n", min);
            printf("Maximum Temp = %.3f\n", max);
            printf("Average Temp = %.3f\n", avg);
            lcd.locate(0,1);
            lcd.printf("Minimum Temp = %.3f", min);
            lcd.locate(0,10);
            lcd.printf("Maximum Temp = %.3f", max);
            lcd.locate(0,20);
            lcd.printf("Average Temp = %.3f", avg);
            fclose(fp);  
            fclose(fp);           
        }
       
        

        ThisThread::sleep_for(10000);
    }
   
}

int main() {
        t1.start(callback(thread_function,(void*)"2"));
        t2.start(callback(thread_function,(void*)"3"));
        thread_function((void*)"1");
        // this solution unsure every thread is accessing the file, the LCD and the LM78B at a diffrent time 


}