gdfgdh

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task633-mbedos-FZ429 by University of Plymouth - Stages 1, 2 and 3

Committer:
noutram
Date:
Mon Apr 03 14:24:10 2017 +0000
Revision:
11:ae623ddb2eff
Parent:
10:3a3d2a571c8f
Child:
12:4605f1c51824
updated to mbed os 5.4

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 11:ae623ddb2eff 8 void thread1();
noutram 11:ae623ddb2eff 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:ae623ddb2eff 45 //MemoryPool<message_t, 16> mpool;
noutram 0:f916cefba2f4 46
noutram 10:3a3d2a571c8f 47 //Message queue - matched to the memory pool
noutram 11:ae623ddb2eff 48 //Queue<message_t, 16> queue;
noutram 11:ae623ddb2eff 49
noutram 11:ae623ddb2eff 50 //Mail queue
noutram 11:ae623ddb2eff 51 Mail<message_t, 16> mail_box;
noutram 11:ae623ddb2eff 52
noutram 8:c5663f5fa848 53
noutram 9:31031bbb59c7 54 // Call this on precise intervals
noutram 9:31031bbb59c7 55 void adcISR() {
noutram 11:ae623ddb2eff 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:ae623ddb2eff 65 message_t *message = mail_box.alloc();
noutram 10:3a3d2a571c8f 66 if (message == NULL) {
noutram 10:3a3d2a571c8f 67 //Out of memory
noutram 11:ae623ddb2eff 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:ae623ddb2eff 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:ae623ddb2eff 85 mail_box.free(message);
noutram 10:3a3d2a571c8f 86 return;
noutram 9:31031bbb59c7 87 }
noutram 11:ae623ddb2eff 88
noutram 11:ae623ddb2eff 89
noutram 8:c5663f5fa848 90 }
noutram 8:c5663f5fa848 91
noutram 9:31031bbb59c7 92 //Normal priority thread (consumer)
noutram 11:ae623ddb2eff 93 void thread1()
noutram 10:3a3d2a571c8f 94 {
noutram 4:dae8898e55fe 95 while (true) {
noutram 10:3a3d2a571c8f 96 //Block on the queue
noutram 11:ae623ddb2eff 97 osEvent evt = mail_box.get();
noutram 9:31031bbb59c7 98
noutram 10:3a3d2a571c8f 99 //Check status
noutram 11:ae623ddb2eff 100 if (evt.status == osEventMail) {
noutram 10:3a3d2a571c8f 101 message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address)
noutram 10:3a3d2a571c8f 102 //Make a copy
noutram 10:3a3d2a571c8f 103 message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State);
noutram 10:3a3d2a571c8f 104 //We are done with this, so give back the memory to the pool
noutram 11:ae623ddb2eff 105 mail_box.free(pMessage);
noutram 10:3a3d2a571c8f 106
noutram 10:3a3d2a571c8f 107 //Echo to the terminal
noutram 10:3a3d2a571c8f 108 printf("ADC Value: %.2f\t", msg.adcValue);
noutram 10:3a3d2a571c8f 109 printf("SW1: %u\t", msg.sw1State);
noutram 10:3a3d2a571c8f 110 printf("SW2: %u\n\r", msg.sw2State);
noutram 11:ae623ddb2eff 111 } else {
noutram 11:ae623ddb2eff 112 printf("ERROR: %x\n\r", evt.status);
noutram 11:ae623ddb2eff 113 }
noutram 11:ae623ddb2eff 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 0:f916cefba2f4 121 redLED = 0;
noutram 0:f916cefba2f4 122 yellowLED = 0;
noutram 2:70084af839d3 123 greenLED = 0;
noutram 8:c5663f5fa848 124
noutram 9:31031bbb59c7 125 //Start message
noutram 10:3a3d2a571c8f 126 printf("Welcome\n");
noutram 10:3a3d2a571c8f 127
noutram 9:31031bbb59c7 128 //Hook up timer interrupt
noutram 9:31031bbb59c7 129 Ticker timer;
noutram 10:3a3d2a571c8f 130 timer.attach(&adcISR, 0.1);
noutram 9:31031bbb59c7 131
noutram 8:c5663f5fa848 132 //Threads
noutram 11:ae623ddb2eff 133 t1 = new Thread();
noutram 11:ae623ddb2eff 134 t1->start(thread1);
noutram 7:cd015e83995a 135
noutram 9:31031bbb59c7 136 printf("Main Thread\n");
noutram 2:70084af839d3 137 while (true) {
noutram 9:31031bbb59c7 138 Thread::wait(5000);
noutram 9:31031bbb59c7 139 puts("Main Thread Alive");
noutram 0:f916cefba2f4 140 }
noutram 0:f916cefba2f4 141 }
noutram 2:70084af839d3 142
noutram 2:70084af839d3 143