threads mbed

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem

Committer:
avadivel3
Date:
Wed Nov 20 01:52:21 2019 +0000
Revision:
0:16571db7d250
MBED RTOS THREADS/IO EXAMPLE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
avadivel3 0:16571db7d250 1 #include "mbed.h"
avadivel3 0:16571db7d250 2 #include "rtos.h"
avadivel3 0:16571db7d250 3 #include "uLCD_4DGL.h"
avadivel3 0:16571db7d250 4 #include "wave_player.h"
avadivel3 0:16571db7d250 5 #include "SDFileSystem.h"
avadivel3 0:16571db7d250 6
avadivel3 0:16571db7d250 7 RawSerial blue(p28,p27);
avadivel3 0:16571db7d250 8 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
avadivel3 0:16571db7d250 9 DigitalOut led1(LED1);
avadivel3 0:16571db7d250 10 Thread thread;
avadivel3 0:16571db7d250 11 Thread threadA;
avadivel3 0:16571db7d250 12 Thread threadB;
avadivel3 0:16571db7d250 13 Thread threadC;
avadivel3 0:16571db7d250 14 //Thread threadD;
avadivel3 0:16571db7d250 15 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
avadivel3 0:16571db7d250 16 AnalogOut DACout(p18);
avadivel3 0:16571db7d250 17 wave_player waver(&DACout);
avadivel3 0:16571db7d250 18 Mutex stdio_mutex;
avadivel3 0:16571db7d250 19 DigitalOut latch(p15);
avadivel3 0:16571db7d250 20 DigitalOut enable(p16);
avadivel3 0:16571db7d250 21 SPI spi(p11, p12, p13);
avadivel3 0:16571db7d250 22 volatile int flag = 0;
avadivel3 0:16571db7d250 23 char bnum = 0;
avadivel3 0:16571db7d250 24
avadivel3 0:16571db7d250 25
avadivel3 0:16571db7d250 26 void RGB_LED(int red, int green, int blue) {
avadivel3 0:16571db7d250 27 unsigned int low_color=0;
avadivel3 0:16571db7d250 28 unsigned int high_color=0;
avadivel3 0:16571db7d250 29 high_color=(blue<<4)|((red&0x3C0)>>6);
avadivel3 0:16571db7d250 30 low_color=(((red&0x3F)<<10)|(green));
avadivel3 0:16571db7d250 31 spi.write(high_color);
avadivel3 0:16571db7d250 32 spi.write(low_color);
avadivel3 0:16571db7d250 33 latch=1;
avadivel3 0:16571db7d250 34 latch=0;
avadivel3 0:16571db7d250 35 }
avadivel3 0:16571db7d250 36
avadivel3 0:16571db7d250 37 void threadBT(){
avadivel3 0:16571db7d250 38 while(1) {
avadivel3 0:16571db7d250 39 if (blue.getc()=='!') {
avadivel3 0:16571db7d250 40 if (blue.getc()=='B') { //button data
avadivel3 0:16571db7d250 41 bnum = blue.getc(); //button number
avadivel3 0:16571db7d250 42 if ((bnum=='4')){ //is a number button 1..4
avadivel3 0:16571db7d250 43 uLCD.cls(); //turn on/off that num LED
avadivel3 0:16571db7d250 44 uLCD.printf("HELLO BT!");}
avadivel3 0:16571db7d250 45 }
avadivel3 0:16571db7d250 46 }
avadivel3 0:16571db7d250 47 }
avadivel3 0:16571db7d250 48 }
avadivel3 0:16571db7d250 49
avadivel3 0:16571db7d250 50 void thread1(){
avadivel3 0:16571db7d250 51 while(true) {
avadivel3 0:16571db7d250 52
avadivel3 0:16571db7d250 53 stdio_mutex.lock();
avadivel3 0:16571db7d250 54 uLCD.cls();
avadivel3 0:16571db7d250 55 uLCD.printf("Pink Panther");
avadivel3 0:16571db7d250 56 uLCD.text_width(2);
avadivel3 0:16571db7d250 57 uLCD.text_height(4);
avadivel3 0:16571db7d250 58 uLCD.color(RED);
avadivel3 0:16571db7d250 59 Thread::wait(1000);
avadivel3 0:16571db7d250 60 uLCD.cls();
avadivel3 0:16571db7d250 61 stdio_mutex.unlock();
avadivel3 0:16571db7d250 62
avadivel3 0:16571db7d250 63 }
avadivel3 0:16571db7d250 64 }
avadivel3 0:16571db7d250 65
avadivel3 0:16571db7d250 66 void thread2(){
avadivel3 0:16571db7d250 67 FILE *wave_file;
avadivel3 0:16571db7d250 68 printf("\n\n\nHello, wave world!\n");
avadivel3 0:16571db7d250 69 wave_file=fopen("/sd/mydir/sdtest.wav","r");
avadivel3 0:16571db7d250 70 waver.play(wave_file);
avadivel3 0:16571db7d250 71 fclose(wave_file);
avadivel3 0:16571db7d250 72 }
avadivel3 0:16571db7d250 73
avadivel3 0:16571db7d250 74 void thread3(){
avadivel3 0:16571db7d250 75
avadivel3 0:16571db7d250 76 int red=0;
avadivel3 0:16571db7d250 77 int green=0;
avadivel3 0:16571db7d250 78 int blue=0;
avadivel3 0:16571db7d250 79 spi.format(16,0);
avadivel3 0:16571db7d250 80 spi.frequency(500000);
avadivel3 0:16571db7d250 81 enable=0;
avadivel3 0:16571db7d250 82 latch=0;
avadivel3 0:16571db7d250 83 wait(2);
avadivel3 0:16571db7d250 84 for (red = 0; red<50; red = red+10) {
avadivel3 0:16571db7d250 85 for (blue = 0; blue<50; blue = blue+10) {
avadivel3 0:16571db7d250 86 for (green = 0; green<50; green = green+10)
avadivel3 0:16571db7d250 87
avadivel3 0:16571db7d250 88 {
avadivel3 0:16571db7d250 89 RGB_LED( red, green, blue);
avadivel3 0:16571db7d250 90 wait(.25);
avadivel3 0:16571db7d250 91 }
avadivel3 0:16571db7d250 92 }
avadivel3 0:16571db7d250 93 }
avadivel3 0:16571db7d250 94
avadivel3 0:16571db7d250 95 }
avadivel3 0:16571db7d250 96
avadivel3 0:16571db7d250 97
avadivel3 0:16571db7d250 98 int main()
avadivel3 0:16571db7d250 99 {
avadivel3 0:16571db7d250 100 thread.start(thread1);
avadivel3 0:16571db7d250 101 threadA.start(thread2);
avadivel3 0:16571db7d250 102 threadB.start(thread3);
avadivel3 0:16571db7d250 103 threadC.start(threadBT);
avadivel3 0:16571db7d250 104 // threadD.start(threadBT2);
avadivel3 0:16571db7d250 105 set_time(1570189620); // Set RTC time to Fri, 4 Oct 2019 11:47:00
avadivel3 0:16571db7d250 106
avadivel3 0:16571db7d250 107
avadivel3 0:16571db7d250 108 while (true) {
avadivel3 0:16571db7d250 109
avadivel3 0:16571db7d250 110 stdio_mutex.lock();
avadivel3 0:16571db7d250 111
avadivel3 0:16571db7d250 112 time_t seconds = time(NULL);
avadivel3 0:16571db7d250 113
avadivel3 0:16571db7d250 114 //uLCD.printf("Time as seconds since January 1, 1970 = %d\n", seconds);
avadivel3 0:16571db7d250 115
avadivel3 0:16571db7d250 116 uLCD.cls();
avadivel3 0:16571db7d250 117 uLCD.printf("Time is : %s", ctime(&seconds));
avadivel3 0:16571db7d250 118 uLCD.text_width(2);
avadivel3 0:16571db7d250 119 uLCD.text_height(4);
avadivel3 0:16571db7d250 120 uLCD.color(RED);
avadivel3 0:16571db7d250 121
avadivel3 0:16571db7d250 122
avadivel3 0:16571db7d250 123 char buffer[32];
avadivel3 0:16571db7d250 124 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
avadivel3 0:16571db7d250 125 //uLCD.printf("Time as a custom formatted string = %s", buffer);
avadivel3 0:16571db7d250 126
avadivel3 0:16571db7d250 127 wait(5);
avadivel3 0:16571db7d250 128 uLCD.cls();
avadivel3 0:16571db7d250 129 stdio_mutex.unlock();
avadivel3 0:16571db7d250 130
avadivel3 0:16571db7d250 131
avadivel3 0:16571db7d250 132
avadivel3 0:16571db7d250 133 }
avadivel3 0:16571db7d250 134 }
avadivel3 0:16571db7d250 135
avadivel3 0:16571db7d250 136
avadivel3 0:16571db7d250 137
avadivel3 0:16571db7d250 138
avadivel3 0:16571db7d250 139
avadivel3 0:16571db7d250 140
avadivel3 0:16571db7d250 141
avadivel3 0:16571db7d250 142