nlgplay for mbed

Dependencies:   SDFileSystemEx mbed

Revision:
1:ec416e6d5739
Parent:
0:2053640461b5
Child:
2:f0b7779b03c7
--- a/main.cpp	Sat May 17 12:54:13 2014 +0000
+++ b/main.cpp	Mon May 19 03:10:50 2014 +0000
@@ -10,6 +10,8 @@
 DigitalIn sw_play(dp24,PullUp);
 DigitalIn sw_next(dp10,PullUp);
 
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
 
 // MOSI, MISO, SCLK, CS
 SDFileSystem sd(dp2, dp1, dp6, dp25, "sd"); 
@@ -106,7 +108,7 @@
     
     /* チップ側の処理を待つ */
     if (!(select & CS_PSG))
-        wait_us(8);
+        wait_us(10);
         
     
     /* データを出力 */
@@ -117,7 +119,7 @@
 
     /* wait if FM */
     if (!(select & CS_PSG))
-        wait_us(8);
+        wait_us(10);
 }
 
 /* PSG出力 */
@@ -165,13 +167,14 @@
 /* ボード初期化 */
 void boardInit(void)
 {
+    wait_ms(20);
     /* ICLのみをLOW(アクティブ)にする */
     ioShiftOut(ACTLOW & ~(ICL));    
-    wait_us(200);
+    wait_ms(50);
     
     /* 元に戻す */
     ioShiftOut(ACTLOW);
-    wait_us(200);
+    wait_ms(10);
 }
 
 typedef unsigned char  byte;
@@ -213,11 +216,11 @@
         ((dword)p[3])<<24;
 }
 
-byte nlg_hdr[0x80];
-
 // NLGファイルを開く
 int OpenNLG(const char *file)
 {    
+    byte nlg_hdr[0x80];
+
     nlg_file = fopen(file, "rb");
         
     if (!nlg_file)
@@ -420,6 +423,10 @@
     int irq_count;
     int irq_loop;
     long loop_address;
+    int irq_counter;
+    int irq_counter2;
+    
+    int song_no;
     
 } NLG_R;
 
@@ -453,6 +460,8 @@
     np->irq_count = 0;
     np->irq_loop = GetLoopPtrNLG();
     np->loop_address = -1;
+    np->irq_counter = 0;
+    np->irq_counter2 = 0;
 
     return 0;
 }
@@ -479,14 +488,13 @@
 
 void DispNLG(NLG_R *np)
 {
-    printf("Time %02d:%02d\r",np->total_sec / 60, np->total_sec % 60);
-    fflush(stdout);
+    // printf("Time %02d:%02d\r",np->total_sec / 60, np->total_sec % 60);
+    // fflush(stdout);
     
     char buf[16];
-    sprintf(buf, "%02d:%02d",np->total_sec / 60, np->total_sec % 60);
+    sprintf(buf, "%d.%02d:%02d",np->song_no, np->total_sec / 60, np->total_sec % 60);
     
-    lcd_setCursor(0,1);
-    lcd_printStr(buf);
+    lcd_printStrY(1, buf);
 }
 
 /* NLGの再生 */
@@ -505,12 +513,8 @@
     t.start();
     total_us = 0;
     
-    printf("wait button release\n");
-
     // wait until release buttons
     while(!sw_next || !sw_play);
-
-    printf("ok\n");
     
     DispNLG(np);
 
@@ -527,7 +531,6 @@
             break;
         }
 
-
         /* ウエイトの処理 */
         while (np->tick >= us_tick)
         {
@@ -592,6 +595,19 @@
                     np->loop_address = TellNLG();
                     np->loop_address -= 1;
                 }
+                np->irq_counter++;
+                if (np->irq_counter >= 48)
+                {
+                    np->irq_counter = 0;
+                    led1 = !led1;
+
+                    np->irq_counter2++;
+                    if (np->irq_counter2 >= 4)
+                    {
+                        np->irq_counter2 = 0;
+                        led2 = !led2;
+                    }
+                }
             break;
             case CMD_CTC0:
                 SetCTC0_NLG(ReadNLG());
@@ -600,7 +616,6 @@
                 SetCTC3_NLG(ReadNLG());
             break;
                 
-                
         }
         /* 秒情報の表示 */
         while (np->tick_sec >= np->base_clk)
@@ -615,14 +630,11 @@
     return result;
 }
 
-DigitalOut led1(LED1);
-
 NLG_R n;
 
-int nlg_play(const char *nlg_file)
+int nlg_play(const char *nlg_file,int song_no)
 {
-
-    printf("init nlg:%s\n",nlg_file);
+    printf("nlg_play:%s\n",nlg_file);
 
        /* NLGの初期化 */
     if (initNLG(&n, nlg_file) < 0)
@@ -634,18 +646,13 @@
         return -1;
     }
     
-    printf("Init board\n");
-
+    n.song_no = song_no;
+    
     /* 再生する */
-    printf("PlayNLG\n");
     PlayNLG(&n, 360);
     printf("\n");
     
-    printf("mute\n");
-
     boardMute();
-
-    printf("close\n");
     
     /* NLGファイルを閉じる */
     CloseNLG();
@@ -710,6 +717,8 @@
     char path[64];
     
     led1 = 0;
+    led2 = 0;
+    
     lcd_init();
     
     boardInit();
@@ -718,7 +727,6 @@
     int disp_mode = 0;
 
     if (!sw_next)
-    
         disp_mode = 1;
     
     files = get_nlg_file(file, -1);
@@ -726,31 +734,28 @@
     if (disp_mode) 
     {
         char buf[16];
-        
-        lcd_setCursor(0,0);
-        lcd_printStr("NBCTRL");
-        lcd_setCursor(0,1);
-        lcd_printStr("Ver 1.0 ");
+        printf("DATE:%s\nTIME:%s", __DATE__, __TIME__);
+        lcd_printStr2("NBCTRL", "Ver 1.04");
         wait(3);
-        sprintf(buf, "%s", __DATE__); 
-        lcd_setCursor(0,0);
-        lcd_printStr("DATE");
-        lcd_setCursor(0,1);
-        lcd_printStr(buf);
+        sprintf(buf, "%8s", __DATE__); 
+        lcd_printStrYscr(1, buf);
+        wait(3);
+        sprintf(buf, "%8s", __TIME__); 
+        lcd_printStrY(1, buf);
         wait(3);
 
-        lcd_setCursor(0,1);
         if (files < 0)
-            lcd_printStr("NO FILES");
+            lcd_printStrY(1, "NO FILES");
         else
         {
             char buf[16];
-            sprintf(buf, "%d files", files);    
-            lcd_printStr(buf);
+            sprintf(buf, "%3dfiles", files);    
+            lcd_printStrY(1, buf);
         }
         wait(3);
         lcd_cls();
     }
+    
     int idx = 0;
     while(files > 0)
     {
@@ -759,34 +764,55 @@
         sprintf(path, "/sd/%s", file);
         printf("path=%s\n",path);
         
-        lcd_setCursor(0, 0);
-        lcd_printStr(file);
+        lcd_printStrY(0, file);
 
-        nlg_play(path);
+        nlg_play(path, idx);
         
+        // wait for chattering 
+        wait(0.25);
         // wait release
         while(!sw_play || !sw_next);
         
+        bool prev_flag = false;
+        
         if (g_stop)
         {
-            lcd_setCursor(0, 1);
-            lcd_printStr("STOP    ");
-                
+            lcd_printStrY(1, "STOP    ");
+
+            // wait push
             while(sw_play && sw_next);
+            
+            if (!sw_play)   
+                prev_flag = true;
+            
+            // wait release
             while(!sw_play || !sw_next);
             g_stop = 0;
+            
+            // reset board
+            boardInit();
         }
-        
-        if (idx + 1 < files)
-            idx++;
+        if (prev_flag)
+        {
+            if (idx - 1 >= 0)
+                idx--;
+            else
+                idx = files - 1;
+        }
         else
-            idx = 0;
+        {
+            if (idx + 1 < files)
+                idx++;
+            else
+                idx = 0;
+        }
     }
 
-    while(1) {
+/*    while(1) {
         led1 = 1;
         wait(1);
         led1 = 0;
         wait(1);
     }
+*/
 }