suono

Dependencies:   X_NUCLEO_CCA01M1

Fork of HelloWorld_CCA01M1_mbedOS by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    March 25th, 2016
00007  * @brief   mbed test application for the STMicroelectronics X-NUCLEO-CCA01M1
00008  *          Sound Terminal Expansion Board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 /* mbed specific header files. */
00043 #include "mbed.h"
00044 #include "rtos.h"
00045 
00046 
00047 #if DEVICE_I2S
00048 
00049 /* Helper header files. */
00050 #include "DevI2C.h"
00051 
00052 /* Component specific header files. */
00053 #include "STA350BW.h"
00054 
00055 /* My song header file. */
00056 #include "my_song.h"
00057 
00058 
00059 /* Definitions ---------------------------------------------------------------*/
00060 
00061 /* Events. */
00062 #define PLAY_STOP_EVENT      (0x1)
00063 
00064 
00065 /* Variables -----------------------------------------------------------------*/
00066 
00067 /* Initialization parameters. */
00068 STA350BW_init_t init =
00069 {
00070     32000,   /* Default Sampling Frequency [Hz]. */
00071     100      /* Default Volume. */
00072 };
00073 
00074 /* Sound Terminal Component. */
00075 STA350BW *sound_terminal;
00076 
00077 /* Thread to manage I2S peripherals. */
00078 static rtos::Thread i2s_bh_daemon;
00079 
00080 /* Threads. */
00081 Thread *play_stop_thread;
00082 
00083 /* User button handling. */
00084 InterruptIn event(USER_BUTTON);
00085 
00086 
00087 /* Functions -----------------------------------------------------------------*/
00088 
00089 /**
00090  * @brief  User button handler function.
00091  * @param  None.
00092  * @retval None.
00093  */
00094 void pressed(void)
00095 {
00096     /* Signal to play/stop handler thread. */
00097     play_stop_thread->signal_set(PLAY_STOP_EVENT);
00098 }
00099 
00100 /**
00101  * @brief  Play/stop handler function.
00102  * @param  None.
00103  * @retval None.
00104  */
00105 void play_stop_handler(void)
00106 {
00107     while (true)
00108     {
00109         static bool stop = true;
00110 
00111         /* Waiting for play/stop events. */
00112         play_stop_thread->signal_wait(PLAY_STOP_EVENT);
00113 
00114         if (stop)
00115             sound_terminal->stop();
00116         else
00117             sound_terminal->play((int16_t *) my_song, (uint16_t) sizeof(my_song), true);
00118         printf("--> %s\r\n", stop ? "Stop." : "Playing...");
00119 
00120         stop = !stop;
00121     }
00122 }
00123 
00124 /**
00125  * @brief  Entry point function of mbedOS.
00126  * @param  None
00127  * @retval None
00128  */
00129 int main(void)
00130 {
00131     /*----- Initialization. -----*/
00132 
00133     /* Initializing I2C bus. */
00134     DevI2C *dev_i2c = new DevI2C(PB_9, PB_8);
00135 
00136     /* Initializing Sound Terminal Component. */
00137 #ifndef USE_I2S2
00138     sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_1, *dev_i2c, PB_15, PB_13, PB_12, NC, PC_6);
00139 #else
00140     sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7);
00141 #endif
00142     if (sound_terminal->init(&init) != COMPONENT_OK)
00143     {
00144         error("Initialization of the Sound Terminal Expansion Board failed.\r\n");
00145         exit(EXIT_FAILURE);
00146     }
00147 
00148     /* Starting a thread to manage I2S peripherals. */
00149     Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever);
00150     i2s_bh_daemon.start(i2s_bh_task);
00151 
00152     /* Scheduling the play/stop function. */
00153     play_stop_thread = new Thread();
00154     osStatus status = play_stop_thread->start(play_stop_handler);
00155     if (status != osOK)
00156         printf("Could not start the play/stop handler thread.\r\n");
00157     event.fall(&pressed);
00158 
00159     /* Setting Sound Terminal Component's parameters. */
00160     sound_terminal->set_frequency(MY_SONG_AUDIO_FREQUENCY);
00161     sound_terminal->set_volume(STA350BW_CHANNEL_MASTER, 60);
00162 
00163     /* Printing to the console. */
00164     printf("Sound Terminal Application Example\r\n\n");
00165 
00166 
00167     /*----- Playing. -----*/
00168 
00169     /* Printing to the console. */
00170     printf("--> Playing...\r\n");
00171     sound_terminal->play((int16_t *) my_song, (uint16_t) sizeof(my_song), true);
00172 }
00173 
00174 #else // DEVICE_I2S
00175 
00176 int main(void)
00177 {
00178     printf("The target does not support I2S API.\r\n");
00179 }
00180 
00181 #endif // DEVICE_I2S