nlgplay for mbed

Dependencies:   SDFileSystemEx mbed

Revision:
6:e48296dca6bb
Parent:
5:27f193e21222
Child:
7:7e183b33c3f9
--- a/main.cpp	Mon Jun 02 01:57:54 2014 +0000
+++ b/main.cpp	Mon Aug 18 00:59:38 2014 +0000
@@ -2,18 +2,25 @@
 #include "SDFileSystem.h"
 #include "lcd.h"
 
+// (pinname, initial value)
 DigitalOut io01(dp13, 0);
 DigitalOut io02(dp26, 0);
 DigitalOut io03(dp17, 0);
 DigitalOut io04(dp4, 0);
 
+DigitalOut io05(dp18, 0);
+DigitalOut io06(dp11, 0);
+
+// (pinname, mode)
 DigitalIn sw_play(dp24, PullUp);
 DigitalIn sw_next(dp10, PullUp);
+DigitalIn sw_prev(dp9, PullUp);
 
+// LED 
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 
-// MOSI, MISO, SCLK, CS
+// SPI: MOSI, MISO, SCLK, CS
 SDFileSystem sd(dp2, dp1, dp6, dp25, "sd"); 
 
 //
@@ -176,7 +183,7 @@
     wait_ms(20);
     /* ICLのみをLOW(アクティブ)にする */
     ioShiftOut(ACTLOW & ~(ICL));    
-    wait_ms(100);
+    wait_ms(150);
     
     /* 元に戻す */
     ioShiftOut(ACTLOW);
@@ -237,7 +244,7 @@
         
     fread(nlg_hdr, 0x60, 1, nlg_file);
     
-    if (memcmp(nlg_hdr,"NLG1",4) != 0)
+    if (memcmp(nlg_hdr, "NLG1", 4) != 0)
     {
         printf("Unknown format!\n");
         fclose(nlg_file);
@@ -502,7 +509,9 @@
     regFM2Out(addr, value);
 }
 
-int g_stop = 0;
+// stop flag
+bool g_stop = false;
+bool g_prev = false;
 
 void DispNLG(NLG_R *np)
 {
@@ -518,6 +527,18 @@
     lcd_printStr(buf);
 }
 
+// wait until release buttons
+void waitButtonRelease(void)
+{
+    while(!sw_next || !sw_play || !sw_prev);
+}
+
+// wait until press buttons
+void waitButtonPress(void)
+{
+    while(sw_next && sw_play && sw_prev);
+}
+
 /* NLGの再生 */
 int PlayNLG(NLG_R *np, int sec)
 {
@@ -536,7 +557,7 @@
     total_us = 0;
     
     // wait until release buttons
-    while(!sw_next || !sw_play);
+    waitButtonRelease();
     
     DispNLG(np);
 
@@ -545,11 +566,18 @@
         // push next
         if (!sw_next)
             break;
+        
+        // push prev
+        if (!sw_prev)
+        {
+            g_prev = true;
+            break;
+        }
 
         // push play
         if (!sw_play)
         {
-            g_stop = 1;
+            g_stop = true;
             break;
         }
 
@@ -675,6 +703,7 @@
     /* 再生する */
     PlayNLG(&n, 360);
     
+    /* mute */
     boardMute();
     
     /* NLGファイルを閉じる */
@@ -683,7 +712,7 @@
     return 0;
 }
 
-int get_nlg_file(char *dest,int index)
+int get_nlg_file(char *dest, int index)
 {
     char *cwd = "/sd/";
     int count = -1;
@@ -693,7 +722,7 @@
     
     if (dir == NULL)
     {
-        lcd_printStr2("SD CARD","ERROR!");
+        lcd_printStr2("SD CARD", "ERROR!");
         return -1;
     } 
     struct dirent *dent;
@@ -733,6 +762,19 @@
     return count;
 }
 
+
+//
+// title
+//
+void putTitle()
+{
+        lcd_printStr2("NBCTRL", "Ver 1.10");
+        wait(3);
+}
+
+//
+// main 
+//
 int main() 
 {
     char file[32];
@@ -741,12 +783,15 @@
     led1 = 0;
     led2 = 0;
     
-    wait_ms(5);
+    wait_ms(20);
+
+    // reset SHIFT REGISTER
+    ioShiftOut(( 0xFF << 8) | (ACTLOW));
     
     lcd_init();
+    
+    putTitle();
 
-    // wait for power up    
-    wait_ms(100);
     boardInit();
     
     int files = 0;
@@ -776,11 +821,9 @@
         result_us = t.read_us();
         t.stop();
         
-        printf("result_us=%dus\n", result_us);        
-        
+        printf("result_us=%dus\n", result_us);            
         printf("DATE:%s\nTIME:%s\n", __DATE__, __TIME__);
-        lcd_printStr2("NBCTRL", "Ver 1.09");
-        wait(3);
+        
         sprintf(buf, "%8s", __DATE__); 
         lcd_printStrYscr(1, buf);
         wait(3);
@@ -825,37 +868,42 @@
         
         // wait for chattering
         wait(0.25);
-        // wait release
-        while(!sw_play || !sw_next);
-        
-        bool prev_flag = false;
-        
+
+        // wait release        
+        waitButtonRelease();
+                
+        // if play button is pressed
         if (g_stop)
         {
+            g_stop = false;
+            g_prev = false;                
+
             lcd_printStrY(1, "  STOP  ");
 
-            // wait push
-            while(sw_play && sw_next);
+            // wait any button is pressed
+            waitButtonPress();
             
             if (!sw_play) {
                 lcd_printStrY(1, "  PREV  ");
-                prev_flag = true;
+                g_prev = true;
             }
             else
             {
                 lcd_printStrY(1, "  NEXT  ");
-                prev_flag = false;                
             }
             
             // wait release
-            while(!sw_play || !sw_next);
-            g_stop = 0;
+            waitButtonRelease();
             
             // reset board
             boardInit();
         }
-        if (prev_flag)
+        
+        // play prev or next song
+        if (g_prev)
         {
+            g_prev = false;
+
             if (idx - 1 >= 0)
                 idx--;
             else
@@ -869,12 +917,4 @@
                 idx = 0;
         }
     }
-
-/*    while(1) {
-        led1 = 1;
-        wait(1);
-        led1 = 0;
-        wait(1);
-    }
-*/
 }