Task 641
Dependencies: ELEC350-Practicals-FZ429
Fork of Task641-mbedos54 by
main.cpp@12:cfdead107f43, 2017-11-23 (annotated)
- Committer:
- noutram
- Date:
- Thu Nov 23 14:13:34 2017 +0000
- Revision:
- 12:cfdead107f43
- Parent:
- 11:09fc22b7f251
Task 641
Who changed what in which revision?
User | Revision | Line number | New 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 | 12:cfdead107f43 | 5 | #include "sample_hardware.hpp" |
noutram | 0:f916cefba2f4 | 6 | |
noutram | 8:c5663f5fa848 | 7 | #define SWITCH1_RELEASE 1 |
noutram | 8:c5663f5fa848 | 8 | |
noutram | 12:cfdead107f43 | 9 | void getSample(); |
noutram | 11:09fc22b7f251 | 10 | void thread1(); |
noutram | 8:c5663f5fa848 | 11 | void switchISR(); |
noutram | 0:f916cefba2f4 | 12 | |
noutram | 12:cfdead107f43 | 13 | //Threads |
noutram | 12:cfdead107f43 | 14 | Thread samplingThread(osPriorityRealtime); |
noutram | 12:cfdead107f43 | 15 | Thread t1; |
noutram | 9:31031bbb59c7 | 16 | |
noutram | 0:f916cefba2f4 | 17 | |
noutram | 10:3a3d2a571c8f | 18 | //Class type |
noutram | 10:3a3d2a571c8f | 19 | class message_t { |
noutram | 10:3a3d2a571c8f | 20 | public: |
noutram | 12:cfdead107f43 | 21 | float adcValue; |
noutram | 10:3a3d2a571c8f | 22 | int sw1State; |
noutram | 10:3a3d2a571c8f | 23 | int sw2State; |
noutram | 10:3a3d2a571c8f | 24 | |
noutram | 10:3a3d2a571c8f | 25 | //Constructor |
noutram | 10:3a3d2a571c8f | 26 | message_t(float f, int s1, int s2) { |
noutram | 10:3a3d2a571c8f | 27 | adcValue = f; |
noutram | 10:3a3d2a571c8f | 28 | sw1State = s1; |
noutram | 10:3a3d2a571c8f | 29 | sw2State = s2; |
noutram | 10:3a3d2a571c8f | 30 | } |
noutram | 10:3a3d2a571c8f | 31 | }; |
noutram | 10:3a3d2a571c8f | 32 | |
noutram | 10:3a3d2a571c8f | 33 | //Memory Pool - with capacity for 16 message_t types |
noutram | 10:3a3d2a571c8f | 34 | MemoryPool<message_t, 16> mpool; |
noutram | 0:f916cefba2f4 | 35 | |
noutram | 10:3a3d2a571c8f | 36 | //Message queue - matched to the memory pool |
noutram | 10:3a3d2a571c8f | 37 | Queue<message_t, 16> queue; |
noutram | 8:c5663f5fa848 | 38 | |
noutram | 12:cfdead107f43 | 39 | |
noutram | 9:31031bbb59c7 | 40 | // Call this on precise intervals |
noutram | 9:31031bbb59c7 | 41 | void adcISR() { |
noutram | 12:cfdead107f43 | 42 | //This is going to be brief ;) |
noutram | 12:cfdead107f43 | 43 | samplingThread.signal_set(1); |
noutram | 12:cfdead107f43 | 44 | } |
noutram | 12:cfdead107f43 | 45 | |
noutram | 12:cfdead107f43 | 46 | //High priority thread dedicated to sampling |
noutram | 12:cfdead107f43 | 47 | void getSample() { |
noutram | 10:3a3d2a571c8f | 48 | |
noutram | 12:cfdead107f43 | 49 | while(1) { |
noutram | 12:cfdead107f43 | 50 | //Waiting for ISR in WAITING state |
noutram | 12:cfdead107f43 | 51 | Thread::signal_wait(1); |
noutram | 12:cfdead107f43 | 52 | |
noutram | 12:cfdead107f43 | 53 | //Read sample - make a copy |
noutram | 12:cfdead107f43 | 54 | float sample = adcIn; |
noutram | 12:cfdead107f43 | 55 | //Grab switch state |
noutram | 12:cfdead107f43 | 56 | uint32_t switch1State = SW1; |
noutram | 12:cfdead107f43 | 57 | uint32_t switch2State = SW2; |
noutram | 12:cfdead107f43 | 58 | |
noutram | 12:cfdead107f43 | 59 | //Allocate a block from the memory pool |
noutram | 12:cfdead107f43 | 60 | message_t *message = mpool.alloc(); |
noutram | 12:cfdead107f43 | 61 | if (message == NULL) { |
noutram | 12:cfdead107f43 | 62 | //Out of memory |
noutram | 12:cfdead107f43 | 63 | redLED = 1; |
noutram | 12:cfdead107f43 | 64 | return; |
noutram | 12:cfdead107f43 | 65 | } |
noutram | 12:cfdead107f43 | 66 | |
noutram | 12:cfdead107f43 | 67 | //Fill in the data |
noutram | 12:cfdead107f43 | 68 | message->adcValue = sample; |
noutram | 12:cfdead107f43 | 69 | message->sw1State = switch1State; |
noutram | 12:cfdead107f43 | 70 | message->sw2State = switch2State; |
noutram | 12:cfdead107f43 | 71 | |
noutram | 12:cfdead107f43 | 72 | //Write to queue |
noutram | 12:cfdead107f43 | 73 | osStatus stat = queue.put(message); //Note we are sending the "pointer" |
noutram | 12:cfdead107f43 | 74 | |
noutram | 12:cfdead107f43 | 75 | //Check if succesful |
noutram | 12:cfdead107f43 | 76 | if (stat == osErrorResource) { |
noutram | 12:cfdead107f43 | 77 | redLED = 1; |
noutram | 12:cfdead107f43 | 78 | printf("queue.put() Error code: %4Xh, Resource not available\r\n", stat); |
noutram | 12:cfdead107f43 | 79 | mpool.free(message); |
noutram | 12:cfdead107f43 | 80 | return; |
noutram | 12:cfdead107f43 | 81 | } |
noutram | 12:cfdead107f43 | 82 | } //end while |
noutram | 8:c5663f5fa848 | 83 | } |
noutram | 8:c5663f5fa848 | 84 | |
noutram | 12:cfdead107f43 | 85 | |
noutram | 12:cfdead107f43 | 86 | |
noutram | 9:31031bbb59c7 | 87 | //Normal priority thread (consumer) |
noutram | 11:09fc22b7f251 | 88 | void thread1() |
noutram | 10:3a3d2a571c8f | 89 | { |
noutram | 4:dae8898e55fe | 90 | while (true) { |
noutram | 10:3a3d2a571c8f | 91 | //Block on the queue |
noutram | 10:3a3d2a571c8f | 92 | osEvent evt = queue.get(); |
noutram | 9:31031bbb59c7 | 93 | |
noutram | 10:3a3d2a571c8f | 94 | //Check status |
noutram | 10:3a3d2a571c8f | 95 | if (evt.status == osEventMessage) { |
noutram | 10:3a3d2a571c8f | 96 | message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address) |
noutram | 12:cfdead107f43 | 97 | //Construct a COPY |
noutram | 10:3a3d2a571c8f | 98 | message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State); |
noutram | 10:3a3d2a571c8f | 99 | //We are done with this, so give back the memory to the pool |
noutram | 10:3a3d2a571c8f | 100 | mpool.free(pMessage); |
noutram | 10:3a3d2a571c8f | 101 | |
noutram | 10:3a3d2a571c8f | 102 | //Echo to the terminal |
noutram | 10:3a3d2a571c8f | 103 | printf("ADC Value: %.2f\t", msg.adcValue); |
noutram | 12:cfdead107f43 | 104 | printf("SW1: %u\t", msg.sw1State); |
noutram | 12:cfdead107f43 | 105 | printf("SW2: %u\t\r", msg.sw2State); |
noutram | 9:31031bbb59c7 | 106 | } |
noutram | 10:3a3d2a571c8f | 107 | |
noutram | 10:3a3d2a571c8f | 108 | |
noutram | 9:31031bbb59c7 | 109 | } //end while |
noutram | 0:f916cefba2f4 | 110 | } |
noutram | 0:f916cefba2f4 | 111 | |
noutram | 4:dae8898e55fe | 112 | |
noutram | 8:c5663f5fa848 | 113 | // Main thread |
noutram | 0:f916cefba2f4 | 114 | int main() { |
noutram | 0:f916cefba2f4 | 115 | redLED = 0; |
noutram | 0:f916cefba2f4 | 116 | yellowLED = 0; |
noutram | 2:70084af839d3 | 117 | greenLED = 0; |
noutram | 8:c5663f5fa848 | 118 | |
noutram | 9:31031bbb59c7 | 119 | //Start message |
noutram | 10:3a3d2a571c8f | 120 | printf("Welcome\n"); |
noutram | 10:3a3d2a571c8f | 121 | |
noutram | 9:31031bbb59c7 | 122 | //Hook up timer interrupt |
noutram | 9:31031bbb59c7 | 123 | Ticker timer; |
noutram | 10:3a3d2a571c8f | 124 | timer.attach(&adcISR, 0.1); |
noutram | 9:31031bbb59c7 | 125 | |
noutram | 8:c5663f5fa848 | 126 | //Threads |
noutram | 12:cfdead107f43 | 127 | samplingThread.start(getSample); |
noutram | 12:cfdead107f43 | 128 | t1.start(thread1); |
noutram | 7:cd015e83995a | 129 | |
noutram | 9:31031bbb59c7 | 130 | printf("Main Thread\n"); |
noutram | 2:70084af839d3 | 131 | while (true) { |
noutram | 9:31031bbb59c7 | 132 | Thread::wait(5000); |
noutram | 9:31031bbb59c7 | 133 | puts("Main Thread Alive"); |
noutram | 0:f916cefba2f4 | 134 | } |
noutram | 0:f916cefba2f4 | 135 | } |
noutram | 2:70084af839d3 | 136 | |
noutram | 2:70084af839d3 | 137 |