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