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

Committer:
icserny
Date:
Thu Jan 28 15:26:56 2016 +0000
Revision:
0:d3948113d6d9
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icserny 0:d3948113d6d9 1 #include "mbed.h"
icserny 0:d3948113d6d9 2 #include "rtos.h"
icserny 0:d3948113d6d9 3 #include "MMA8451Q.h"
icserny 0:d3948113d6d9 4
icserny 0:d3948113d6d9 5 MMA8451Q acc(PTE25,PTE24,0x3A); //SDA, SCL, I2C address lef shifted
icserny 0:d3948113d6d9 6 PwmOut rled(LED_RED); //configure RGB LED pins as PWM outputs
icserny 0:d3948113d6d9 7 PwmOut gled(LED_GREEN);
icserny 0:d3948113d6d9 8 PwmOut bled(LED_BLUE);
icserny 0:d3948113d6d9 9 PwmOut buzzer(D3); //used to play music
icserny 0:d3948113d6d9 10
icserny 0:d3948113d6d9 11 float frequency[]= {659,554,659,554,550,494,554,587,494,659,554,440}; //frequency array
icserny 0:d3948113d6d9 12 uint8_t beat[]= {2,2,2,2,2,1,1,2,2,2,2,4}; //beat array
icserny 0:d3948113d6d9 13
icserny 0:d3948113d6d9 14 void music(void const *args)
icserny 0:d3948113d6d9 15 {
icserny 0:d3948113d6d9 16 while (1) {
icserny 0:d3948113d6d9 17 for (int i=0; i<12; i++) {
icserny 0:d3948113d6d9 18 buzzer.period(1/frequency[i]); // set PWM period
icserny 0:d3948113d6d9 19 buzzer=0.5; // set duty cycle
icserny 0:d3948113d6d9 20 Thread::wait(250*beat[i]); // hold for beat period
icserny 0:d3948113d6d9 21 }
icserny 0:d3948113d6d9 22 }
icserny 0:d3948113d6d9 23 }
icserny 0:d3948113d6d9 24
icserny 0:d3948113d6d9 25 void thermometer(void const *args)
icserny 0:d3948113d6d9 26 {
icserny 0:d3948113d6d9 27 AnalogIn ain(A0); // Analog input at PTB0
icserny 0:d3948113d6d9 28 uint32_t mysum; // Used for summation
icserny 0:d3948113d6d9 29
icserny 0:d3948113d6d9 30 printf("\r\nTask3: analog thermometer - with averaging\r\n");
icserny 0:d3948113d6d9 31 while(1) {
icserny 0:d3948113d6d9 32 mysum = 0;
icserny 0:d3948113d6d9 33 for(int i=0; i<3300; i++) {
icserny 0:d3948113d6d9 34 mysum += ain.read_u16(); // sum up raw 16-bit data
icserny 0:d3948113d6d9 35 }
icserny 0:d3948113d6d9 36 float voltage = mysum>>16; // voltage in millivolts
icserny 0:d3948113d6d9 37 float tempC = (voltage -500)/10; // tempereature in Celsius
icserny 0:d3948113d6d9 38 printf("voltage: %5.0f mV temp: %5.1f C\r\n",voltage,tempC);
icserny 0:d3948113d6d9 39 Thread::wait(2000);
icserny 0:d3948113d6d9 40 }
icserny 0:d3948113d6d9 41 }
icserny 0:d3948113d6d9 42
icserny 0:d3948113d6d9 43
icserny 0:d3948113d6d9 44 int main(void)
icserny 0:d3948113d6d9 45 {
icserny 0:d3948113d6d9 46 Thread thread2(music); //Define a new task
icserny 0:d3948113d6d9 47 thread2.set_priority(osPriorityHigh); //Give it high priority
icserny 0:d3948113d6d9 48
icserny 0:d3948113d6d9 49 Thread thread3(thermometer); //Define another new task
icserny 0:d3948113d6d9 50 thread3.set_priority(osPriorityLow); //Give it high priority
icserny 0:d3948113d6d9 51
icserny 0:d3948113d6d9 52 while (true) { //Run the default task
icserny 0:d3948113d6d9 53 float x, y, z;
icserny 0:d3948113d6d9 54 x = abs(acc.getAccX()); //Read X component of acceleration
icserny 0:d3948113d6d9 55 y = abs(acc.getAccY()); //Read Y component of acceleration
icserny 0:d3948113d6d9 56 z = abs(acc.getAccZ()); //Read Z component of acceleration
icserny 0:d3948113d6d9 57 rled = 1.0f - x; //Negative logic is used as the LEDs
icserny 0:d3948113d6d9 58 gled = 1.0f - y; //are of common anode type...
icserny 0:d3948113d6d9 59 bled = 1.0f - z;
icserny 0:d3948113d6d9 60 Thread::wait(100); //Time period is ~ 100 ms
icserny 0:d3948113d6d9 61 }
icserny 0:d3948113d6d9 62 }