Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP280
Revision 10:261f2b69c4c7, committed 2018-01-07
- Comitter:
- mwthewsey
- Date:
- Sun Jan 07 23:40:10 2018 +0000
- Parent:
- 9:ac5673cca703
- Child:
- 11:b538e73841ae
- Commit message:
- Before removing serial variables;
Changed in this revision
--- a/Sampling.cpp Sat Jan 06 16:13:37 2018 +0000
+++ b/Sampling.cpp Sun Jan 07 23:40:10 2018 +0000
@@ -1,4 +1,5 @@
#include "Sampling.h"
+#include "TimeInterface.h"
#include "mbed.h"
#include "rtos.h"
@@ -12,7 +13,7 @@
float tempReadings[BUFFERSIZE] = {};
float presReadings[BUFFERSIZE] = {};
float LDRReadings[BUFFERSIZE] = {};
-float timeReadings[BUFFERSIZE] = {};
+time_t timeReadings[BUFFERSIZE] = {};
Thread t1; //Sample Enviromental Sensor
Thread t2; //Sample LDR Sensor
@@ -24,9 +25,9 @@
bool NewLDRSample; //Is there new data from the LDR to output?
//Index
-unsigned short nextIndex = 0;
-unsigned short currentIndex = 0;
-unsigned short oldestIndex = 0;
+volatile unsigned short nextIndex = 0;
+volatile unsigned short currentIndex = 0;
+volatile unsigned short oldestIndex = 0;
bool firstSample = true;
@@ -52,7 +53,7 @@
t1.start(&ThreadSampleEnvSensor);
t2.start(&ThreadSampleLDR);
- sampleRate.attach(&SampleTimerISR, 1); //15 second interval
+ sampleRate.attach(&SampleTimerISR, SAMPLERATE); //15 second interval
}
void AddTempSample(float temp)
@@ -89,15 +90,23 @@
LDRReadingsLock.unlock(); // Release the key
}
+void AddTimeSample(time_t sampledTime)
+{
+ timeReadingsLock.lock(); //Take the key
+ timeReadings[nextIndex] = sampledTime; //Add the sample after the most recent
+ timeReadingsLock.unlock(); // Release the key
+}
+
void ThreadSampleLDR(void)
{
while (true) {
Thread::signal_wait(1); //Wait for signal 1
//get readings
float LDRval = LDRSensor; //Read the analogue pin value
- //get time function
+ time_t currentTime = time(0); //Get the system time
AddLDRSample(LDRval);
- //add time sample
+
+ AddTimeSample(currentTime);
NewLDRSample = true; //signal to main thread
}
}
@@ -112,14 +121,14 @@
} else {
currentIndex = IndexIncrement(currentIndex);
if (currentIndex == oldestIndex) { //When current index overflows, start infrementing oldest
- oldestIndex = IndexIncrement(oldestIndex);
+ oldestIndex = IndexIncrement(oldestIndex);
}
}
}
unsigned short IndexIncrement(unsigned short thisIndex)
{
- if (thisIndex+1 == BUFFERSIZE) {
+ if (thisIndex+1 == BUFFERSIZE) {
thisIndex = 0; //When index reached buffersize, reset to 0
} else {
thisIndex++; //Else increment
@@ -127,6 +136,40 @@
return thisIndex;
}
+unsigned short IndexDecrement(unsigned short thisIndex)
+{
+ if (thisIndex-1 == -1) { // Wait for underflow
+ thisIndex = BUFFERSIZE-1; //When index reaches 0 reset to Buffersize-1
+ } else {
+ thisIndex--; //Else decrement
+ }
+ return thisIndex;
+}
+
+void Sampling(bool inputState)
+{
+ if (inputState) {
+ sampleRate.attach(&SampleTimerISR, SAMPLERATE);
+ } else {
+ sampleRate.detach();
+ }
+}
+
+void TakeKeys(bool inputState)
+{
+ if (inputState) {
+ tempReadingsLock.lock(); //Take the key
+ presReadingsLock.lock();
+ LDRReadingsLock.lock();
+ timeReadingsLock.lock();
+ } else {
+ tempReadingsLock.unlock(); // Release the key
+ presReadingsLock.unlock();
+ LDRReadingsLock.unlock();
+ timeReadingsLock.unlock();
+ }
+}
+
void FlipSamplingLED(void)
{
--- a/Sampling.h Sat Jan 06 16:13:37 2018 +0000 +++ b/Sampling.h Sun Jan 07 23:40:10 2018 +0000 @@ -5,7 +5,8 @@ #include "BMP280.h" #include "rtos.h" -#define BUFFERSIZE 8 +#define BUFFERSIZE 16 +#define SAMPLERATE 1 //Thread Sync Tools @@ -18,13 +19,13 @@ extern float tempReadings[BUFFERSIZE]; extern float presReadings[BUFFERSIZE]; extern float LDRReadings[BUFFERSIZE]; -extern float timeReadings[BUFFERSIZE]; +extern time_t timeReadings[BUFFERSIZE]; -extern unsigned short currentIndex; +extern volatile unsigned short currentIndex; //Position in the buffer of the newest sample -extern unsigned short nextIndex; +extern volatile unsigned short nextIndex; //Position in the buffer where the next sample needs to be writen to -extern unsigned short oldestIndex; +extern volatile unsigned short oldestIndex; //Position in the buffer of the oldest sample extern bool firstSample; @@ -67,9 +68,9 @@ //When flagged by interrupt will read time and LDR value. void AddLDRSample(float LDR); -//Poducer Function +//Producer Function -void AddTimeSample(/*Whatever time*/); +void AddTimeSample(time_t sampledTime); //Producer Function void IncrementIndex(void); @@ -80,6 +81,15 @@ //Called by timeout, turns of LED unsigned short IndexIncrement(unsigned short thisIndex); -//Incrementing the index with respect for the buffersize +//Incrementing the index with respect for the buffersize. Used to prevent overflow. + +unsigned short IndexDecrement(unsigned short thisIndex); +//Decrementing the index with respect for the buffersize. Used to prevent overflow. + +void Sampling(bool inputState); +//Start or stop sampling. true = start, false = stop. + +void TakeKeys(bool inputState); +//Lock or unlock sampling variables. true = take keys. false = return keys #endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Serial.cpp Sun Jan 07 23:40:10 2018 +0000
@@ -0,0 +1,221 @@
+#include "Serial.h"
+#include "mbed.h"
+#include "Sampling.h"
+#include "TimeInterface.h"
+#include <string>
+
+Serial PC(USBTX, USBRX);
+Thread SerialThread;
+
+int DateTimeVar[3];
+char InputBufferText[128];
+unsigned short InputBufferNum;
+unsigned short internalIndex;
+float tempReadingsSerial[BUFFERSIZE] = {};
+float presReadingsSerial[BUFFERSIZE] = {};
+float LDRReadingsSerial[BUFFERSIZE] = {};
+time_t timeReadingsSerial[BUFFERSIZE] = {};
+
+
+void SerialStart(void)
+{
+ SerialThread.start(&SerialCode); //Start thread running
+ PC.attach(&RXInterruptISR, Serial::RxIrq); //Attach interrupt function to hardware interrupt
+}
+
+
+void RXInterruptISR(void)
+{
+ SerialThread.signal_set(1); //Set thread signal to start SerialCode
+ PC.attach(NULL, Serial::RxIrq); //Disable interrupt
+}
+
+void UpdateSerialData(void)
+{
+
+ TakeKeys(true); //Take keys
+
+ *tempReadingsSerial = *tempReadings; //Copy samples to serial variables
+ *presReadingsSerial = *presReadings;
+ *LDRReadingsSerial = *LDRReadings;
+ *timeReadingsSerial = *timeReadings;
+
+ TakeKeys(false); //Return keys
+}
+
+
+void SerialCode(void)
+{
+ while(true) {
+ Thread::signal_wait(1); //Wait untill signal set
+ PC.scanf("%s", InputBufferText); //Scan serial
+ PC.printf("Command: %s\n\r", InputBufferText); //Print input string. Readback
+
+ string InputBufferString(InputBufferText); //Convert array of char to string
+
+
+ if (InputBufferString == "test") {
+ PC.printf("testsuccess\n\r");
+
+
+ } else if (InputBufferString == "help") {
+ PC.printf("Here is an usefull list of commands:\n\r");
+ PC.printf(" readall\n\r deleteall\n\r read <n>\n\r delete <n>\n\r setdate <dd><mm><yyyy>\n\r settime <hh><mm><ss>\n\r sett <T>\n\r state <x>\n\r logging <x>\n\r");
+
+
+ } else if (InputBufferString == "readall") {
+ internalIndex = currentIndex; //InternalIndex for incrementing out data
+ PC.printf("Printing all %d reccords\n\r",BUFFERSIZE);
+ PC.printf(" Date | Time | Temp | Pressure | Light\n\r");
+ tm T = ReturnDateTimeStruct(timeReadingsSerial[internalIndex]);
+ TakeKeys(true); //Take keys
+
+ for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize
+ //InternalIndex was set as newest. We will now decrement to display oldest to newest
+ PC.printf(" %d/%d/%d %d:%d:%d %2.2f %2.2f %2.2f\n\r",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec,tempReadings[internalIndex],presReadings[internalIndex],LDRReadings[internalIndex]);
+ internalIndex = IndexDecrement(internalIndex); //Decrement internal index
+ }
+ TakeKeys(false); //Return keys
+
+
+ } else if (InputBufferString == "deleteall") {
+ //Stop sampling, take all keys, clear all, reset index, firstSample=true, unlock all, start sampling.
+
+ Sampling(false); //Stop sampling
+
+ TakeKeys(true); //Take keys
+
+ memset(tempReadings, 0, BUFFERSIZE); //Fill array with 0s
+ memset(presReadings, 0, BUFFERSIZE);
+ memset(LDRReadings, 0, BUFFERSIZE);
+ memset(timeReadings, 0, BUFFERSIZE);
+
+ nextIndex = 0; //Reset Index
+ currentIndex = 0;
+ oldestIndex = 0;
+
+ firstSample = true;
+
+ TakeKeys(false); //Return keys
+
+ Sampling(true); //Start sampling
+
+ PC.printf("Deleted %d records\n\r",BUFFERSIZE);
+
+
+ } else if (InputBufferString == "read") {
+
+ PC.printf("How many records would you like to view:\n\r");
+ PC.attach(&RXInterruptISR, Serial::RxIrq); //Enable interrupt
+ Thread::signal_wait(1); //Wait untill signal set
+
+ PC.scanf("%d", &InputBufferNum); //Scan serial
+
+ if (InputBufferNum >= BUFFERSIZE) { //If requested number of samples is greater than buffersize
+ InputBufferNum = BUFFERSIZE; //Then set number of samples equal to buffersize
+ }
+
+ internalIndex = currentIndex; //InternalIndex for incrementing out data
+ PC.printf("Showing the newest %d records:\n\r", InputBufferNum);
+ PC.printf(" Date | Time | Temp | Pressure | Light\n\r");
+ TakeKeys(true); //Take keys
+
+ for (short i = 0; i < InputBufferNum; i++) { //For loop of length InputBufferNum
+ PC.printf(" %d %d %2.2f %2.2f %2.2f\n\r",150496,165700,tempReadings[internalIndex],presReadings[internalIndex],LDRReadings[internalIndex]);
+ internalIndex = IndexDecrement(internalIndex); //Decrement internal index
+ }
+ TakeKeys(false); //Return keys
+
+
+ } else if (InputBufferString == "delete") {
+
+ PC.printf("How many records would you like to delete:\n\r");
+ PC.attach(&RXInterruptISR, Serial::RxIrq); //Enable interrupt
+ Thread::signal_wait(1); //Wait untill signal set
+
+ PC.scanf("%d", &InputBufferNum); //Scan serial
+ Sampling(false); //Stop sampling
+ TakeKeys(true); //Take keys
+
+ for (int i = 0; i < InputBufferNum; i++) {
+ tempReadings[oldestIndex] = 0; //Fill array with 0s
+ presReadings[oldestIndex] = 0;
+ LDRReadings[oldestIndex] = 0;
+
+ oldestIndex = IndexIncrement(oldestIndex);
+ }
+
+ TakeKeys(false); //Return keys
+ Sampling(true); //Start sampling
+ PC.printf("Deleated %d records", InputBufferNum); //Scan serial
+
+
+ } else if (InputBufferString == "setdate") {
+
+ Sampling(false); //Stop sampling
+ PC.printf("Setting Date\n\r");
+
+ for (int i = 0; i < 3; i++) {
+ if (i==0) {
+ PC.printf("Input Year <YYYY>:\n\r");
+ } else if(i==1) {
+ PC.printf("Input Month <MM>:\n\r");
+ } else if (i==2) {
+ PC.printf("Input Date <DD>:\n\r");
+ }
+
+ PC.attach(&RXInterruptISR, Serial::RxIrq); //Enable interrupt
+ Thread::signal_wait(1); //Wait untill signal set
+
+ PC.scanf("%d", &DateTimeVar[i]); //Scan serial
+ }
+
+ SetDate(DateTimeVar[2], DateTimeVar[1], DateTimeVar[0]);
+ PC.printf("Date set to : %d/%d/%d",DateTimeVar[2], DateTimeVar[1], DateTimeVar[0]);
+ Sampling(true); //Stop sampling
+
+
+ } else if (InputBufferString == "settime") {
+
+ Sampling(false); //Stop sampling
+ PC.printf("Setting Time\n\r");
+
+ for (int i = 0; i < 3; i++) {
+ if (i==0) {
+ PC.printf("Input Hour <hh>:\n\r");
+ } else if(i==1) {
+ PC.printf("Input Minute <mm>:\n\r");
+ } else if (i==2) {
+ PC.printf("Input Seccond <ss>:\n\r");
+ }
+
+ PC.attach(&RXInterruptISR, Serial::RxIrq); //Enable interrupt
+ Thread::signal_wait(1); //Wait untill signal set
+
+ PC.scanf("%d", &DateTimeVar[i]); //Scan serial
+ }
+
+ SetTime(DateTimeVar[0], DateTimeVar[1], DateTimeVar[2]);
+ PC.printf("Time set to : %d:%d:%d",DateTimeVar[0], DateTimeVar[1], DateTimeVar[2]);
+ Sampling(true); //Stop sampling
+
+
+ } else if (InputBufferString == "sett") {
+
+
+ } else if (InputBufferString == "state") {
+
+
+ } else if (InputBufferString == "logging") {
+ internalIndex = currentIndex;
+ for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize
+ //InternalIndex was set as newest. We will now decrement to display oldest to newest
+ PC.printf(" %d\n\r",timeReadings[internalIndex]);
+ internalIndex = IndexDecrement(internalIndex); //Decrement internal index
+ }
+ }
+
+
+ PC.attach(&RXInterruptISR, Serial::RxIrq); //Enable interrupt
+ }
+}
\ No newline at end of file
--- a/Serial.h Sat Jan 06 16:13:37 2018 +0000 +++ b/Serial.h Sun Jan 07 23:40:10 2018 +0000 @@ -1,7 +1,39 @@ #ifndef __Serial__ #define __Serial__ +/* +* These functions handle the USB serial interface +*/ -//These functions handle the USB serial interface +#include "mbed.h" +#include "Sampling.h" + +//Variables +extern int DateTimeVar[3]; +extern char InputBufferText[128]; +extern unsigned short InputBufferNum; +extern unsigned short internalIndex; +extern float tempReadingsSerial[BUFFERSIZE]; +extern float presReadingsSerial[BUFFERSIZE]; +extern float LDRReadingsSerial[BUFFERSIZE]; +extern time_t timeReadingsSerial[BUFFERSIZE]; + +//Objects +extern Thread SampleThread; // +extern Serial PC; + +//Functions + +void SerialStart(void); +//Initialising serial interface + +void RXInterruptISR(void); +//Function attached to the RX interrupt, sets SerialThread signal. + +void SerialCode(void); +//Code that runs in the SerialThread + +void UpdateSerialData(void); +//Copies new sample data to serial variables.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TimeInterface.cpp Sun Jan 07 23:40:10 2018 +0000
@@ -0,0 +1,48 @@
+#include "mbed.h"
+#include "TimeInterface.h"
+
+void SetDate(int DD, int MM, int YYYY)
+{
+ time_t currentTime = time(0); //Get the currenttime
+ tm* current_tm = localtime(¤tTime); //Convert the time integer to a stuct
+ tm newtime = *current_tm; //structure holding new time
+ newtime.tm_year = YYYY - 1900; //Year since 1900
+ newtime.tm_mon = MM - 1; //month of the year (0-11)
+ newtime.tm_mday = DD; //day of month
+ //All other parameters remain unchanged
+
+ int timeint = mktime(&newtime); //Convert time strucutre to int
+ if (timeint == -1)
+ {
+ //error code
+ } else {
+ set_time(timeint); //Set the new time
+ }
+}
+
+void SetTime(int HH, int mm, int ss)
+{
+ time_t currentTime = time(0); //Get the currenttime
+ tm* current_tm = localtime(¤tTime); //Convert the time integer to a stuct
+ tm newtime = *current_tm; //structure holding new time
+
+ newtime.tm_hour = HH; //Set hours
+ newtime.tm_min = mm; //Set mins
+ newtime.tm_sec = ss; //Set secs
+ //All other parameters remain unchanged
+
+ int timeint = mktime(&newtime); //Convert time strucutre to int
+ if (timeint == -1)
+ {
+ //error code
+ } else {
+ set_time(timeint); //Set the new time
+ }
+}
+
+struct tm ReturnDateTimeStruct(time_t seconds)
+{
+ tm* ltm = localtime(&seconds); //Turn given time integer into time structure pointer
+ //ltm.tm_mon += 1; //adjust from the standard of 0-11 for months to 1-12
+ return *ltm; //Return the data held at this address.
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TimeInterface.h Sun Jan 07 23:40:10 2018 +0000
@@ -0,0 +1,32 @@
+#include "mbed.h"
+/*
+* These functions provide an easy way to set and read the onboard time of day clock.
+* At start, the clock will read 1970 and so needs to be set for meaningful values.
+* Time is returned in a tm data strucure. Variables held in the structure are
+* listed at the bottom of the header file.
+*/
+void SetDate(int DD, int MM, int YYYY);
+//Sets the given date as system date. Does not modify time.
+
+void SetTime(int HH, int mm, int ss);
+
+struct tm ReturnDateTimeStruct(time_t seconds);
+//Returns a time structure from the integer time given.
+//Passing in time(0) will pass in the current system time.
+
+/* tm structure:
+*struct tm {
+* int tm_sec; // seconds of minutes from 0 to 61
+* int tm_min; // minutes of hour from 0 to 59
+* int tm_hour; // hours of day from 0 to 24
+* int tm_mday; // day of month from 1 to 31
+* int tm_mon; // month of year from 1 to 12 (customised)
+* int tm_year; // year since 1900
+* int tm_wday; // days since sunday
+* int tm_yday; // days since January 1st
+* int tm_isdst; // hours of daylight savings time
+*}
+tm T = ReturnDateTimeStruct(3456784567);
+T.tm_hour;
+
+*/
\ No newline at end of file
--- a/main.cpp Sat Jan 06 16:13:37 2018 +0000
+++ b/main.cpp Sun Jan 07 23:40:10 2018 +0000
@@ -22,6 +22,7 @@
//Hardware Self Test
//Initialise interrupts and times
+ SerialStart();
ConfigThreadsAndIR();
firstSample = true; //Set only at start of program
@@ -35,6 +36,8 @@
//LCD Update Function
NewEnvSample = false;
NewLDRSample = false;
+ //Serial update
+ UpdateSerialData();
}
--- a/mbed-os.lib Sat Jan 06 16:13:37 2018 +0000 +++ b/mbed-os.lib Sun Jan 07 23:40:10 2018 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#78474a5129e18e136cc7e872adbaa5b74fbb8f6a +https://github.com/ARMmbed/mbed-os/#eca67ca7dafab4ef70c21e2463b541132d0dd691