semaf

Dependencies:   Freetronics_16x2_LCD sd-driver

Fork of HelloWorld_CCA01M1_mbedOS45 by STarks

Revision:
12:d847a3c0cf76
Parent:
7:94e3191477c5
Child:
14:244ec24ee6dd
--- a/main.cpp	Wed Jul 12 13:43:29 2017 +0000
+++ b/main.cpp	Wed Jan 10 10:34:40 2018 +0000
@@ -1,51 +1,9 @@
-/**
- ******************************************************************************
- * @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>&copy; 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
-
+#include "FATFileSystem.h"
+#include "SDBlockDevice.h"
+#include <stdio.h>
+#include <errno.h>
+#include "freetronicsLCDShield.h"
 /* Helper header files. */
 #include "DevI2C.h"
 
@@ -55,10 +13,10 @@
 /* My song header file. */
 #include "my_song.h"
 
+SDBlockDevice bd(PA_7, PA_6, PA_5, PA_8);
+FATFileSystem fs("fs");
+freetronicsLCDShield lcd(D8, D9, D4, D5, D6, D2, D3, A0);
 
-/* Definitions ---------------------------------------------------------------*/
-
-/* Events. */
 #define PLAY_STOP_EVENT      (0x1)
 
 
@@ -119,16 +77,27 @@
 
         stop = !stop;
     }
+}    
+    
+    
+
+void return_error(int ret_val){
+  if (ret_val)
+    printf("Failure. %d\r\n", ret_val);
+  else
+    printf("done.\r\n");
 }
 
-/**
- * @brief  Entry point function of mbedOS.
- * @param  None
- * @retval None
- */
-int main(void)
-{
-    /*----- Initialization. -----*/
+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);
@@ -163,19 +132,90 @@
     /* Printing to the console. */
     printf("Sound Terminal Application Example\r\n\n");
 
-
-    /*----- Playing. -----*/
+      
+      
+      
+      
+      
+      
+      
+      
+      
+      // 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);
 
-    /* Printing to the console. */
-    printf("--> Playing...\r\n");
-    sound_terminal->play((int16_t *) my_song, (uint16_t) sizeof(my_song), true);
-}
+  printf("Mounting the filesystem on \"/fs\". ");
+  error = fs.mount(&bd);
+  return_error(error);
+
+  printf("Opening a new file, numbers.txt.");
+  FILE* fd = fopen("/fs/numbers.txt", "w");
+  errno_error(fd);
+
+  for (int i = 0; i < 20; i++){
+    printf("Writing decimal numbers to a file (%d/20)\r", i);
+    fprintf(fd, "%d\r\n", i);
+  }
+  printf("Writing decimal numbers to a file (20/20) done.\r\n");
+
+  printf("Closing file.");
+  fclose(fd);
+  printf(" done.\r\n");
 
-#else // DEVICE_I2S
+  printf("Re-opening file read-only.");
+  fd = fopen("/fs/campagnola.wav", "r");
+  errno_error(fd);
+char a;
+char c;
+  printf("Dumping file to screen.\r\n");
+  int buff[160] = {0};
+  while (!feof(fd)){
+     for( int i=0;i<160;i++){
+     // printf("inizo file to screen.\r\n");
+fscanf( fd , "%c" , & a ) ;
+fscanf( fd , "%c" , & c) ;
+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");
+//lcd.printf ( "%d " , a ) ;
 
-int main(void)
-{
-    printf("The target does not support I2S API.\r\n");
-}
+    buff[i]=b;
+    
+    }
+int size=160;
+   // 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");
 
-#endif // DEVICE_I2S
+  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) {}
+}
\ No newline at end of file