Task 1,7,8 Working

Dependencies:   BME280 BMP280 ELEC350-Coursework-2017 TextLCD

Fork of ELEC350-CWTEMPLATE-2017 by University of Plymouth - Stages 1, 2 and 3

Files at this revision

API Documentation at this revision

Comitter:
thomasmorris
Date:
Sun Dec 10 17:30:00 2017 +0000
Parent:
4:d26b261b76c9
Commit message:
Task 1,5,8 Working

Changed in this revision

LED.cpp Show annotated file Show diff for this revision Revisions of this file
LED.hpp Show annotated file Show diff for this revision Revisions of this file
img/uvision.png Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LED.cpp	Sun Dec 10 17:30:00 2017 +0000
@@ -0,0 +1,32 @@
+#include "mbed.h"
+#include "LED.hpp"
+
+
+LED::LED(PinName pinName):pin(pinName)//Constuctor
+//Constuctor runs whenever a new instave of the class is created
+//The constructor has the same name as the class
+//The constructor does not have a return type
+//When called it needs to be given parameters Led redLed(D7);
+{
+    
+}
+
+void LED::switchOn() //type void class is Led function is switchon
+{
+    this->pin =1; //You access the attributes of the class by using the this command and then the name of the attribute
+}
+
+void LED::switchOff()
+{
+    this->pin =0;
+}
+void LED::flash(float time)
+{
+    this->pin = 1;
+    wait(time);
+    this->pin = 0;   
+}
+void LED::Toggle()
+{
+    this->pin= !this->pin;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LED.hpp	Sun Dec 10 17:30:00 2017 +0000
@@ -0,0 +1,21 @@
+#ifndef _LED_HPP_ //Known as header guards
+#define _LED_HPP_
+
+class LED //This creates a class called Led
+{
+    
+//Below are methods of the class these are the opperations the class will support
+public: //The public: word enables the methods to be accessed outside of the class itself
+
+    LED(PinName pinName);
+    void switchOn(); //Void is the return type as no values will be output the return type is void
+    void switchOff();   //SwitchOff is the name of the method/function
+    void flash(float time); //float time is the parameter of the function in this case it creates a floating poi t variable known as time and this is then passed o the function when called.
+    void Toggle();
+    
+private:    //This is used here to make digitalout pin only useable from inside class Led    
+    DigitalOut pin; //DigitalOut is the Class and the object is called pin the object pin is now an attribute of led this is also known as a member variable or field    
+};
+
+
+#endif
\ No newline at end of file
--- a/main.cpp	Thu Dec 07 15:43:01 2017 +0000
+++ b/main.cpp	Sun Dec 10 17:30:00 2017 +0000
@@ -1,19 +1,80 @@
 #include "sample_hardware.hpp"
 #include "Networkbits.hpp"
+#include "rtos.h"
+#include "LED.hpp"
+#define SamplingTime 1
+#define NotSamplingTime 0
+#define TimerInterval 15 //This is in seconds
+
+Serial pc(USBTX, USBRX);
 
 // This is a very short demo that demonstrates all the hardware used in the coursework.
 // You will need a network connection set up (covered elsewhere). The host PC should have the address 10.0.0.1
+//Thread ID
+osThreadId idMain;
+osThreadId id1;
+osThreadId id2;
+osThreadId id3;
+osThreadId id4;
 
+LED Red_led(PE_15);
+LED Yellow_led(PB_10);
+LED Green_led(PB_11);
+
+Ticker Sample_timer;
 //Threads
 Thread nwrkThread;
- 
+Thread t1;
+Thread t2;
+
+void Serial_Comms()//Thread for Serial Communications
+{
+    pc.printf("Hello World \n");
+    while(1)
+    {
+        pc.printf("Test\n");
+        Thread::wait(1000);   
+    }
+}
+
+void Sample_signal_set()//Sets the Signal for when to sample the sensors
+{
+    t1.signal_set(SamplingTime);   
+}
 
-int main() {
+void Sample()//Samples the hardware and prints the result to the LCD
+{
+    while(1)
+    {     
+        Thread::signal_wait(SamplingTime);
+        //Read environmental sensors
+        double temp = sensor.getTemperature();
+        double pressure = sensor.getPressure();
+        double lux = adcIn.read();
+        //Write new data to LCD (not fast!)
+        lcd.cls();
+        lcd.printf("Temp Pres  li\n"); 
+        lcd.printf("%1.1f ",temp);
+        lcd.printf("%1.1f ",pressure);
+        lcd.printf("%1.1f\n",lux);
+        
+        //Write to SD (potentially slow)
+        //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
+    
+        Red_led.Toggle();
+        t1.signal_set(NotSamplingTime);
+        //Thread::wait(15000);//Time interval
+    }
+} 
+
+int main() 
+{  
     //Greeting
     printf("Testing\n\n");    
     
-    //Power on self test
-    post();
+    pc.baud(9600);//Sets the Serial Comms Baud Rate
+    
+    post();//Power on Self Test
     
     //Initialise the SD card (this needs to move)
     if ( sd.init() != 0) {
@@ -38,26 +99,29 @@
     //Last message before sampling begins
     lcd.cls();
     lcd.printf("READY\n\n");
-        
-        
+    
+    //Run interrupt
+    Sample_timer.attach(&Sample_signal_set,TimerInterval);
+    
+    //Run Threads
+    
+    
+    t1.start(Sample);
+    t2.start(Serial_Comms);
+    
+    //Main thread ID
+    
+    idMain = osThreadGetId();   //CMSIS RTOS call
+    
+    //Thread ID
+    id1 = t1.gettid();
+    id2 = t2.gettid();
+    
+    
+    //Toggle Green LED after a button has been pressed
     //Press either switch to unmount
     while ((SW1 == 0) && (SW2 == 0)) {
-        
-        //Base loop delay
-        wait(1.0);
-        
-        //Read environmental sensors
-        double temp = sensor.getTemperature();
-        double pressure = sensor.getPressure();
-        
-        //Write new data to LCD (not fast!)
-        lcd.cls();
-        lcd.printf("Temp   Pressure\n"); 
-        lcd.printf("%6.1f ",temp);
-        lcd.printf("%.2f\n",pressure);
-        
-        //Write to SD (potentially slow)
-        fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
+    
     }
     
     //Close File
@@ -69,7 +133,6 @@
     lcd.cls();
     lcd.printf("Unmounted...\n\n");
     
-    //Flash to indicate goodness
     while(true) {
         greenLED = 1;
         wait(0.5);