nlgplay for mbed

Dependencies:   SDFileSystemEx mbed

Revision:
10:b8d41133e499
Parent:
9:834e9eed0d72
Child:
11:68032f2a11b9
--- a/main.cpp	Thu Aug 21 00:44:22 2014 +0000
+++ b/main.cpp	Sat Sep 06 09:16:14 2014 +0000
@@ -1,14 +1,22 @@
 //
 // NLGPLAY : NLG file player for mbed
 //
+// example to write : 
+// ./lpc21isp -bin file.bin /dev/cu.usbserial-??? 115200 12000
+//
 
 #include "mbed.h"
 #include "SDFileSystem.h"
 #include "lcd.h"
 
-#define USE_NBV3
+// #define USE_NBV3
 #define USE_FASTIO
 
+#define NBCTRL_VER "Ver 1.13"
+
+#pragma O3
+#pragma Otime
+
 
 // (pinname, mode)
 DigitalIn sw_play(dp24, PullUp);
@@ -16,8 +24,8 @@
 DigitalIn sw_prev(dp9, PullUp);
 
 // LED 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
+DigitalOut led1(LED1, 0);
+DigitalOut led2(LED2, 0);
 
 // SPI: MOSI, MISO, SCLK, CS
 SDFileSystem sd(dp2, dp1, dp6, dp25, "sd"); 
@@ -170,11 +178,9 @@
 
 }
 
-
 #endif
 
 
-
 /* 制御信号定義 */
 #define CS_PSG (1 << 1)
 #define CS_FM1 (1 << 2)
@@ -187,8 +193,8 @@
 #define ACTLOW (CS_PSG | CS_FM1 | CS_FM2 | WR | ICL)
 
 #ifdef USE_NBV3
-/* 16bit出力 */
-void regOutBase(int addr, int data,int select)
+/* NBV3 16bit出力 */
+void regOutBase(int addr, int data, int select)
 {
     /* アドレスを出力 */
     /* A0をローにして待つ */
@@ -697,11 +703,12 @@
             int us = np->tick / us_tick;
             np->tick -= (us * us_tick);
             
+            // 次のタイミングまで待つ
             while(t.read_us() < total_us + us);
             
             total_us += us;
             
-            // reset timer
+            // 1秒ごとにリセットする
             if (total_us >= 1000000)
             {
                 total_us -= t.read_us();
@@ -820,6 +827,13 @@
     return 0;
 }
 
+void error_sdcard(void)
+{
+    lcd_printStr2("SD CARD", "ERROR!");
+    
+    while(1);
+}
+
 int get_nlg_file(char *dest, int index)
 {
     char *cwd = "/sd/";
@@ -830,7 +844,6 @@
     
     if (dir == NULL)
     {
-        lcd_printStr2("SD CARD", "ERROR!");
         return -1;
     } 
     struct dirent *dent;
@@ -870,27 +883,106 @@
     return count;
 }
 
+//
+// COM mode
+//
+void loop_for_com()
+{
+    Serial pc(USBTX, USBRX);
+    
+    lcd_printStrY(1,"COM MODE");
+
+    pc.printf("com_mode\n");
+    
+    while(1)
+    {
+        unsigned char sw, adr, val;
+        
+        sw = pc.getc();
+        if (sw == 0x00)
+            continue;
+            
+        adr = pc.getc();
+        val = pc.getc();
+        switch(sw)
+        {
+            case 1:
+                regFMOut(adr, val);
+            break;
+            case 2:
+                regFM2Out(adr, val);
+            break;
+            case 3:
+                regPSGOut(adr, val);
+            break;
+            case 0x0f:
+                pc.baud(9600 * val);
+            break;
+        }
+
+        // pc.printf("sw = %02x, adr = %02x, val = %02x\n", sw, adr, val);
+    }
+}
 
 //
 // title
 //
 void putTitle()
 {
-        lcd_printStr2("NBCTRL", "Ver 1.12");
+        lcd_printStr2("NBCTRL", NBCTRL_VER);
         wait(1.5);
 }
 
+// 情報の表示
+void show_info(int files)
+{
+    char buf[16];
+
+    Timer t;
+    int result_us = 0;
+    t.reset();
+    t.start();
+            
+    // actual timing
+    regFMOut(0x20, 0x00);
+    
+    result_us = t.read_us();
+    t.stop();
+    
+    printf("result_us=%dus\n", result_us);            
+    sprintf(buf, "R:%dus", result_us); 
+    lcd_printStrY(1, buf);
+    wait(3);
+            
+    sprintf(buf, "%8s", __DATE__); 
+    lcd_printStrYscr(1, buf);
+    wait(3);
+
+    if (files < 0)
+        lcd_printStrY(1, "NO FILES");
+    else
+    {
+        char buf[16];
+        sprintf(buf, "%3dfiles", files);    
+        lcd_printStrY(1, buf);
+    }
+    wait(3);
+    lcd_cls();
+}
+
 //
 // main 
 //
 int main() 
 {
+    bool info_mode = false;
+    bool com_mode = false;
+
+    int files = 0;
+    
     char file[32];
     char path[48];
     
-    led1 = 0;
-    led2 = 0;
-    
     ioInit();
     
     wait_ms(20);
@@ -898,54 +990,33 @@
     // reset SHIFT REGISTER
     ioShiftOut((0xFF << 8) | (ACTLOW));
     
-    lcd_init();
-    
-    putTitle();
-
-    boardInit();
+    // モード
+    if (!sw_next)
+        info_mode = true;
+    if (!sw_prev)
+        com_mode = true;
     
-    int files = 0;
-    int disp_mode = 0;
+    // 初期化とタイトル表示
+    lcd_init();
+    putTitle();
+    
+    // ボードの初期化
+    boardInit();
 
-    if (!sw_next)
-        disp_mode = 1;
-    
+    // 通信モードに移行    
+    if (com_mode)
+            loop_for_com();
+        
     files = get_nlg_file(file, -1);
     
-    if (disp_mode) 
-    {        
-        char buf[16];
-        Timer t;
-        int result_us = 0;
-        t.reset();
-        t.start();
-                
-        // actual timing
-        regFMOut(0x20, 0x00);
-        
-        result_us = t.read_us();
-        t.stop();
-        
-        printf("result_us=%dus\n", result_us);            
-        sprintf(buf, "R:%dus", result_us); 
-        lcd_printStrY(1, buf);
-        wait(3);
-                
-        sprintf(buf, "%8s", __DATE__); 
-        lcd_printStrYscr(1, buf);
-        wait(3);
+    // エラー表示
+    if (files < 0)
+        error_sdcard();
+    
+    // 情報モード
+    if (info_mode)
+        show_info(files);
 
-        if (files < 0)
-            lcd_printStrY(1, "NO FILES");
-        else
-        {
-            char buf[16];
-            sprintf(buf, "%3dfiles", files);    
-            lcd_printStrY(1, buf);
-        }
-        wait(3);
-        lcd_cls();
-    }
     
     bool repeat_flag = false;
     int idx = 0;