my code as off 16/12/2018

Dependencies:   mbed BMP280 ELEC350-Practicals-FZ429_2

Files at this revision

API Documentation at this revision

Comitter:
ChrisHayes
Date:
Sun Dec 16 15:25:04 2018 +0000
Parent:
0:d01bf1d73c6e
Commit message:
this is my code up two 16/12/2018

Changed in this revision

ELEC350-Practicals-FZ429.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.cpp 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
sample_buffer.cpp Show annotated file Show diff for this revision Revisions of this file
sample_buffer.hpp Show annotated file Show diff for this revision Revisions of this file
set_time_on_startup.cpp Show annotated file Show diff for this revision Revisions of this file
set_time_on_startup.hpp Show annotated file Show diff for this revision Revisions of this file
settime.cpp Show annotated file Show diff for this revision Revisions of this file
take_a_sample_queue.cpp Show annotated file Show diff for this revision Revisions of this file
take_a_sample_queue.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/ELEC350-Practicals-FZ429.lib	Fri Nov 16 13:19:35 2018 +0000
+++ b/ELEC350-Practicals-FZ429.lib	Sun Dec 16 15:25:04 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/teams/ELEC-351-environmental-sensor/code/ELEC350-Practicals-FZ429/#7edf4423b4db
+https://os.mbed.com/teams/ELEC-351-environmental-sensor/code/ELEC350-Practicals-FZ429_2/#6287241badda
--- a/TextLCD.cpp	Fri Nov 16 13:19:35 2018 +0000
+++ b/TextLCD.cpp	Sun Dec 16 15:25:04 2018 +0000
@@ -57,6 +57,7 @@
     writeCommand(0x01); // cls, and set cursor to 0
     wait(0.00164f);     // This command takes 1.64 ms
     locate(0, 0);
+    
 }
 
 void TextLCD::locate(int column, int row) {
--- a/main.cpp	Fri Nov 16 13:19:35 2018 +0000
+++ b/main.cpp	Sun Dec 16 15:25:04 2018 +0000
@@ -1,56 +1,95 @@
 #include "mbed.h"
 #include "TextLCD.h"
 #include "sample_hardware.hpp"
-#include "mbed_events.h"
+#include "sample_buffer.hpp"
+//#include "mbed_events.h"
+#include "take_a_sample_queue.hpp"
+#include "set_time_on_startup.hpp"
+
+//#include "stdio.h"
+
+
+Ticker timing;
 
 
 TextLCD lcd(D9, D8, D7, D6, D4, D2); // rs, e, d4
 DigitalOut myled(LED1);
-AnalogIn LDRin(PA0); 
+//AnalogIn LDRin(PA0); 
 
 EventQueue queue;
+EventQueue remove_sample;
 
-void takeSample()
+
+
+//--------------------------------------------------------------------------------
+
+
+
+Thread t1;    //sample event queue 
+Thread t2;    //counting up time  
+Thread t3;    //website 
+Thread t4;    //serial 
+
+void take_sample (void)
 {
+queue.call_every(1, takeSampleFun);
+queue.dispatch();
+}
+
+void display_sample_lcd (void)
+{
+    while(1)
+    {
+    greenLED = !greenLED;
+    float pressure = takeDataFromBuffer(2); 
+     
+    float light = takeDataFromBuffer(1);
     
-    float temp = sensor.getTemperature();
-    float pressure = sensor.getPressure();
-    float light = LDR_in
+    float temp = takeDataFromBuffer(0);
+   
+  lcd.printf("T=%5.1f", temp);
+  lcd.printf("L=%5.1f\n", light);
+  lcd.printf(" P=%1.5f\n", pressure); 
+    Thread::wait(2000);
+    }
+}
+ 
+void test3 (void)
+{
+ while(1)
+ {
+ redLED = !redLED; 
+ Thread::wait(1000);
+ }
+}
+
+void test4 (void)
+{
+ while(1)
+ {
+ //yellowLED = !yellowLED;
+ Thread::wait(1000);
+ }
+}
     
     
-    lcd.cls();
-    
-    //Display on LCD screen
-    lcd.printf("T= %5.1f", temp);
-    lcd.printf(" L= %5.1f", light);
-    lcd.printf("P= %5.1f\n", pressure);
-
- 
-       
-}
-
-
 int main() {
     
-        post();
-     lcd.printf("Hello World!\n");
-     lcd.printf("Suck my enormous\n");
-     
+    post();   
+    
+    lcd.printf("Hello World!\n");
+    lcd.printf("Uhhhh\n");
     wait(1);
     
     lcd.cls();
+ //   timing.attach(&takeSampleFun, 2.0);
+    button_time();
     
-    wait(1);
-        
-    while(1) {
     
-        
-    //Set up tasks on the main thread
-    queue.call_every(5000, takeSample);
-    
-    //Main queue event loop
-    queue.dispatch();
-    
-  
-    }
+t1.start(take_sample);
+t2.start(display_sample_lcd);
+t3.start(test3);
+t4.start(test4);
+
+           
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sample_buffer.cpp	Sun Dec 16 15:25:04 2018 +0000
@@ -0,0 +1,93 @@
+#include "sample_buffer.hpp"
+#include "sample_hardware.hpp"
+
+//Thread sychronisation primatives
+//Semaphore spaceAvailable(BUFFERSIZE);
+Semaphore samplesInBuffer(0);
+Mutex bufferLock;
+int NInBuffer[4];
+
+//Output buffer
+float buffer[4][BUFFERSIZE];
+unsigned int newestIndex[4] ={ BUFFERSIZE-1 };    //First time it is incremented, it will be 0
+unsigned int oldestIndex[4] ={ BUFFERSIZE-1 };   
+int32_t Nspace =  BUFFERSIZE;
+
+//Producer
+void addDataToBuffer(float data, int row)
+{       
+    //Is there space?
+   // int32_t Nspaces = spaceAvailable.wait();
+     
+     
+             
+    //Ok, there is space - take the lock
+    bufferLock.lock();
+  //  redLED = 1;       
+    
+    if (NInBuffer[row] < 120)
+    {
+    NInBuffer[row] = NInBuffer[row] +1; 
+    }
+    
+    if (NInBuffer[row] == 120)
+    {
+     oldestIndex[row] = (oldestIndex[row]+1) % BUFFERSIZE;
+    }
+    
+    
+    //Update buffer
+    newestIndex[row] = (newestIndex[row]+1) % BUFFERSIZE;  
+    
+    buffer[row][newestIndex[row]] = data;
+//    printf("NInBuffer = %d", NInBuffer);
+//    printf("oldestIndex= %d", oldestIndex);
+    printf("\tAdded data: %5.1f to buffer \r\n", data);
+    
+    //Release lock
+    bufferLock.unlock();
+  //  redLED = 0;
+    
+    //Signal that a sample has been added
+    samplesInBuffer.release();
+}
+
+//Consumer
+float takeDataFromBuffer(int row)
+{    
+    if(NInBuffer[row] >0)
+    {
+    NInBuffer[row] = NInBuffer[row] -1;
+    }
+    
+    //Are thre any samples in the buffer
+    int32_t Nsamples = samplesInBuffer.wait();
+        
+    //Ok, there are samples - take the lock
+    bufferLock.lock();   
+ //   yellowLED = 1;
+    
+    //Update buffer - remove oldest
+    oldestIndex[row] = (oldestIndex[row]+1) % BUFFERSIZE;
+    float data = buffer[row][oldestIndex[row]];  
+//    printf("NInBuffer = %d", NInBuffer);
+//    printf("\t\tTaking data: %5.1f from buffer\r\n", data);
+    
+    //Release lock
+    bufferLock.unlock();
+ //   yellowLED = 0;
+    
+    //Signal there is space in the buffer
+   // spaceAvailable.release();
+    
+    //return a copy of the result
+    return data;
+}
+
+char readDataFromBuffer(int row)
+{
+ bufferLock.lock(); 
+ float data = buffer[row][oldestIndex[row]];
+ bufferLock.unlock();
+ return data;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sample_buffer.hpp	Sun Dec 16 15:25:04 2018 +0000
@@ -0,0 +1,19 @@
+#ifndef __SAMPLE_BUFFER__
+#define __SAMPLE_BUFFER__
+
+#include "mbed.h"
+
+//Size of the morse character buffer
+#define BUFFERSIZE 120
+
+//extern Semaphore spaceAvailable;
+//extern Semaphore samplesInBuffer;
+//extern Mutex bufferLock;
+
+
+extern void addDataToBuffer(float temp, int row);
+extern float takeDataFromBuffer(int row);
+
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/set_time_on_startup.cpp	Sun Dec 16 15:25:04 2018 +0000
@@ -0,0 +1,153 @@
+#include "mbed.h"
+#include "TextLCD.h"
+#include "sample_hardware.hpp"
+#include "set_time_on_startup.hpp"
+
+TextLCD lcdp(D9, D8, D7, D6, D4, D2); // rs, e, d4
+Timer tmr;
+int state =0;
+
+int year = 1970;
+int month = 0;
+int day = 0;
+int hours = 0;
+int mins = 0; 
+int seconds = 0;
+
+//--------------------------------------------------------------------------------
+void settime()
+   {
+    switch (state)
+    {
+    case 0:
+    if (tmr.read_ms() >= 50 && SW2.read())
+    {
+     year++;  
+     if (year == 2100)
+     {
+      year=1970;   
+     } 
+     tmr.reset();
+    }
+    break;
+ //------------------------------------------------------   
+    
+    case 1:
+    if ( tmr.read_ms() >= 50 && SW2.read())
+    {
+     month++;   
+     
+      if (month == 13)
+     {
+      month =0;   
+     }
+      tmr.reset();
+    }
+    break;
+ //--------------------------------------------------------   
+    
+    case 2 :
+    if ( tmr.read_ms() >= 50 && SW2.read())
+    {
+     day++;   
+     if (day == 31)
+     {
+      day=0;   
+     }
+      tmr.reset();
+    }
+    break;
+ //----------------------------------------------------------   
+    
+    case 3:
+        if ( tmr.read_ms() >= 50 && SW2.read())
+    {
+     hours++;   
+     if (hours == 25)
+     {
+      hours=0;   
+     }
+      tmr.reset();
+    }
+    break;
+ //------------------------------------------------------------   
+    
+    case 4:
+      if ( tmr.read_ms() >= 50 && SW2.read())
+    {
+     mins++;   
+     if (mins == 61)
+     {
+      mins=0;   
+     }
+      tmr.reset();
+    }
+    break;
+    
+    
+    case 5:
+     if(tmr.read())
+     {
+       if (SW2.read())
+       {  
+     state = 10;
+     }
+     }
+     
+    break;
+  //---------------------------------------------------------------- 
+    
+    default:
+    
+    break;
+    
+    }
+
+}   
+
+
+
+void button_time (void)
+{
+ 
+ tmr.start();
+    while(state < 10) 
+    {  
+   
+      settime();
+ 
+     // lcd.printf("current time is...");
+      lcdp.printf("%.2d/%.2d/%.4d\r\n", day, month, year);       
+      lcdp.printf("%.2d:%.2d:%.2d\r\n", hours, mins, seconds);  
+     // lcd.setCursor();
+     // lcd.writeCommand(0x0D);
+ //     lcd.writeCommand(13);
+      
+    //Set up tasks on the main thread
+
+    
+    //Main queue event loop
+  //  queue.dispatch();
+     if ( tmr.read_ms() >= 300 && SW1.read())
+     {
+         state++;
+         if (state==6)
+         {
+             state =0;
+         }
+         tmr.reset();
+     }
+    
+    
+    }
+    
+    lcdp.cls();
+    lcdp.printf("TIME HAS BEEN SET!");
+    wait(1);
+    
+ }
+
+
+
+
+//setting time 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/set_time_on_startup.hpp	Sun Dec 16 15:25:04 2018 +0000
@@ -0,0 +1,10 @@
+#ifndef __SET_TIME_ON_STARTUP__
+#define __SET_TIME_ON_STARTUP__
+
+#include "mbed.h"
+
+extern void settime();
+
+extern void button_time();
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/settime.cpp	Sun Dec 16 15:25:04 2018 +0000
@@ -0,0 +1,14 @@
+#include "mbed.h"
+
+
+void set_time_startup()
+{
+ long usertime = 1256799737;
+ 
+ set_time(usertime);
+    
+    
+    
+   
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/take_a_sample_queue.cpp	Sun Dec 16 15:25:04 2018 +0000
@@ -0,0 +1,47 @@
+//#include "mbed.h"
+//#include "TextLCD.h"
+#include "sample_hardware.hpp"
+#include "sample_buffer.hpp"
+
+void takeSampleFun()
+{    
+yellowLED = !yellowLED;
+  // TextLCD lcd(D9, D8, D7, D6, D4, D2); // rs, e, d4
+    float temp = sensor.getTemperature();
+    float pressure = sensor.getPressure();
+    float light = adcIn;
+    float data=0;
+    int row =0;   
+    
+    
+ //   lcd.cls();
+    
+    //Display on LCD screen
+ //   lcd.printf("T=%5.1f", temp);
+ //   lcd.printf("L=%5.1f\n", light);
+ //   lcd.printf(" P=%1.5f\n", pressure);    
+    
+    
+    //adding temp to the buffer
+    data = temp;
+    row = 0;
+    addDataToBuffer(data, row); 
+    
+    
+    //adding light to the buffer
+    data = light;
+    row = 1;
+    addDataToBuffer(data,row);
+    
+    //adding pressure to the buffer 
+    data = pressure;
+    row = 2;
+    addDataToBuffer(data,row); 
+     
+     //this is where the samples are taken from the buffer
+       
+ //   takeDataFromBuffer(2);  
+ //   takeDataFromBuffer(1);
+ //   takeDataFromBuffer(0);
+   // takeDataFromBuffer(3); 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/take_a_sample_queue.hpp	Sun Dec 16 15:25:04 2018 +0000
@@ -0,0 +1,10 @@
+#ifndef __TAKE_A_SAMPLE_QUEUE__
+#define __TAKE_A_SAMPLE_QUEUE__
+
+#include "mbed.h"
+
+
+extern void takeSampleFun();
+
+
+#endif
\ No newline at end of file