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.
Fork of Thread_Communication_V4_fortest by
Revision 6:64d346936f0e, committed 2017-12-26
- Comitter:
- GeorgeJourneaux
- Date:
- Tue Dec 26 21:54:41 2017 +0000
- Parent:
- 5:ea3ec65cbf5f
- Child:
- 7:f017a37bcf1b
- Commit message:
- READ<n>, DELETE ALL
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Dec 20 21:28:52 2017 +0000
+++ b/main.cpp Tue Dec 26 21:54:41 2017 +0000
@@ -2,7 +2,7 @@
#include "main.h"
#define ENTER_KEY 1
-#define MAX_SAMPLES 120
+#define MAX_SAMPLES 4
LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15);
BMP280 Sensor(D14, D15);
@@ -13,13 +13,13 @@
void Serial_CMD();
//data FIFO buffer
-char data_buffer[MAX_SAMPLES][50];
+char data_buffer[MAX_SAMPLES][64];
+int sample_h = 0;
+int sample_t = 0;
int data_h = 0;
int data_t = 0;
-
-//Time-Date data
struct tm * sample_epoch;
-char sample_time[20];
+Mutex DataBuffer;
//Serial_CMD
volatile int rx_in=0;
@@ -53,43 +53,44 @@
while(1) {
Green_int = !Green_int;
- //Read sensors, send to mail-queue
+ //Read sensors, send to mail-queue
mail_t *mail = mail_box.alloc();
mail->LDR_Value = LDR_In.read();
mail->temp_Value = Sensor.getTemperature();
mail->press_Value = Sensor.getPressure();
mail_box.put(mail);
+
+ //Lock data buffer
+ DataBuffer.lock();
- //Format samples, send to FIFO buffer
- memset(data_buffer[data_h],NULL,50);
+ //Format samples, send to FIFO buffer head
+ memset(data_buffer[sample_h],NULL,64);
time( &raw_time );
sample_epoch = localtime( &raw_time );
+ char sample_time[20];
strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch);
- sprintf(data_buffer[data_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, mail->temp_Value, mail->press_Value, mail->LDR_Value);
+ sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, mail->temp_Value, mail->press_Value, mail->LDR_Value);
memset(sample_time,NULL,20);
- //Print all samples to serial
- for(int n = data_t; n<= MAX_SAMPLES; n++){
- pc.puts(data_buffer[n]);
+ //Set seperate FIFO head and tail for printing data
+ data_h = sample_h;
+ data_t = sample_t;
+
+ //Move sample FIFO buffer head to next row in buffer
+ sample_h++;
+ //Check sample FIFO buffer head
+ if(sample_h >= MAX_SAMPLES){
+ sample_h = 0;
}
-
- if(data_t > data_h){
- for(int n = 0; n<= data_h; n++){
- pc.puts(data_buffer[n]);
+ //Check sample FIFO buffer tail
+ if(sample_t == sample_h){
+ sample_t++;
+ if(sample_t >= (MAX_SAMPLES)){
+ sample_t = 0;
}
}
-
- //Prepare buffer for next samples
- data_h++;
- if(data_h >= (MAX_SAMPLES)){
- data_h = 0;
- }
- if(data_h == data_t){
- data_t++;
- if(data_t >= (MAX_SAMPLES)){
- data_t = 0;
- }
- }
+ //Unlock data buffer
+ DataBuffer.unlock();
Thread::wait (15000);
}
@@ -158,13 +159,19 @@
//Interrupt when recieving from serial port
void Rx_interrupt() {
+//Wait for serial input
while (pc.readable()) {
+
+ //Return input to serial
rx_buffer[rx_in] = pc.getc();
pc.putc(rx_buffer[rx_in]);
-
+
+ //If enter key is pressed, set serial thread signal
if(rx_buffer[rx_in] == 0xD){
S_CMD.signal_set(ENTER_KEY);
}
+
+ //Increment buffer head
else{
rx_in = (rx_in + 1);
}
@@ -175,85 +182,173 @@
void Serial_CMD(){
while(1){
+ //Wait for thread signal
Thread::signal_wait(ENTER_KEY);
+
+ //Detach serial interrupt
pc.attach(NULL, Serial::RxIrq);
-
+
struct tm * s_time;
char tm_n[4];
- if(strstr(rx_buffer, "READ ALL")){
+/*----CARRAGE RETURN-------------*/
+ if(rx_buffer[0] == 0xD){
+ pc.puts("\n\r");
+ }
+/*----READ ALL----------------------------------*/
+ else if(strstr(rx_buffer, "READ ALL")){
+ pc.puts(" READ ALL\n\r");
+
+ //Lock data buffer
+ DataBuffer.lock();
+
+ //Print all samples to serial
+ for(int n=data_t; n<=MAX_SAMPLES; n++){
+ pc.puts(data_buffer[n]);
+ }
+ if(data_t>data_h){
+ for(int n=0; n<=(data_t-1); n++){
+ pc.puts(data_buffer[n]);
+ }
+ }
- /*time ( &raw_time );
- s_time = localtime ( &raw_time );
- strftime(serial_buffer, 80, "%d/%m/%Y, %X\n\r", time);
- pc.puts(serial_buffer);*/
+ //Lock data buffer
+ DataBuffer.unlock();
}
+/*----DELETE ALL----------------------------------*/
else if(strstr(rx_buffer, "DELETE ALL")){
- pc.printf("DELETE ALL\n\r");
+ pc.puts(" DELETE ALL\n\r");
+
+ //Lock data buffer
+ DataBuffer.lock();
+
+ //Delete all sampled data
+ for(int n=0; n<=MAX_SAMPLES; n++){
+ memset(data_buffer[n], NULL, 64);
+ }
+ data_h = data_t;
+ sample_h = sample_t;
+
+ //Unlock data buffer
+ DataBuffer.unlock();
}
+/*----READ----------------------------------*/
else if(strstr(rx_buffer, "READ")){
- pc.printf("READ\n\r");
+ pc.puts(" READ \n\r");
+ int N = atoi(strncpy(tm_n,&rx_buffer[5],4));
+ int S = 0;
+ pc.printf("N = %d\n\r",N);
+
+ //Lock data buffer
+ DataBuffer.lock();
+
+ //Check if N is greater than buffer size
+ if(N >= MAX_SAMPLES){
+ N = MAX_SAMPLES;
+ }
+
+ //Read N samples from FIFO buffer
+ if(N <= 0){
+ pc.puts("####ERROR####\n\r");
+ }
+ else{
+ for(int n=data_t; n<=MAX_SAMPLES-1; n++){
+ if(S>=N){}
+ else{
+ pc.puts(data_buffer[n]);
+ S++;
+ }
+ }
+ for(int n=0; n<=data_t; n++){
+ if(S>=N){}
+ else{
+ pc.puts(data_buffer[n]);
+ S++;
+ }
+ }
+ }
+
+ //Unlock data buffer
+ DataBuffer.unlock();
}
+/*----DELETE----------------------------------*/
else if(strstr(rx_buffer, "DELETE")){
- pc.printf("DELETE\n\r");
+ pc.puts(" DELETE \n\r");
}
+/*----SETDATE----------------------------------*/
else if(strstr(rx_buffer, "SETDATE")){
time(&raw_time);
s_time = localtime(&raw_time);
+ //Update day in time structure
int dd = atoi(strncpy(tm_n,&rx_buffer[8],2));
s_time->tm_mday = dd;
memset(tm_n, NULL, 4);
+ //Update month in time structure
int mm = atoi(strncpy(tm_n,&rx_buffer[11],2));
s_time->tm_mon = mm-1;
memset(tm_n, NULL, 4);
+ //Update year in time structure
int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4));
s_time->tm_year = yyyy-1900;
memset(tm_n, NULL, 4);
+ //Set date from updated time structure
set_time(mktime(s_time));
- strftime(serial_buffer, 80, "%d/%m/%Y\n\r", s_time);
+ strftime(serial_buffer, 80, "\n\r Set Date: %d/%m/%Y\n\r", s_time);
pc.puts(serial_buffer);
}
+/*----SETTIME---------------------------------*/
else if(strstr(rx_buffer, "SETTIME")){
time(&raw_time);
s_time = localtime(&raw_time);
+ //Update seconds in time structure
int ss = atoi(strncpy(tm_n,&rx_buffer[14],2));
s_time->tm_sec = ss;
memset(tm_n, NULL, 4);
-
+
+ //Update minutes in time structure
int mm = atoi(strncpy(tm_n,&rx_buffer[11],2));
s_time->tm_min = mm;
memset(tm_n, NULL, 4);
+ //Update hour in time structure
int hh = atoi(strncpy(tm_n,&rx_buffer[8],2));
s_time->tm_hour = hh;
memset(tm_n, NULL, 4);
-
+
+ //Set time from updated time structure
set_time(mktime(s_time));
- strftime(serial_buffer, 80, "%X\n\r", s_time);
+ strftime(serial_buffer, 80, "\n\r Set Time: %X\n\r", s_time);
pc.puts(serial_buffer);
}
+/*----SETT----------------------------------*/
else if(strstr(rx_buffer, "SETT")){
- pc.printf("SETT\n\r");
+ pc.puts(" SETT\n\r");
}
+/*----STATE----------------------------------*/
else if(strstr(rx_buffer, "STATE")){
- pc.printf("STATE\n\r");
+ pc.puts(" STATE\n\r");
}
+/*----LOGGING----------------------------------*/
else if(strstr(rx_buffer, "LOGGING")){
- pc.printf("LOGGING\n\r");
+ pc.puts(" LOGGING\n\r");
}
+/*----ERROR---*/
else{
- pc.printf("ERROR\n\r");
+ pc.puts("####ERROR####\n\r");
}
-
+/*----------------------------------------------*/
+
+ //Clear serial buffers
memset(serial_buffer, NULL, 80);
memset(rx_buffer, NULL, 32);
rx_in = 0;
+ //Attach serial interrupt
pc.attach(&Rx_interrupt, Serial::RxIrq);
}
}
