Demonstration of a message queue + memory pool

Committer:
noutram
Date:
Mon Apr 03 14:16:17 2017 +0000
Revision:
11:6cfaf7dfecb9
Parent:
10:3a3d2a571c8f
updated for mbed os 5.4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:f916cefba2f4 1 #include "mbed.h"
noutram 2:70084af839d3 2 #include "string.h"
noutram 2:70084af839d3 3 #include <stdio.h>
noutram 2:70084af839d3 4 #include <ctype.h>
noutram 0:f916cefba2f4 5
noutram 8:c5663f5fa848 6 #define SWITCH1_RELEASE 1
noutram 8:c5663f5fa848 7
noutram 11:6cfaf7dfecb9 8 void thread1();
noutram 11:6cfaf7dfecb9 9 void thread2();
noutram 8:c5663f5fa848 10 void switchISR();
noutram 0:f916cefba2f4 11
noutram 9:31031bbb59c7 12 //Analogue inputs
noutram 9:31031bbb59c7 13 AnalogIn adcIn(A0);
noutram 9:31031bbb59c7 14
noutram 0:f916cefba2f4 15 //Digital outputs
noutram 0:f916cefba2f4 16 DigitalOut onBoardLED(LED1);
noutram 0:f916cefba2f4 17 DigitalOut redLED(D7);
noutram 0:f916cefba2f4 18 DigitalOut yellowLED(D6);
noutram 0:f916cefba2f4 19 DigitalOut greenLED(D5);
noutram 0:f916cefba2f4 20
noutram 0:f916cefba2f4 21 //Digital inputs
noutram 0:f916cefba2f4 22 DigitalIn onBoardSwitch(USER_BUTTON);
noutram 8:c5663f5fa848 23 DigitalIn sw1(D4); //CONSIDER CHANGING THIS TO AN INTERRUPT
noutram 8:c5663f5fa848 24 DigitalIn sw2(D3);
noutram 8:c5663f5fa848 25
noutram 8:c5663f5fa848 26 //Threads
noutram 8:c5663f5fa848 27 Thread *t1;
noutram 0:f916cefba2f4 28
noutram 10:3a3d2a571c8f 29 //Class type
noutram 10:3a3d2a571c8f 30 class message_t {
noutram 10:3a3d2a571c8f 31 public:
noutram 10:3a3d2a571c8f 32 float adcValue;
noutram 10:3a3d2a571c8f 33 int sw1State;
noutram 10:3a3d2a571c8f 34 int sw2State;
noutram 10:3a3d2a571c8f 35
noutram 10:3a3d2a571c8f 36 //Constructor
noutram 10:3a3d2a571c8f 37 message_t(float f, int s1, int s2) {
noutram 10:3a3d2a571c8f 38 adcValue = f;
noutram 10:3a3d2a571c8f 39 sw1State = s1;
noutram 10:3a3d2a571c8f 40 sw2State = s2;
noutram 10:3a3d2a571c8f 41 }
noutram 10:3a3d2a571c8f 42 };
noutram 10:3a3d2a571c8f 43
noutram 10:3a3d2a571c8f 44 //Memory Pool - with capacity for 16 message_t types
noutram 10:3a3d2a571c8f 45 MemoryPool<message_t, 16> mpool;
noutram 0:f916cefba2f4 46
noutram 10:3a3d2a571c8f 47 //Message queue - matched to the memory pool
noutram 10:3a3d2a571c8f 48 Queue<message_t, 16> queue;
noutram 8:c5663f5fa848 49
noutram 9:31031bbb59c7 50 // Call this on precise intervals
noutram 9:31031bbb59c7 51 void adcISR() {
noutram 9:31031bbb59c7 52
noutram 10:3a3d2a571c8f 53 //Read sample - make a copy
noutram 10:3a3d2a571c8f 54 float sample = adcIn;
noutram 10:3a3d2a571c8f 55 //Grab switch state
noutram 10:3a3d2a571c8f 56 uint32_t switch1State = sw1;
noutram 10:3a3d2a571c8f 57 uint32_t switch2State = sw2;
noutram 9:31031bbb59c7 58
noutram 10:3a3d2a571c8f 59 //Allocate a block from the memory pool
noutram 10:3a3d2a571c8f 60 message_t *message = mpool.alloc();
noutram 10:3a3d2a571c8f 61 if (message == NULL) {
noutram 10:3a3d2a571c8f 62 //Out of memory
noutram 10:3a3d2a571c8f 63 redLED = 1;
noutram 10:3a3d2a571c8f 64 return;
noutram 10:3a3d2a571c8f 65 }
noutram 10:3a3d2a571c8f 66
noutram 10:3a3d2a571c8f 67 //Fill in the data
noutram 10:3a3d2a571c8f 68 message->adcValue = sample;
noutram 10:3a3d2a571c8f 69 message->sw1State = switch1State;
noutram 10:3a3d2a571c8f 70 message->sw2State = switch2State;
noutram 9:31031bbb59c7 71
noutram 9:31031bbb59c7 72 //Write to queue
noutram 10:3a3d2a571c8f 73 osStatus stat = queue.put(message); //Note we are sending the "pointer"
noutram 9:31031bbb59c7 74
noutram 9:31031bbb59c7 75 //Check if succesful
noutram 9:31031bbb59c7 76 if (stat == osErrorResource) {
noutram 9:31031bbb59c7 77 redLED = 1;
noutram 9:31031bbb59c7 78 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);
noutram 10:3a3d2a571c8f 79 mpool.free(message);
noutram 10:3a3d2a571c8f 80 return;
noutram 9:31031bbb59c7 81 }
noutram 8:c5663f5fa848 82 }
noutram 8:c5663f5fa848 83
noutram 9:31031bbb59c7 84 //Normal priority thread (consumer)
noutram 11:6cfaf7dfecb9 85 void thread1()
noutram 10:3a3d2a571c8f 86 {
noutram 4:dae8898e55fe 87 while (true) {
noutram 10:3a3d2a571c8f 88 //Block on the queue
noutram 10:3a3d2a571c8f 89 osEvent evt = queue.get();
noutram 9:31031bbb59c7 90
noutram 10:3a3d2a571c8f 91 //Check status
noutram 10:3a3d2a571c8f 92 if (evt.status == osEventMessage) {
noutram 10:3a3d2a571c8f 93 message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address)
noutram 10:3a3d2a571c8f 94 //Make a copy
noutram 10:3a3d2a571c8f 95 message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State);
noutram 10:3a3d2a571c8f 96 //We are done with this, so give back the memory to the pool
noutram 10:3a3d2a571c8f 97 mpool.free(pMessage);
noutram 10:3a3d2a571c8f 98
noutram 10:3a3d2a571c8f 99 //Echo to the terminal
noutram 10:3a3d2a571c8f 100 printf("ADC Value: %.2f\t", msg.adcValue);
noutram 10:3a3d2a571c8f 101 printf("SW1: %u\t", msg.sw1State);
noutram 10:3a3d2a571c8f 102 printf("SW2: %u\n\r", msg.sw2State);
noutram 9:31031bbb59c7 103 }
noutram 10:3a3d2a571c8f 104
noutram 10:3a3d2a571c8f 105
noutram 9:31031bbb59c7 106 } //end while
noutram 0:f916cefba2f4 107 }
noutram 0:f916cefba2f4 108
noutram 4:dae8898e55fe 109
noutram 8:c5663f5fa848 110 // Main thread
noutram 0:f916cefba2f4 111 int main() {
noutram 0:f916cefba2f4 112 redLED = 0;
noutram 0:f916cefba2f4 113 yellowLED = 0;
noutram 2:70084af839d3 114 greenLED = 0;
noutram 8:c5663f5fa848 115
noutram 9:31031bbb59c7 116 //Start message
noutram 10:3a3d2a571c8f 117 printf("Welcome\n");
noutram 10:3a3d2a571c8f 118
noutram 9:31031bbb59c7 119 //Hook up timer interrupt
noutram 9:31031bbb59c7 120 Ticker timer;
noutram 10:3a3d2a571c8f 121 timer.attach(&adcISR, 0.1);
noutram 9:31031bbb59c7 122
noutram 8:c5663f5fa848 123 //Threads
noutram 11:6cfaf7dfecb9 124 t1 = new Thread();
noutram 11:6cfaf7dfecb9 125 t1->start(thread1);
noutram 7:cd015e83995a 126
noutram 9:31031bbb59c7 127 printf("Main Thread\n");
noutram 2:70084af839d3 128 while (true) {
noutram 9:31031bbb59c7 129 Thread::wait(5000);
noutram 9:31031bbb59c7 130 puts("Main Thread Alive");
noutram 0:f916cefba2f4 131 }
noutram 0:f916cefba2f4 132 }
noutram 2:70084af839d3 133
noutram 2:70084af839d3 134