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
Serial.cpp
- Committer:
- mwthewsey
- Date:
- 2018-01-08
- Revision:
- 11:b538e73841ae
- Parent:
- 10:261f2b69c4c7
- Child:
- 12:03589f1d5c30
File content as of revision 11:b538e73841ae:
#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 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");
TakeKeys(true); //Take keys
for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize
tm T = ReturnDateTimeStruct(timeReadings[internalIndex]);
//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]);
//PC.printf("%d\n\r",T.tm_year);
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.0, BUFFERSIZE * sizeof(float)); //Fill array with 0s
memset(presReadings, 0.0, BUFFERSIZE * sizeof(float));
memset(LDRReadings, 0.0, BUFFERSIZE * sizeof(float));
memset(timeReadings, 0.0, BUFFERSIZE * sizeof(float));
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;
timeReadings[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
}
}