![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
guiiiii
Dependencies: mbed wave_player mbed-rtos SDFileSystem
main.cpp@3:f53cb35273f8, 2020-12-03 (annotated)
- Committer:
- dimitryjl23
- Date:
- Thu Dec 03 19:41:11 2020 +0000
- Revision:
- 3:f53cb35273f8
- Parent:
- 2:b2c4f3766090
some changes;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
shyamgatech | 2:b2c4f3766090 | 2 | #include "rtos.h" |
mbed_official | 0:bdbd3d6fc5d5 | 3 | #include "SDFileSystem.h" |
shyamgatech | 2:b2c4f3766090 | 4 | #include "wave_player.h" |
shyamgatech | 2:b2c4f3766090 | 5 | #include <string> |
shyamgatech | 2:b2c4f3766090 | 6 | |
shyamgatech | 2:b2c4f3766090 | 7 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
shyamgatech | 2:b2c4f3766090 | 8 | Serial pc (USBTX,USBRX); |
shyamgatech | 2:b2c4f3766090 | 9 | DigitalOut led(LED1); |
shyamgatech | 2:b2c4f3766090 | 10 | AnalogOut DACout(p18); // speaker |
shyamgatech | 2:b2c4f3766090 | 11 | wave_player waver(&DACout); |
shyamgatech | 2:b2c4f3766090 | 12 | //#define MAXLINE 100 |
shyamgatech | 2:b2c4f3766090 | 13 | //char myline [MAXLINE]; |
shyamgatech | 2:b2c4f3766090 | 14 | //int i, inch ; |
shyamgatech | 2:b2c4f3766090 | 15 | string dir; |
dimitryjl23 | 3:f53cb35273f8 | 16 | //string song = "africa-toto"; |
shyamgatech | 2:b2c4f3766090 | 17 | string song; |
shyamgatech | 2:b2c4f3766090 | 18 | bool play; |
shyamgatech | 2:b2c4f3766090 | 19 | Mutex speaker_lock; |
shyamgatech | 2:b2c4f3766090 | 20 | |
shyamgatech | 2:b2c4f3766090 | 21 | |
shyamgatech | 2:b2c4f3766090 | 22 | |
shyamgatech | 2:b2c4f3766090 | 23 | void speaker_thread(void const* args) |
shyamgatech | 2:b2c4f3766090 | 24 | { |
shyamgatech | 2:b2c4f3766090 | 25 | while(1) { |
shyamgatech | 2:b2c4f3766090 | 26 | // check helper function for new song in GUI |
shyamgatech | 2:b2c4f3766090 | 27 | // grab file here and put together string |
shyamgatech | 2:b2c4f3766090 | 28 | // string song_title = "/sd/" + "" + ".wav"; |
dimitryjl23 | 3:f53cb35273f8 | 29 | |
dimitryjl23 | 3:f53cb35273f8 | 30 | |
dimitryjl23 | 3:f53cb35273f8 | 31 | if(play == true) |
dimitryjl23 | 3:f53cb35273f8 | 32 | { |
dimitryjl23 | 3:f53cb35273f8 | 33 | //speaker_lock.lock(); |
dimitryjl23 | 3:f53cb35273f8 | 34 | FILE *wave_file; |
dimitryjl23 | 3:f53cb35273f8 | 35 | dir = "/sd/" + song + ".wav"; |
dimitryjl23 | 3:f53cb35273f8 | 36 | wave_file=fopen(dir.c_str(),"r"); |
shyamgatech | 2:b2c4f3766090 | 37 | waver.play(wave_file); |
dimitryjl23 | 3:f53cb35273f8 | 38 | fclose(wave_file); |
dimitryjl23 | 3:f53cb35273f8 | 39 | //speaker_lock.unlock(); |
shyamgatech | 2:b2c4f3766090 | 40 | } |
dimitryjl23 | 3:f53cb35273f8 | 41 | Thread::wait(1000); |
shyamgatech | 2:b2c4f3766090 | 42 | } |
shyamgatech | 2:b2c4f3766090 | 43 | } |
shyamgatech | 2:b2c4f3766090 | 44 | |
shyamgatech | 2:b2c4f3766090 | 45 | |
shyamgatech | 2:b2c4f3766090 | 46 | |
shyamgatech | 2:b2c4f3766090 | 47 | |
shyamgatech | 2:b2c4f3766090 | 48 | |
shyamgatech | 2:b2c4f3766090 | 49 | |
shyamgatech | 2:b2c4f3766090 | 50 | |
shyamgatech | 2:b2c4f3766090 | 51 | |
shyamgatech | 2:b2c4f3766090 | 52 | |
shyamgatech | 2:b2c4f3766090 | 53 | |
shyamgatech | 2:b2c4f3766090 | 54 | |
shyamgatech | 2:b2c4f3766090 | 55 | |
shyamgatech | 2:b2c4f3766090 | 56 | int main() |
shyamgatech | 2:b2c4f3766090 | 57 | { |
dimitryjl23 | 3:f53cb35273f8 | 58 | |
dimitryjl23 | 3:f53cb35273f8 | 59 | char c; |
shyamgatech | 2:b2c4f3766090 | 60 | Thread speaker(speaker_thread); |
dimitryjl23 | 3:f53cb35273f8 | 61 | //led = 1; |
shyamgatech | 2:b2c4f3766090 | 62 | while(1) { |
shyamgatech | 2:b2c4f3766090 | 63 | wait(0.2); |
dimitryjl23 | 3:f53cb35273f8 | 64 | |
shyamgatech | 2:b2c4f3766090 | 65 | while(!pc.readable()) { |
shyamgatech | 2:b2c4f3766090 | 66 | Thread::wait(1); |
shyamgatech | 2:b2c4f3766090 | 67 | } |
shyamgatech | 2:b2c4f3766090 | 68 | if (pc.getc() == '!') { |
shyamgatech | 2:b2c4f3766090 | 69 | c = pc.getc(); |
shyamgatech | 2:b2c4f3766090 | 70 | switch(c) { |
shyamgatech | 2:b2c4f3766090 | 71 | case '1': |
shyamgatech | 2:b2c4f3766090 | 72 | song = "africa-toto"; |
dimitryjl23 | 3:f53cb35273f8 | 73 | //led = 1; |
shyamgatech | 2:b2c4f3766090 | 74 | break; |
shyamgatech | 2:b2c4f3766090 | 75 | case '2': |
shyamgatech | 2:b2c4f3766090 | 76 | song = "around_the_world-atc"; |
shyamgatech | 2:b2c4f3766090 | 77 | break; |
shyamgatech | 2:b2c4f3766090 | 78 | case '3': |
shyamgatech | 2:b2c4f3766090 | 79 | song = "beautiful_life-ace_of_base"; |
shyamgatech | 2:b2c4f3766090 | 80 | break; |
shyamgatech | 2:b2c4f3766090 | 81 | case '4': |
shyamgatech | 2:b2c4f3766090 | 82 | song = "dont_speak-no_doubt"; |
shyamgatech | 2:b2c4f3766090 | 83 | break; |
shyamgatech | 2:b2c4f3766090 | 84 | case '5': |
shyamgatech | 2:b2c4f3766090 | 85 | song = "my-love"; |
shyamgatech | 2:b2c4f3766090 | 86 | break; |
shyamgatech | 2:b2c4f3766090 | 87 | case '6': |
shyamgatech | 2:b2c4f3766090 | 88 | song = "Song1_test"; |
dimitryjl23 | 3:f53cb35273f8 | 89 | //speaker.terminate(); |
shyamgatech | 2:b2c4f3766090 | 90 | break; |
shyamgatech | 2:b2c4f3766090 | 91 | case 'P': |
shyamgatech | 2:b2c4f3766090 | 92 | // wave_player plays |
shyamgatech | 2:b2c4f3766090 | 93 | play = true; |
shyamgatech | 2:b2c4f3766090 | 94 | break; |
shyamgatech | 2:b2c4f3766090 | 95 | case 'S': |
shyamgatech | 2:b2c4f3766090 | 96 | // wave_player stops |
shyamgatech | 2:b2c4f3766090 | 97 | play = false; |
dimitryjl23 | 3:f53cb35273f8 | 98 | //led!=led; |
dimitryjl23 | 3:f53cb35273f8 | 99 | //speaker.terminate(); |
shyamgatech | 2:b2c4f3766090 | 100 | break; |
shyamgatech | 2:b2c4f3766090 | 101 | default: |
shyamgatech | 2:b2c4f3766090 | 102 | break; |
shyamgatech | 2:b2c4f3766090 | 103 | } |
dimitryjl23 | 3:f53cb35273f8 | 104 | |
dimitryjl23 | 3:f53cb35273f8 | 105 | if(play==true){led=1;} |
dimitryjl23 | 3:f53cb35273f8 | 106 | else{led=0;} |
shyamgatech | 2:b2c4f3766090 | 107 | } |
shyamgatech | 2:b2c4f3766090 | 108 | Thread::wait(200); |
shyamgatech | 2:b2c4f3766090 | 109 | } |
shyamgatech | 2:b2c4f3766090 | 110 | } |
shyamgatech | 2:b2c4f3766090 | 111 | |
shyamgatech | 2:b2c4f3766090 | 112 | |
shyamgatech | 2:b2c4f3766090 | 113 | |
shyamgatech | 2:b2c4f3766090 | 114 | |
shyamgatech | 2:b2c4f3766090 | 115 | |
shyamgatech | 2:b2c4f3766090 | 116 | |
shyamgatech | 2:b2c4f3766090 | 117 | |
shyamgatech | 2:b2c4f3766090 | 118 | //============================================================================// |
shyamgatech | 2:b2c4f3766090 | 119 | /* |
mbed_official | 0:bdbd3d6fc5d5 | 120 | int main() { |
shyamgatech | 2:b2c4f3766090 | 121 | printf("\n\r= = = = = = = = = ="); |
shyamgatech | 2:b2c4f3766090 | 122 | printf("\n\rHello World!\n\r"); |
shyamgatech | 2:b2c4f3766090 | 123 | mkdir("/sd/mydir", 0777); // Test that previous run had created permanent folder |
shyamgatech | 2:b2c4f3766090 | 124 | // write |
shyamgatech | 2:b2c4f3766090 | 125 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); // Commenting out, in a later run, from here .. |
mbed_official | 0:bdbd3d6fc5d5 | 126 | if(fp == NULL) { |
mbed_official | 0:bdbd3d6fc5d5 | 127 | error("Could not open file for write\n"); |
mbed_official | 0:bdbd3d6fc5d5 | 128 | } |
shyamgatech | 2:b2c4f3766090 | 129 | fprintf(fp, "Hello from SD Card World!"); |
shyamgatech | 2:b2c4f3766090 | 130 | fclose(fp); |
shyamgatech | 2:b2c4f3766090 | 131 | printf("Created file in sdcard\n\r"); //.. to here shows file really is permanent .. .. since it still reads correctly. |
shyamgatech | 2:b2c4f3766090 | 132 | // read |
shyamgatech | 2:b2c4f3766090 | 133 | printf("Reading file\n"); |
shyamgatech | 2:b2c4f3766090 | 134 | string inputString; |
shyamgatech | 2:b2c4f3766090 | 135 | FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r"); |
shyamgatech | 2:b2c4f3766090 | 136 | if (fp1 == NULL) { |
shyamgatech | 2:b2c4f3766090 | 137 | printf("Error Open \n"); |
shyamgatech | 2:b2c4f3766090 | 138 | } else { |
shyamgatech | 2:b2c4f3766090 | 139 | printf("\r"); |
shyamgatech | 2:b2c4f3766090 | 140 | while(fscanf(fp1, "%s", inputString)!=EOF) { |
shyamgatech | 2:b2c4f3766090 | 141 | printf("%s ", inputString.c_str()); |
shyamgatech | 2:b2c4f3766090 | 142 | } |
shyamgatech | 2:b2c4f3766090 | 143 | } |
shyamgatech | 2:b2c4f3766090 | 144 | fclose(fp1); // close the file |
shyamgatech | 2:b2c4f3766090 | 145 | printf("\n\rFinished reading the file"); |
shyamgatech | 2:b2c4f3766090 | 146 | printf("\n\r= = = = = = = = = ="); |
shyamgatech | 2:b2c4f3766090 | 147 | |
shyamgatech | 2:b2c4f3766090 | 148 | return 0; |
mbed_official | 0:bdbd3d6fc5d5 | 149 | } |
shyamgatech | 2:b2c4f3766090 | 150 | */ |