Liam Grazier
/
Final351CW_FINAL
SEND
Fork of 0NicksCoursework_copywithserialtime by
Revision 10:098c2fa0a1a6, committed 2018-01-09
- Comitter:
- liam_grazier
- Date:
- Tue Jan 09 11:33:14 2018 +0000
- Parent:
- 9:e27b3f34de24
- Commit message:
- fin commented
Changed in this revision
--- a/Components/components.cpp Tue Jan 09 06:14:41 2018 +0000 +++ b/Components/components.cpp Tue Jan 09 11:33:14 2018 +0000 @@ -1,3 +1,7 @@ +/* ELEC351 COURSEWORK 2018 +DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL +LIAM GRAZIER // DOUG TILLEY // ALEX BARON + */ #include "mbed.h" #include "components.hpp" #include "lglcd.h" @@ -5,17 +9,22 @@ #include "stdio.h" #define RED_DONE 1 #define YELLOW_DONE 2 -//Digital outputs +//setup i/o DigitalIn onBoardSwitch(USER_BUTTON); DigitalOut onBoardLED(LED1); DigitalOut redLED(PE_15); DigitalOut yellowLED(PB_10); DigitalOut greenLED(PB_11); +DigitalIn SW1(PE_12); +DigitalIn SW2(PE_14); +AnalogIn adcIn(PA_0); +//sd block class init SDBlockDevice sd(PB_5, D12, D13, D10);// miso, sclk, cs +//setup mutex locks Mutex Lock1; Mutex Lock2; Mutex Remove; -//Inputs +//time setup start time_t rawtime; struct tm * timeinfo; int year, month ,day, hour, minute, second; @@ -23,115 +32,101 @@ char jtime[32]; char jdate[32]; char jdate1[32]; -DigitalIn SW1(PE_12); -DigitalIn SW2(PE_14); -//Serial pc(USBTX, USBRX); -AnalogIn adcIn(PA_0); -//Environmental Sensor driver +//time setup end +//temp,pres sensor setup. #ifdef BME BME280 sensor(D14, D15); #else BMP280 sensor(D14, D15); #endif +//lcd init from my class/ lglcd mylcd(D7,D6,D5,D4,D3,D2); - -//POWER ON SELF TEST -void post() +void runanalysis(void) //task gets data, converts and prints to lcd { - //posttest -} -void runanalysis(void){ - while(1){ -Lock1.lock(); -double temp = sensor.getTemperature(); -double pressure = sensor.getPressure(); -double lightin = adcIn; -char TEM[6]; -char PRE[5]; -char LIGHT[6]; -sprintf(TEM,"%.2f", temp); -sprintf(PRE,"%.2f", pressure); -sprintf(LIGHT,"%.2f", lightin); -mylcd.setline(1,1); -mylcd.write("L:"); -if(lightin > 0.7 && lightin < 0.9) -{ -mylcd.setline(1,4); - mylcd.write("|||||||||MAX"); -} -else if(lightin > 0.55 && lightin < 0.69) -{ -mylcd.setline(1,4); - mylcd.write("||||||| "); -} -else if(lightin > 0.5 && lightin < 0.54) -{ -mylcd.setline(1,4); - mylcd.write("||||| "); + while(1) + { + Lock1.lock(); + double temp = sensor.getTemperature(); //get temp + double pressure = sensor.getPressure(); //get pressure + double lightin = adcIn; //get light as a number 0-1 range + //storage for the above three lines for later conversion + char TEM[6]; + char PRE[5]; + char LIGHT[6]; + //conversion from double floats to a Chars + sprintf(TEM,"%.2f", temp); + sprintf(PRE,"%.2f", pressure); + sprintf(LIGHT,"%.2f", lightin); + mylcd.setline(1,1); + mylcd.write("L:"); + //below if's for determining light levels + if(lightin > 0.7 && lightin < 0.9) + { + mylcd.setline(1,4); + mylcd.write("|||||||||MAX"); + } + else if(lightin > 0.55 && lightin < 0.69) + { + mylcd.setline(1,4); + mylcd.write("||||||| "); + } + else if(lightin > 0.5 && lightin < 0.54) + { + mylcd.setline(1,4); + mylcd.write("||||| "); + } + else if(lightin > 0.4 && lightin < 0.54) + { + mylcd.setline(1,4); + mylcd.write("||| "); + } + else if(lightin > 0.3 && lightin < 0.39) + { + mylcd.setline(1,4); + mylcd.write("|| "); + } + else if(lightin > 0.06 && lightin < 0.29) + { + mylcd.setline(1,4); + mylcd.write("LOW LIGHT "); + } + else if(lightin < 0.05) + { + mylcd.setline(1,4); + mylcd.write(" "); + mylcd.setline(1,4); + mylcd.write("disconnected"); //LDR most likely connected + } + mylcd.setline(2,1); + mylcd.write("P:"); + mylcd.write(PRE); + mylcd.setline(2,10); + mylcd.write("T:"); + mylcd.write(TEM); + wait(0.01); + Lock1.unlock(); + Thread::signal_wait(SIG_READY); //singal thread triggered by ticker + } } -else if(lightin > 0.4 && lightin < 0.54) -{ -mylcd.setline(1,4); - mylcd.write("||| "); -} -else if(lightin > 0.3 && lightin < 0.39) -{ -mylcd.setline(1,4); - mylcd.write("|| "); -} -else if(lightin > 0.06 && lightin < 0.29) -{ -mylcd.setline(1,4); - mylcd.write("LOW LIGHT "); -} -else if(lightin < 0.05) -{ -mylcd.setline(1,4); -mylcd.write(" "); -mylcd.setline(1,4); -mylcd.write("disconnected"); -} -mylcd.setline(2,1); -mylcd.write("P:"); -mylcd.write(PRE); -mylcd.setline(2,10); -mylcd.write("T:"); -mylcd.write(TEM); -wait(0.01); -Lock1.unlock(); -Thread::signal_wait(SIG_READY); -} -} -void sdwrite(void) -{ +void sdwipe(void)//sd delete function +{ while(1) { Lock2.lock(); time_t seconds = time(NULL); +char prefix[4]; +char jdate2[32]; +strcpy(prefix,"/sd/"); strftime(jdate1, 32, "%F", localtime(&seconds)); strftime(jdate, 32, "%F\n\r", localtime(&seconds)); strftime(jtime, 32, "%X\n\r", localtime(&seconds)); +sprintf(jdate2,"%s%s",prefix,jdate1); FATFileSystem fs("sd", &sd); char filename[32]; char suffix[4]; -char prefix[4]; -strcpy(prefix,"/sd/"); strcpy(suffix,".txt"); -Lock1.lock(); -double P = sensor.getPressure(); -double L = adcIn; -double t = sensor.getTemperature(); -char tem[6]; -char pre[5]; -char light[6]; -char com[1]; -strcpy(com, ","); -sprintf(pre,"%.2f", P); -sprintf(tem,"%.2f",t); -sprintf(light,"%.2f\n\r", L); -sprintf(filename,"%s%s%s",prefix,jdate1,suffix); -Lock1.unlock(); -FILE* fp = fopen(filename,"a"); +sprintf(filename,"%s%s",jdate2,suffix); //calculates filename to be used using date +FILE* fp = fopen(filename,"w");//w overwrites file (deleteing) with a blank string if (fp == NULL) { errorCode(FATAL); printf("SD FAIL\n\r"); @@ -140,89 +135,147 @@ mylcd.write("SD FAIL"); } if (fp != NULL){ - //printf("SD Success\n\r"); - // fprintf(fp,"%s\n\r", "*C, mbar, light level 0-1 scale"); - fprintf(fp,"%s",jdate);//date - fprintf(fp,"%s",com); - fprintf(fp,"%s",jtime);//time - fprintf(fp,"%s",com); - fprintf(fp,"%s",pre); - fprintf(fp,"%s",com); - fprintf(fp,"%s",tem); - fprintf(fp,"%s",com); - fprintf(fp,"%s\n\r",light); + printf("ALL RECORDS DELETED FROM SD\n\r"); + fprintf(fp,"%s" "\n\r"); //blank string in for blank placehold wait(0.01); + break; } - fclose(fp); - Lock2.unlock(); - Thread::signal_wait(SIG_READY2); - + break; + fclose(fp); //close file } } -void sdrun(void) +void sdwrite(void) //write data to SD { -if ( sd.init() != 0) { -printf("Init failed\n\r"); -mylcd.clear(); -mylcd.setline(1,1); -mylcd.write("CANNOT INIT SD"); -errorCode(FATAL); -} -if( sd.init() == 0){ -printf("Init Success \n\r"); -mylcd.clear(); -mylcd.setline(1,1); -mylcd.write("SD GOOD MAN"); -wait(0.5); //flash the SD error / good code! -} + while(1) + { + Lock2.lock(); + time_t seconds = time(NULL); + char prefix[4]; + char jdate2[32]; + //conversions for time / floats to strings + strcpy(prefix,"/sd/"); + strftime(jdate1, 32, "%F", localtime(&seconds)); + strftime(jdate, 32, "%F\n\r", localtime(&seconds)); + strftime(jtime, 32, "%X\n\r", localtime(&seconds)); + sprintf(jdate2,"%s%s",prefix,jdate1); + FATFileSystem fs("sd", &sd); + Lock1.lock(); + //get data + double P = sensor.getPressure(); + double L = adcIn; + double t = sensor.getTemperature(); + //create datastores + char filename[32]; + char suffix[4]; + char tem[6]; + char pre[5]; + char light[6]; + char com[1]; + //convert to data store + strcpy(com, ","); + sprintf(pre,"%.2f", P); + sprintf(tem,"%.2f",t); + sprintf(light,"%.2f\n\r", L); + strcpy(suffix,".txt"); + Lock1.unlock(); + Lock1.lock(); + //calculate filename + sprintf(filename,"%s%s",jdate2,suffix); + Lock1.unlock(); + FILE* fp = fopen(filename,"a"); //open file + if (fp == NULL) //if sd file empty (NOT ABLE TO OPEN FILE) + { + errorCode(FATAL); + printf("SD FAIL\n\r"); + mylcd.clear(); + mylcd.setline(1,0); + mylcd.write("SD FAIL"); + } + if (fp != NULL) //if sd IS NOT empty (ABLE TO OPEN FILE) + { + char sdbuf[64];//buffer for write to SD + sprintf(sdbuf,"%s%s%s%s%s%s%s%s%s\n\r",jdate,com,jtime,com,pre,com,tem,com,light); //conversion of all data for print to char + fprintf(fp,"%s",sdbuf); //print the char converted above + wait(0.01);//small wait + } + fclose(fp); //close file + Lock2.unlock(); + Thread::signal_wait(SIG_READY2); //thread signal from ticket in main + } } -void sdcheck(void){ -if ( sd.init() != 0) { -printf("Init failed \n\r"); -mylcd.clear(); -mylcd.setline(1,1); -mylcd.write("CANNOT INIT SD\n\r"); -errorCode(FATAL); +void sdrun(void) //task for checking the SD card is In and init. +{ + if ( sd.init() != 0) //if sd not initaliseed + { + printf("Init failed\n\r"); + mylcd.clear(); + mylcd.setline(1,1); + mylcd.write("CANNOT INIT SD"); + errorCode(FATAL); + } + if( sd.init() == 0) //if sd is initalised (BLOCKING IF NOT AS NO RETURN FUNCTION) + { + printf("Init Success \n\r"); + mylcd.clear(); + mylcd.setline(1,1); + mylcd.write("SD GOOD MAN"); + wait(0.5); //flash the SD error / good code! + } } -if( sd.init() == 0){ -//printf("SD Good\n\r"); -} -} -void sdremove(void) +void sdcheck(void) //same as sdrun blocking but enables error codes also. { - while(1){ - Thread::signal_wait(SIG_REMOVE); - mylcd.clear(); - Remove.lock(); - sd.deinit(); - mylcd.clear(); - Lock1.lock(); - mylcd.setline(2,0); - greenLED = 1; - mylcd.write("R"); - Lock1.unlock(); - printf("SD REMOVED\n\r"); - errorCode(FATAL); - Remove.unlock(); + if(sd.init() != 0) + { + printf("Init failed \n\r"); + mylcd.clear(); + mylcd.setline(1,1); + mylcd.write("CANNOT INIT SD\n\r"); + errorCode(FATAL); + } + if( sd.init() == 0) + { + //printf("SD Good\n\r"); //used for debugging + } } +void sdremove(void) //sd card removal (triggered by interrupt) +{ + while(1) + { + Thread::signal_wait(SIG_REMOVE); //singal triggered by ticker in main + mylcd.clear(); + Remove.lock(); + sd.deinit(); //denit the sd (SAFE TO REMOVE) + mylcd.clear(); + Lock1.lock(); + mylcd.setline(2,0); + greenLED = 1; + mylcd.write("R"); //write a R character to empty placehold on the LCD indicating safe to remove. + Lock1.unlock(); + printf("SD REMOVED\n\r"); + errorCode(FATAL); //Fire LEDS to indicate + Remove.unlock(); + } } -void lcdstart(void){ +void lcdstart(void) //task for testing LCD on start of code. +{ mylcd.clear(); mylcd.setline(1,1); mylcd.write("INIT. SYSTEM"); mylcd.setline(2,1); mylcd.write("ELEC351"); - } -void errorCode(ELEC350_ERROR_CODE err) +} +void errorCode(ELEC350_ERROR_CODE err) //nicks error code from sample { - switch (err) { - case OK: + switch (err) + { + case OK: greenLED = 1; wait(1.0); greenLED = 0; return; - case FATAL: - while(1) { + case FATAL: + while(1) + { redLED = 1; wait(0.1); redLED = 0; @@ -230,78 +283,110 @@ } } } - -///dougs code - -void DispTime(void) +void DispTime(void) //gets time from epoc and converts and prints. { time_t Count = time(NULL); //Read the RTC Time printf("Current Time - %s\n\r", ctime(&Count)); //Print the current time } - -void setuptime(void) +void setuptime(void) //setupp and init time function { - /*Initialising the time for our program to easy edit*/ time ( &rawtime ); timeinfo = localtime ( &rawtime ); - - /*Setting the time to the deadline time*/ - /*Same as button Code*/ - //Set the initialisation time to: Tuesday 9th January 2018, 16:00:00. - //set_time(1515513600); - //Initialisation of the times. time ( &rawtime ); - timeinfo = localtime ( &rawtime ); - //Displays the initialisation time - DispTime(); - /*End the initialisation */ - + timeinfo = localtime ( &rawtime ); } - - void runtime(void){ +void runtime(void) //edit time function called in serial +{ while(1) //When added to the main code this will be changed to a while "SETDATE" { + /*promts the user to input which edit they would like*/ + printf ("What part do you want to edit? Time(T)/All(A).\n\r"); + fflush(stdout); + scanf ("%s",&input); + /*Switch case input*/ + switch(input) + { + case 'T': + //Sequential Entering, Hour, Minute, Second respectively + printf ("Enter hour:(00-23) \n\r"); + fflush(stdout); + scanf ("%d",&hour); + printf ("Enter minute:(00-59) \n\r"); + fflush(stdout); + scanf ("%d",&minute); + printf ("Enter second:(00-59) \n\r"); + fflush(stdout); + scanf ("%d",&second); + break; + /*Case A ----- All values Update sequence*/ + case 'A': + printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year); + printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month); + printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day); + printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour); + printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute); + printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second); + break; + /*default to reset ----- Month Update sequence*/ + default: + printf ("Invalid\n\r"); + set_time(1515513600); + } + /*Updating all the timings after the user has input all the data*/ + /*Put here as once the user has finished editing it does a batch update*/ + timeinfo->tm_year = year - 1900; + timeinfo->tm_mon = month - 1; + timeinfo->tm_mday = day; + timeinfo->tm_hour = hour; + timeinfo->tm_min = minute; + timeinfo->tm_sec = second; + time_t CurrTime = mktime(timeinfo); //Convert the to UNIX time + set_time(CurrTime); //Sets time using the UNIX time + DispTime(); + return; //Display the new time + } +} +void rundate(void)//setup date function +{ + while(1) //When added to the main code this will be changed to a while "SETDATE" + { /*promts the user to input which edit they would like*/ - printf ("What part do you want to edit? Time(T)/All(A).\n\r"); + printf ("What part do you want to edit? Date(D)/All(A).\n\r"); fflush(stdout); scanf ("%s",&input); /*Switch case input*/ switch(input) { - case 'T': - //Sequential Entering, Hour, Minute, Second respectively - printf ("Enter hour:(00-23) \n\r"); - fflush(stdout); - scanf ("%d",&hour); - - printf ("Enter minute:(00-59) \n\r"); - fflush(stdout); - scanf ("%d",&minute); - - printf ("Enter second:(00-59) \n\r"); - fflush(stdout); - scanf ("%d",&second); - - break; - - /*Case A ----- All values Update sequence*/ - case 'A': - printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year); - printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month); - printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day); - printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour); - printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute); - printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second); - break; - - /*default to reset ----- Month Update sequence*/ - default: - printf ("Invalid\n\r"); - set_time(1515513600); - } - /*Updating all the timings after the user has input all the data*/ - /*Put here as once the user has finished editing it does a batch update*/ + /*Case D ----- Date Update sequence*/ + case 'D': + //Sequential Entering, Day, Month, Year respectively + printf ("Enter day:(01-31) \n\r"); + fflush(stdout); + scanf ("%d",&day); + printf ("Enter month:(01-12) \n\r"); + fflush(stdout); + scanf ("%d",&month); + printf ("Enter year:(1970-9999) \n\r"); + fflush(stdout); + scanf ("%d",&year); + break; + /*Case A ----- All values Update sequence*/ + case 'A': + printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year); + printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month); + printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day); + printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour); + printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute); + printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second); + break; + /*default to reset ----- Month Update sequence*/ + default: + printf ("Invalid\n\r"); + set_time(1515513600); + } + /*Updating all the timings after the user has input all the data*/ + /*Put here as once the user has finished editing it does a batch update*/ timeinfo->tm_year = year - 1900; timeinfo->tm_mon = month - 1; timeinfo->tm_mday = day; @@ -313,57 +398,4 @@ DispTime(); return; //Display the new time } - } - -void rundate(void){ - while(1) //When added to the main code this will be changed to a while "SETDATE" - { - - /*promts the user to input which edit they would like*/ - printf ("What part do you want to edit? Date(D)/All(A).\n\r"); - fflush(stdout); - scanf ("%s",&input); - /*Switch case input*/ - switch(input) - { - /*Case D ----- Date Update sequence*/ - case 'D': - //Sequential Entering, Day, Month, Year respectively - printf ("Enter day:(01-31) \n\r"); - fflush(stdout); - scanf ("%d",&day); - printf ("Enter month:(01-12) \n\r"); - fflush(stdout); - scanf ("%d",&month); - printf ("Enter year:(1970-9999) \n\r"); - fflush(stdout); - scanf ("%d",&year); - break; - /*Case A ----- All values Update sequence*/ - case 'A': - printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year); - printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month); - printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day); - printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour); - printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute); - printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second); - break; - /*default to reset ----- Month Update sequence*/ - default: - printf ("Invalid\n\r"); - set_time(1515513600); - } - /*Updating all the timings after the user has input all the data*/ - /*Put here as once the user has finished editing it does a batch update*/ - timeinfo->tm_year = year - 1900; - timeinfo->tm_mon = month - 1; - timeinfo->tm_mday = day; - timeinfo->tm_hour = hour; - timeinfo->tm_min = minute; - timeinfo->tm_sec = second; - time_t CurrTime = mktime(timeinfo); //Convert the to UNIX time - set_time(CurrTime); //Sets time using the UNIX time - DispTime(); - return; //Display the new time - } - } \ No newline at end of file +} \ No newline at end of file
--- a/Components/components.hpp Tue Jan 09 06:14:41 2018 +0000 +++ b/Components/components.hpp Tue Jan 09 11:33:14 2018 +0000 @@ -1,5 +1,10 @@ -#ifndef __sample_hardware__ -#define __sample_hardware__ +/* ELEC351 COURSEWORK 2018 +DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL +LIAM GRAZIER // DOUG TILLEY // ALEX BARON + */ +#ifndef __components__ +#define __components__ +//defines for signals for threads #define SIG_READY 1 #define SIG_READY2 1 #define SIG_REMOVE 1 @@ -10,21 +15,23 @@ #else #include "BMP280.h" #endif +//sdcard includes #include "SDBlockDevice.h" #include "FATFileSystem.h" -//dougs -void DispTime(void); -void setuptime(void); -void runtime(void); -void rundate(void); -/// -void runanalysis(void); -void sdrun(void); -void sdcheck(void); -void lcdstart(void); -void sdwrite(void); -void sdremove(void); +//task voids +void DispTime(void);//print time to terminak +void setuptime(void); //task for init the background for time over serial +void runtime(void); //set time over serial +void rundate(void); //set date over serial +void runanalysis(void); //lcd printing/sensor store and refresh +void sdrun(void); //setup sd +void sdcheck(void); //check sd still init. +void lcdstart(void); //start the lcd/routine for start +void sdwrite(void); //write data to SD +void sdwipe(void); //wipe data from SD +void sdremove(void); //remove the SD card safely enum ELEC350_ERROR_CODE {OK, FATAL}; +//setup i/o extern DigitalOut onBoardLED; extern DigitalOut redLED; extern DigitalOut yellowLED; @@ -32,13 +39,11 @@ extern DigitalIn onBoardSwitch; extern DigitalIn SW1; extern DigitalIn SW2; -//extern Serial pc; extern AnalogIn adcIn; #ifdef BME extern BME280 sensor; #else extern BMP280 sensor; #endif -extern void post(); extern void errorCode(ELEC350_ERROR_CODE err); #endif \ No newline at end of file
--- a/LGLCD.lib Tue Jan 09 06:14:41 2018 +0000 +++ b/LGLCD.lib Tue Jan 09 11:33:14 2018 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/liam_grazier/code/LGLCDv2/#9020af47a312 +https://os.mbed.com/users/liam_grazier/code/LGLCD2/#d812a2a643bc
--- a/Network/network.cpp Tue Jan 09 06:14:41 2018 +0000 +++ b/Network/network.cpp Tue Jan 09 11:33:14 2018 +0000 @@ -1,3 +1,7 @@ +/* ELEC351 COURSEWORK 2018 +DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL +LIAM GRAZIER // DOUG TILLEY // ALEX BARON + */ #if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif @@ -5,13 +9,13 @@ #include "EthernetInterface.h" #include "TCPServer.h" #include "TCPSocket.h" - char realtime[32]; +char realtime[32]; //char for storing the RTC #include <iostream> #include <string> #include "BMP280.h" #include "components.hpp" #include "network.hpp" -Mutex Net; +Mutex Net; //mutex lock for the network. #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" #define HTTP_MESSAGE_BODY1 "" \ @@ -69,80 +73,75 @@ "\r\n" \ HTTP_MESSAGE_BODY "\r\n" -#define IP "10.0.0.10" -#define NETMASK "255.0.0.0" -#define GATEWAY "10.0.0.1" -void dispstralltime(){ +#define IP "10.0.0.10" //ipaddress +#define NETMASK "255.0.0.0" //subnetmask +#define GATEWAY "10.0.0.1" //defaultgateway +void dispstralltime() //function for getting time from the RTC +{ time_t seconds = time(NULL); - strftime(realtime, 32, "%c\n\r", localtime(&seconds)); - } + strftime(realtime, 32, "%c\n\r", localtime(&seconds)); //realtime value = yyyy/mm/dd/hh/mm/ss +} void networksend(void) { // interrupt routine setup Net.lock(); - printf("Network Enabled\n\r");//n + printf("Network Enabled\n\r");//here to show the user in terminal network is initalised //Configure an ethernet connection EthernetInterface eth; eth.set_network(IP, NETMASK, GATEWAY); eth.connect(); -// Net.unlock(); //Now setup a web server TCPServer srv; //TCP/IP Server TCPSocket clt_sock; //Socket for communication SocketAddress clt_addr; //Address of incoming connection - /* Open the server on ethernet stack */ srv.open(ð); - /* Bind the HTTP port (TCP 80) to the server */ srv.bind(eth.get_ip_address(), 80); - /* Can handle 5 simultaneous connections */ - srv.listen(5); - - while (true) { + srv.listen(5); + while (true) + { Net.lock(); using namespace std; //Block and wait on an incoming connection srv.accept(&clt_sock, &clt_addr); -// printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); - + //printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); double temp = sensor.getTemperature(); double pres = sensor.getPressure(); //Uses a C++ string to make it easier to concatinate string response; - //This is a C string + //Chars for storing the variables char adcIn_str[64]; char pres_str[64]; char temp_str[64]; - dispstralltime(); + dispstralltime(); //calltime //Read the LDR value + //declairing floats for storing data float u = adcIn; float b = pres; float a = temp; - - //Convert to a C String + //Converterting the floats to chars ready for print sprintf(adcIn_str, "%5.3f", u ); sprintf(pres_str, "%4.2f", b); sprintf(temp_str, "%3.1f", a); - //Build the C++ string response response += HTTP_TIME; - response += realtime; + response += realtime; //write time response += HTTP_MESSAGE_BODY1; - response += adcIn_str; + response += adcIn_str; //writeadc response += HTTP_Temperature1; - response += temp_str; + response += temp_str; //writetemp response += HTTP_Temperature2; response += HTTP_Pressure1; - response += pres_str; + response += pres_str; //writepressure response += HTTP_Pressure2; response += HTTP_MESSAGE_TIME; response += HTTP_MESSAGE_BODY2; //Send static HTML response (as a C string) clt_sock.send(response.c_str(), response.size()+6); Net.unlock(); - Thread::signal_wait(SIG_NET); + Thread::signal_wait(SIG_NET);//thead signal triggered by ticket in main. } }
--- a/Network/network.hpp Tue Jan 09 06:14:41 2018 +0000 +++ b/Network/network.hpp Tue Jan 09 11:33:14 2018 +0000 @@ -1,7 +1,11 @@ +/* ELEC351 COURSEWORK 2018 +DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL +LIAM GRAZIER // DOUG TILLEY // ALEX BARON + */ #ifndef __network__ #define __network__ -void networksend(void); -void dispstralltime(void); -#include "time.h" -#include "stdio.h" +void networksend(void); //task for setup,converting and printing sensor readings in html +void dispstralltime(void); //task for determining time from RTC +#include "time.h" //needed for the rtc +#include "stdio.h" #endif \ No newline at end of file
--- a/Time/DandTEdit_RTC_Fin.cpp Tue Jan 09 06:14:41 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ -///*DandTEdit_RTC_Fin.cpp*/ -///*RTC Timer serial inputs*/ -///*Version to get the input data*/ -// -///*Elec 351 Coursework Semester 2*/ -///* Designed and built by Doug Tilley, Liam Grazier, Alex Baron */ -///*http://www.cplusplus.com/reference/ctime/ - with reference to*/ -//#include "mbed.h" -//#include "stdio.h" /* IO Functions*/ -//#include "time.h" /* RTC Time functions*/ -//#include "DandTEdit_RTC_Fin.hpp" /*Headerfile*/ -// -// -///*Function to display the time*/ -// -////PROGRAM END
--- a/Time/DandTEdit_RTC_Fin.hpp Tue Jan 09 06:14:41 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -///*DandTEdit_RTC_Fin.hpp*/ -///*Header file for DandTEdit_RTC_Fin.cpp*/ -// -//#ifndef __DandTEdit_RTC_Fin__ //safeguards -//#define __DandTEdit_RTC_Fin__ -//// -//#include "mbed.h" -//#include <stdio.h> -//#include <time.h> -// -// //Setting up our time, structures and variables. -// -//#endif \ No newline at end of file
--- a/main.cpp Tue Jan 09 06:14:41 2018 +0000 +++ b/main.cpp Tue Jan 09 11:33:14 2018 +0000 @@ -1,69 +1,77 @@ +/* ELEC351 COURSEWORK 2018 +DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL +LIAM GRAZIER // DOUG TILLEY // ALEX BARON + */ #include "components.hpp" #include "mbed.h" #include "network.hpp" #include "lglcd.h" #include "stx.hpp" +//voids for ticker attached tasks +void inter1(); +void inter2(); +void inter3(); +void inter4(); +void inter5(); +//thread initalised with priorities Thread samplesThread(osPriorityNormal); Thread sdThread(osPriorityHigh); Thread sdRemoveThread(osPriorityHigh); Thread timebuttonThread(osPriorityNormal); Thread stxThread(osPriorityLow); Thread netThread(osPriorityRealtime); -Mutex SD; -void inter1(); -void inter2(); -void inter3(); -void inter4(); -void inter5(); -void inter6(); +//interrupt assign +InterruptIn sdex(USER_BUTTON); +//ticker assigns Ticker samples; Ticker remov; -InterruptIn sdex(USER_BUTTON); Ticker storage; Ticker netTick; Ticker serielTick; -Ticker buttonTick; -int T = 5; -void inter1(){ - sdcheck(); - samplesThread.signal_set(SIG_READY); +//sd card attached signal flag task +void inter1() +{ + sdcheck(); + samplesThread.signal_set(SIG_READY); } -void inter2(){ - sdThread.signal_set(SIG_READY2); +//sampling attached signal flag task +void inter2() +{ + sdThread.signal_set(SIG_READY2); } -void inter3(){ - //printf("CHECK REMOVE BUTTON\n\r"); - if(sdex == 1){ - //printf("BUTTON IN"); - sdRemoveThread.signal_set(SIG_REMOVE); - } - } - void inter4(){ - netThread.signal_set(SIG_NET); +//sd card remove attached signal flag task +void inter3() +{ + if(sdex == 1)//checks if remove button *USER BUTTON triggered by interrupt has been flagged to indicated remove + { + sdRemoveThread.signal_set(SIG_REMOVE); + } } -void inter5(){ - stxThread.signal_set(SIG_SX); +//network attached signal flag task +void inter4() +{ + netThread.signal_set(SIG_NET); } -//void inter6(){ -// timebuttonThread.signal_set(SIG_button); -//} -int main(){ -lglcd mylcd(D7,D6,D5,D4,D3,D2); -setuptime(); -printf("NEWIT"); -sdrun(); -lcdstart(); -welcomemsg(); -sdThread.start(sdwrite); -storage.attach(&inter2,T); //runs send to sd based on last number (10) in this case (every 10 seconds) -samplesThread.start(runanalysis); -samples.attach(&inter1,T); //runs analysis based on last number (1) in this case (every 1 second) -sdRemoveThread.start(sdremove); -remov.attach(&inter3,3);//checks sd remove key every 3 second -netThread.start(networksend); -netTick.attach(&inter4,0.01); -stxThread.start(useseriel); -serielTick.attach(&inter5,0.1); -//timebuttonThread.start(//insertnamehere); -//buttonTick.attach(&inter6,0.3); +//setial comms attached signal flag task +void inter5() +{ + stxThread.signal_set(SIG_SX); +} +int main() +{ + lglcd mylcd(D7,D6,D5,D4,D3,D2);//setup lcd + setuptime();//setup time to default + sdrun(); //init sd + lcdstart();//run lcd start code + welcomemsg(); //display welcome msg in the terminal + sdThread.start(sdwrite); + storage.attach(&inter2,1); //attached the storage SD ticker to the allocated flag task, this triggers every 1secs + samplesThread.start(runanalysis); + samples.attach(&inter1,1); //attached the samples ticker to the allocated flag task, this triggers every 1secs + sdRemoveThread.start(sdremove); + remov.attach(&inter3,3);//attached the SD Remove ticker to the allocated flag task, this triggers every 3secs + netThread.start(networksend); + netTick.attach(&inter4,0.01);//attached the network ticker to the allocated flag task, this triggers every 0.01secs + stxThread.start(useseriel);// + serielTick.attach(&inter5,0.1);//attached the serial ticker to the allocated flag task, this triggers every 0.1secs } \ No newline at end of file
--- a/serialtx/stx.cpp Tue Jan 09 06:14:41 2018 +0000 +++ b/serialtx/stx.cpp Tue Jan 09 11:33:14 2018 +0000 @@ -1,109 +1,130 @@ +/* ELEC351 COURSEWORK 2018 +DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL +LIAM GRAZIER // DOUG TILLEY // ALEX BARON + */ #include "mbed.h" #include "stx.hpp" #include "components.hpp" -Mutex Sx; -char buffer[255]; -int empty = 0; -void welcomemsg(void){ -printf("WELCOME TO ELEC351 ENVIRONMENTAL SERIAL INTERFACE\n\rFOR ASSISTANCE TYPE HELP\n\r"); +Mutex Sx; //mutex lock for serial tx (Sx) Thread +char buffer[255]; //buffer used for storing scanf data from serial rx +int empty = 0; //empty flag +void welcomemsg(void) //function for welcome msg for the main. +{ + printf("WELCOME TO ELEC351 ENVIRONMENTAL SERIAL INTERFACE\n\rFOR ASSISTANCE TYPE HELP\n\r"); //msg that is printed } -void datain(void){ - if(empty == 0){ - Sx.lock(); - scanf("%s", &buffer); - Sx.unlock(); - } +void datain(void) //function for scaning the serial rx input and writing to the Char Buffer +{ + if(empty == 0) + { + Sx.lock(); //locks to protect the scan + scanf("%s", &buffer); + Sx.unlock(); } -void printcommandlist(){ +} +void printcommandlist() //function for printing the command list to the terminal +{ Sx.lock(); printf("Command List:\n\r READALL\n\r DELETEALL\n\r SETDATE\n\r DISPLAYTIME\n\r SETTIME\n\r SETT\n\r STATEON(Sampling State)\n\r STATEOFF(Sampling State)\n\r LOGGINGON\n\r LOGGINGOFF\n\r COMMANDLIST\n\r" ); Sx.unlock(); - } -void readdata(){ - if (buffer != ""){ - if (strstr(buffer, "READALL")){ - readalldata(); - } - else if(strstr(buffer, "COMMANDLIST")){ - printcommandlist(); +} +void readdata() //function for deciding what to do with the data in the buffer +{ + if (buffer != "") + { + if (strstr(buffer, "READALL")) + { + printf("COMMAND NOT AVAILABLE\n\r");//command not coded yet } - else if(strstr(buffer, "DISPLAYTIME")){ - printcommandlist(); + else if(strstr(buffer, "COMMANDLIST")) //prints command list to terminal + { + printcommandlist(); + } + else if(strstr(buffer, "DISPLAYTIME")) //displays current RTC time via command + { + DispTime(); } - else if(strstr(buffer, "HELP")){ - help(); + else if(strstr(buffer, "HELP")) //calls help command + { + help(); } - else if(strstr(buffer, "DELETEALL")){ - deletealldata(); + else if(strstr(buffer, "DELETEALL"))//calls delete all records (working) + { + deletealldata(); } - else if(strstr(buffer, "SETDATE")){////////COME BACK HERE LATE - rundate(); + else if(strstr(buffer, "SETDATE")) //calls set date function (working) + { + rundate(); } - else if(strstr(buffer, "SETTIME")){////////COME BACK HERE LATE - runtime(); + else if(strstr(buffer, "SETTIME")) //calls set time function (WORKING) + { + runtime(); } - else if(strstr(buffer, "SETT")){////////COME BACK HERE LATE - setT(); + else if(strstr(buffer, "SETT")) + { + printf("ENTER SAMPLING RATE RANGLE 0.1<T<60\n\r"); + printf("COMMAND NOT AVAILABLE\n\r"); //command not coded yet } - else if(strstr(buffer, "STATEON")){ - stateon(); + else if(strstr(buffer, "STATEON")) + { + printf("COMMAND NOT AVAILABLE\n\r"); //command not coded yet } - else if(strstr(buffer, "STATEOFF")){ - stateoff(); + else if(strstr(buffer, "STATEOFF")) + { + printf("COMMAND NOT AVAILABLE\n\r"); //command not coded yet } - else if(strstr(buffer, "LOGGINGON")){ - loggingon(); + else if(strstr(buffer, "LOGGINGON")) + { + printf("IF YOU ARE SEEING THIS, NO ISSUES"); //command not coded yet, just a bit of a joke } - else if(strstr(buffer, "LOGGINGOFF")){ - loggingoff(); + else if(strstr(buffer, "LOGGINGOFF")) + { + printf("COMMAND NOT AVAILABLE\n\r"); //command not coded yet } else { - printf("UNRECOGNISED\n\r"); + printf("UNRECOGNISED\n\r"); //returns unrecognised for all comments not matching criteria above } - } - } -void help(){ - printf("HELP: \n\rFOR COMMAND LIST, type COMMANDLIST\n\r"); - } -void readalldata(){ + } +} +void help() //help command +{ + printf("HELP: \n\rFOR COMMAND LIST, type COMMANDLIST\n\r"); //prints this statement when HELP is typed +} +void readalldata() //command not coded (placeholder for code to be called from +{ printf("read all data\n\r"); - } //displays date, tim, temperature, presure, ligt -void deletealldata(){ - printf("delete all data \n\r"); - } //deletes all memory from th iternal memory -void setdate(){ - printf("Set Date\n\r"); - } //sets the day month and year -void settime(){ -printf("Set Time\n\r"); -} //sets the clock hours, minutes, seconds -void setT() +} +void deletealldata()//commadn for wiping the SD (WORKING) +{ + sdwipe(); +} +void setT()//command not coded (placeholder for code to be called from { printf("Set Sampling Period 'T'\n\r"); - } //sets the sampling period to <T> seconds -void stateon(){ +} +void stateon()//command not coded (placeholder for code to be called from +{ printf("Set Sampling ON\n\r"); - } //turns ampling ON and OFF -void stateoff(){ +} +void stateoff()//command not coded (placeholder for code to be called from +{ printf("Set Sampling OFF\n\r"); - } -void loggingon(){ - - printf("Logging On\n\r"); - } -void loggingoff(){ - - printf("Logging Off\n\r"); - } - //turns diagnostic logging ON and OFF -void useseriel(){ - - help(); - while(true){ - Thread::signal_wait(SIG_SX); - datain(); - readdata(); - } - } - \ No newline at end of file +} +void loggingon()//command not coded (placeholder for code to be called from +{ +printf("Logging On\n\r"); +} +void loggingoff() //command not coded (placeholder for code to be called from +{ +printf("Logging Off\n\r"); +} +void useseriel() +{ + help(); //prints the help command when serial init. (this prints in terminal) + while(true) + { + Thread::signal_wait(SIG_SX); //this signal triggers on ticker from main + datain(); //getdata + readdata(); //readata and decide what to do with it + } +}
--- a/serialtx/stx.hpp Tue Jan 09 06:14:41 2018 +0000 +++ b/serialtx/stx.hpp Tue Jan 09 11:33:14 2018 +0000 @@ -1,9 +1,13 @@ +/* ELEC351 COURSEWORK 2018 +DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL +LIAM GRAZIER // DOUG TILLEY // ALEX BARON + */ #ifndef __stx__ #define __stx__ -#define SIG_SX 1 -void welcomemsg(void); -void datain(); -void useseriel(); +#define SIG_SX 1 //thread signal for serial "SX" thread +void welcomemsg(void);//function for displayign welcome msg on serial terminal +void datain();//function for reading data in from the serial terminal +void useseriel(); //function for using the data in and analysising content void readalldata(); //displays date, tim, temperature, presure, ligt void deletealldata(); //deletes all memory from th iternal memory void setdate(); //sets the day month and year @@ -11,10 +15,10 @@ void setT(); //sets the sampling period to <T> seconds void stateon(); //turns ampling ON and OFF void stateoff(); //turns ampling ON and OFF -void help(); -void loggingon(); -void loggingoff(); //turns diagnostic logging ON and OFF -void deleten();//deletes the <n> records -void readdata(); -void printcommandlist(); +void help(); //shows help display on serial termninal +void loggingon(); //function for enable logging +void loggingoff(); //function for disable logging +void deleten();//function fordeletes the <n> records +void readdata(); //function for read data code +void printcommandlist();//function for printing command list to serial terminal #endif \ No newline at end of file