Version 8, working version with Alix, sams and ollies code. Displays time, date and sensor info onto terminal, LCD and networking, and saves onto SD card.

Dependencies:   BMP280 ELEC350-Practicals-FZ429 TextLCD BME280 ntp-client

Committer:
O_Thom
Date:
Sun Nov 25 17:49:47 2018 +0000
Revision:
2:38d31b2e0956
Parent:
1:f89c930c6491
Child:
3:82612f4ae4c5
Inclusion of the mail queue code - Single queue added. Multiple to be added later to route to the LCD, Networking, Serial Port, etc. threads.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
O_Thom 0:f9a18207d99c 1 #include "mbed.h"
O_Thom 0:f9a18207d99c 2 #define Activate_Flag 1
O_Thom 0:f9a18207d99c 3
O_Thom 2:38d31b2e0956 4 // Class for Sampled Data
O_Thom 2:38d31b2e0956 5 class message_t
O_Thom 2:38d31b2e0956 6 {
O_Thom 2:38d31b2e0956 7 public:
O_Thom 2:38d31b2e0956 8 float temp;
O_Thom 2:38d31b2e0956 9 float pressure;
O_Thom 2:38d31b2e0956 10 int sw1State;
O_Thom 2:38d31b2e0956 11 int sw2State;
O_Thom 2:38d31b2e0956 12 message_t(float f1, float f2, int s1, int s2)
O_Thom 2:38d31b2e0956 13 {
O_Thom 2:38d31b2e0956 14 temp = f1;
O_Thom 2:38d31b2e0956 15 pressure = f2;
O_Thom 2:38d31b2e0956 16 sw1State = s1;
O_Thom 2:38d31b2e0956 17 sw2State = s2;
O_Thom 2:38d31b2e0956 18 }
O_Thom 2:38d31b2e0956 19 };
O_Thom 2:38d31b2e0956 20
O_Thom 2:38d31b2e0956 21 class MailQueue
O_Thom 2:38d31b2e0956 22 // Potentially place all of the mail queue code into a class. Include pushing and popping functions
O_Thom 2:38d31b2e0956 23 // Circular buffer management also - Rewrite the oldest sample -> Include in the mail queue or in each respective thread??
O_Thom 2:38d31b2e0956 24 {
O_Thom 2:38d31b2e0956 25 private:
O_Thom 2:38d31b2e0956 26
O_Thom 2:38d31b2e0956 27 public:
O_Thom 2:38d31b2e0956 28
O_Thom 2:38d31b2e0956 29 };
O_Thom 2:38d31b2e0956 30
O_Thom 0:f9a18207d99c 31 class Sampler
O_Thom 0:f9a18207d99c 32 {
O_Thom 0:f9a18207d99c 33 private:
O_Thom 2:38d31b2e0956 34 Thread t1; // Sample Thread
O_Thom 2:38d31b2e0956 35 MemoryPool<message_t, 20> mpool; //Memory Pool has 20 data blocks
O_Thom 2:38d31b2e0956 36 Queue<message_t, 20> queue; //Message queue
O_Thom 2:38d31b2e0956 37 public:
O_Thom 2:38d31b2e0956 38 void mailqueuePush(float tsample, float psample, int switch1State, int switch2State)
O_Thom 0:f9a18207d99c 39 {
O_Thom 2:38d31b2e0956 40 message_t *message = mpool.alloc(); // Allocate a block from the memory pool
O_Thom 2:38d31b2e0956 41 if (message == NULL) // Catch the error if the pool is full
O_Thom 2:38d31b2e0956 42 {
O_Thom 2:38d31b2e0956 43 printf("Memory Full"); // Complete the handling of this
O_Thom 2:38d31b2e0956 44 return;
O_Thom 2:38d31b2e0956 45 }
O_Thom 2:38d31b2e0956 46 message->temp = tsample; // Load data into the message object
O_Thom 2:38d31b2e0956 47 message->pressure = psample;
O_Thom 2:38d31b2e0956 48 message->sw1State = switch1State;
O_Thom 2:38d31b2e0956 49 message->sw2State = switch2State;
O_Thom 2:38d31b2e0956 50 osStatus stat = queue.put(message); // Write the pointer of the message to the queue
O_Thom 2:38d31b2e0956 51 if (stat == osErrorResource) // Catch the error if the 'put' failed
O_Thom 2:38d31b2e0956 52 {
O_Thom 2:38d31b2e0956 53 printf("queue->put() Error code: %4Xh\r\n", stat);
O_Thom 2:38d31b2e0956 54 mpool.free(message);
O_Thom 2:38d31b2e0956 55 return;
O_Thom 2:38d31b2e0956 56 }
O_Thom 0:f9a18207d99c 57 }
O_Thom 0:f9a18207d99c 58
O_Thom 2:38d31b2e0956 59 void activate()
O_Thom 0:f9a18207d99c 60 {
O_Thom 0:f9a18207d99c 61 t1.signal_set(Activate_Flag); // Signal the sampling thread to move from WAITING to READY
O_Thom 0:f9a18207d99c 62 }
O_Thom 2:38d31b2e0956 63
O_Thom 2:38d31b2e0956 64 void samplingThread()
O_Thom 1:f89c930c6491 65 {
O_Thom 0:f9a18207d99c 66 while(1)
O_Thom 0:f9a18207d99c 67 {
O_Thom 0:f9a18207d99c 68 Thread::signal_wait(Activate_Flag);
O_Thom 0:f9a18207d99c 69 printf("\033[2J"); // Clear screen
O_Thom 0:f9a18207d99c 70 printf("\033[H"); // Home Position
O_Thom 2:38d31b2e0956 71 printf("**********Sample**********\n");
O_Thom 2:38d31b2e0956 72 int sw1State = SW1.read();
O_Thom 2:38d31b2e0956 73 int sw2state = SW2.read();
O_Thom 2:38d31b2e0956 74 printf("SW1: %d\tSW2: %d\n\r", sw1State, sw2state);
O_Thom 1:f89c930c6491 75 printf("LDR: %3.5f\n\r", adcIn.read()*4095);
O_Thom 0:f9a18207d99c 76 float temp = sensor.getTemperature();
O_Thom 0:f9a18207d99c 77 float pressure = sensor.getPressure();
O_Thom 0:f9a18207d99c 78 #ifdef BME
O_Thom 0:f9a18207d99c 79 float humidity = sensor.getHumidity();
O_Thom 0:f9a18207d99c 80 #endif
O_Thom 0:f9a18207d99c 81 printf("Temperature: %5.1f\n", temp);
O_Thom 0:f9a18207d99c 82 printf("Pressure: %5.1f\n", pressure);
O_Thom 0:f9a18207d99c 83 #ifdef BME
O_Thom 0:f9a18207d99c 84 printf("Pressure: %5.1f\n", humidity);
O_Thom 2:38d31b2e0956 85 #endif
O_Thom 2:38d31b2e0956 86 mailqueuePush(temp, pressure, sw1State, sw2state); // Place onto the mailqueue
O_Thom 2:38d31b2e0956 87 }
O_Thom 0:f9a18207d99c 88 }
O_Thom 0:f9a18207d99c 89
O_Thom 0:f9a18207d99c 90 Sampler()
O_Thom 2:38d31b2e0956 91 { //Constructor
O_Thom 0:f9a18207d99c 92 // IDs
O_Thom 0:f9a18207d99c 93 osThreadId idMain;
O_Thom 0:f9a18207d99c 94 osThreadId idSample;
O_Thom 2:38d31b2e0956 95 idMain = osThreadGetId(); // CMSIS RTOS Call
O_Thom 2:38d31b2e0956 96 idSample = t1.gettid(); // Assign the id to the thread handle (Check this)
O_Thom 2:38d31b2e0956 97 t1.start(this, &Sampler::samplingThread); // Start the sampling thread
O_Thom 1:f89c930c6491 98 // NVIC_SetPriority(TIMER0_IRQn,osPriorityHigh); // Uncomment for priority setting in the NVIC
O_Thom 0:f9a18207d99c 99 }
O_Thom 1:f89c930c6491 100
O_Thom 0:f9a18207d99c 101 //Destructor - should the instance go out of scope, this is called
O_Thom 0:f9a18207d99c 102 ~Sampler()
O_Thom 0:f9a18207d99c 103 {
O_Thom 2:38d31b2e0956 104
O_Thom 0:f9a18207d99c 105 }
O_Thom 0:f9a18207d99c 106 };