Plymouth ELEC351 Group T
/
ELEC351_Group_T
FINAL PROJECT isn't it
Fork of ELEC351 by
Diff: main.cpp
- Revision:
- 24:7d2da96e05ad
- Parent:
- 23:3c85d7f657a2
- Child:
- 25:36699ed589ab
--- a/main.cpp Sat Dec 30 20:55:48 2017 +0000 +++ b/main.cpp Sat Dec 30 21:31:20 2017 +0000 @@ -12,8 +12,76 @@ */ //Includes and Definitions +#include "sample_hardware.hpp" +#include "Networkbits.hpp" +#include "rtos.h" +#include "LED.hpp" +#include "DATA.hpp" +#include "LCD.hpp" +#define SamplingTime 1 +#define NotSamplingTime 0 +#define Print_Time_to_LCD 1 +#define Dont_Print_Time_to_LCD 0 +#define TimerInterval 2 //This is in seconds +#define EDGE_RISEN 1 +#define EDGE_FALLEN 0 +#define mailsize 5 +#include <iostream> +Serial pc(USBTX, USBRX); +//SW1+SW2 are declared as interrupt ins in sample hardware.cpp -#include "SETUP.hpp" +void Serial_Comms(); + +//Thread IDs +osThreadId idMain; +osThreadId id1; +osThreadId id2; +osThreadId id3; +osThreadId id4; +osThreadId id5; + + +Timeout sw1TimeOut;//Used to prevent switch bounce + +LED Red_led(PE_15); +LED Yellow_led(PB_10); +LED Green_led(PB_11); + +LCD LCD(D8,D9,D4,A0,A1,A2,A3,D7,D6,D3,D1); +char LCD_buffer [50]; +int LCD_sprintf; + +//Tickers + +Ticker Sample_timer; + +//Threads +Thread t1; +Thread t2; +Thread t3; +Thread t4; +Thread t5; + +double temp = 0; +double pressure = 0; +double lux = 0; +string buffer_time = 0; + +char buffer[32]; +char scom_time_buffer[32]; +char msel_time_buffer[32]; + +void SW1FallingEdge(); +void SW1TimeOutHandler(); + + + +using namespace std; + +int mode = 0; + +Mail<DATA, mailsize> mail_box; //Mail Queue, Type DATA, Capacity mailsize(defined above), name mail_box + void Network() //Interrupt service routine for handling the timeout { osEvent evt_network = mail_box.get(); //Get the latest entry from "mail_box" @@ -29,7 +97,7 @@ msg_network.set_light(Rec_Data_Network->get_light()); //Copy light from mailbox to temporary instance mail_box.free(Rec_Data_Network); //Free space in the mailbox (delete earliest sample taken) - //networktest();//Run the network + networktest(); } } @@ -38,7 +106,7 @@ sw1TimeOut.detach(); //Stop the timeout counter firing SW1.fall(&SW1FallingEdge); //Now wait for a falling edge } -void SDWrite() +void SDWrite()//End of skype chat { osEvent evt_sd = mail_box.get(); //Get the latest entry from "mail_box" @@ -72,7 +140,7 @@ { while(1){ - Thread::wait(100);//Wait the thread + //Thread::wait(1000); osEvent evt_lcd = mail_box.get(); //Get the latest entry from "mail_box" @@ -91,7 +159,7 @@ { //Write new data to LCD (not fast!) - sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",msg_lcd.get_temperature(),msg_lcd.get_pressure(),msg_lcd.get_light());//Used for converting to a sting + /*LCD_sprintf = */sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",msg_lcd.get_temperature(),msg_lcd.get_pressure(),msg_lcd.get_light());//Used for converting to a sting //LCD.Display_Clear(); LCD.DDRAM_Address(0x00); @@ -110,7 +178,7 @@ time_t msel_time = msg_lcd.get_time(); //Declare local variable for time strftime(scom_time_buffer, 32, "%I:%M %p", localtime(&msel_time)); //Format time as a string LCD.Display_Clear(); - sprintf (LCD_buffer, "%s",scom_time_buffer); + LCD_sprintf = sprintf (LCD_buffer, "%s",scom_time_buffer); LCD.DDRAM_Address(0x00); LCD.Write_String("Current Time:"); LCD.DDRAM_Address(0x40); @@ -124,81 +192,56 @@ } } } - -void Check_Serial_Input() -{ - t6.signal_set(1); - while(1) - { - Thread::signal_wait(1); - Thread::wait(100);//Waits 100ms - cout << "In Check_Serial_Input" << endl; - cin >> Serial_Enter_Check;//Only run this if Seriak Commands input is finished - if (Serial_Enter_Check == "c") - { - t5.signal_set(SerialTime); - t2.signal_set(0); - //Run Serial Commands window and stop the printing of the last command - Serial_Enter_Check = "0"; - } - else - { - t6.signal_set(1); - } - } - -} void Serial_Commands() { + string Serial_Input; while(1) { - t5.signal_set(NotSerialTime); - Thread::signal_wait(SerialTime); - cout << "Please type in a Serial command" << endl; - //Mutex this - Sample_timer.detach(); - t6.signal_set(0); + cout << "Please type in a command" << endl; + cin >> Serial_Input; - t6.signal_set(1); + if (Serial_Input == "Test") { - cout << "Testing Serial Comms" <<endl; + cout << "Test Confirmed" << endl; } - else if(Serial_Input == "READALL") + else if(Serial_Input == "READ-ALL") { - Serial_timer.attach(&Serial_Comms_set,1); + cout << "Read All Confirmed" << endl; + t2.start(Serial_Comms); } - else if(Serial_Input == "DELETE_ALL") + else if(Serial_Input == "DELETE ALL") { } - else if(Serial_Input == "READ_<n>") + else if(Serial_Input == "READ <n>") { } - else if(Serial_Input == "DELETE_<n>") + else if(Serial_Input == "DELETE <n>") { } - else if(Serial_Input == "SETDATE_<dd>_<mm>_<yyyy>") + else if(Serial_Input == "SETDATE <dd> <mm> <yyyy>") { } - else if(Serial_Input == "SETT_<T>") + else if(Serial_Input == "SETT <T>") { } - else if(Serial_Input == "STATE_<x>") + else if(Serial_Input == "STATE <x>") { } - else if(Serial_Input == "LOGGING_<x>") + else if(Serial_Input == "LOGGING <x>") { } else if(Serial_Input == "HELP")// Use this to display all of the availble commands { - HELP(); + cout << "Avalible Commands are: "<< endl; + cout << "READ ALL , DELETE ALL READ<n> ,DELETE<n> , SETDATE<dd> <mm> <yyyy> , SETTIME <hh> <mm> <ss>, SETT <T> , STATE <x>, LOGGING <x>, HELP"<< endl; } else { @@ -206,15 +249,12 @@ } } } -void Serial_Comms()//Thread for Serial Communications //Alter this function +void Serial_Comms()//Thread for Serial Communications { - //cout << "In Serial Comms" << endl; + cout << "In Serial Comms" << endl; while(1) { - cout << "In Serial Comms" << endl; - Thread::signal_wait(SERIAL_COMMS); - //t2.signal_set(SERIAL_COMMS); - cout << "In Serial Comms 2" << endl; + Thread::signal_wait(1); osEvent evt_serial = mail_box.get(); //Get the latest entry from "mail_box" if (evt_serial.status == osEventMail) @@ -236,21 +276,15 @@ pc.printf("Pressure = %f\t", msg_serial.get_pressure()); //Print Pressure pc.printf("Light = %f\n\r", msg_serial.get_light()); //Print Light } - t2.signal_set(NotSERIAL_COMMS); Green_led.Toggle(); - //Thread::wait(1000); } } -void Serial_Comms_set() -{ - t2.signal_set(SERIAL_COMMS); -} - void Sample_signal_set() //Sets the Signal for when to sample the sensors { t1.signal_set(SamplingTime); //Set the sampling thread signal high + t2.signal_set(1); } void Sample()//Samples the hardware and prints the result to the LCD { @@ -288,6 +322,9 @@ } int main() { + + + //Greeting pc.printf("Test Start"); pc.printf("\n\r"); @@ -296,27 +333,19 @@ pc.baud(9600); //Sets the Serial Comms Baud Rate LCD.Initialise(); - LCD.DDRAM_Address(0x00); + LCD.DDRAM_Address(0x40); - post(); //Power on Self Test + + /*post(); //Power on Self Test //Initialise the SD card (this needs to move) if ( sd.init() != 0) { printf("Init failed \n"); - LCD.Display_Clear(); - LCD.Write_String("CANNOT INIT SD"); + lcd.cls(); + lcd.printf("CANNOT INIT SD"); //Change me errorCode(FATAL); } - - - - LCD.Display_Clear(); - sprintf (LCD_buffer, "%s",scom_time_buffer); - LCD.DDRAM_Address(0x00); - LCD.Write_String("Current Time:"); - LCD.DDRAM_Address(0x40); - LCD.Write_String(LCD_buffer); //Create a filing system for SD Card FATFileSystem fs("sd", &sd); @@ -324,33 +353,27 @@ FILE* fp = fopen("/sd/test.csv","a"); if (fp == NULL) { error("Could not open file for write\n"); - LCD.Display_Clear(); - LCD.Write_String("CANNOT OPEN FILE"); + lcd.cls(); + lcd.printf("CANNOT OPEN FILE\n\n");//Change me errorCode(FATAL); } //Last message before sampling begins - LCD.Display_Clear(); - LCD.Write_String("POST"); - LCD.DDRAM_Address(0x40); - LCD.Write_String("SUCCESSFUL"); - wait(1); - LCD.Display_Clear(); - LCD.Write_String("READY"); + lcd.cls();//change me + lcd.printf("READY\n\n");//change me - t5.signal_set(NotSerialTime); - //Run interrupts + */ + + //Run interrupt + Sample_timer.attach(&Sample_signal_set,TimerInterval); SW1.fall(&SW1FallingEdge); - Sample_timer.attach(&Sample_signal_set,TimerInterval); - //Run Threads - - //Thread t4(osPriorityAboveNormal);//Sets the Priority bigly + t1.start(Sample); - t2.start(Serial_Comms); t3.start(ModeSelection); - t4.start(Network); + //t4.start(Network); t5.start(Serial_Commands); - t6.start(Check_Serial_Input); + + //Main thread ID idMain = osThreadGetId(); //CMSIS RTOS call @@ -360,6 +383,7 @@ id2 = t2.gettid(); id3 = t3.gettid(); id4 = t4.gettid(); + id5 = t5.gettid(); //Toggle Green LED after a button has been pressed