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