V4
Dependencies: BMP280
Fork of Thread_Communication_V3 by
Diff: main.cpp
- Revision:
- 10:c10d1337d754
- Parent:
- 9:b838c5787ed7
- Child:
- 11:19135c83c208
--- a/main.cpp Sat Dec 30 15:12:09 2017 +0000 +++ b/main.cpp Sat Dec 30 18:55:22 2017 +0000 @@ -44,6 +44,7 @@ volatile double PRES = 0; volatile double TEMP = 0; +volatile int sampleTime = 15; // int to hold current switch state int userButtonState = FallingEdge; @@ -82,6 +83,7 @@ pc.attach(&Rx_interrupt, Serial::RxIrq); POST(); + pc.printf("\n\n\nType HELP for list of available Commands\n\n\n\r"); _serialCMD.start(serialCMD); _PrintLCD.start(PrintLCD); _sensorRead.start(sensorRead); @@ -90,8 +92,7 @@ _Network1.start(Network1); userButton.rise(&userButtonRise); - read.attach(readISR, SAMPLING_PERIOD); - + read.attach(&readISR, sampleTime); while (1) { Yellow_ext = ON; @@ -101,6 +102,7 @@ }// End While } // End Main /*--------------------------------------------------------------------*/ +/*-----------------Circular Buffer------------------------------------*/ void circBuff () { while(1) { @@ -141,9 +143,9 @@ } //Unlock data buffer DataBuffer.unlock(); - } -} - + }// End While +}// End Circular buffer +/*-------------------------------------------------------------*/ /*---------------------Read Sensors ---------------------------*/ void readISR () { // Ticker interrupt defined in main @@ -177,7 +179,7 @@ Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt } } - +/*--------------------------------------------------------------------*/ /*--------------------------------LCD---------------------------------*/ void PrintLCD () { @@ -205,21 +207,21 @@ mail_box.free(mail); } - lcd.Write("Light Level:"); + lcd.Write("Light:"); lcd.RowSelect(1); lcd.Write(lightString); i++; break; } case 1: - lcd.Write("Temperature:"); + lcd.Write("Temperature(C):"); lcd.RowSelect(1); lcd.Write(tempString); i++; break; case 2: - lcd.Write("Pressure:"); + lcd.Write("Pressure(mBar):"); lcd.RowSelect(1); lcd.Write(pressString); i =0; @@ -410,6 +412,10 @@ /*----SETT----------------------------------*/ else if(strstr(rx_buffer, "SETT")){ pc.puts(" SETT\n\r"); + // read.detach(); + // sampleTime = atoi(rx_buffer); + // pc.printf("Sample Time %d\n\r", sampleTime); + // read.attach(&readISR, sampleTime); } /*----STATE----------------------------------*/ else if(strstr(rx_buffer, "STATE")){ @@ -419,6 +425,15 @@ else if(strstr(rx_buffer, "LOGGING")){ pc.puts(" LOGGING\n\r"); } +/*----HELP--------------------------------------*/ + else if (strstr(rx_buffer, "HELP")) { + pc.puts("\n\n\nCurrently Available Commands:\n\r"); + pc.puts("\tREAD n - Read n previous samples\n\r"); + pc.puts("\tREAD ALL - Read All previous samples held in memory\n\r"); + pc.puts("\tSETTIME hh:mm::ss - Set time in 24hr format\n\r"); + pc.puts("\tSETDATE dd/mm/yyyy - Set time in specified format\n\r"); + pc.puts("\tDELETE ALL - Delete all sampled held in internal memory\n\n\n\r"); + } /*----ERROR---*/ else{ pc.puts("####ERROR####\n\r"); @@ -435,7 +450,160 @@ } } /*------------------------------------------------*/ +/*---------------SD THread------------------------*/ +void writeRemove_SD() { + + while(1) { + Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal + int sd_state = sdIn; + switch (sd_state) { + case 1: + pc.printf("SD Card not inserted!\n\r"); + pc.printf("Insert SD Card and press User button again\n\r"); + break; + default: + pc.printf("This should never happen\n\r"); + break; + case 0: + pc.printf("Initalising SD Card\n\r"); + //check init + if (sd.init() != 0) { + pc.printf(" ERROR - SD card failed to initialise.\n\rRestart board\n\r"); + } + + // Create Filing system for SD Card + FATFileSystem fs("sd", &sd); + + //OpenFiles to write/append to + pc.printf("Writing to SDC\n\r"); + + FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "w"); //"w" to overwrite file ftb + + // Check for error in opening file + if (fp == NULL) { + pc.printf("*****ERROR - Could not open file for write*****\n\r"); + } + //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) + //Lock data buffer + DataBuffer.lock(); + dataLock.lock(); + //Print all samples to SD + for(int n=data_t; n<=MAX_SAMPLES; n++) { + fputs(data_buffer[n], fp); + fprintf(fp, "\n\r"); + } + if(data_t>data_h) { + for(int n=0; n<=(data_t-1); n++) { + fputs(data_buffer[n], fp); + + } + } + + //Lock data buffer + DataBuffer.unlock(); + dataLock.unlock(); + //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); + + fclose(fp); + + pc.printf("Write Sucessful!\n\r"); + + sd.deinit(); + pc.printf("SD Card Ready to Remove\n\r"); + Green_ext = 1; + Thread::wait(500); + Green_ext = 0; + Thread::wait(500); + Green_ext = 1; + Thread::wait(500); + Green_ext = 0; + Thread::wait(500); + Green_ext = 1; + Thread::wait(500); + Green_ext = 0; + Thread::wait(500); + Green_ext = 1; + Thread::wait(500); + Green_ext = 0; + Thread::wait(500); + }//End Switch + }// End While +}// End Thread +/*--------------------------------------------------------------------------*/ + +/*---------------------------Networking Thread------------------------------*/ +void Network1 () { + + printf("Setting up server\n\r"); + + //Configure an ethernet connection + EthernetInterface eth; + eth.set_network(IP, NETMASK, GATEWAY); + eth.connect(); + printf("The target IP address is '%s'\n\r", eth.get_ip_address()); + + //Now setup a web server + TCPServer srv; //TCP/IP Server + + 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) { + + TCPSocket clt_sock; //Socket for communication + using namespace std; + //Block and wait on an incoming connection + srv.accept(&clt_sock, &clt_addr); + //printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port()); + + //Uses a C++ string to make it easier to concatinate + string response; + string strL = "LDR:"; + string strP = ", Pressure(mBar): "; + string strT = ", Temp(C): "; + //This is a C string + char l_str[64]; + char p_str[64]; + char t_str[64]; + + //Read the LDR value + dataLock.lock(); + float L = LDR ; + float T = TEMP; + float P = PRES; + dataLock.unlock(); + + //Convert to a C String + sprintf(l_str, "%1.3f", L ); + sprintf(t_str, "%2.2f", T); + sprintf(p_str, "%4.2f", P); + + + //Build the C++ string response + response = HTTP_MESSAGE_BODY1; + // response += strL; + response += l_str; + response += strT; + response += t_str; + response += strP; + response += p_str; + response += HTTP_MESSAGE_BODY2; + + //Send static HTML response (as a C string) + clt_sock.send(response.c_str(), response.size()+6); + + } +} +/*---------------------------POST--------------------------------------------*/ void POST () { pc.printf(" ALL Leds should be flashing\n\r"); @@ -476,144 +644,6 @@ lcd.RowSelect(2); lcd.Write("******TEST******"); wait(1); - lcd.Clear(); + lcd.Clear(); + pc.printf("Basic POST Pass\n\r"); } - -void writeRemove_SD() { - - while(1) { - Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal - pc.printf("Initalising SD Card\n\r"); - - //check init - if (sd.init() != 0) { - pc.printf("******SD Initialise FAIL*******\n\r"); - } - - // Create Filing system for SD Card - FATFileSystem fs("sd", &sd); - - //OpenFiles to write/append to - pc.printf("Writing to SDC\n\r"); - - FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "w"); //"w" to overwrite file ftb - - // Check for error in opening file - if (fp == NULL) { - pc.printf("*****ERROR - Could not open file for write*****\n\r"); - } - //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) - //Lock data buffer - DataBuffer.lock(); - dataLock.lock(); - //Print all samples to SD - for(int n=data_t; n<=MAX_SAMPLES; n++){ - fputs(data_buffer[n], fp); - fprintf(fp, "\n\r"); - } - if(data_t>data_h){ - for(int n=0; n<=(data_t-1); n++){ - fputs(data_buffer[n], fp); - - } - } - - //Lock data buffer - DataBuffer.unlock(); - dataLock.unlock(); - //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); - - fclose(fp); - - pc.printf("Write Sucessful!\n\r"); - - sd.deinit(); - pc.printf("SD Card Ready to Remove\n\r"); - Green_ext = 1; - Thread::wait(500); - Green_ext = 0; - Thread::wait(500); - Green_ext = 1; - Thread::wait(500); - Green_ext = 0; - Thread::wait(500); - Green_ext = 1; - Thread::wait(500); - Green_ext = 0; - Thread::wait(500); - Green_ext = 1; - Thread::wait(500); - Green_ext = 0; - Thread::wait(500); - }// End While -}// End Thread - -void Network1 () { - - printf("Basic HTTP server example\n"); - - //Configure an ethernet connection - EthernetInterface eth; - eth.set_network(IP, NETMASK, GATEWAY); - eth.connect(); - printf("The target IP address is '%s'\n", eth.get_ip_address()); - - //Now setup a web server - TCPServer srv; //TCP/IP Server - - 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) { - - TCPSocket clt_sock; //Socket for communication - using namespace std; - //Block and wait on an incoming connection - srv.accept(&clt_sock, &clt_addr); - printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port()); - - //Uses a C++ string to make it easier to concatinate - string response; - string strL = "LDR:"; - string strP = ", Pressure(mBar): "; - string strT = ", Temp(C): "; - //This is a C string - char l_str[64]; - char p_str[64]; - char t_str[64]; - - //Read the LDR value - dataLock.lock(); - float L = LDR ; - float T = TEMP; - float P = PRES; - dataLock.unlock(); - - //Convert to a C String - sprintf(l_str, "%1.3f", L ); - sprintf(t_str, "%2.2f", T); - sprintf(p_str, "%4.2f", P); - - - //Build the C++ string response - response = HTTP_MESSAGE_BODY1; - // response += strL; - response += l_str; - response += strT; - response += t_str; - response += strP; - response += p_str; - response += HTTP_MESSAGE_BODY2; - - //Send static HTML response (as a C string) - clt_sock.send(response.c_str(), response.size()+6); - } -}