Nicholas Outram / Mbed OS Task631-mbedos54

Fork of Task631-mbedos54 by Stage-1 Students SoCEM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "string.h"
00004 #include <stdio.h>
00005 #include <ctype.h>
00006 
00007 #define SWITCH1_RELEASE 1
00008 
00009 void thread1();
00010 void thread2();
00011 void switchISR();
00012 
00013 //Analogue inputs
00014 AnalogIn adcIn(A0);
00015 
00016 //Digital outputs
00017 DigitalOut onBoardLED(LED1);
00018 DigitalOut redLED(D7);
00019 DigitalOut yellowLED(D6);
00020 DigitalOut greenLED(D5);
00021 
00022 //Digital inputs
00023 DigitalIn  onBoardSwitch(USER_BUTTON);
00024 DigitalIn  sw1(D4);                         //CONSIDER CHANGING THIS TO AN INTERRUPT
00025 DigitalIn  sw2(D3);
00026 
00027 //Threads
00028 Thread *t1;
00029 
00030 //Queues - "A message can be a integer or pointer value  to a certain type T that is sent to a thread or interrupt service routine."
00031 Queue<uint32_t, 5> *queue;
00032 
00033 
00034 // Call this on precise intervals
00035 void adcISR() {
00036     
00037     //Option to starve the queue
00038     if (sw2 == 1) return;
00039     
00040     //Read sample - make a copy
00041     uint32_t sample = (uint32_t)(4095*adcIn.read());
00042     
00043     //Write to queue
00044     osStatus stat = queue->put((uint32_t*)sample);
00045     
00046     //Check if succesful
00047     if (stat == osErrorResource) {
00048         redLED = 1; 
00049         printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);   
00050     }
00051     
00052 }
00053 
00054 //Normal priority thread (consumer)
00055 void thread1() 
00056 {    
00057     while (true) {
00058         //Read queue - block (with timeout)
00059         osEvent evt = queue->get(5000);     //With timeout
00060         
00061         //Check status of get()
00062         switch (evt.status) { 
00063             case osEventMessage:
00064                 //Normal status
00065                 printf("value = %d\n\r", evt.value.v);
00066                 greenLED = !greenLED;
00067                 break;
00068             case osEventTimeout:
00069                 //Timeout
00070                 printf("queue->get() returned %02x status (timeout)\n\r", evt.status);
00071                 break;
00072             default:
00073                 //All other errors (see cmsis_os.h for meaning of error code)
00074                 printf("queue->get() returned %02x status\n\r", evt.status);
00075                 break;
00076         }
00077                 
00078         //Block up consumer if switch is held down
00079         //Will fill the queue if held long enough
00080         while (sw1 == 1);
00081         
00082     } //end while
00083 }
00084 
00085 
00086 // Main thread
00087 int main() {
00088     redLED    = 0;
00089     yellowLED = 0;
00090     greenLED  = 0;
00091            
00092     //Start message
00093     printf("Welcome\n");
00094            
00095     //Queue
00096     queue = new Queue<uint32_t,5>();
00097 
00098     //Hook up timer interrupt   
00099     Ticker timer; 
00100     timer.attach(&adcISR, 1.0);
00101                
00102     //Threads
00103     t1 = new Thread(); 
00104     t1->start(thread1);
00105     
00106     printf("Main Thread\n");
00107     while (true) {
00108         Thread::wait(5000);
00109         puts("Main Thread Alive");
00110     }
00111 }
00112 
00113