Solution
Dependencies: ELEC350-Practicals-FZ429
Fork of Task650-mbedos-FZ429ZI-eventloop by
main.cpp@9:c46e831f8e4a, 2017-04-03 (annotated)
- Committer:
- noutram
- Date:
- Mon Apr 03 14:02:26 2017 +0000
- Revision:
- 9:c46e831f8e4a
- Parent:
- 8:b28defacd894
- Child:
- 10:3ab2495f24d9
updated for mbed os 5.4
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:b28defacd894 | 7 | #define SWITCH1_RELEASE 1 |
noutram | 8:b28defacd894 | 8 | |
noutram | 9:c46e831f8e4a | 9 | void thread1(); |
noutram | 9:c46e831f8e4a | 10 | void thread2(); |
noutram | 8:b28defacd894 | 11 | void switchISR(); |
noutram | 0:f916cefba2f4 | 12 | |
noutram | 0:f916cefba2f4 | 13 | //Digital outputs |
noutram | 0:f916cefba2f4 | 14 | DigitalOut onBoardLED(LED1); |
noutram | 0:f916cefba2f4 | 15 | DigitalOut redLED(D7); |
noutram | 0:f916cefba2f4 | 16 | DigitalOut yellowLED(D6); |
noutram | 0:f916cefba2f4 | 17 | DigitalOut greenLED(D5); |
noutram | 0:f916cefba2f4 | 18 | |
noutram | 2:70084af839d3 | 19 | //Serial Interface |
noutram | 2:70084af839d3 | 20 | Serial pc(USBTX, USBRX); |
noutram | 2:70084af839d3 | 21 | |
noutram | 0:f916cefba2f4 | 22 | //Digital inputs |
noutram | 0:f916cefba2f4 | 23 | DigitalIn onBoardSwitch(USER_BUTTON); |
noutram | 8:b28defacd894 | 24 | InterruptIn sw1(D4); |
noutram | 8:b28defacd894 | 25 | DigitalIn sw2(D3); |
noutram | 8:b28defacd894 | 26 | |
noutram | 8:b28defacd894 | 27 | //Threads |
noutram | 8:b28defacd894 | 28 | Thread *t1; |
noutram | 8:b28defacd894 | 29 | Thread *t2; |
noutram | 0:f916cefba2f4 | 30 | |
noutram | 0:f916cefba2f4 | 31 | //Thread ID for the Main function (CMSIS API) |
noutram | 0:f916cefba2f4 | 32 | osThreadId tidMain; |
noutram | 8:b28defacd894 | 33 | osThreadId tid1; |
noutram | 8:b28defacd894 | 34 | osThreadId tid2; |
noutram | 0:f916cefba2f4 | 35 | |
noutram | 8:b28defacd894 | 36 | |
noutram | 8:b28defacd894 | 37 | //Called on the falling edge of a switch |
noutram | 8:b28defacd894 | 38 | void switchISR() { |
noutram | 8:b28defacd894 | 39 | t1->signal_set(SWITCH1_RELEASE); //Very short |
noutram | 8:b28defacd894 | 40 | } |
noutram | 8:b28defacd894 | 41 | |
noutram | 8:b28defacd894 | 42 | //High priority thread |
noutram | 9:c46e831f8e4a | 43 | void thread1() |
noutram | 4:dae8898e55fe | 44 | { |
noutram | 8:b28defacd894 | 45 | redLED = 1; |
noutram | 4:dae8898e55fe | 46 | while (true) { |
noutram | 8:b28defacd894 | 47 | Thread::signal_wait(SWITCH1_RELEASE); |
noutram | 8:b28defacd894 | 48 | redLED = !redLED; |
noutram | 8:b28defacd894 | 49 | Thread::wait(1000); |
noutram | 8:b28defacd894 | 50 | redLED = !redLED; |
noutram | 8:b28defacd894 | 51 | Thread::wait(1000); |
noutram | 8:b28defacd894 | 52 | t1->signal_clr(SWITCH1_RELEASE); //Debounce |
noutram | 4:dae8898e55fe | 53 | } |
noutram | 2:70084af839d3 | 54 | } |
noutram | 2:70084af839d3 | 55 | |
noutram | 8:b28defacd894 | 56 | //This thread has normal priority |
noutram | 9:c46e831f8e4a | 57 | void thread2() |
noutram | 0:f916cefba2f4 | 58 | { |
noutram | 8:b28defacd894 | 59 | greenLED = 1; |
noutram | 2:70084af839d3 | 60 | while (true) { |
noutram | 8:b28defacd894 | 61 | //Thread::wait(2000); |
noutram | 8:b28defacd894 | 62 | Thread::wait(500); // WAIT FOR 0.5 SECONDS |
noutram | 8:b28defacd894 | 63 | greenLED = !greenLED; |
noutram | 6:2e463846b575 | 64 | } |
noutram | 0:f916cefba2f4 | 65 | } |
noutram | 0:f916cefba2f4 | 66 | |
noutram | 4:dae8898e55fe | 67 | |
noutram | 8:b28defacd894 | 68 | // Main thread |
noutram | 0:f916cefba2f4 | 69 | int main() { |
noutram | 0:f916cefba2f4 | 70 | redLED = 0; |
noutram | 0:f916cefba2f4 | 71 | yellowLED = 0; |
noutram | 2:70084af839d3 | 72 | greenLED = 0; |
noutram | 8:b28defacd894 | 73 | |
noutram | 8:b28defacd894 | 74 | //Threads |
noutram | 9:c46e831f8e4a | 75 | t1 = new Thread(osPriorityRealtime); |
noutram | 9:c46e831f8e4a | 76 | t2 = new Thread(osPriorityNormal); |
noutram | 9:c46e831f8e4a | 77 | |
noutram | 9:c46e831f8e4a | 78 | t1->start(thread1); |
noutram | 9:c46e831f8e4a | 79 | t2->start(thread2); |
noutram | 9:c46e831f8e4a | 80 | |
noutram | 8:b28defacd894 | 81 | // Thread IDs |
noutram | 0:f916cefba2f4 | 82 | tidMain = Thread::gettid(); |
noutram | 8:b28defacd894 | 83 | tid1 = t1->gettid(); |
noutram | 8:b28defacd894 | 84 | tid2 = t2->gettid(); |
noutram | 7:cd015e83995a | 85 | |
noutram | 8:b28defacd894 | 86 | //Hook up interrupt |
noutram | 8:b28defacd894 | 87 | sw1.fall(switchISR); |
noutram | 8:b28defacd894 | 88 | |
noutram | 4:dae8898e55fe | 89 | pc.printf("Main Thread\n"); |
noutram | 2:70084af839d3 | 90 | while (true) { |
noutram | 4:dae8898e55fe | 91 | Thread::wait(osWaitForever); |
noutram | 0:f916cefba2f4 | 92 | } |
noutram | 0:f916cefba2f4 | 93 | |
noutram | 0:f916cefba2f4 | 94 | } |
noutram | 2:70084af839d3 | 95 | |
noutram | 2:70084af839d3 | 96 |