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