633 solution updated for mbed os 54

Fork of Task633Solution-mbedos54 by Stage-1 Students SoCEM

Committer:
noutram
Date:
Mon Apr 03 14:28:21 2017 +0000
Revision:
12:6441be7b83c2
Parent:
11:0dd6d5a87d77
updated for mbed os 54

Who changed what in which revision?

UserRevisionLine numberNew 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 0:f916cefba2f4 5
noutram 8:c5663f5fa848 6 #define SWITCH1_RELEASE 1
noutram 8:c5663f5fa848 7
noutram 12:6441be7b83c2 8 void thread1();
noutram 12:6441be7b83c2 9 void thread2();
noutram 8:c5663f5fa848 10 void switchISR();
noutram 0:f916cefba2f4 11
noutram 9:31031bbb59c7 12 //Analogue inputs
noutram 9:31031bbb59c7 13 AnalogIn adcIn(A0);
noutram 9:31031bbb59c7 14
noutram 0:f916cefba2f4 15 //Digital outputs
noutram 0:f916cefba2f4 16 DigitalOut onBoardLED(LED1);
noutram 0:f916cefba2f4 17 DigitalOut redLED(D7);
noutram 0:f916cefba2f4 18 DigitalOut yellowLED(D6);
noutram 0:f916cefba2f4 19 DigitalOut greenLED(D5);
noutram 0:f916cefba2f4 20
noutram 0:f916cefba2f4 21 //Digital inputs
noutram 0:f916cefba2f4 22 DigitalIn onBoardSwitch(USER_BUTTON);
noutram 8:c5663f5fa848 23 DigitalIn sw1(D4); //CONSIDER CHANGING THIS TO AN INTERRUPT
noutram 8:c5663f5fa848 24 DigitalIn sw2(D3);
noutram 8:c5663f5fa848 25
noutram 8:c5663f5fa848 26 //Threads
noutram 8:c5663f5fa848 27 Thread *t1;
noutram 0:f916cefba2f4 28
noutram 10:3a3d2a571c8f 29 //Class type
noutram 10:3a3d2a571c8f 30 class message_t {
noutram 10:3a3d2a571c8f 31 public:
noutram 10:3a3d2a571c8f 32 float adcValue;
noutram 10:3a3d2a571c8f 33 int sw1State;
noutram 10:3a3d2a571c8f 34 int sw2State;
noutram 10:3a3d2a571c8f 35
noutram 10:3a3d2a571c8f 36 //Constructor
noutram 10:3a3d2a571c8f 37 message_t(float f, int s1, int s2) {
noutram 10:3a3d2a571c8f 38 adcValue = f;
noutram 10:3a3d2a571c8f 39 sw1State = s1;
noutram 10:3a3d2a571c8f 40 sw2State = s2;
noutram 10:3a3d2a571c8f 41 }
noutram 10:3a3d2a571c8f 42 };
noutram 10:3a3d2a571c8f 43
noutram 10:3a3d2a571c8f 44 //Memory Pool - with capacity for 16 message_t types
noutram 11:0dd6d5a87d77 45 //MemoryPool<message_t, 16> mpool;
noutram 0:f916cefba2f4 46
noutram 10:3a3d2a571c8f 47 //Message queue - matched to the memory pool
noutram 11:0dd6d5a87d77 48 //Queue<message_t, 16> queue;
noutram 11:0dd6d5a87d77 49
noutram 11:0dd6d5a87d77 50 //Mail queue
noutram 11:0dd6d5a87d77 51 Mail<message_t, 16> mail_box;
noutram 11:0dd6d5a87d77 52
noutram 8:c5663f5fa848 53
noutram 9:31031bbb59c7 54 // Call this on precise intervals
noutram 9:31031bbb59c7 55 void adcISR() {
noutram 11:0dd6d5a87d77 56
noutram 9:31031bbb59c7 57
noutram 10:3a3d2a571c8f 58 //Read sample - make a copy
noutram 10:3a3d2a571c8f 59 float sample = adcIn;
noutram 10:3a3d2a571c8f 60 //Grab switch state
noutram 10:3a3d2a571c8f 61 uint32_t switch1State = sw1;
noutram 10:3a3d2a571c8f 62 uint32_t switch2State = sw2;
noutram 9:31031bbb59c7 63
noutram 10:3a3d2a571c8f 64 //Allocate a block from the memory pool
noutram 11:0dd6d5a87d77 65 message_t *message = mail_box.alloc();
noutram 10:3a3d2a571c8f 66 if (message == NULL) {
noutram 10:3a3d2a571c8f 67 //Out of memory
noutram 11:0dd6d5a87d77 68 printf("Out of memory\n\r");
noutram 10:3a3d2a571c8f 69 redLED = 1;
noutram 10:3a3d2a571c8f 70 return;
noutram 10:3a3d2a571c8f 71 }
noutram 10:3a3d2a571c8f 72
noutram 10:3a3d2a571c8f 73 //Fill in the data
noutram 10:3a3d2a571c8f 74 message->adcValue = sample;
noutram 10:3a3d2a571c8f 75 message->sw1State = switch1State;
noutram 10:3a3d2a571c8f 76 message->sw2State = switch2State;
noutram 9:31031bbb59c7 77
noutram 9:31031bbb59c7 78 //Write to queue
noutram 11:0dd6d5a87d77 79 osStatus stat = mail_box.put(message); //Note we are sending the "pointer"
noutram 9:31031bbb59c7 80
noutram 9:31031bbb59c7 81 //Check if succesful
noutram 9:31031bbb59c7 82 if (stat == osErrorResource) {
noutram 9:31031bbb59c7 83 redLED = 1;
noutram 9:31031bbb59c7 84 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);
noutram 11:0dd6d5a87d77 85 mail_box.free(message);
noutram 10:3a3d2a571c8f 86 return;
noutram 9:31031bbb59c7 87 }
noutram 11:0dd6d5a87d77 88
noutram 11:0dd6d5a87d77 89
noutram 8:c5663f5fa848 90 }
noutram 8:c5663f5fa848 91
noutram 9:31031bbb59c7 92 //Normal priority thread (consumer)
noutram 12:6441be7b83c2 93 void thread1()
noutram 11:0dd6d5a87d77 94 {
noutram 11:0dd6d5a87d77 95 static int count = 0;
noutram 11:0dd6d5a87d77 96
noutram 4:dae8898e55fe 97 while (true) {
noutram 10:3a3d2a571c8f 98 //Block on the queue
noutram 11:0dd6d5a87d77 99 osEvent evt = mail_box.get();
noutram 9:31031bbb59c7 100
noutram 10:3a3d2a571c8f 101 //Check status
noutram 11:0dd6d5a87d77 102 if (evt.status == osEventMail) {
noutram 10:3a3d2a571c8f 103 message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address)
noutram 10:3a3d2a571c8f 104 //Make a copy
noutram 10:3a3d2a571c8f 105 message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State);
noutram 10:3a3d2a571c8f 106 //We are done with this, so give back the memory to the pool
noutram 11:0dd6d5a87d77 107 mail_box.free(pMessage);
noutram 10:3a3d2a571c8f 108
noutram 10:3a3d2a571c8f 109 //Echo to the terminal
noutram 10:3a3d2a571c8f 110 printf("ADC Value: %.2f\t", msg.adcValue);
noutram 10:3a3d2a571c8f 111 printf("SW1: %u\t", msg.sw1State);
noutram 10:3a3d2a571c8f 112 printf("SW2: %u\n\r", msg.sw2State);
noutram 11:0dd6d5a87d77 113
noutram 11:0dd6d5a87d77 114 //Update state
noutram 11:0dd6d5a87d77 115 if ((msg.sw1State == 1) && (msg.sw2State == 1)) {
noutram 11:0dd6d5a87d77 116 count++;
noutram 11:0dd6d5a87d77 117 } else {
noutram 11:0dd6d5a87d77 118 count = 0;
noutram 11:0dd6d5a87d77 119 }
noutram 11:0dd6d5a87d77 120 if (count == 10) {
noutram 11:0dd6d5a87d77 121 greenLED = !greenLED;
noutram 11:0dd6d5a87d77 122 count = 0;
noutram 11:0dd6d5a87d77 123 }
noutram 11:0dd6d5a87d77 124 } else {
noutram 11:0dd6d5a87d77 125 printf("ERROR: %x\n\r", evt.status);
noutram 11:0dd6d5a87d77 126 }
noutram 11:0dd6d5a87d77 127
noutram 9:31031bbb59c7 128 } //end while
noutram 0:f916cefba2f4 129 }
noutram 0:f916cefba2f4 130
noutram 4:dae8898e55fe 131
noutram 8:c5663f5fa848 132 // Main thread
noutram 0:f916cefba2f4 133 int main() {
noutram 0:f916cefba2f4 134 redLED = 0;
noutram 0:f916cefba2f4 135 yellowLED = 0;
noutram 2:70084af839d3 136 greenLED = 0;
noutram 8:c5663f5fa848 137
noutram 9:31031bbb59c7 138 //Start message
noutram 10:3a3d2a571c8f 139 printf("Welcome\n");
noutram 10:3a3d2a571c8f 140
noutram 9:31031bbb59c7 141 //Hook up timer interrupt
noutram 9:31031bbb59c7 142 Ticker timer;
noutram 10:3a3d2a571c8f 143 timer.attach(&adcISR, 0.1);
noutram 9:31031bbb59c7 144
noutram 8:c5663f5fa848 145 //Threads
noutram 12:6441be7b83c2 146 t1 = new Thread();
noutram 12:6441be7b83c2 147 t1->start(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