funzionicchia male thread
Dependencies: Freetronics_16x2_LCD
Fork of HelloWorld_CCA01M1_mbedOS by
main.cpp@15:3624055fac29, 2018-01-22 (annotated)
- Committer:
- vidica94
- Date:
- Mon Jan 22 12:20:32 2018 +0000
- Revision:
- 15:3624055fac29
- Parent:
- 14:244ec24ee6dd
con i thread
Who changed what in which revision?
User | Revision | Line number | New 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); |
vidica94 | 15:3624055fac29 | 20 | Thread Leggi_SD_Thread; |
vidica94 | 15:3624055fac29 | 21 | Thread Suona_Thread; |
vidica94 | 15:3624055fac29 | 22 | argomento* arg=(argomento*) malloc(sizeof(argomento)); |
vidica94 | 15:3624055fac29 | 23 | |
vidica94 | 15:3624055fac29 | 24 | |
vidica94 | 15:3624055fac29 | 25 | |
vidica94 | 15:3624055fac29 | 26 | |
vidica94 | 15:3624055fac29 | 27 | |
vidica94 | 15:3624055fac29 | 28 | void Leggi_SD(argomento * arg){ |
vidica94 | 15:3624055fac29 | 29 | |
vidica94 | 15:3624055fac29 | 30 | while(1){ |
vidica94 | 15:3624055fac29 | 31 | |
vidica94 | 15:3624055fac29 | 32 | if(arg->sel == 0){ |
vidica94 | 15:3624055fac29 | 33 | fread(arg->ping,sizeof(int16_t), arg->size, arg->fd); |
vidica94 | 15:3624055fac29 | 34 | }else if(arg->sel == 1){ |
vidica94 | 15:3624055fac29 | 35 | fread(arg->pong, sizeof(int16_t), arg->size, arg->fd); |
vidica94 | 15:3624055fac29 | 36 | } |
vidica94 | 15:3624055fac29 | 37 | } |
vidica94 | 15:3624055fac29 | 38 | |
vidica94 | 15:3624055fac29 | 39 | } |
vidica94 | 15:3624055fac29 | 40 | |
vidica94 | 15:3624055fac29 | 41 | |
nikapov | 0:8d09369ca839 | 42 | |
nikapov | 0:8d09369ca839 | 43 | #define PLAY_STOP_EVENT (0x1) |
vidica94 | 14:244ec24ee6dd | 44 | int16_t buff[3000] = {0}; |
nikapov | 0:8d09369ca839 | 45 | |
nikapov | 0:8d09369ca839 | 46 | /* Variables -----------------------------------------------------------------*/ |
nikapov | 0:8d09369ca839 | 47 | |
nikapov | 0:8d09369ca839 | 48 | /* Initialization parameters. */ |
Davidroid | 7:94e3191477c5 | 49 | STA350BW_init_t init = |
nikapov | 0:8d09369ca839 | 50 | { |
vidica94 | 15:3624055fac29 | 51 | 32000, /* Default Sampling Frequency [Hz]. */ |
nikapov | 0:8d09369ca839 | 52 | 100 /* Default Volume. */ |
nikapov | 0:8d09369ca839 | 53 | }; |
nikapov | 0:8d09369ca839 | 54 | |
nikapov | 0:8d09369ca839 | 55 | /* Sound Terminal Component. */ |
nikapov | 0:8d09369ca839 | 56 | STA350BW *sound_terminal; |
nikapov | 0:8d09369ca839 | 57 | |
nikapov | 0:8d09369ca839 | 58 | /* Thread to manage I2S peripherals. */ |
nikapov | 0:8d09369ca839 | 59 | static rtos::Thread i2s_bh_daemon; |
nikapov | 0:8d09369ca839 | 60 | |
nikapov | 0:8d09369ca839 | 61 | /* Threads. */ |
nikapov | 0:8d09369ca839 | 62 | Thread *play_stop_thread; |
nikapov | 0:8d09369ca839 | 63 | |
nikapov | 0:8d09369ca839 | 64 | /* User button handling. */ |
nikapov | 0:8d09369ca839 | 65 | InterruptIn event(USER_BUTTON); |
nikapov | 0:8d09369ca839 | 66 | |
nikapov | 0:8d09369ca839 | 67 | |
nikapov | 0:8d09369ca839 | 68 | /* Functions -----------------------------------------------------------------*/ |
nikapov | 0:8d09369ca839 | 69 | |
nikapov | 0:8d09369ca839 | 70 | /** |
nikapov | 0:8d09369ca839 | 71 | * @brief User button handler function. |
nikapov | 0:8d09369ca839 | 72 | * @param None. |
nikapov | 0:8d09369ca839 | 73 | * @retval None. |
nikapov | 0:8d09369ca839 | 74 | */ |
nikapov | 0:8d09369ca839 | 75 | void pressed(void) |
nikapov | 0:8d09369ca839 | 76 | { |
nikapov | 0:8d09369ca839 | 77 | /* Signal to play/stop handler thread. */ |
nikapov | 0:8d09369ca839 | 78 | play_stop_thread->signal_set(PLAY_STOP_EVENT); |
nikapov | 0:8d09369ca839 | 79 | } |
nikapov | 0:8d09369ca839 | 80 | |
nikapov | 0:8d09369ca839 | 81 | /** |
nikapov | 0:8d09369ca839 | 82 | * @brief Play/stop handler function. |
nikapov | 0:8d09369ca839 | 83 | * @param None. |
nikapov | 0:8d09369ca839 | 84 | * @retval None. |
nikapov | 0:8d09369ca839 | 85 | */ |
vidica94 | 15:3624055fac29 | 86 | |
davide.aliprandi@st.com | 5:ac729170df5f | 87 | void play_stop_handler(void) |
nikapov | 0:8d09369ca839 | 88 | { |
nikapov | 0:8d09369ca839 | 89 | while (true) |
nikapov | 0:8d09369ca839 | 90 | { |
nikapov | 0:8d09369ca839 | 91 | static bool stop = true; |
nikapov | 0:8d09369ca839 | 92 | |
vidica94 | 15:3624055fac29 | 93 | // Waiting for play/stop events. |
nikapov | 0:8d09369ca839 | 94 | play_stop_thread->signal_wait(PLAY_STOP_EVENT); |
nikapov | 0:8d09369ca839 | 95 | |
nikapov | 0:8d09369ca839 | 96 | if (stop) |
davide.aliprandi@st.com | 5:ac729170df5f | 97 | sound_terminal->stop(); |
nikapov | 0:8d09369ca839 | 98 | else |
vidica94 | 15:3624055fac29 | 99 | // sound_terminal->play((int16_t *) my_song, (uint16_t) sizeof(my_song), true); |
nikapov | 0:8d09369ca839 | 100 | printf("--> %s\r\n", stop ? "Stop." : "Playing..."); |
nikapov | 0:8d09369ca839 | 101 | |
nikapov | 0:8d09369ca839 | 102 | stop = !stop; |
nikapov | 0:8d09369ca839 | 103 | } |
vidica94 | 12:d847a3c0cf76 | 104 | } |
vidica94 | 12:d847a3c0cf76 | 105 | |
vidica94 | 15:3624055fac29 | 106 | |
vidica94 | 12:d847a3c0cf76 | 107 | |
vidica94 | 12:d847a3c0cf76 | 108 | void return_error(int ret_val){ |
vidica94 | 12:d847a3c0cf76 | 109 | if (ret_val) |
vidica94 | 12:d847a3c0cf76 | 110 | printf("Failure. %d\r\n", ret_val); |
vidica94 | 12:d847a3c0cf76 | 111 | else |
vidica94 | 12:d847a3c0cf76 | 112 | printf("done.\r\n"); |
nikapov | 0:8d09369ca839 | 113 | } |
nikapov | 0:8d09369ca839 | 114 | |
vidica94 | 12:d847a3c0cf76 | 115 | void errno_error(void* ret_val){ |
vidica94 | 12:d847a3c0cf76 | 116 | if (ret_val == NULL) |
vidica94 | 12:d847a3c0cf76 | 117 | printf(" Failure. %d \r\n", errno); |
vidica94 | 12:d847a3c0cf76 | 118 | else |
vidica94 | 12:d847a3c0cf76 | 119 | printf(" done.\r\n"); |
vidica94 | 12:d847a3c0cf76 | 120 | } |
vidica94 | 12:d847a3c0cf76 | 121 | |
vidica94 | 15:3624055fac29 | 122 | void Suona(argomento * arg){ |
vidica94 | 15:3624055fac29 | 123 | |
vidica94 | 15:3624055fac29 | 124 | while(1){ |
vidica94 | 15:3624055fac29 | 125 | if(arg->sel == 0){ |
vidica94 | 15:3624055fac29 | 126 | sound_terminal->play((int16_t *) arg->pong, (uint16_t) sizeof(int16_t)*arg->size, true); |
vidica94 | 15:3624055fac29 | 127 | } else if( arg->sel == 1) { |
vidica94 | 15:3624055fac29 | 128 | sound_terminal->play((int16_t *) arg->ping, (uint16_t) sizeof(int16_t)*arg->size, true); |
vidica94 | 15:3624055fac29 | 129 | } |
vidica94 | 15:3624055fac29 | 130 | |
vidica94 | 15:3624055fac29 | 131 | arg->sel = !(arg->sel); |
vidica94 | 15:3624055fac29 | 132 | } |
vidica94 | 15:3624055fac29 | 133 | |
vidica94 | 15:3624055fac29 | 134 | } |
vidica94 | 15:3624055fac29 | 135 | |
vidica94 | 12:d847a3c0cf76 | 136 | int main() { |
vidica94 | 12:d847a3c0cf76 | 137 | |
vidica94 | 12:d847a3c0cf76 | 138 | /*----- Initialization. -----*/ |
vidica94 | 15:3624055fac29 | 139 | arg->sel=0; |
vidica94 | 15:3624055fac29 | 140 | arg->size=4000; |
nikapov | 0:8d09369ca839 | 141 | |
nikapov | 0:8d09369ca839 | 142 | /* Initializing I2C bus. */ |
Davidroid | 4:6bb9e11d322b | 143 | DevI2C *dev_i2c = new DevI2C(PB_9, PB_8); |
nikapov | 0:8d09369ca839 | 144 | |
nikapov | 0:8d09369ca839 | 145 | /* Initializing Sound Terminal Component. */ |
nikapov | 0:8d09369ca839 | 146 | #ifndef USE_I2S2 |
nikapov | 0:8d09369ca839 | 147 | sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_1, *dev_i2c, PB_15, PB_13, PB_12, NC, PC_6); |
nikapov | 0:8d09369ca839 | 148 | #else |
nikapov | 0:8d09369ca839 | 149 | sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7); |
nikapov | 0:8d09369ca839 | 150 | #endif |
davide.aliprandi@st.com | 5:ac729170df5f | 151 | if (sound_terminal->init(&init) != COMPONENT_OK) |
nikapov | 0:8d09369ca839 | 152 | { |
nikapov | 0:8d09369ca839 | 153 | error("Initialization of the Sound Terminal Expansion Board failed.\r\n"); |
nikapov | 0:8d09369ca839 | 154 | exit(EXIT_FAILURE); |
nikapov | 0:8d09369ca839 | 155 | } |
nikapov | 0:8d09369ca839 | 156 | |
nikapov | 0:8d09369ca839 | 157 | /* Starting a thread to manage I2S peripherals. */ |
nikapov | 0:8d09369ca839 | 158 | Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever); |
nikapov | 0:8d09369ca839 | 159 | i2s_bh_daemon.start(i2s_bh_task); |
nikapov | 0:8d09369ca839 | 160 | |
nikapov | 0:8d09369ca839 | 161 | /* Scheduling the play/stop function. */ |
nikapov | 0:8d09369ca839 | 162 | play_stop_thread = new Thread(); |
davide.aliprandi@st.com | 5:ac729170df5f | 163 | osStatus status = play_stop_thread->start(play_stop_handler); |
nikapov | 0:8d09369ca839 | 164 | if (status != osOK) |
nikapov | 0:8d09369ca839 | 165 | printf("Could not start the play/stop handler thread.\r\n"); |
nikapov | 0:8d09369ca839 | 166 | event.fall(&pressed); |
nikapov | 0:8d09369ca839 | 167 | |
nikapov | 0:8d09369ca839 | 168 | /* Setting Sound Terminal Component's parameters. */ |
davide.aliprandi@st.com | 5:ac729170df5f | 169 | sound_terminal->set_frequency(MY_SONG_AUDIO_FREQUENCY); |
davide.aliprandi@st.com | 5:ac729170df5f | 170 | sound_terminal->set_volume(STA350BW_CHANNEL_MASTER, 60); |
nikapov | 0:8d09369ca839 | 171 | |
nikapov | 0:8d09369ca839 | 172 | /* Printing to the console. */ |
nikapov | 0:8d09369ca839 | 173 | printf("Sound Terminal Application Example\r\n\n"); |
nikapov | 0:8d09369ca839 | 174 | |
vidica94 | 12:d847a3c0cf76 | 175 | |
vidica94 | 12:d847a3c0cf76 | 176 | |
vidica94 | 12:d847a3c0cf76 | 177 | |
vidica94 | 12:d847a3c0cf76 | 178 | |
vidica94 | 12:d847a3c0cf76 | 179 | |
vidica94 | 12:d847a3c0cf76 | 180 | |
vidica94 | 12:d847a3c0cf76 | 181 | |
vidica94 | 12:d847a3c0cf76 | 182 | |
vidica94 | 12:d847a3c0cf76 | 183 | |
vidica94 | 12:d847a3c0cf76 | 184 | // turn on the back light (it's off by default) |
vidica94 | 12:d847a3c0cf76 | 185 | lcd.setBackLight(true); |
vidica94 | 12:d847a3c0cf76 | 186 | |
vidica94 | 12:d847a3c0cf76 | 187 | int error = 0; |
vidica94 | 12:d847a3c0cf76 | 188 | printf("Welcome to the filesystem example.\r\n" |
vidica94 | 12:d847a3c0cf76 | 189 | "Formatting a FAT, RAM-backed filesystem. "); |
vidica94 | 12:d847a3c0cf76 | 190 | //error = FATFileSystem::format(&bd); |
vidica94 | 12:d847a3c0cf76 | 191 | return_error(error); |
nikapov | 0:8d09369ca839 | 192 | |
vidica94 | 12:d847a3c0cf76 | 193 | printf("Mounting the filesystem on \"/fs\". "); |
vidica94 | 12:d847a3c0cf76 | 194 | error = fs.mount(&bd); |
vidica94 | 12:d847a3c0cf76 | 195 | return_error(error); |
vidica94 | 12:d847a3c0cf76 | 196 | |
vidica94 | 12:d847a3c0cf76 | 197 | |
vidica94 | 12:d847a3c0cf76 | 198 | printf(" done.\r\n"); |
nikapov | 0:8d09369ca839 | 199 | |
vidica94 | 12:d847a3c0cf76 | 200 | printf("Re-opening file read-only."); |
vidica94 | 14:244ec24ee6dd | 201 | FILE* fd = fopen("/fs/campagnola.wav", "r"); |
vidica94 | 12:d847a3c0cf76 | 202 | errno_error(fd); |
vidica94 | 15:3624055fac29 | 203 | arg->fd=fd; |
vidica94 | 15:3624055fac29 | 204 | Leggi_SD_Thread.start(callback(Leggi_SD,arg)); |
vidica94 | 15:3624055fac29 | 205 | Suona_Thread.start(callback(Suona,arg)); |
vidica94 | 15:3624055fac29 | 206 | |
vidica94 | 15:3624055fac29 | 207 | |
vidica94 | 12:d847a3c0cf76 | 208 | printf("Dumping file to screen.\r\n"); |
vidica94 | 14:244ec24ee6dd | 209 | |
vidica94 | 12:d847a3c0cf76 | 210 | while (!feof(fd)){ |
vidica94 | 14:244ec24ee6dd | 211 | |
vidica94 | 12:d847a3c0cf76 | 212 | // printf("inizo file to screen.\r\n"); |
vidica94 | 14:244ec24ee6dd | 213 | //fscanf( fd , "%c%c" , & a ) ; |
vidica94 | 14:244ec24ee6dd | 214 | //fscanf( fd , "%c" , & c) ; |
vidica94 | 15:3624055fac29 | 215 | //fread(buff,sizeof(int16_t),3000,fd); |
vidica94 | 14:244ec24ee6dd | 216 | //int b=(((int)a*256)+((int )c)); |
vidica94 | 14:244ec24ee6dd | 217 | //b=b-32768; |
vidica94 | 14:244ec24ee6dd | 218 | //printf("%d \r\n",b); |
vidica94 | 12:d847a3c0cf76 | 219 | //printf ( "%c-" , a[1]) ;printf ( "%c\r\n" , a[0]) ; |
vidica94 | 12:d847a3c0cf76 | 220 | //printf("--> Playing...\r\n"); |
vidica94 | 14:244ec24ee6dd | 221 | //printf ( "%d\r\n " , buff[i] ) ; |
nikapov | 0:8d09369ca839 | 222 | |
vidica94 | 14:244ec24ee6dd | 223 | //buff[i]=a; |
vidica94 | 12:d847a3c0cf76 | 224 | |
vidica94 | 14:244ec24ee6dd | 225 | |
vidica94 | 14:244ec24ee6dd | 226 | |
vidica94 | 12:d847a3c0cf76 | 227 | // int size = fread(&buff[0], 1, 159, fd); |
vidica94 | 12:d847a3c0cf76 | 228 | //fwrite(&buff[0], 1, size, stdout); |
vidica94 | 12:d847a3c0cf76 | 229 | |
vidica94 | 15:3624055fac29 | 230 | //sound_terminal->play((int16_t *) buff, (uint16_t) sizeof(buff), true ); |
vidica94 | 12:d847a3c0cf76 | 231 | } |
vidica94 | 12:d847a3c0cf76 | 232 | printf("EOF.\r\n"); |
vidica94 | 12:d847a3c0cf76 | 233 | |
vidica94 | 12:d847a3c0cf76 | 234 | printf("Closing file."); |
vidica94 | 12:d847a3c0cf76 | 235 | fclose(fd); |
vidica94 | 12:d847a3c0cf76 | 236 | printf(" done.\r\n"); |
nikapov | 0:8d09369ca839 | 237 | |
vidica94 | 12:d847a3c0cf76 | 238 | printf("Opening root directory."); |
vidica94 | 12:d847a3c0cf76 | 239 | DIR* dir = opendir("/fs/"); |
vidica94 | 12:d847a3c0cf76 | 240 | errno_error(fd); |
vidica94 | 12:d847a3c0cf76 | 241 | |
vidica94 | 12:d847a3c0cf76 | 242 | struct dirent* de; |
vidica94 | 12:d847a3c0cf76 | 243 | printf("Printing all filenames:\r\n"); |
vidica94 | 12:d847a3c0cf76 | 244 | while((de = readdir(dir)) != NULL){ |
vidica94 | 12:d847a3c0cf76 | 245 | printf(" %s\r\n", &(de->d_name)[0]); |
vidica94 | 12:d847a3c0cf76 | 246 | } |
vidica94 | 12:d847a3c0cf76 | 247 | |
vidica94 | 12:d847a3c0cf76 | 248 | printf("Closeing root directory. "); |
vidica94 | 12:d847a3c0cf76 | 249 | error = closedir(dir); |
vidica94 | 12:d847a3c0cf76 | 250 | return_error(error); |
vidica94 | 12:d847a3c0cf76 | 251 | printf("Filesystem Demo complete.\r\n"); |
vidica94 | 12:d847a3c0cf76 | 252 | |
vidica94 | 12:d847a3c0cf76 | 253 | while (true) {} |
vidica94 | 12:d847a3c0cf76 | 254 | } |