Version 5
Dependencies: BMP280 TextLCD BME280
Sampler.hpp@3:82612f4ae4c5, 2018-11-27 (annotated)
- Committer:
- O_Thom
- Date:
- Tue Nov 27 12:33:36 2018 +0000
- Revision:
- 3:82612f4ae4c5
- Parent:
- 2:38d31b2e0956
- Child:
- 4:740cba3f2716
Commit before changing to EventQueue structure.;
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:82612f4ae4c5 | 5 | class sample_message |
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 | 3:82612f4ae4c5 | 12 | sample_message(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 | 3:82612f4ae4c5 | 35 | MemoryPool<sample_message, 20> mpool; //Memory Pool has 20 data blocks |
O_Thom | 3:82612f4ae4c5 | 36 | Queue<sample_message, 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 | 3:82612f4ae4c5 | 40 | sample_message *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 | 3:82612f4ae4c5 | 59 | void publishSample() |
O_Thom | 3:82612f4ae4c5 | 60 | { |
O_Thom | 3:82612f4ae4c5 | 61 | |
O_Thom | 3:82612f4ae4c5 | 62 | } |
O_Thom | 3:82612f4ae4c5 | 63 | |
O_Thom | 2:38d31b2e0956 | 64 | void activate() |
O_Thom | 0:f9a18207d99c | 65 | { |
O_Thom | 0:f9a18207d99c | 66 | t1.signal_set(Activate_Flag); // Signal the sampling thread to move from WAITING to READY |
O_Thom | 0:f9a18207d99c | 67 | } |
O_Thom | 2:38d31b2e0956 | 68 | |
O_Thom | 2:38d31b2e0956 | 69 | void samplingThread() |
O_Thom | 1:f89c930c6491 | 70 | { |
O_Thom | 0:f9a18207d99c | 71 | while(1) |
O_Thom | 0:f9a18207d99c | 72 | { |
O_Thom | 0:f9a18207d99c | 73 | Thread::signal_wait(Activate_Flag); |
O_Thom | 0:f9a18207d99c | 74 | printf("\033[2J"); // Clear screen |
O_Thom | 0:f9a18207d99c | 75 | printf("\033[H"); // Home Position |
O_Thom | 2:38d31b2e0956 | 76 | printf("**********Sample**********\n"); |
O_Thom | 2:38d31b2e0956 | 77 | int sw1State = SW1.read(); |
O_Thom | 2:38d31b2e0956 | 78 | int sw2state = SW2.read(); |
O_Thom | 2:38d31b2e0956 | 79 | printf("SW1: %d\tSW2: %d\n\r", sw1State, sw2state); |
O_Thom | 1:f89c930c6491 | 80 | printf("LDR: %3.5f\n\r", adcIn.read()*4095); |
O_Thom | 0:f9a18207d99c | 81 | float temp = sensor.getTemperature(); |
O_Thom | 0:f9a18207d99c | 82 | float pressure = sensor.getPressure(); |
O_Thom | 0:f9a18207d99c | 83 | #ifdef BME |
O_Thom | 0:f9a18207d99c | 84 | float humidity = sensor.getHumidity(); |
O_Thom | 0:f9a18207d99c | 85 | #endif |
O_Thom | 0:f9a18207d99c | 86 | printf("Temperature: %5.1f\n", temp); |
O_Thom | 0:f9a18207d99c | 87 | printf("Pressure: %5.1f\n", pressure); |
O_Thom | 0:f9a18207d99c | 88 | #ifdef BME |
O_Thom | 0:f9a18207d99c | 89 | printf("Pressure: %5.1f\n", humidity); |
O_Thom | 2:38d31b2e0956 | 90 | #endif |
O_Thom | 2:38d31b2e0956 | 91 | mailqueuePush(temp, pressure, sw1State, sw2state); // Place onto the mailqueue |
O_Thom | 2:38d31b2e0956 | 92 | } |
O_Thom | 0:f9a18207d99c | 93 | } |
O_Thom | 0:f9a18207d99c | 94 | |
O_Thom | 0:f9a18207d99c | 95 | Sampler() |
O_Thom | 2:38d31b2e0956 | 96 | { //Constructor |
O_Thom | 0:f9a18207d99c | 97 | osThreadId idMain; |
O_Thom | 0:f9a18207d99c | 98 | osThreadId idSample; |
O_Thom | 2:38d31b2e0956 | 99 | idMain = osThreadGetId(); // CMSIS RTOS Call |
O_Thom | 2:38d31b2e0956 | 100 | idSample = t1.gettid(); // Assign the id to the thread handle (Check this) |
O_Thom | 2:38d31b2e0956 | 101 | t1.start(this, &Sampler::samplingThread); // Start the sampling thread |
O_Thom | 1:f89c930c6491 | 102 | // NVIC_SetPriority(TIMER0_IRQn,osPriorityHigh); // Uncomment for priority setting in the NVIC |
O_Thom | 0:f9a18207d99c | 103 | } |
O_Thom | 1:f89c930c6491 | 104 | |
O_Thom | 0:f9a18207d99c | 105 | //Destructor - should the instance go out of scope, this is called |
O_Thom | 0:f9a18207d99c | 106 | ~Sampler() |
O_Thom | 0:f9a18207d99c | 107 | { |
O_Thom | 3:82612f4ae4c5 | 108 | // Code |
O_Thom | 0:f9a18207d99c | 109 | } |
O_Thom | 0:f9a18207d99c | 110 | }; |