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 7:f017a37bcf1b, committed 2017-12-28
- Comitter:
- dnonoo
- Date:
- Thu Dec 28 19:32:22 2017 +0000
- Parent:
- 6:64d346936f0e
- Child:
- 8:ab6322afa341
- Commit message:
- Mostly Georges buffer code using one mail queue and global variables with mutex locks.;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| main.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Dec 26 21:54:41 2017 +0000
+++ b/main.cpp Thu Dec 28 19:32:22 2017 +0000
@@ -1,8 +1,6 @@
#include "mbed.h"
#include "main.h"
-
-#define ENTER_KEY 1
-#define MAX_SAMPLES 4
+#include "stdio.h"
LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15);
BMP280 Sensor(D14, D15);
@@ -10,92 +8,124 @@
//Define Functions
void PrintLCD ();
void Rx_interrupt();
-void Serial_CMD();
+void serialCMD();
+void sensorRead();
+void readISR();
+void circBuff();
-//data FIFO buffer
-char data_buffer[MAX_SAMPLES][64];
-int sample_h = 0;
-int sample_t = 0;
-int data_h = 0;
-int data_t = 0;
-struct tm * sample_epoch;
+/* LOCKS */
Mutex DataBuffer;
+Mutex sensorData;
-//Serial_CMD
-volatile int rx_in=0;
-char rx_buffer[32];
-time_t raw_time = time(NULL);
-char serial_buffer[80];
+/* THREADS */
+Thread _PrintLCD, _serialCMD, _circBuff;
+Thread _sensorRead (osPriorityRealtime); //sensorData Thread
+
-/* Mail */
-typedef struct {
- float LDR_Value;
- float temp_Value;
- float press_Value;
-} mail_t;
+/* GLOBAL DATA */
+volatile float LDR = 0;
+volatile double PRES = 0;
+volatile double TEMP = 0;
-Mail<mail_t, 16> mail_box;
+Ticker read;
-//Threads
-Thread t1;
-Thread t2;
-Thread S_CMD;
-
+/* INTERRUPTS */
+
/*--------------------------------MAIN--------------------------------*/
int main() {
-
- t1.start(PrintLCD);
pc.baud(9600);
pc.attach(&Rx_interrupt, Serial::RxIrq);
- S_CMD.start(Serial_CMD);
+ POST();
+
+ _serialCMD.start(serialCMD);
+ _PrintLCD.start(PrintLCD);
+ _sensorRead.start(sensorRead);
+ _circBuff.start(circBuff);
+
+ read.attach(readISR, SAMPLING_PERIOD);
- while(1) {
- Green_int = !Green_int;
+
+ while (1) {}
+ }
+/*--------------------------------------------------------------------*/
+void circBuff () {
+
+ while(1) {
+ Thread::signal_wait(DATA_READY); // wait for signal from sensorRead
- //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
+ //Lock data buffer
DataBuffer.lock();
- //Format samples, send to FIFO buffer head
+ //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[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, mail->temp_Value, mail->press_Value, mail->LDR_Value);
+
+ sensorData.lock(); //lock critical section
+ sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, TEMP, PRES, LDR);
+ sensorData.unlock(); //unlock critical section
+
memset(sample_time,NULL,20);
- //Set seperate FIFO head and tail for printing data
+ //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
+ //Move sample FIFO buffer head to next row in buffer
sample_h++;
- //Check sample FIFO buffer head
+ //Check sample FIFO buffer head
if(sample_h >= MAX_SAMPLES){
sample_h = 0;
}
- //Check sample FIFO buffer tail
+ //Check sample FIFO buffer tail
if(sample_t == sample_h){
sample_t++;
if(sample_t >= (MAX_SAMPLES)){
sample_t = 0;
}
}
- //Unlock data buffer
+ //Unlock data buffer
DataBuffer.unlock();
+ }
+}
+
+/*---------------------Read Sensors ---------------------------*/
+
+void readISR () { // Ticker interrupt defined in main
+
+ _sensorRead.signal_set(SENSOR_UPDATE);
+}
+
+void sensorRead () {
+
+ while (1) {
+
+ sensorData.lock(); // Entering Critial Section
+
+ // Store Data in global Variables
+ LDR = LDR_In.read();
+ TEMP = Sensor.getTemperature();
+ PRES = Sensor.getPressure();
+
+ sensorData.unlock(); // Exiting Critical Section
+
+ Green_int = !Green_int; // debugging
- Thread::wait (15000);
- }
+ //Read sensors, send to mail-queue
+ mail_t *mail = mail_box.alloc();
+ mail->LDR_Value = LDR;
+ mail->temp_Value = TEMP;
+ mail->press_Value = PRES;
+ mail_box.put(mail);
+
+ _circBuff.signal_set(DATA_READY); // Set signal to buffer to store updated values
+ Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt
+ }
}
-/*--------------------------------------------------------------------*/
+
/*--------------------------------LCD---------------------------------*/
void PrintLCD () {
@@ -159,19 +189,19 @@
//Interrupt when recieving from serial port
void Rx_interrupt() {
-//Wait for serial input
+ //Wait for serial input
while (pc.readable()) {
- //Return input to serial
+ //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 enter key is pressed, set serial thread signal
if(rx_buffer[rx_in] == 0xD){
- S_CMD.signal_set(ENTER_KEY);
+ _serialCMD.signal_set(ENTER_KEY);
}
- //Increment buffer head
+ //Increment buffer head
else{
rx_in = (rx_in + 1);
}
@@ -179,13 +209,13 @@
}
//Check what command what recieved and execute
-void Serial_CMD(){
+void serialCMD(){
while(1){
- //Wait for thread signal
+ //Wait for thread signal
Thread::signal_wait(ENTER_KEY);
- //Detach serial interrupt
+ //Detach serial interrupt
pc.attach(NULL, Serial::RxIrq);
struct tm * s_time;
@@ -199,10 +229,10 @@
else if(strstr(rx_buffer, "READ ALL")){
pc.puts(" READ ALL\n\r");
- //Lock data buffer
+ //Lock data buffer
DataBuffer.lock();
- //Print all samples to serial
+ //Print all samples to serial
for(int n=data_t; n<=MAX_SAMPLES; n++){
pc.puts(data_buffer[n]);
}
@@ -212,7 +242,7 @@
}
}
- //Lock data buffer
+ //Lock data buffer
DataBuffer.unlock();
}
/*----DELETE ALL----------------------------------*/
@@ -352,4 +382,50 @@
pc.attach(&Rx_interrupt, Serial::RxIrq);
}
}
-/*--------------------------------------------------------------------*/
\ No newline at end of file
+/*------------------------------------------------*/
+
+void POST () {
+
+ pc.printf(" ALL Leds should be flashing\n\r");
+
+ for(unsigned int n = 0; n<10; n++) {
+ Green_int = ON;
+ Blue_int = ON;
+ Red_int = ON;
+ Green_ext = ON;
+ Yellow_ext = ON;
+ Red_ext = ON;
+
+ wait (0.2);
+ Green_int = OFF;
+ Blue_int = OFF;
+ Red_int = OFF;
+ Green_ext = OFF;
+ Yellow_ext = OFF;
+ Red_ext = OFF;
+ wait (0.2);
+ }
+
+ pc.printf("Switch states:\n\r");
+ pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read());
+ pc.printf("\tSW_B: %d\n\r", SW_B.read());
+
+ float Temp = Sensor.getTemperature();
+ float Pres = Sensor.getPressure();
+ float ldrs = LDR_In.read();
+
+ pc.printf("Sensor test:\n\r");
+ pc.printf("T: %f\tP: %f\tL: %f\n\r",Temp,Pres,ldrs);
+
+ pc.printf("LCD Test\n\r");
+
+ lcd.Clear();
+ lcd.RowSelect(1);
+ lcd.Write("*******LCD******");
+ lcd.RowSelect(2);
+ lcd.Write("******TEST******");
+ wait(1);
+ lcd.Clear();
+}
+
+
\ No newline at end of file
--- a/main.h Tue Dec 26 21:54:41 2017 +0000
+++ b/main.h Thu Dec 28 19:32:22 2017 +0000
@@ -1,30 +1,60 @@
#include "LCD.h"
#include "BMP280.h"
+#define ENTER_KEY 1
+#define MAX_SAMPLES 120
+#define SENSOR_UPDATE 1
+#define DATA_READY 1
+#define SAMPLING_PERIOD 10
+#define ON 1
+#define OFF 0
+
extern LCD lcd;
extern BMP280 sensor;
-
-#define ON 1
-#define OFF 0
-
/* External LEDs as Open Drain */
-DigitalOut Red_ext (PE_15);
-DigitalOut Yellow_ext (PB_10);
-DigitalOut Green_ext (PB_11);
+extern DigitalOut Red_ext (PE_15);
+extern DigitalOut Yellow_ext (PB_10);
+extern DigitalOut Green_ext (PB_11);
/* Configure On-board LEDS */
-DigitalOut Green_int (LED1);
-DigitalOut Blue_int (LED2);
-DigitalOut Red_int (LED3);
+extern DigitalOut Green_int (LED1);
+extern DigitalOut Blue_int (LED2);
+extern DigitalOut Red_int (LED3);
/* Configure Digital In Switches */
-DigitalIn SW_L (PE_12);
-DigitalIn SW_R (PE_14);
-
+extern DigitalIn SW_L (PE_12);
+extern DigitalIn SW_R (PE_14);
+extern DigitalIn SW_B (USER_BUTTON);
/* Configure Analogue Pins */
/* Analogue IN */
-AnalogIn LDR_In (PA_0);
+extern AnalogIn LDR_In (PA_0);
/* Congfigure Serial interface */
-Serial pc(USBTX, USBRX);
\ No newline at end of file
+Serial pc(USBTX, USBRX);
+
+/* Mail */
+typedef struct {
+ float LDR_Value;
+ float temp_Value;
+ float press_Value;
+} mail_t;
+
+Mail<mail_t, 16> mail_box;
+
+//data FIFO buffer
+char data_buffer[MAX_SAMPLES][64];
+int sample_h = 0;
+int sample_t = 0;
+int data_h = 0;
+int data_t = 0;
+struct tm * sample_epoch;
+
+
+//Serial_CMD
+volatile int rx_in=0;
+char rx_buffer[32];
+time_t raw_time = time(NULL);
+char serial_buffer[80];
+
+extern void POST();
