funzionicchia male thread

Dependencies:   Freetronics_16x2_LCD

Fork of HelloWorld_CCA01M1_mbedOS by vincenzo di capua

Committer:
vidica94
Date:
Fri Jan 19 15:03:29 2018 +0000
Revision:
14:244ec24ee6dd
Parent:
12:d847a3c0cf76
Child:
15:3624055fac29
funzionicchia

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:8d09369ca839 1 #include "mbed.h"
vidica94 12:d847a3c0cf76 2 #include "FATFileSystem.h"
vidica94 12:d847a3c0cf76 3 #include "SDBlockDevice.h"
vidica94 12:d847a3c0cf76 4 #include <stdio.h>
vidica94 12:d847a3c0cf76 5 #include <errno.h>
vidica94 12:d847a3c0cf76 6 #include "freetronicsLCDShield.h"
nikapov 0:8d09369ca839 7 /* Helper header files. */
nikapov 0:8d09369ca839 8 #include "DevI2C.h"
nikapov 0:8d09369ca839 9
nikapov 0:8d09369ca839 10 /* Component specific header files. */
davide.aliprandi@st.com 5:ac729170df5f 11 #include "STA350BW.h"
nikapov 0:8d09369ca839 12
nikapov 0:8d09369ca839 13 /* My song header file. */
nikapov 0:8d09369ca839 14 #include "my_song.h"
nikapov 0:8d09369ca839 15
vidica94 14:244ec24ee6dd 16 //SDBlockDevice bd(PB_5, PB_4, PB_3, PA_15);
vidica94 12:d847a3c0cf76 17 SDBlockDevice bd(PA_7, PA_6, PA_5, PA_8);
vidica94 12:d847a3c0cf76 18 FATFileSystem fs("fs");
vidica94 12:d847a3c0cf76 19 freetronicsLCDShield lcd(D8, D9, D4, D5, D6, D2, D3, A0);
nikapov 0:8d09369ca839 20
nikapov 0:8d09369ca839 21 #define PLAY_STOP_EVENT (0x1)
vidica94 14:244ec24ee6dd 22 int16_t buff[3000] = {0};
nikapov 0:8d09369ca839 23
nikapov 0:8d09369ca839 24 /* Variables -----------------------------------------------------------------*/
nikapov 0:8d09369ca839 25
nikapov 0:8d09369ca839 26 /* Initialization parameters. */
Davidroid 7:94e3191477c5 27 STA350BW_init_t init =
nikapov 0:8d09369ca839 28 {
vidica94 14:244ec24ee6dd 29 48000, /* Default Sampling Frequency [Hz]. */
nikapov 0:8d09369ca839 30 100 /* Default Volume. */
nikapov 0:8d09369ca839 31 };
nikapov 0:8d09369ca839 32
nikapov 0:8d09369ca839 33 /* Sound Terminal Component. */
nikapov 0:8d09369ca839 34 STA350BW *sound_terminal;
nikapov 0:8d09369ca839 35
nikapov 0:8d09369ca839 36 /* Thread to manage I2S peripherals. */
nikapov 0:8d09369ca839 37 static rtos::Thread i2s_bh_daemon;
nikapov 0:8d09369ca839 38
nikapov 0:8d09369ca839 39 /* Threads. */
nikapov 0:8d09369ca839 40 Thread *play_stop_thread;
nikapov 0:8d09369ca839 41
nikapov 0:8d09369ca839 42 /* User button handling. */
nikapov 0:8d09369ca839 43 InterruptIn event(USER_BUTTON);
nikapov 0:8d09369ca839 44
nikapov 0:8d09369ca839 45
nikapov 0:8d09369ca839 46 /* Functions -----------------------------------------------------------------*/
nikapov 0:8d09369ca839 47
nikapov 0:8d09369ca839 48 /**
nikapov 0:8d09369ca839 49 * @brief User button handler function.
nikapov 0:8d09369ca839 50 * @param None.
nikapov 0:8d09369ca839 51 * @retval None.
nikapov 0:8d09369ca839 52 */
nikapov 0:8d09369ca839 53 void pressed(void)
nikapov 0:8d09369ca839 54 {
nikapov 0:8d09369ca839 55 /* Signal to play/stop handler thread. */
nikapov 0:8d09369ca839 56 play_stop_thread->signal_set(PLAY_STOP_EVENT);
nikapov 0:8d09369ca839 57 }
nikapov 0:8d09369ca839 58
nikapov 0:8d09369ca839 59 /**
nikapov 0:8d09369ca839 60 * @brief Play/stop handler function.
nikapov 0:8d09369ca839 61 * @param None.
nikapov 0:8d09369ca839 62 * @retval None.
nikapov 0:8d09369ca839 63 */
davide.aliprandi@st.com 5:ac729170df5f 64 void play_stop_handler(void)
nikapov 0:8d09369ca839 65 {
nikapov 0:8d09369ca839 66 while (true)
nikapov 0:8d09369ca839 67 {
nikapov 0:8d09369ca839 68 static bool stop = true;
nikapov 0:8d09369ca839 69
nikapov 0:8d09369ca839 70 /* Waiting for play/stop events. */
nikapov 0:8d09369ca839 71 play_stop_thread->signal_wait(PLAY_STOP_EVENT);
nikapov 0:8d09369ca839 72
nikapov 0:8d09369ca839 73 if (stop)
davide.aliprandi@st.com 5:ac729170df5f 74 sound_terminal->stop();
nikapov 0:8d09369ca839 75 else
davide.aliprandi@st.com 5:ac729170df5f 76 sound_terminal->play((int16_t *) my_song, (uint16_t) sizeof(my_song), true);
nikapov 0:8d09369ca839 77 printf("--> %s\r\n", stop ? "Stop." : "Playing...");
nikapov 0:8d09369ca839 78
nikapov 0:8d09369ca839 79 stop = !stop;
nikapov 0:8d09369ca839 80 }
vidica94 12:d847a3c0cf76 81 }
vidica94 12:d847a3c0cf76 82
vidica94 12:d847a3c0cf76 83
vidica94 12:d847a3c0cf76 84
vidica94 12:d847a3c0cf76 85 void return_error(int ret_val){
vidica94 12:d847a3c0cf76 86 if (ret_val)
vidica94 12:d847a3c0cf76 87 printf("Failure. %d\r\n", ret_val);
vidica94 12:d847a3c0cf76 88 else
vidica94 12:d847a3c0cf76 89 printf("done.\r\n");
nikapov 0:8d09369ca839 90 }
nikapov 0:8d09369ca839 91
vidica94 12:d847a3c0cf76 92 void errno_error(void* ret_val){
vidica94 12:d847a3c0cf76 93 if (ret_val == NULL)
vidica94 12:d847a3c0cf76 94 printf(" Failure. %d \r\n", errno);
vidica94 12:d847a3c0cf76 95 else
vidica94 12:d847a3c0cf76 96 printf(" done.\r\n");
vidica94 12:d847a3c0cf76 97 }
vidica94 12:d847a3c0cf76 98
vidica94 12:d847a3c0cf76 99 int main() {
vidica94 12:d847a3c0cf76 100
vidica94 12:d847a3c0cf76 101 /*----- Initialization. -----*/
nikapov 0:8d09369ca839 102
nikapov 0:8d09369ca839 103 /* Initializing I2C bus. */
Davidroid 4:6bb9e11d322b 104 DevI2C *dev_i2c = new DevI2C(PB_9, PB_8);
nikapov 0:8d09369ca839 105
nikapov 0:8d09369ca839 106 /* Initializing Sound Terminal Component. */
nikapov 0:8d09369ca839 107 #ifndef USE_I2S2
nikapov 0:8d09369ca839 108 sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_1, *dev_i2c, PB_15, PB_13, PB_12, NC, PC_6);
nikapov 0:8d09369ca839 109 #else
nikapov 0:8d09369ca839 110 sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7);
nikapov 0:8d09369ca839 111 #endif
davide.aliprandi@st.com 5:ac729170df5f 112 if (sound_terminal->init(&init) != COMPONENT_OK)
nikapov 0:8d09369ca839 113 {
nikapov 0:8d09369ca839 114 error("Initialization of the Sound Terminal Expansion Board failed.\r\n");
nikapov 0:8d09369ca839 115 exit(EXIT_FAILURE);
nikapov 0:8d09369ca839 116 }
nikapov 0:8d09369ca839 117
nikapov 0:8d09369ca839 118 /* Starting a thread to manage I2S peripherals. */
nikapov 0:8d09369ca839 119 Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever);
nikapov 0:8d09369ca839 120 i2s_bh_daemon.start(i2s_bh_task);
nikapov 0:8d09369ca839 121
nikapov 0:8d09369ca839 122 /* Scheduling the play/stop function. */
nikapov 0:8d09369ca839 123 play_stop_thread = new Thread();
davide.aliprandi@st.com 5:ac729170df5f 124 osStatus status = play_stop_thread->start(play_stop_handler);
nikapov 0:8d09369ca839 125 if (status != osOK)
nikapov 0:8d09369ca839 126 printf("Could not start the play/stop handler thread.\r\n");
nikapov 0:8d09369ca839 127 event.fall(&pressed);
nikapov 0:8d09369ca839 128
nikapov 0:8d09369ca839 129 /* Setting Sound Terminal Component's parameters. */
davide.aliprandi@st.com 5:ac729170df5f 130 sound_terminal->set_frequency(MY_SONG_AUDIO_FREQUENCY);
davide.aliprandi@st.com 5:ac729170df5f 131 sound_terminal->set_volume(STA350BW_CHANNEL_MASTER, 60);
nikapov 0:8d09369ca839 132
nikapov 0:8d09369ca839 133 /* Printing to the console. */
nikapov 0:8d09369ca839 134 printf("Sound Terminal Application Example\r\n\n");
nikapov 0:8d09369ca839 135
vidica94 12:d847a3c0cf76 136
vidica94 12:d847a3c0cf76 137
vidica94 12:d847a3c0cf76 138
vidica94 12:d847a3c0cf76 139
vidica94 12:d847a3c0cf76 140
vidica94 12:d847a3c0cf76 141
vidica94 12:d847a3c0cf76 142
vidica94 12:d847a3c0cf76 143
vidica94 12:d847a3c0cf76 144
vidica94 12:d847a3c0cf76 145 // turn on the back light (it's off by default)
vidica94 12:d847a3c0cf76 146 lcd.setBackLight(true);
vidica94 12:d847a3c0cf76 147
vidica94 12:d847a3c0cf76 148 int error = 0;
vidica94 12:d847a3c0cf76 149 printf("Welcome to the filesystem example.\r\n"
vidica94 12:d847a3c0cf76 150 "Formatting a FAT, RAM-backed filesystem. ");
vidica94 12:d847a3c0cf76 151 //error = FATFileSystem::format(&bd);
vidica94 12:d847a3c0cf76 152 return_error(error);
nikapov 0:8d09369ca839 153
vidica94 12:d847a3c0cf76 154 printf("Mounting the filesystem on \"/fs\". ");
vidica94 12:d847a3c0cf76 155 error = fs.mount(&bd);
vidica94 12:d847a3c0cf76 156 return_error(error);
vidica94 12:d847a3c0cf76 157
vidica94 12:d847a3c0cf76 158
vidica94 12:d847a3c0cf76 159 printf(" done.\r\n");
nikapov 0:8d09369ca839 160
vidica94 12:d847a3c0cf76 161 printf("Re-opening file read-only.");
vidica94 14:244ec24ee6dd 162 FILE* fd = fopen("/fs/campagnola.wav", "r");
vidica94 12:d847a3c0cf76 163 errno_error(fd);
vidica94 14:244ec24ee6dd 164 int16_t a;
vidica94 12:d847a3c0cf76 165 char c;
vidica94 12:d847a3c0cf76 166 printf("Dumping file to screen.\r\n");
vidica94 14:244ec24ee6dd 167
vidica94 12:d847a3c0cf76 168 while (!feof(fd)){
vidica94 14:244ec24ee6dd 169
vidica94 12:d847a3c0cf76 170 // printf("inizo file to screen.\r\n");
vidica94 14:244ec24ee6dd 171 //fscanf( fd , "%c%c" , & a ) ;
vidica94 14:244ec24ee6dd 172 //fscanf( fd , "%c" , & c) ;
vidica94 14:244ec24ee6dd 173 fread(buff,sizeof(int16_t),3000,fd);
vidica94 14:244ec24ee6dd 174 //int b=(((int)a*256)+((int )c));
vidica94 14:244ec24ee6dd 175 //b=b-32768;
vidica94 14:244ec24ee6dd 176 //printf("%d \r\n",b);
vidica94 12:d847a3c0cf76 177 //printf ( "%c-" , a[1]) ;printf ( "%c\r\n" , a[0]) ;
vidica94 12:d847a3c0cf76 178 //printf("--> Playing...\r\n");
vidica94 14:244ec24ee6dd 179 //printf ( "%d\r\n " , buff[i] ) ;
nikapov 0:8d09369ca839 180
vidica94 14:244ec24ee6dd 181 //buff[i]=a;
vidica94 12:d847a3c0cf76 182
vidica94 14:244ec24ee6dd 183
vidica94 14:244ec24ee6dd 184
vidica94 12:d847a3c0cf76 185 // int size = fread(&buff[0], 1, 159, fd);
vidica94 12:d847a3c0cf76 186 //fwrite(&buff[0], 1, size, stdout);
vidica94 12:d847a3c0cf76 187
vidica94 12:d847a3c0cf76 188 sound_terminal->play((int16_t *) buff, (uint16_t) sizeof(buff), true );
vidica94 12:d847a3c0cf76 189 }
vidica94 12:d847a3c0cf76 190 printf("EOF.\r\n");
vidica94 12:d847a3c0cf76 191
vidica94 12:d847a3c0cf76 192 printf("Closing file.");
vidica94 12:d847a3c0cf76 193 fclose(fd);
vidica94 12:d847a3c0cf76 194 printf(" done.\r\n");
nikapov 0:8d09369ca839 195
vidica94 12:d847a3c0cf76 196 printf("Opening root directory.");
vidica94 12:d847a3c0cf76 197 DIR* dir = opendir("/fs/");
vidica94 12:d847a3c0cf76 198 errno_error(fd);
vidica94 12:d847a3c0cf76 199
vidica94 12:d847a3c0cf76 200 struct dirent* de;
vidica94 12:d847a3c0cf76 201 printf("Printing all filenames:\r\n");
vidica94 12:d847a3c0cf76 202 while((de = readdir(dir)) != NULL){
vidica94 12:d847a3c0cf76 203 printf(" %s\r\n", &(de->d_name)[0]);
vidica94 12:d847a3c0cf76 204 }
vidica94 12:d847a3c0cf76 205
vidica94 12:d847a3c0cf76 206 printf("Closeing root directory. ");
vidica94 12:d847a3c0cf76 207 error = closedir(dir);
vidica94 12:d847a3c0cf76 208 return_error(error);
vidica94 12:d847a3c0cf76 209 printf("Filesystem Demo complete.\r\n");
vidica94 12:d847a3c0cf76 210
vidica94 12:d847a3c0cf76 211 while (true) {}
vidica94 12:d847a3c0cf76 212 }