Sampling ldr temp and pressure data into 120 sample FIFO buffer.

Dependencies:   BME280 BMP280

Fork of Task690-mbed-os-FZ429ZI by University of Plymouth - Stages 1, 2 and 3

Files at this revision

API Documentation at this revision

Comitter:
osmith2
Date:
Sat Dec 23 18:44:37 2017 +0000
Parent:
5:cc1b9f5c27a0
Commit message:
used sprintf to put all info (date,time,temp,pressure,ldr) into a string of characters, called DaT; ; It is my recommendation that the addToBuffer function be changed from float to char, as the only thing that needs to go into the buffer is DaT.

Changed in this revision

Hardware_Setup/hardware_setup.cpp Show annotated file Show diff for this revision Revisions of this file
Hardware_Setup/hardware_setup.hpp Show annotated file Show diff for this revision Revisions of this file
LCD/LCD.cpp Show annotated file Show diff for this revision Revisions of this file
LCD/LCD.h Show annotated file Show diff for this revision Revisions of this file
buffer.hpp 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
diff -r cc1b9f5c27a0 -r c2299e3de428 Hardware_Setup/hardware_setup.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Hardware_Setup/hardware_setup.cpp	Sat Dec 23 18:44:37 2017 +0000
@@ -0,0 +1,45 @@
+#include "mbed.h"
+#include "hardware_setup.hpp"
+
+#define RED_DONE 1
+#define YELLOW_DONE 2
+
+//Digital outputs
+DigitalOut onBoardLED(LED1);
+DigitalOut redLED(PE_15);
+DigitalOut yellowLED(PB_10);
+DigitalOut greenLED(PB_11);
+
+//Inputs
+DigitalIn  onBoardSwitch(USER_BUTTON);
+DigitalIn  SW1(PE_14);
+DigitalIn  SW2(PE_12);
+//Serial pc(USBTX, USBRX);
+AnalogIn adcIn(A0);
+
+NewLCD LCD(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); // RS, E, D4-D7
+
+//POWER ON SELF TEST
+void post() 
+{
+    //POWER ON SELF TEST (POST)
+    puts("ALL LEDs should be blinking");
+    for (unsigned int n=0; n<10; n++) {
+        redLED    = 1;
+        yellowLED = 1;
+        greenLED  = 1;
+        wait(0.05);
+        redLED    = 0;
+        yellowLED = 0;
+        greenLED  = 0;     
+        wait(0.05);         
+    } 
+    
+    //LCD Test
+    for (unsigned int n=0; n<3; n++) {
+        LCD.printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
+        wait(0.1);
+        LCD.clearScreen();
+        wait(0.1);
+    } 
+}
\ No newline at end of file
diff -r cc1b9f5c27a0 -r c2299e3de428 Hardware_Setup/hardware_setup.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Hardware_Setup/hardware_setup.hpp	Sat Dec 23 18:44:37 2017 +0000
@@ -0,0 +1,20 @@
+#include "LCD.h"
+#ifndef __hardware_setup__
+#define __hardware_setup__
+extern DigitalOut onBoardLED;
+extern DigitalOut redLED;
+extern DigitalOut yellowLED;
+extern DigitalOut greenLED;
+
+extern DigitalIn  onBoardSwitch;
+extern DigitalIn  SW1;
+extern DigitalIn  SW2;
+
+//extern Serial pc;
+extern AnalogIn adcIn;
+
+extern NewLCD LCD;
+
+extern void post();
+
+#endif
\ No newline at end of file
diff -r cc1b9f5c27a0 -r c2299e3de428 LCD/LCD.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD/LCD.cpp	Sat Dec 23 18:44:37 2017 +0000
@@ -0,0 +1,223 @@
+#include "mbed.h"
+#include "LCD.h"
+
+int setInt = 0;
+Ticker T;
+
+NewLCD::NewLCD(PinName RS, PinName E, PinName D0, PinName D1, PinName D2, PinName D3) : _RS(RS),_E(E), _D(D0, D1, D2, D3) {    
+
+    _E = 0;
+    wait(0.000040f);
+    _E = 1;
+    _RS = 0;                //Command mode
+    
+    wait(0.015);            //Wait 15ms to ensure device is powered
+    
+    for (int i=0; i<3; i++) {
+        writeByte(0x03);    //set DDRAM address to 0. Do this multiple times
+        wait(0.00164);  
+    }
+    
+    writeByte(0x2);         //4-bit mode. Not sure how this sets bits 5 & 4 when we shift them using writeByte. 0010 0000 or 0000 0010?
+    wait(0.000040f);        
+        
+    writeCommand(0x28);     //4-bit mode, 2 lines, 5x8 dots.    0010 1000
+    writeCommand(0x0F);     //display on, cursor off, blinking cursor off.  0000 1100  (0x0C for nomral operation, 0x0F for everything on)
+    writeCommand(0x06);      //cursor moves to right when displaying text.   0000 0110
+    wait(0.00152);
+    
+    hours = 0;
+    minutes = 0;
+    day = 0;
+    month = 0;
+    year = 2017;
+    firstSet = true;
+    
+}
+
+void NewLCD::charDisp(int column, int row, char c) {
+    int addr = DDRAMAddress(column, row);
+    writeCommand(addr);
+    writeData(c);
+}
+
+int NewLCD::DDRAMAddress(int column, int row) {
+    return (0x80 + (row * 0x40) + column);
+}
+
+void NewLCD::cursorLocation(int column, int row) {
+    _column = column;
+    _row = row;
+}
+
+void NewLCD::shiftCursor(string direction) {
+    if (direction == "right") {         //shift to right
+        writeCommand(0x14);         //0001 0100
+    } else if (direction == "left") {  //shift to left
+        writeCommand(0x10);         //0001 0000
+    }
+}
+
+void NewLCD::clearScreen() {
+    writeCommand(0x01);         //clear screen and set cursor to 0
+    wait(0.00164f);             //this command takes 1.64 ms
+    cursorLocation(0, 0);
+}
+
+int NewLCD::_putc(int txt) {
+    if (txt == '\n') {
+        _column = 0;
+        _row++;
+        if (_row >= 2) {
+            _row = 0;
+        }
+    } else {
+        charDisp(_column, _row, txt);
+        _column++;
+        if (_column >= 16) {
+            _column = 0;
+            _row++;
+            if (_row >= 2) {
+                _row = 0;
+            }
+        }
+    }
+    return txt;
+}
+int NewLCD::_repc(int txt) {
+    if (txt == '\n') {
+        _column = 0;
+        _row++;
+        if (_row >= 2) {
+            _row = 0;
+        }
+    } else {
+        charDisp(_column, _row, txt);
+        if (_column >= 16) {
+            _column = 0;
+            _row++;
+            if (_row >= 2) {
+                _row = 0;
+            }
+        }
+    }
+    return txt;
+}
+
+
+int NewLCD::_getc() {
+    return -1;
+}
+
+void NewLCD::writeByte(char byteToSend) {
+    _D = byteToSend >> 4;
+    wait(0.000040f); // most instructions take 40us
+    _E = 0;
+    wait(0.000040f);
+    _E = 1;
+    _D = byteToSend >> 0;
+    wait(0.000040f);
+    _E = 0;
+    wait(0.000040f);  // most instructions take 40us
+    _E = 1;
+}
+
+void NewLCD::writeCommand(char commandToSend) {
+    _RS = 0;
+    writeByte(commandToSend);
+}
+
+void NewLCD::writeData(char dataToSend) {
+    _RS = 1;
+    writeByte(dataToSend);
+}
+
+void NewLCD::setDateAndTime(int sw1s, int sw2s) {
+        
+        clearScreen();
+        printf("Time: %d:%d\n", hours, minutes);
+        printf("Date: %d/%d/%d", day, month, year);
+        if(sw1s == 1){
+            wait(0.2);
+            setInt = setInt + 1;
+            if(setInt >= 5) {
+                setInt = 1;
+                }
+            }
+        
+        switch(setInt){
+            case 1:
+                if(sw2s == 1) {
+                    wait(0.2);
+                    minutes = minutes + 1;
+                    if(minutes >= 60) {
+                        minutes = 0;
+                        }
+
+                    }
+                    break;
+            case 2:
+                if(sw2s == 1) {
+                    wait(0.2);
+                    hours = hours + 1;
+                    if(hours >= 24) {
+                        hours = 0;
+                        }
+
+                    }
+                    break;
+            case 3:
+            
+                if(sw2s == 1) {
+                    wait(0.2);
+                    month = month + 1;
+                    if(month >= 12) {
+                        month = 0;
+                        }
+
+                    }
+
+                    break;
+            case 4:
+                if(sw2s == 1) {
+                    wait(0.2);
+                    day = day + 1;
+                    if(day >= 31) {
+                        day = 0;
+                        }
+
+                    }
+                    break;
+            default:
+                    break;
+}
+
+}
+    void NewLCD::updateClock() {
+    minutes = minutes + 1;
+    if(minutes >= 60) {
+        minutes = 0;
+        hours = hours + 1;
+        if(hours >= 24) {
+            hours = 0;
+            day = day + 1;
+                if(day >= 31) {
+                    day = 0;
+                    month = month + 1;
+                    if(month >= 12) {
+                        month = 0;
+                        }
+                }           
+        }        
+    }
+        clearScreen();
+        printf("Time: %d:%d\n", hours, minutes);
+        printf("Date: %d/%d/%d", day, month, year);
+}
+
+void NewLCD::startClock() {
+    T.attach(this, &NewLCD::updateClock, 60);
+}
+    
+
+    
diff -r cc1b9f5c27a0 -r c2299e3de428 LCD/LCD.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD/LCD.h	Sat Dec 23 18:44:37 2017 +0000
@@ -0,0 +1,91 @@
+#include "mbed.h"
+#include <string>
+
+class NewLCD : public Stream {
+    
+    public:
+    
+        /*  Create a new LCD interface
+        
+        RS      :   Intruction/data register control pin. 0 = Instruction register. 1 = Data register.
+        E       :   Start data read/write.
+        D0-3    :   Data lines.
+        
+        */
+        NewLCD(PinName RS, PinName E, PinName D0, PinName D1, PinName D2, PinName D3);
+        
+        /*  Write a character to the display
+        
+        c   :   The character to display
+        
+        */
+        //int putc(int txt);
+        
+        /*  Write a string to the display
+        
+        stringToDisplay  :  A string to display on the LCD, followed by potential variables that are
+                            used in the string.
+                            
+        */        
+        //int printf(const char* stringToDisplay, ...);
+        
+        /*  Move starting point of where the display will start printing
+        
+        column  :   The horizontal position from the left, starting at 0
+        row     :   The row selection from the top, starting at 0
+        
+        */
+        void cursorLocation(int column, int row);
+        
+        /*  Clear the screen    */
+        void clearScreen();
+        
+        /*  Function to shift the cursor left or right. 0 for left, 1 for right */
+        void shiftCursor(string direction);
+        
+        /*  Variables used to set the time and date */
+        char hours;
+        char minutes;
+        char day;
+        char month;
+        int year;
+        
+        /*  Function to set the time and date using the switches    */
+        void setDateAndTime(int sw1s, int sw2s);
+        void updateClock();
+        void startClock();
+        
+    protected:
+    
+        //Function to print characters. This gets called from printf.
+        virtual int _putc(int txt);
+        virtual int _getc();
+        virtual int _repc(int txt);
+    
+        //Sets DDRAM Address (this corresponds to the cursor location)
+        int DDRAMAddress(int column, int row);
+        
+        //Function to write a single character to the display in a given location
+        void charDisp(int column, int row, char c);
+        
+        //Function to send information down the data lines to instruction/data register
+        void writeByte(char byteToSend);
+        
+        //Function to set RS pin low and call writeByte to send command
+        void writeCommand(char commandToSend);
+        
+        //Function to set RS pin high and call writeByte to send data
+        void writeData(char dataToSend);
+        
+        
+        DigitalOut _RS, _E;
+        BusOut _D;
+        
+        int _column;
+        int _row;
+        
+        bool firstSet;
+        
+};
+        
+    
\ No newline at end of file
diff -r cc1b9f5c27a0 -r c2299e3de428 buffer.hpp
--- a/buffer.hpp	Sun Dec 10 20:17:31 2017 +0000
+++ b/buffer.hpp	Sat Dec 23 18:44:37 2017 +0000
@@ -4,7 +4,7 @@
 #include "mbed.h"
 
 //Size of the morse character buffer
-#define BUFFERSIZE 10 // CW specified 120 samples
+#define BUFFERSIZE 20 // CW specified 120 samples
 
 extern Semaphore spaceAvailable;
 extern Semaphore samplesInBuffer;
diff -r cc1b9f5c27a0 -r c2299e3de428 main.cpp
--- a/main.cpp	Sun Dec 10 20:17:31 2017 +0000
+++ b/main.cpp	Sat Dec 23 18:44:37 2017 +0000
@@ -1,4 +1,6 @@
 #include "mbed.h"
+
+#include <string.h>
 //#define BME
 #ifdef BME
 #include "BME280.h"
@@ -9,7 +11,8 @@
 
 // Buffer
 #include "buffer.hpp"
-
+// LCD
+#include "hardware_setup.hpp"
 
  
 #define Signal 1
@@ -61,8 +64,8 @@
 
 void FunctionSample()
 {
-    pc.printf("Testing");
-    pc.printf("%d\n", BUFFERSIZE);
+    pc.printf("Testing with %d sample buffer", BUFFERSIZE);
+    //pc.printf("%d\n", BUFFERSIZE);
     while (true) {
         Thread::signal_wait(Signal);
         led1 = !led1;
@@ -73,9 +76,6 @@
         fLDR = LDR_In;
         pc.printf("LDRinThread = %6.4f\n", fLDR);
         
-        addToBuffer(fLDR);
-        
-        
         //Read BMP280 Sensors (I2C)
         float temp = sensor.getTemperature();
         float pressure = sensor.getPressure();
@@ -83,16 +83,24 @@
         pc.printf("Temperature: %5.1f\n", temp);
         pc.printf("Pressure: %5.1f\n", pressure);
         
+        addToBuffer(fLDR);
+        addToBuffer(temp);
+        addToBuffer(pressure);
+
+        // date and time
+        int year = LCD.year;
+        char month = LCD.month;
+        char day = LCD.day;
+        char hour = LCD.hours;
+        char minute = LCD.minutes;
         
-        //decrement old data when buffer is full
-        //int32_t Nsamples = samplesInBuffer.wait();
-       // pc.printf("Nsamples = %i\n", Nsamples);
-        //if (Nspaces == BUFFERSIZE)
-        //{
-            
+        char DaT[100];
+        int n; 
+        n=sprintf (DaT, "%d.%d.%d %d:%d Data Temp:%5.1f Pressure:%5.1f LDR:%6.4f", year, month, day, hour, minute, temp, pressure, fLDR);
+        pc.printf ("[%s] is a string %d chars long\n",DaT,n);
+        //addToBuffer(DaT); CHANGE addToBuffer to char, as now we only need to put in DaT, which is a char
         
-        //nextChar = tolower(nextChar);
-        //}        
+        
         
         sensorLock.unlock();
         
@@ -122,7 +130,7 @@
         //float _ldr = fLDR;
         //ldrLock.unlock();
         //pc.printf("LDR = %6.4f\n", _ldr);
-        Thread::wait(1000);
+        Thread::wait(3000);
         
         // make scheduler put the board to sleep until a signal is set?
         //Thread::wait(osWaitForever);