elec 350 courcework by oscar simons

Dependencies:   BMP280

Fork of Task617-mbedos by University of Plymouth - Stages 1, 2 and 3

Committer:
jkfadsjk
Date:
Tue Jan 09 19:38:56 2018 +0000
Revision:
8:eb72f789f912
Parent:
7:3d054c1a26bf
Oscar Simons courcework;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:f916cefba2f4 1 #include "mbed.h"
noutram 0:f916cefba2f4 2 #include "rtos.h"
noutram 2:70084af839d3 3 #include "string.h"
noutram 2:70084af839d3 4 #include <stdio.h>
noutram 2:70084af839d3 5 #include <ctype.h>
jkfadsjk 8:eb72f789f912 6 #include "TextLCD.h"
jkfadsjk 8:eb72f789f912 7 #include "SDBlockDevice.h"
jkfadsjk 8:eb72f789f912 8 #include "FATFileSystem.h"
jkfadsjk 8:eb72f789f912 9 #include "sample_hardware.hpp"
jkfadsjk 8:eb72f789f912 10 BusOut binaryOutput(A2, A3, A4);
noutram 0:f916cefba2f4 11
jkfadsjk 8:eb72f789f912 12 //DigitalIn onBoardSwitch(USER_BUTTON);
jkfadsjk 8:eb72f789f912 13 //DigitalIn SW1(D12);
jkfadsjk 8:eb72f789f912 14 //DigitalIn SW2(D13);
jkfadsjk 8:eb72f789f912 15 Serial pc(USBTX, USBRX);
jkfadsjk 8:eb72f789f912 16 Thread t1; //thread used for thread one witch is the thread responsable for writeing and reading to the pcs serial port
jkfadsjk 8:eb72f789f912 17 Thread t2; //thread used for reading the sensor data from bothe the enviromental sensor and the ldr
jkfadsjk 8:eb72f789f912 18 Thread t3; // thread used to print to the lcd screen
noutram 0:f916cefba2f4 19
noutram 0:f916cefba2f4 20 //Thread ID for the Main function (CMSIS API)
jkfadsjk 8:eb72f789f912 21 osThreadId tidMain; // used only for setting up the main thread is not used after the initial setup and is set so sleep
jkfadsjk 8:eb72f789f912 22 osThreadId id1; //used for thread one witch is the thread responsable for writeing and reading to the pcs serial port
jkfadsjk 8:eb72f789f912 23 osThreadId id2; ///used for reading the sensor data from bothe the enviromental sensor and the ldr
jkfadsjk 8:eb72f789f912 24 osThreadId id3;// thread used to print to the lcd screen
noutram 2:70084af839d3 25
jkfadsjk 8:eb72f789f912 26 SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
jkfadsjk 8:eb72f789f912 27
jkfadsjk 8:eb72f789f912 28 TextLCD lcd(A1, A0, D6, D7, D8, D9); // rs, e, d4-d7 //TextLCD lcd(A1, A0, D6, D7, D8, D9); // rs, e, d4-d7
jkfadsjk 8:eb72f789f912 29 int waiting = 90;
jkfadsjk 8:eb72f789f912 30 float fLDR = 0.0; //used to log the LDR level this has to be a global varable to be used in bothe threads
jkfadsjk 8:eb72f789f912 31 float temp = 0.0; //used to log the tempreture and has to be global because it is used in two threads
jkfadsjk 8:eb72f789f912 32 float pressure = 0.0; //used to log the presure and has to be global because it is used in two threads
jkfadsjk 8:eb72f789f912 33 int i=0; // this is the number of samples taken from the last reset of the board
jkfadsjk 8:eb72f789f912 34 int j=0; // this is used to as a pointer for the sensor readings array
jkfadsjk 8:eb72f789f912 35 int k=0; //used for the user to count the umber of printed cycles befor the used is asked what to do
jkfadsjk 8:eb72f789f912 36 int flag_for_debug =0;
jkfadsjk 8:eb72f789f912 37 float sensor_readings[101][4];
jkfadsjk 8:eb72f789f912 38 void thread1() //serial comunnicaton and writeing to the LCD screen
jkfadsjk 8:eb72f789f912 39 {//create a varable for the recived data to print will also include the serial debbugger and when this is activaled thread must be called every 0.5 secconds
jkfadsjk 8:eb72f789f912 40 pc.printf("if you would like to se all the infomation plese type Y into the keynord if you do not type n");
jkfadsjk 8:eb72f789f912 41 char choice = pc.getc();
jkfadsjk 8:eb72f789f912 42 if(choice == 'y') {
jkfadsjk 8:eb72f789f912 43 pc.printf("all the infomation will be shown\r\r\n");
jkfadsjk 8:eb72f789f912 44 binaryOutput = 1;// this indicates that all of the infomation will be shown
jkfadsjk 8:eb72f789f912 45 }else{
jkfadsjk 8:eb72f789f912 46 pc.printf("only key infomation will be shown\r\r\n");
jkfadsjk 8:eb72f789f912 47 binaryOutput =0;
jkfadsjk 8:eb72f789f912 48 }
jkfadsjk 8:eb72f789f912 49 if (k=100){
jkfadsjk 8:eb72f789f912 50 pc.printf("if you would like to vuie or delete sensor data plese type Y into the keynord to delete the data if you do not type n to vuie all sensor\n\r\n");
jkfadsjk 8:eb72f789f912 51 char choice3 = pc.getc(); //gets the users choice
jkfadsjk 8:eb72f789f912 52 if(choice == 'y') {
jkfadsjk 8:eb72f789f912 53 pc.printf("information in the array will now be deleted\r\r\n");
jkfadsjk 8:eb72f789f912 54 delete[] sensor_readings;// this delets all the sensor readings
jkfadsjk 8:eb72f789f912 55 }else{
jkfadsjk 8:eb72f789f912 56 pc.printf("infomation in the array is still stored and is the following\r\r\n");
jkfadsjk 8:eb72f789f912 57 int k=0;
jkfadsjk 8:eb72f789f912 58 for (k=0; k<=101; k++){//for loop is used to increment the sensor
jkfadsjk 8:eb72f789f912 59 pc.printf("\n\r");
jkfadsjk 8:eb72f789f912 60 pc.printf("%f", sensor_readings[k][1]);//prints the reading id number
jkfadsjk 8:eb72f789f912 61 pc.printf("\n\r");
jkfadsjk 8:eb72f789f912 62 pc.printf("%f", sensor_readings[k][2]);//prints the ldr value
jkfadsjk 8:eb72f789f912 63 pc.printf("\n\r");
jkfadsjk 8:eb72f789f912 64 pc.printf("%f", sensor_readings[k][3]);//prints the tempreture value
jkfadsjk 8:eb72f789f912 65 pc.printf("\n\r");
jkfadsjk 8:eb72f789f912 66 pc.printf("%f", sensor_readings[k][4]);//prints the presure value
jkfadsjk 8:eb72f789f912 67 }
jkfadsjk 8:eb72f789f912 68 k=0;//this is used so that once it reaches 101 sensor samples it will overite the first one saved and be a fifo
jkfadsjk 8:eb72f789f912 69 }
jkfadsjk 8:eb72f789f912 70 }
jkfadsjk 8:eb72f789f912 71
jkfadsjk 8:eb72f789f912 72 pc.printf("plese type whitch sampling rate you would like a=10, b=100, c=200");
jkfadsjk 8:eb72f789f912 73 char choice2 = pc.getc(); // reads in the used input
jkfadsjk 8:eb72f789f912 74 if(choice2 == 'a') { //option for the choice as you can see from the prinf a sets it to 10
jkfadsjk 8:eb72f789f912 75 if(choice == 'y') {
jkfadsjk 8:eb72f789f912 76 pc.printf("you have sussesfully tested 10\r\r\n"); //conferms the users chice
noutram 4:dae8898e55fe 77 }
jkfadsjk 8:eb72f789f912 78 waiting = 10;
jkfadsjk 8:eb72f789f912 79 }
jkfadsjk 8:eb72f789f912 80 if(choice2 == 'b'){
jkfadsjk 8:eb72f789f912 81 if(choice == 'y') {//option for the choice as you can see from the prinf a sets it to 100
jkfadsjk 8:eb72f789f912 82 pc.printf("you have sussesfully tested 100\r\r\n");//conferms the users chice
jkfadsjk 8:eb72f789f912 83 }
jkfadsjk 8:eb72f789f912 84 waiting = 100;
jkfadsjk 8:eb72f789f912 85 }
jkfadsjk 8:eb72f789f912 86 if(choice2 == 'c'){
jkfadsjk 8:eb72f789f912 87 waiting = 200;
jkfadsjk 8:eb72f789f912 88 if(choice == 'y') {//option for the choice as you can see from the prinf a sets it to 200
jkfadsjk 8:eb72f789f912 89 pc.printf("you have sussesfully tested 200\r\r\n");//conferms the users chice
jkfadsjk 8:eb72f789f912 90 }
jkfadsjk 8:eb72f789f912 91 };
jkfadsjk 8:eb72f789f912 92 // aditinal if will alow the user to see and stop so they are sure of the sampling rate befor the start
jkfadsjk 8:eb72f789f912 93 pc.printf("\n\r The sampleing rate is set to");
jkfadsjk 8:eb72f789f912 94 pc.printf("\n\r %i", waiting); //prints out to the user what they have selected
jkfadsjk 8:eb72f789f912 95 pc.printf(" nanoseconds \n");
jkfadsjk 8:eb72f789f912 96 pc.printf("\n\r press enter to continue");
jkfadsjk 8:eb72f789f912 97 choice2 = pc.getc(); // this means the user can look bak on anythin ther have typed before this
jkfadsjk 8:eb72f789f912 98 while(1){
jkfadsjk 8:eb72f789f912 99 i++;//increments the sample taken
jkfadsjk 8:eb72f789f912 100 if(choice == 'y') {// this only gives some infor then
jkfadsjk 8:eb72f789f912 101 pc.printf("\r\n\r\n");
jkfadsjk 8:eb72f789f912 102 pc.printf("sample number %i \n\n\r", i); // prints the sample number to the pc scree
jkfadsjk 8:eb72f789f912 103 } // prints all the values
jkfadsjk 8:eb72f789f912 104 pc.printf("tLDR = %6.4f\n\n\r", fLDR); // prints the LDR reading to the screen
jkfadsjk 8:eb72f789f912 105 pc.printf("temp = %6.4f\n\n\r", temp); //prints the tempreture to the screen
jkfadsjk 8:eb72f789f912 106 pc.printf("presure = %6.4f\n\n\r", pressure); //prints the presure reading to the screen
jkfadsjk 8:eb72f789f912 107 time_t seconds = time(NULL); //sets up the timer
jkfadsjk 8:eb72f789f912 108 //printf("Time as seconds since January 1, 1970 = %d\n", seconds); //prints the data
jkfadsjk 8:eb72f789f912 109 //printf("Time as a basic string = %s", ctime(&seconds));// printe the seconds
jkfadsjk 8:eb72f789f912 110 char buffer[32];
jkfadsjk 8:eb72f789f912 111 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); //gatters the time in a custom format
jkfadsjk 8:eb72f789f912 112 if (choice == 'y'){
jkfadsjk 8:eb72f789f912 113 printf("Time as a custom formatted string = %s", buffer); // prints the time
jkfadsjk 8:eb72f789f912 114 }// if chouse is n there is no action taken
jkfadsjk 8:eb72f789f912 115 if (choice == 'y'){
jkfadsjk 8:eb72f789f912 116 pc.printf(" infomation has now been sent to the LCD screen \n\n\r"); // this will thell the user when the lcd screen has been updated
jkfadsjk 8:eb72f789f912 117 flag_for_debug = 0; //resets the flag
jkfadsjk 8:eb72f789f912 118 }// if chouse is n there is no action taken
jkfadsjk 8:eb72f789f912 119 k++; // increments print no to the pc screen by one
jkfadsjk 8:eb72f789f912 120 Thread::wait(1.5);// sleep for 1.5 seconds
jkfadsjk 8:eb72f789f912 121 }
noutram 2:70084af839d3 122 }
noutram 2:70084af839d3 123
noutram 6:bd736256c32d 124 void thread2()
jkfadsjk 8:eb72f789f912 125 {//this hapens in real time and is only blocked by the bottom where the time betwen samples is set from thread 1 or as a defult value of 90
jkfadsjk 8:eb72f789f912 126 AnalogIn LDD_ADC_In(A5); //creates the anoalog input for the ldr ciurcit
jkfadsjk 8:eb72f789f912 127 fLDR = 0.0; //sets the LDr mesurement to 0
jkfadsjk 8:eb72f789f912 128 fLDR = LDD_ADC_In; // reads in the value from the LDR sensor
jkfadsjk 8:eb72f789f912 129 temp = sensor.getTemperature(); //reads in the tempreture into a global varable
jkfadsjk 8:eb72f789f912 130 pressure = sensor.getPressure(); //reads in the presure into a gobal varable
jkfadsjk 8:eb72f789f912 131 sensor_readings[i][0] = i; //saves the reading number to the array
jkfadsjk 8:eb72f789f912 132 sensor_readings[i][1] = fLDR; //saves the LDR value to the array
jkfadsjk 8:eb72f789f912 133 sensor_readings[i][2] = temp; //saves the tempreture reading to the array
jkfadsjk 8:eb72f789f912 134 sensor_readings[i][3] = pressure; //saves the presure sensor to the array
jkfadsjk 8:eb72f789f912 135 if (i==100){//checks to see if the records has reached 100
jkfadsjk 8:eb72f789f912 136 i=0; //if the records has reached that i is set to 0 so it will start recording over the initial values
jkfadsjk 8:eb72f789f912 137 }
jkfadsjk 8:eb72f789f912 138 Thread::wait(waiting); //this thread will then sleep for 15 secconds the sampleing rate should be acurete as the tread has nothing blocking in it
noutram 0:f916cefba2f4 139 }
jkfadsjk 8:eb72f789f912 140 void thread3() //used to write to the LCD screen only this mean that the code dose not waste time printing when it dose not need to
jkfadsjk 8:eb72f789f912 141 { // global varable are used to print the sensor data so all LCD printing can happen in one threaad
jkfadsjk 8:eb72f789f912 142 lcd.printf("the data last recived is...... \n");//this is for 11 need to use objects for the enviromental sensors(12)
jkfadsjk 8:eb72f789f912 143 lcd.printf("tLDR = %6.4f\n\n\r", fLDR); // prints the LDR reading to the screen
jkfadsjk 8:eb72f789f912 144 lcd.printf("temp = %6.4f\n\n\r", temp); //prints the tempreture to the screen
jkfadsjk 8:eb72f789f912 145 lcd.printf("presure = %6.4f\n\n\r", pressure); //prints the presure reading to the screen
jkfadsjk 8:eb72f789f912 146 flag_for_debug = 1;//used by another tread to print to a pc screen that the sensors have been writen to the LCD screen
jkfadsjk 8:eb72f789f912 147 Thread::wait(1.5);// sleep for 1.5 seconds
jkfadsjk 8:eb72f789f912 148 }
noutram 0:f916cefba2f4 149 //Main thread
noutram 0:f916cefba2f4 150 int main() {
jkfadsjk 8:eb72f789f912 151
jkfadsjk 8:eb72f789f912 152 TextLCD lcd(A1, A0, D6, D7, D8, D9); //TextLCD lcd(A1, A0, D6, D7, D8, D9); // rs, e, d4-d7
jkfadsjk 8:eb72f789f912 153 pc.baud(9600);// sets up a speed to comunicate through the comp port to the pc
jkfadsjk 8:eb72f789f912 154 pc.printf("power on test\r\n"); // this prints to the pc serial port to ensure a conection is established for serial trasmition
jkfadsjk 8:eb72f789f912 155 lcd.printf("power on test\n"); //prints to the LCD screen this will make sure that it is working
jkfadsjk 8:eb72f789f912 156
jkfadsjk 8:eb72f789f912 157 t1.start(thread1); //starts thread one
jkfadsjk 8:eb72f789f912 158 t2.start(thread2); //starts thread two
jkfadsjk 8:eb72f789f912 159 t3.start(thread3); //starts thread 3
jkfadsjk 8:eb72f789f912 160
jkfadsjk 8:eb72f789f912 161 id1 = t1.gettid(); // sets up tread identified
jkfadsjk 8:eb72f789f912 162 id2 = t2.gettid(); //sets up thread identifier
jkfadsjk 8:eb72f789f912 163 id3 = t3.gettid(); //sets up thread identifier
jkfadsjk 8:eb72f789f912 164
noutram 2:70084af839d3 165 while (true) {
jkfadsjk 8:eb72f789f912 166 Thread::wait(osWaitForever); // main thread is now not used ant it will let the other threads take over
noutram 0:f916cefba2f4 167 }
noutram 0:f916cefba2f4 168
noutram 0:f916cefba2f4 169 }
noutram 2:70084af839d3 170
noutram 2:70084af839d3 171