RTOS application with 3 threads of different priorities: play music, measure temperature (and print results via stdio) and plays with the MMA8451Q sensor and the RGB LEDs

Dependencies:   MMA8451Q mbed-rtos mbed

Fork of 09_rtos_threads by Istvan Cserny

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 "MMA8451Q.h"
00004 
00005 MMA8451Q acc(PTE25,PTE24,0x3A);             //SDA, SCL, I2C address lef shifted
00006 PwmOut rled(LED_RED);                       //configure RGB LED pins as PWM outputs
00007 PwmOut gled(LED_GREEN);
00008 PwmOut bled(LED_BLUE);
00009 PwmOut buzzer(D3);                          //used to play music
00010 
00011 float frequency[]= {659,554,659,554,550,494,554,587,494,659,554,440}; //frequency array
00012 uint8_t beat[]= {2,2,2,2,2,1,1,2,2,2,2,4};  //beat array
00013 
00014 void music(void const *args)
00015 {
00016     while (1) {
00017         for (int i=0; i<12; i++) {
00018             buzzer.period(1/frequency[i]);  // set PWM period
00019             buzzer=0.5;                     // set duty cycle
00020             Thread::wait(250*beat[i]);      // hold for beat period
00021         }
00022     }
00023 }
00024 
00025 void thermometer(void const *args)
00026 {
00027     AnalogIn ain(A0);                       // Analog input at PTB0
00028     uint32_t mysum;                         // Used for summation
00029 
00030     printf("\r\nTask3: analog thermometer - with averaging\r\n");
00031     while(1) {
00032         mysum = 0;
00033         for(int i=0; i<3300; i++) {
00034             mysum += ain.read_u16();        // sum up raw 16-bit data
00035         }
00036         float voltage = mysum>>16;          // voltage in millivolts
00037         float tempC = (voltage -500)/10;    // tempereature in Celsius
00038         printf("voltage: %5.0f mV temp: %5.1f C\r\n",voltage,tempC);
00039         Thread::wait(2000);
00040     }
00041 }
00042 
00043 
00044 int main(void)
00045 {
00046     Thread thread2(music);                  //Define a new task
00047     thread2.set_priority(osPriorityHigh);   //Give it high priority
00048 
00049     Thread thread3(thermometer);            //Define another new task
00050     thread3.set_priority(osPriorityLow);    //Give it high priority
00051 
00052     while (true) {                          //Run the default task
00053         float x, y, z;
00054         x = abs(acc.getAccX());             //Read X component of acceleration
00055         y = abs(acc.getAccY());             //Read Y component of acceleration
00056         z = abs(acc.getAccZ());             //Read Z component of acceleration
00057         rled = 1.0f - x;                    //Negative logic is used as the LEDs
00058         gled = 1.0f - y;                    //are of common anode type...
00059         bled = 1.0f - z;
00060         Thread::wait(100);                  //Time period is ~ 100 ms
00061     }
00062 }