![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
suono
Dependencies: X_NUCLEO_CCA01M1
Fork of HelloWorld_CCA01M1_mbedOS by
Diff: main.cpp
- Revision:
- 0:8d09369ca839
- Child:
- 4:6bb9e11d322b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Feb 22 17:43:34 2017 +0100 @@ -0,0 +1,184 @@ +/** + ****************************************************************************** + * @file main.cpp + * @author Davide Aliprandi, STMicroelectronics + * @version V1.0.0 + * @date March 25th, 2016 + * @brief mbed test application for the STMicroelectronics X-NUCLEO-CCA01M1 + * Sound Terminal Expansion Board. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + + +/* Includes ------------------------------------------------------------------*/ + +/* mbed specific header files. */ +#include "mbed.h" +#include "rtos.h" + + +#if DEVICE_I2S + +/* Helper header files. */ +#include "DevI2C.h" + +/* Component specific header files. */ +#include "sta350bw_class.h" + +/* My song header file. */ +#include "my_song.h" + + +/* Definitions ---------------------------------------------------------------*/ + +/* Events. */ +#define PLAY_STOP_EVENT (0x1) + + +/* Variables -----------------------------------------------------------------*/ + +/* Initialization parameters. */ +STA350BW_Init_t init = +{ + 32000, /* Default Sampling Frequency [Hz]. */ + 100 /* Default Volume. */ +}; + +/* Sound Terminal Component. */ +STA350BW *sound_terminal; + +/* Thread to manage I2S peripherals. */ +static rtos::Thread i2s_bh_daemon; + +/* Threads. */ +Thread *play_stop_thread; + +/* User button handling. */ +InterruptIn event(USER_BUTTON); + + +/* Functions -----------------------------------------------------------------*/ + +/** + * @brief User button handler function. + * @param None. + * @retval None. + */ +void pressed(void) +{ + /* Signal to play/stop handler thread. */ + play_stop_thread->signal_set(PLAY_STOP_EVENT); +} + +/** + * @brief Play/stop handler function. + * @param None. + * @retval None. + */ +void PlayStopHandler(void) +{ + while (true) + { + static bool stop = true; + + /* Waiting for play/stop events. */ + play_stop_thread->signal_wait(PLAY_STOP_EVENT); + + if (stop) + sound_terminal->Stop(); + else + sound_terminal->Play((int16_t *) my_song, (uint16_t) sizeof(my_song), true); + printf("--> %s\r\n", stop ? "Stop." : "Playing..."); + + stop = !stop; + } +} + +/** + * @brief Entry point function of mbedOS. + * @param None + * @retval None + */ +int main(void) +{ + /*----- Initialization. -----*/ + + /* Initializing I2C bus. */ + DevI2C *dev_i2c = new DevI2C( + PB_9, PullUp, + PB_8, PullUp + ); + + /* Initializing Sound Terminal Component. */ +#ifndef USE_I2S2 + sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_1, *dev_i2c, PB_15, PB_13, PB_12, NC, PC_6); +#else + sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7); +#endif + if (sound_terminal->Init(&init) != COMPONENT_OK) + { + error("Initialization of the Sound Terminal Expansion Board failed.\r\n"); + exit(EXIT_FAILURE); + } + + /* Starting a thread to manage I2S peripherals. */ + Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever); + i2s_bh_daemon.start(i2s_bh_task); + + /* Scheduling the play/stop function. */ + play_stop_thread = new Thread(); + osStatus status = play_stop_thread->start(PlayStopHandler); + if (status != osOK) + printf("Could not start the play/stop handler thread.\r\n"); + event.fall(&pressed); + + /* Setting Sound Terminal Component's parameters. */ + sound_terminal->SetFrequency(MY_SONG_AUDIO_FREQUENCY); + sound_terminal->SetVolume(STA350BW_CHANNEL_MASTER, 60); + + /* Printing to the console. */ + printf("Sound Terminal Application Example\r\n\n"); + + + /*----- Playing. -----*/ + + /* Printing to the console. */ + printf("--> Playing...\r\n"); + sound_terminal->Play((int16_t *) my_song, (uint16_t) sizeof(my_song), true); +} + +#else // DEVICE_I2S + +int main(void) +{ + printf("The target does not support I2S API.\r\n"); +} + +#endif // DEVICE_I2S