funzionicchia male thread

Dependencies:   Freetronics_16x2_LCD

Fork of HelloWorld_CCA01M1_mbedOS by vincenzo di capua

main.cpp

Committer:
vidica94
Date:
2018-01-19
Revision:
14:244ec24ee6dd
Parent:
12:d847a3c0cf76
Child:
15:3624055fac29

File content as of revision 14:244ec24ee6dd:

#include "mbed.h"
#include "FATFileSystem.h"
#include "SDBlockDevice.h"
#include <stdio.h>
#include <errno.h>
#include "freetronicsLCDShield.h"
/* Helper header files. */
#include "DevI2C.h"

/* Component specific header files. */
#include "STA350BW.h"

/* My song header file. */
#include "my_song.h"

//SDBlockDevice bd(PB_5, PB_4, PB_3, PA_15);
SDBlockDevice bd(PA_7, PA_6, PA_5, PA_8);
FATFileSystem fs("fs");
freetronicsLCDShield lcd(D8, D9, D4, D5, D6, D2, D3, A0);

#define PLAY_STOP_EVENT      (0x1)
 int16_t buff[3000] = {0};

/* Variables -----------------------------------------------------------------*/

/* Initialization parameters. */
STA350BW_init_t init =
{
    48000,   /* 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 play_stop_handler(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;
    }
}    
    
    

void return_error(int ret_val){
  if (ret_val)
    printf("Failure. %d\r\n", ret_val);
  else
    printf("done.\r\n");
}

void errno_error(void* ret_val){
  if (ret_val == NULL)
    printf(" Failure. %d \r\n", errno);
  else
    printf(" done.\r\n");
}

int main() {
      
       /*----- Initialization. -----*/

    /* Initializing I2C bus. */
    DevI2C *dev_i2c = new DevI2C(PB_9, PB_8);

    /* 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(play_stop_handler);
    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->set_frequency(MY_SONG_AUDIO_FREQUENCY);
    sound_terminal->set_volume(STA350BW_CHANNEL_MASTER, 60);

    /* Printing to the console. */
    printf("Sound Terminal Application Example\r\n\n");

      
      
      
      
      
      
      
      
      
      // turn on the back light (it's off by default)
  lcd.setBackLight(true);
  
  int error = 0;
  printf("Welcome to the filesystem example.\r\n"
         "Formatting a FAT, RAM-backed filesystem. ");
  //error = FATFileSystem::format(&bd);
  return_error(error);

  printf("Mounting the filesystem on \"/fs\". ");
  error = fs.mount(&bd);
  return_error(error);


  printf(" done.\r\n");

  printf("Re-opening file read-only.");
 FILE* fd = fopen("/fs/campagnola.wav", "r");
  errno_error(fd);
int16_t a;
char c;
  printf("Dumping file to screen.\r\n");
 
  while (!feof(fd)){
     
     // printf("inizo file to screen.\r\n");
//fscanf( fd , "%c%c" , & a ) ;
//fscanf( fd , "%c" , & c) ;
fread(buff,sizeof(int16_t),3000,fd);
//int b=(((int)a*256)+((int )c));
//b=b-32768;
//printf("%d \r\n",b);
//printf ( "%c-" , a[1]) ;printf ( "%c\r\n" , a[0]) ;
//printf("--> Playing...\r\n");
//printf ( "%d\r\n " , buff[i] ) ;

    //buff[i]=a;
    
    

   // int size = fread(&buff[0], 1, 159, fd);
    //fwrite(&buff[0], 1, size, stdout);
    
     sound_terminal->play((int16_t *) buff, (uint16_t) sizeof(buff), true );
  }
  printf("EOF.\r\n");

  printf("Closing file.");
  fclose(fd);
  printf(" done.\r\n");

  printf("Opening root directory.");
  DIR* dir = opendir("/fs/");
  errno_error(fd);

  struct dirent* de;
  printf("Printing all filenames:\r\n");
  while((de = readdir(dir)) != NULL){
    printf("  %s\r\n", &(de->d_name)[0]);
  }

  printf("Closeing root directory. ");
  error = closedir(dir);
  return_error(error);
  printf("Filesystem Demo complete.\r\n");

  while (true) {}
}