Quick hack to make NSX-39 (Poke-Miku) USB MIDI device to speak "mbed" from mbed which acts as an USB host.

Dependencies:   FatFileSystem mbed

Fork of MIDI_BlueUSB by Radio Junk Box

Description of the project

This is quick hack to control Poke-miku (NSX-39) from mbed. The mbed acts as an USB host and controls USB MIDI device NSX-39. It speaks "mbed" if you send "s¥n" from virtual USB serial (connected to PC or Mac) or push SW connected to p21. It plays MIDI file "test.mid" on local file-system if you push SW connected to p22. You can find files that I have tested at the bottom. The standard MIDI file support is still preliminary. See TestShell.cpp for the hack. This program is derived from MIDI_BlueUSB (http://mbed.org/users/radiojunkbox/code/MIDI_BlueUSB/) by Radio Junk Box.

ポケミク(NSX-39)を無改造のままmbedから鳴らせるようにしてみました。mbedがUSB hostになって、USB MIDIデバイスのポケミクを鳴らします。mbedのバーチャルシリアル(USBシリアル)にPCからs\nを送るか、p21につないだスイッチを押すとmbedとしゃべります。p22につないだスイッチを押すと、ローカルファイルシステム(.binと同じ場所)に保存した test.mid を再生します。試したファイルは下にある test1.mid と test2.mid です。MIDIファイルのサポートはまだまだ完全とはいえません。

tested MIDI files

Video: Poke-miku speaks `mbed'

Files at this revision

API Documentation at this revision

Comitter:
non
Date:
Sun Apr 27 01:36:30 2014 +0000
Parent:
1:892f8922bdc4
Child:
3:31fbce33c25b
Commit message:
Now it plays when you push SW connected to p21.

Changed in this revision

TestShell.cpp Show annotated file Show diff for this revision Revisions of this file
TestShell.h Show annotated file Show diff for this revision Revisions of this file
common.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/TestShell.cpp	Sat Apr 05 06:25:43 2014 +0000
+++ b/TestShell.cpp	Sun Apr 27 01:36:30 2014 +0000
@@ -31,6 +31,12 @@
 #include "USBHost.h"
 #include "hci.h"
 
+#include "common.h"
+#include "TestShell.h"
+
+static DigitalOut myLED1(LED1);
+static DigitalIn mySW(SWpin);
+
 void printf(const BD_ADDR* addr)
 {
     const u8* a = addr->addr;
@@ -86,13 +92,16 @@
 
 #define WII_REMOTE 0x042500
 
+#define NSX39_device 1
+#define NSX39_endpoint 2
+
 void NoteOn(u8 ch, u8 note, u8 velocity)
 {
     static u8 d[64] = {0x09};
     d[1] = 0x90 | (ch & 0x7f);
     d[2] = note & 0x7f;
     d[3] = velocity & 0x7f;
-    USBBulkTransfer(1, 2, d, 4, NULL, NULL);
+    USBBulkTransfer(NSX39_device, NSX39_endpoint, d, 4, NULL, NULL);
 }
 
 void NoteOff(u8 ch, u8 note, u8 velocity)
@@ -101,7 +110,7 @@
     d[1] = 0x80 | (ch & 0xf);
     d[2] = note & 0x7f;
     d[3] = velocity & 0x7f;
-    USBBulkTransfer(1, 2, d, 4, NULL, NULL);
+    USBBulkTransfer(NSX39_device, NSX39_endpoint, d, 4, NULL, NULL);
 }
 
 void Miku(u8 chr)
@@ -110,7 +119,7 @@
                        0x04, 0x09, 0x11, 0x0a, 
                        0x07, 0x00, 0x00, 0xf7};
     d[10] = chr;
-    USBBulkTransfer(1, 2, d, 12, NULL, NULL);
+    USBBulkTransfer(NSX39_device, NSX39_endpoint, d, 12, NULL, NULL);
 }
 
 
@@ -221,7 +230,6 @@
 
 #define MAX_HID_DEVICES 8
 
-int GetConsoleChar();
 class ShellApp
 {
     char _line[64];
@@ -333,40 +341,41 @@
     {
         for(;;)
         {
-            const char* cmd = ReadLine();
-            if (strcmp(cmd,"s") == 0) {
-/*
-                static u8 d[64] = {0x09, 0x91, 0x4e, 0x7f};
-                USBBulkTransfer(1, 2, d, 4, NULL, NULL);
-*/
-    Miku(3);
-    wait(0.001);
-    NoteOn(0, 72, 0x7f);
-    wait(0.8);
-    NoteOff(0, 72, 0x7f);
-    wait(0.001);
+            const char* cmd = "";
+            USBLoop();
+            if (IsConsoleReadable())
+                cmd = ReadLine();
+
+            if ((strcmp(cmd,"s") == 0) || (mySW == 0)) {
+                myLED1 = 1;
+                Miku(3);
+                wait(0.001);
+                NoteOn(0, 72, 0x7f);
+                wait(0.8);
+                NoteOff(0, 72, 0x7f);
+                wait(0.001);
  
-    Miku(124);
-    wait(0.001);
-    NoteOn(0, 74, 0x7f);
-    wait(0.5);
-    NoteOff(0, 74, 0x7f);
-    wait(0.001);
+                Miku(124);
+                wait(0.001);
+                NoteOn(0, 74, 0x7f);
+                wait(0.5);
+                NoteOff(0, 74, 0x7f);
+                wait(0.001);
  
-    Miku(79);
-    wait(0.001);
-    NoteOn(0, 76, 0x7f);
-    wait(0.5);
-    NoteOff(0, 76, 0x7f);
+                Miku(79);
+                wait(0.001);
+                NoteOn(0, 76, 0x7f);
+                wait(0.5);
+                NoteOff(0, 76, 0x7f);
 
-    wait(0.2);
+                wait(0.2);
 
-    Miku(50);
-    wait(0.001);
-    NoteOn(0, 76, 0x7f);
-    wait(0.5);
-    NoteOff(0, 76, 0x7f);
-
+                Miku(50);
+                wait(0.001);
+                NoteOn(0, 76, 0x7f);
+                wait(0.5);
+                NoteOff(0, 76, 0x7f);
+                myLED1 = 0;
             } else 
             if (strcmp(cmd,"scan") == 0 || strcmp(cmd,"inquiry") == 0)
                 Inquiry();
@@ -380,6 +389,8 @@
             {
                 Quit();
                 break;
+            } else if (*cmd == 0) {
+                /* ignore */
             } else {
                 printf("eh? %s\n",cmd);
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestShell.h	Sun Apr 27 01:36:30 2014 +0000
@@ -0,0 +1,6 @@
+#ifndef __TESTSHELL_H__
+#define __TESTSHELL_H__
+
+extern void TestShell();
+extern void InitSerialMIDI();  // Added by RadioJunkBox
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common.h	Sun Apr 27 01:36:30 2014 +0000
@@ -0,0 +1,8 @@
+#ifndef __COMMON_H__
+#define __COMMON_H__
+
+#define SWpin p21
+
+extern int GetConsoleChar();
+extern bool IsConsoleReadable();
+#endif
--- a/main.cpp	Sat Apr 05 06:25:43 2014 +0000
+++ b/main.cpp	Sun Apr 27 01:36:30 2014 +0000
@@ -29,6 +29,9 @@
 #include "Utils.h"
 #include "FATFileSystem.h"
 
+#include "common.h"
+#include "TestShell.h"
+
 int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize);
 int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize);
 int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize);
@@ -115,14 +118,24 @@
     return c;
 }
 
-void TestShell();
-void InitSerialMIDI();  // Added by RadioJunkBox
+bool IsConsoleReadable()
+{
+    return pc.readable();
+}
 
 
+static DigitalIn mySW(SWpin);
+static DigitalOut myLED(LED1);
+
 int main()
 {
 //    pc.baud(460800);
     printf("BlueUSB\nNow get a bunch of usb or bluetooth things and plug them in\n");
     InitSerialMIDI();   // Added by RadioJunkBox
+    if (mySW.read() == 0) {
+        myLED = 1;
+        wait(0.5);
+        myLED = 0;
+    }
     TestShell();
 }