4 thread with comments
Dependencies: C12832 LM75B mbed-rtos mbed
Fork of rtos_basic by
main.cpp@12:2b54f52b1034, 2015-02-13 (annotated)
- Committer:
- cathal66
- Date:
- Fri Feb 13 14:06:33 2015 +0000
- Revision:
- 12:2b54f52b1034
- Parent:
- 8:0f773a5937f0
Four thread running with commits
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cathal66 | 12:2b54f52b1034 | 1 | #include "mbed.h" //Include the mbed.h file |
cathal66 | 12:2b54f52b1034 | 2 | #include "rtos.h" //include the RTOS.h file |
cathal66 | 12:2b54f52b1034 | 3 | #include "LM75B.h" //include the LM75B.h file |
cathal66 | 12:2b54f52b1034 | 4 | #include "C12832.h" //include the C12832.h file |
cathal66 | 8:0f773a5937f0 | 5 | |
cathal66 | 12:2b54f52b1034 | 6 | C12832 lcd(p5, p7, p6, p8, p11); //Setup the LCD |
cathal66 | 8:0f773a5937f0 | 7 | |
cathal66 | 12:2b54f52b1034 | 8 | LM75B sensor(p28,p27); //Setup the Temp Sensor I2C |
cathal66 | 12:2b54f52b1034 | 9 | Serial pc(USBTX,USBRX); //Setup the serial comm |
cathal66 | 8:0f773a5937f0 | 10 | |
cathal66 | 12:2b54f52b1034 | 11 | float Temp_Value; //global var for temp |
cathal66 | 8:0f773a5937f0 | 12 | |
cathal66 | 12:2b54f52b1034 | 13 | PwmOut spkr(p26); //PWM speaker |
cathal66 | 12:2b54f52b1034 | 14 | PwmOut LED_red(p23); //PWM LED red |
cathal66 | 12:2b54f52b1034 | 15 | PwmOut LED_blue(p25); //PWM LED blue |
cathal66 | 12:2b54f52b1034 | 16 | |
cathal66 | 12:2b54f52b1034 | 17 | DigitalOut led1(LED1); //Setup LED1 to the varible name led1 |
cathal66 | 12:2b54f52b1034 | 18 | DigitalOut led2(LED2); //Setup LED2 to the varible name led2 |
cathal66 | 8:0f773a5937f0 | 19 | |
cathal66 | 12:2b54f52b1034 | 20 | Mutex Temp_Mutex; //Setup Mutex |
emilmont | 1:491820ee784d | 21 | |
cathal66 | 8:0f773a5937f0 | 22 | |
cathal66 | 12:2b54f52b1034 | 23 | void Tempature_Senor(void const *args) { //Function or the thread to be called |
cathal66 | 8:0f773a5937f0 | 24 | |
cathal66 | 12:2b54f52b1034 | 25 | |
cathal66 | 12:2b54f52b1034 | 26 | if (sensor.open()) { //Try to open the LM75B |
cathal66 | 12:2b54f52b1034 | 27 | printf("Device detected!\n"); //printf if device connects |
cathal66 | 8:0f773a5937f0 | 28 | |
cathal66 | 8:0f773a5937f0 | 29 | while (1) { |
cathal66 | 12:2b54f52b1034 | 30 | lcd.cls(); //Clears LCD |
cathal66 | 12:2b54f52b1034 | 31 | lcd.locate(0,3); //Locartion on LCD display |
cathal66 | 12:2b54f52b1034 | 32 | Temp_Mutex.lock(); //Mutex Lock sensor |
cathal66 | 12:2b54f52b1034 | 33 | lcd.printf("Temp = %.3f\n", (float)sensor); //Print Temp value on LCD |
cathal66 | 12:2b54f52b1034 | 34 | Temp_Value = (float)sensor; //read sonsor and store in Temp_value |
cathal66 | 12:2b54f52b1034 | 35 | Temp_Mutex.unlock(); //Mutex unlock sensor |
cathal66 | 12:2b54f52b1034 | 36 | Thread::wait(1.0); //Thread wait |
cathal66 | 8:0f773a5937f0 | 37 | } |
cathal66 | 8:0f773a5937f0 | 38 | |
cathal66 | 8:0f773a5937f0 | 39 | } else { |
cathal66 | 12:2b54f52b1034 | 40 | error("Device not detected!\n"); //Printf on serial if error |
cathal66 | 12:2b54f52b1034 | 41 | } //End Super loop |
cathal66 | 8:0f773a5937f0 | 42 | } |
cathal66 | 8:0f773a5937f0 | 43 | |
cathal66 | 12:2b54f52b1034 | 44 | void Speaker(void const *args) { //Function or the thread to be called |
cathal66 | 12:2b54f52b1034 | 45 | while (true) { //Super loop |
cathal66 | 12:2b54f52b1034 | 46 | spkr.period(1.0/5000); //set period |
cathal66 | 12:2b54f52b1034 | 47 | spkr = 0.25; //set PWM |
cathal66 | 12:2b54f52b1034 | 48 | Thread::wait(800); //Thread wait |
cathal66 | 12:2b54f52b1034 | 49 | spkr.period(1.0/3000); //set period |
cathal66 | 12:2b54f52b1034 | 50 | spkr = 0.25; //set PWM |
cathal66 | 12:2b54f52b1034 | 51 | Thread::wait(800); //Thread wait |
cathal66 | 12:2b54f52b1034 | 52 | } //End Super loop |
cathal66 | 8:0f773a5937f0 | 53 | } |
cathal66 | 8:0f773a5937f0 | 54 | |
cathal66 | 12:2b54f52b1034 | 55 | void led_R_B_flash(void const *args) { //Function or the thread to be called |
cathal66 | 12:2b54f52b1034 | 56 | while (true) { //Super loop |
cathal66 | 8:0f773a5937f0 | 57 | |
cathal66 | 12:2b54f52b1034 | 58 | LED_red = 0.5; //PWM LED to half brightness |
cathal66 | 12:2b54f52b1034 | 59 | LED_blue = 1; //Turn blue LED off |
cathal66 | 8:0f773a5937f0 | 60 | Thread::wait(800); |
cathal66 | 12:2b54f52b1034 | 61 | LED_red = 1; //Turn red LED off |
cathal66 | 12:2b54f52b1034 | 62 | LED_blue = 0.5; //PWM LED to half brightness |
cathal66 | 12:2b54f52b1034 | 63 | Thread::wait(800); //Thread wait |
cathal66 | 12:2b54f52b1034 | 64 | } //End Super loop |
cathal66 | 12:2b54f52b1034 | 65 | } //End Function / thread |
emilmont | 1:491820ee784d | 66 | |
cathal66 | 12:2b54f52b1034 | 67 | int main() { //Main |
cathal66 | 12:2b54f52b1034 | 68 | float local_Temp_V=0; //SETup local var |
cathal66 | 12:2b54f52b1034 | 69 | Thread thread_Temp(Tempature_Senor); //start thread |
cathal66 | 12:2b54f52b1034 | 70 | Thread thread_LED_Flash(led_R_B_flash); //start thread |
cathal66 | 12:2b54f52b1034 | 71 | Thread thread_Speaker_Sound(Speaker); //start thread |
emilmont | 1:491820ee784d | 72 | |
cathal66 | 8:0f773a5937f0 | 73 | while (1) { |
cathal66 | 12:2b54f52b1034 | 74 | Temp_Mutex.lock(); //Mutex lock temp value |
cathal66 | 12:2b54f52b1034 | 75 | local_Temp_V = Temp_Value; //store temp value in local variable |
cathal66 | 12:2b54f52b1034 | 76 | Temp_Mutex.unlock(); //mutex unlock |
cathal66 | 12:2b54f52b1034 | 77 | printf("Temp = %.3f\n", local_Temp_V ); //printf on serial of local temp value |
cathal66 | 12:2b54f52b1034 | 78 | Thread::wait(1.0); //thread wait |
cathal66 | 8:0f773a5937f0 | 79 | } |
cathal66 | 8:0f773a5937f0 | 80 | |
cathal66 | 8:0f773a5937f0 | 81 | |
cathal66 | 7:10edddb7f1a8 | 82 | } //End main |