![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
MailBox solution
Dependencies: ELEC350-Practicals-FZ429
Fork of Task633Solution-mbedos54 by
main.cpp@11:0dd6d5a87d77, 2016-05-03 (annotated)
- Committer:
- noutram
- Date:
- Tue May 03 08:53:09 2016 +0000
- Revision:
- 11:0dd6d5a87d77
- Parent:
- 10:3a3d2a571c8f
- Child:
- 12:6441be7b83c2
docs
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 | 11:0dd6d5a87d77 | 46 | //MemoryPool<message_t, 16> mpool; |
noutram | 0:f916cefba2f4 | 47 | |
noutram | 10:3a3d2a571c8f | 48 | //Message queue - matched to the memory pool |
noutram | 11:0dd6d5a87d77 | 49 | //Queue<message_t, 16> queue; |
noutram | 11:0dd6d5a87d77 | 50 | |
noutram | 11:0dd6d5a87d77 | 51 | //Mail queue |
noutram | 11:0dd6d5a87d77 | 52 | Mail<message_t, 16> mail_box; |
noutram | 11:0dd6d5a87d77 | 53 | |
noutram | 8:c5663f5fa848 | 54 | |
noutram | 9:31031bbb59c7 | 55 | // Call this on precise intervals |
noutram | 9:31031bbb59c7 | 56 | void adcISR() { |
noutram | 11:0dd6d5a87d77 | 57 | |
noutram | 9:31031bbb59c7 | 58 | |
noutram | 10:3a3d2a571c8f | 59 | //Read sample - make a copy |
noutram | 10:3a3d2a571c8f | 60 | float sample = adcIn; |
noutram | 10:3a3d2a571c8f | 61 | //Grab switch state |
noutram | 10:3a3d2a571c8f | 62 | uint32_t switch1State = sw1; |
noutram | 10:3a3d2a571c8f | 63 | uint32_t switch2State = sw2; |
noutram | 9:31031bbb59c7 | 64 | |
noutram | 10:3a3d2a571c8f | 65 | //Allocate a block from the memory pool |
noutram | 11:0dd6d5a87d77 | 66 | message_t *message = mail_box.alloc(); |
noutram | 10:3a3d2a571c8f | 67 | if (message == NULL) { |
noutram | 10:3a3d2a571c8f | 68 | //Out of memory |
noutram | 11:0dd6d5a87d77 | 69 | printf("Out of memory\n\r"); |
noutram | 10:3a3d2a571c8f | 70 | redLED = 1; |
noutram | 10:3a3d2a571c8f | 71 | return; |
noutram | 10:3a3d2a571c8f | 72 | } |
noutram | 10:3a3d2a571c8f | 73 | |
noutram | 10:3a3d2a571c8f | 74 | //Fill in the data |
noutram | 10:3a3d2a571c8f | 75 | message->adcValue = sample; |
noutram | 10:3a3d2a571c8f | 76 | message->sw1State = switch1State; |
noutram | 10:3a3d2a571c8f | 77 | message->sw2State = switch2State; |
noutram | 9:31031bbb59c7 | 78 | |
noutram | 9:31031bbb59c7 | 79 | //Write to queue |
noutram | 11:0dd6d5a87d77 | 80 | osStatus stat = mail_box.put(message); //Note we are sending the "pointer" |
noutram | 9:31031bbb59c7 | 81 | |
noutram | 9:31031bbb59c7 | 82 | //Check if succesful |
noutram | 9:31031bbb59c7 | 83 | if (stat == osErrorResource) { |
noutram | 9:31031bbb59c7 | 84 | redLED = 1; |
noutram | 9:31031bbb59c7 | 85 | printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); |
noutram | 11:0dd6d5a87d77 | 86 | mail_box.free(message); |
noutram | 10:3a3d2a571c8f | 87 | return; |
noutram | 9:31031bbb59c7 | 88 | } |
noutram | 11:0dd6d5a87d77 | 89 | |
noutram | 11:0dd6d5a87d77 | 90 | |
noutram | 8:c5663f5fa848 | 91 | } |
noutram | 8:c5663f5fa848 | 92 | |
noutram | 9:31031bbb59c7 | 93 | //Normal priority thread (consumer) |
noutram | 4:dae8898e55fe | 94 | void thread1( const void* arg ) |
noutram | 11:0dd6d5a87d77 | 95 | { |
noutram | 11:0dd6d5a87d77 | 96 | static int count = 0; |
noutram | 11:0dd6d5a87d77 | 97 | |
noutram | 4:dae8898e55fe | 98 | while (true) { |
noutram | 10:3a3d2a571c8f | 99 | //Block on the queue |
noutram | 11:0dd6d5a87d77 | 100 | osEvent evt = mail_box.get(); |
noutram | 9:31031bbb59c7 | 101 | |
noutram | 10:3a3d2a571c8f | 102 | //Check status |
noutram | 11:0dd6d5a87d77 | 103 | if (evt.status == osEventMail) { |
noutram | 10:3a3d2a571c8f | 104 | message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address) |
noutram | 10:3a3d2a571c8f | 105 | //Make a copy |
noutram | 10:3a3d2a571c8f | 106 | message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State); |
noutram | 10:3a3d2a571c8f | 107 | //We are done with this, so give back the memory to the pool |
noutram | 11:0dd6d5a87d77 | 108 | mail_box.free(pMessage); |
noutram | 10:3a3d2a571c8f | 109 | |
noutram | 10:3a3d2a571c8f | 110 | //Echo to the terminal |
noutram | 10:3a3d2a571c8f | 111 | printf("ADC Value: %.2f\t", msg.adcValue); |
noutram | 10:3a3d2a571c8f | 112 | printf("SW1: %u\t", msg.sw1State); |
noutram | 10:3a3d2a571c8f | 113 | printf("SW2: %u\n\r", msg.sw2State); |
noutram | 11:0dd6d5a87d77 | 114 | |
noutram | 11:0dd6d5a87d77 | 115 | //Update state |
noutram | 11:0dd6d5a87d77 | 116 | if ((msg.sw1State == 1) && (msg.sw2State == 1)) { |
noutram | 11:0dd6d5a87d77 | 117 | count++; |
noutram | 11:0dd6d5a87d77 | 118 | } else { |
noutram | 11:0dd6d5a87d77 | 119 | count = 0; |
noutram | 11:0dd6d5a87d77 | 120 | } |
noutram | 11:0dd6d5a87d77 | 121 | if (count == 10) { |
noutram | 11:0dd6d5a87d77 | 122 | greenLED = !greenLED; |
noutram | 11:0dd6d5a87d77 | 123 | count = 0; |
noutram | 11:0dd6d5a87d77 | 124 | } |
noutram | 11:0dd6d5a87d77 | 125 | } else { |
noutram | 11:0dd6d5a87d77 | 126 | printf("ERROR: %x\n\r", evt.status); |
noutram | 11:0dd6d5a87d77 | 127 | } |
noutram | 11:0dd6d5a87d77 | 128 | |
noutram | 9:31031bbb59c7 | 129 | } //end while |
noutram | 0:f916cefba2f4 | 130 | } |
noutram | 0:f916cefba2f4 | 131 | |
noutram | 4:dae8898e55fe | 132 | |
noutram | 8:c5663f5fa848 | 133 | // Main thread |
noutram | 0:f916cefba2f4 | 134 | int main() { |
noutram | 0:f916cefba2f4 | 135 | redLED = 0; |
noutram | 0:f916cefba2f4 | 136 | yellowLED = 0; |
noutram | 2:70084af839d3 | 137 | greenLED = 0; |
noutram | 8:c5663f5fa848 | 138 | |
noutram | 9:31031bbb59c7 | 139 | //Start message |
noutram | 10:3a3d2a571c8f | 140 | printf("Welcome\n"); |
noutram | 10:3a3d2a571c8f | 141 | |
noutram | 9:31031bbb59c7 | 142 | //Hook up timer interrupt |
noutram | 9:31031bbb59c7 | 143 | Ticker timer; |
noutram | 10:3a3d2a571c8f | 144 | timer.attach(&adcISR, 0.1); |
noutram | 9:31031bbb59c7 | 145 | |
noutram | 8:c5663f5fa848 | 146 | //Threads |
noutram | 9:31031bbb59c7 | 147 | t1 = new Thread(&thread1); |
noutram | 7:cd015e83995a | 148 | |
noutram | 9:31031bbb59c7 | 149 | printf("Main Thread\n"); |
noutram | 2:70084af839d3 | 150 | while (true) { |
noutram | 9:31031bbb59c7 | 151 | Thread::wait(5000); |
noutram | 9:31031bbb59c7 | 152 | puts("Main Thread Alive"); |
noutram | 0:f916cefba2f4 | 153 | } |
noutram | 0:f916cefba2f4 | 154 | } |
noutram | 2:70084af839d3 | 155 | |
noutram | 2:70084af839d3 | 156 |