Peter Barrett氏のBlueUSBにMIDI USB HOST機能を加えたサンプルプログラムです。KORG nanoKEYなどのUSB MIDIストリームをシリアルMIDI(Serial TX p9)にブリッジします。動作確認はKORG nanoKEY、AKAI LPK-25、EDIROL PC-50のみです。
main.cpp@0:79620c558b0c, 2012-05-11 (annotated)
- Committer:
- radiojunkbox
- Date:
- Fri May 11 10:05:40 2012 +0000
- Revision:
- 0:79620c558b0c
Rev. 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
radiojunkbox | 0:79620c558b0c | 1 | /* |
radiojunkbox | 0:79620c558b0c | 2 | Copyright (c) 2010 Peter Barrett |
radiojunkbox | 0:79620c558b0c | 3 | |
radiojunkbox | 0:79620c558b0c | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
radiojunkbox | 0:79620c558b0c | 5 | of this software and associated documentation files (the "Software"), to deal |
radiojunkbox | 0:79620c558b0c | 6 | in the Software without restriction, including without limitation the rights |
radiojunkbox | 0:79620c558b0c | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
radiojunkbox | 0:79620c558b0c | 8 | copies of the Software, and to permit persons to whom the Software is |
radiojunkbox | 0:79620c558b0c | 9 | furnished to do so, subject to the following conditions: |
radiojunkbox | 0:79620c558b0c | 10 | |
radiojunkbox | 0:79620c558b0c | 11 | The above copyright notice and this permission notice shall be included in |
radiojunkbox | 0:79620c558b0c | 12 | all copies or substantial portions of the Software. |
radiojunkbox | 0:79620c558b0c | 13 | |
radiojunkbox | 0:79620c558b0c | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
radiojunkbox | 0:79620c558b0c | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
radiojunkbox | 0:79620c558b0c | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
radiojunkbox | 0:79620c558b0c | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
radiojunkbox | 0:79620c558b0c | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
radiojunkbox | 0:79620c558b0c | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
radiojunkbox | 0:79620c558b0c | 20 | THE SOFTWARE. |
radiojunkbox | 0:79620c558b0c | 21 | */ |
radiojunkbox | 0:79620c558b0c | 22 | |
radiojunkbox | 0:79620c558b0c | 23 | /* |
radiojunkbox | 0:79620c558b0c | 24 | May 11 2012 RadioJunkBox : added MIDI USB support |
radiojunkbox | 0:79620c558b0c | 25 | */ |
radiojunkbox | 0:79620c558b0c | 26 | |
radiojunkbox | 0:79620c558b0c | 27 | #include "mbed.h" |
radiojunkbox | 0:79620c558b0c | 28 | #include "USBHost.h" |
radiojunkbox | 0:79620c558b0c | 29 | #include "Utils.h" |
radiojunkbox | 0:79620c558b0c | 30 | #include "FATFileSystem.h" |
radiojunkbox | 0:79620c558b0c | 31 | |
radiojunkbox | 0:79620c558b0c | 32 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize); |
radiojunkbox | 0:79620c558b0c | 33 | int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize); |
radiojunkbox | 0:79620c558b0c | 34 | int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize); |
radiojunkbox | 0:79620c558b0c | 35 | |
radiojunkbox | 0:79620c558b0c | 36 | class USBFileSystem : public FATFileSystem |
radiojunkbox | 0:79620c558b0c | 37 | { |
radiojunkbox | 0:79620c558b0c | 38 | int _device; |
radiojunkbox | 0:79620c558b0c | 39 | u32 _blockSize; |
radiojunkbox | 0:79620c558b0c | 40 | u32 _blockCount; |
radiojunkbox | 0:79620c558b0c | 41 | |
radiojunkbox | 0:79620c558b0c | 42 | public: |
radiojunkbox | 0:79620c558b0c | 43 | USBFileSystem() : FATFileSystem("usb"),_device(0),_blockSize(0),_blockCount(0) |
radiojunkbox | 0:79620c558b0c | 44 | { |
radiojunkbox | 0:79620c558b0c | 45 | } |
radiojunkbox | 0:79620c558b0c | 46 | |
radiojunkbox | 0:79620c558b0c | 47 | void SetDevice(int device) |
radiojunkbox | 0:79620c558b0c | 48 | { |
radiojunkbox | 0:79620c558b0c | 49 | _device = device; |
radiojunkbox | 0:79620c558b0c | 50 | } |
radiojunkbox | 0:79620c558b0c | 51 | |
radiojunkbox | 0:79620c558b0c | 52 | virtual int disk_initialize() |
radiojunkbox | 0:79620c558b0c | 53 | { |
radiojunkbox | 0:79620c558b0c | 54 | return MassStorage_ReadCapacity(_device,&_blockCount,&_blockSize); |
radiojunkbox | 0:79620c558b0c | 55 | } |
radiojunkbox | 0:79620c558b0c | 56 | |
radiojunkbox | 0:79620c558b0c | 57 | virtual int disk_write(const char *buffer, int block_number) |
radiojunkbox | 0:79620c558b0c | 58 | { |
radiojunkbox | 0:79620c558b0c | 59 | return MassStorage_Write(_device,block_number,1,(u8*)buffer,_blockSize); |
radiojunkbox | 0:79620c558b0c | 60 | } |
radiojunkbox | 0:79620c558b0c | 61 | |
radiojunkbox | 0:79620c558b0c | 62 | virtual int disk_read(char *buffer, int block_number) |
radiojunkbox | 0:79620c558b0c | 63 | { |
radiojunkbox | 0:79620c558b0c | 64 | return MassStorage_Read(_device,block_number,1,(u8*)buffer,_blockSize); |
radiojunkbox | 0:79620c558b0c | 65 | } |
radiojunkbox | 0:79620c558b0c | 66 | |
radiojunkbox | 0:79620c558b0c | 67 | virtual int disk_sectors() |
radiojunkbox | 0:79620c558b0c | 68 | { |
radiojunkbox | 0:79620c558b0c | 69 | return _blockCount; |
radiojunkbox | 0:79620c558b0c | 70 | } |
radiojunkbox | 0:79620c558b0c | 71 | }; |
radiojunkbox | 0:79620c558b0c | 72 | |
radiojunkbox | 0:79620c558b0c | 73 | void DumpFS(int depth, int count) |
radiojunkbox | 0:79620c558b0c | 74 | { |
radiojunkbox | 0:79620c558b0c | 75 | DIR *d = opendir("/usb"); |
radiojunkbox | 0:79620c558b0c | 76 | if (!d) |
radiojunkbox | 0:79620c558b0c | 77 | { |
radiojunkbox | 0:79620c558b0c | 78 | printf("USB file system borked\n"); |
radiojunkbox | 0:79620c558b0c | 79 | return; |
radiojunkbox | 0:79620c558b0c | 80 | } |
radiojunkbox | 0:79620c558b0c | 81 | |
radiojunkbox | 0:79620c558b0c | 82 | printf("\nDumping root dir\n"); |
radiojunkbox | 0:79620c558b0c | 83 | struct dirent *p; |
radiojunkbox | 0:79620c558b0c | 84 | for(;;) |
radiojunkbox | 0:79620c558b0c | 85 | { |
radiojunkbox | 0:79620c558b0c | 86 | p = readdir(d); |
radiojunkbox | 0:79620c558b0c | 87 | if (!p) |
radiojunkbox | 0:79620c558b0c | 88 | break; |
radiojunkbox | 0:79620c558b0c | 89 | int len = sizeof( dirent); |
radiojunkbox | 0:79620c558b0c | 90 | printf("%s %d\n", p->d_name, len); |
radiojunkbox | 0:79620c558b0c | 91 | } |
radiojunkbox | 0:79620c558b0c | 92 | closedir(d); |
radiojunkbox | 0:79620c558b0c | 93 | } |
radiojunkbox | 0:79620c558b0c | 94 | |
radiojunkbox | 0:79620c558b0c | 95 | int OnDiskInsert(int device) |
radiojunkbox | 0:79620c558b0c | 96 | { |
radiojunkbox | 0:79620c558b0c | 97 | USBFileSystem fs; |
radiojunkbox | 0:79620c558b0c | 98 | fs.SetDevice(device); |
radiojunkbox | 0:79620c558b0c | 99 | DumpFS(0,0); |
radiojunkbox | 0:79620c558b0c | 100 | return 0; |
radiojunkbox | 0:79620c558b0c | 101 | } |
radiojunkbox | 0:79620c558b0c | 102 | |
radiojunkbox | 0:79620c558b0c | 103 | /* |
radiojunkbox | 0:79620c558b0c | 104 | Simple test shell to exercise mouse,keyboard,mass storage and hubs. |
radiojunkbox | 0:79620c558b0c | 105 | Add 2 15k pulldown resistors between D+/D- and ground, attach a usb socket and have at it. |
radiojunkbox | 0:79620c558b0c | 106 | */ |
radiojunkbox | 0:79620c558b0c | 107 | |
radiojunkbox | 0:79620c558b0c | 108 | Serial pc(USBTX, USBRX); |
radiojunkbox | 0:79620c558b0c | 109 | int GetConsoleChar() |
radiojunkbox | 0:79620c558b0c | 110 | { |
radiojunkbox | 0:79620c558b0c | 111 | if (!pc.readable()) |
radiojunkbox | 0:79620c558b0c | 112 | return -1; |
radiojunkbox | 0:79620c558b0c | 113 | char c = pc.getc(); |
radiojunkbox | 0:79620c558b0c | 114 | pc.putc(c); // echo |
radiojunkbox | 0:79620c558b0c | 115 | return c; |
radiojunkbox | 0:79620c558b0c | 116 | } |
radiojunkbox | 0:79620c558b0c | 117 | |
radiojunkbox | 0:79620c558b0c | 118 | void TestShell(); |
radiojunkbox | 0:79620c558b0c | 119 | void InitSerialMIDI(); // Added by RadioJunkBox |
radiojunkbox | 0:79620c558b0c | 120 | |
radiojunkbox | 0:79620c558b0c | 121 | int main() |
radiojunkbox | 0:79620c558b0c | 122 | { |
radiojunkbox | 0:79620c558b0c | 123 | // pc.baud(460800); |
radiojunkbox | 0:79620c558b0c | 124 | printf("BlueUSB\nNow get a bunch of usb or bluetooth things and plug them in\n"); |
radiojunkbox | 0:79620c558b0c | 125 | InitSerialMIDI(); // Added by RadioJunkBox |
radiojunkbox | 0:79620c558b0c | 126 | TestShell(); |
radiojunkbox | 0:79620c558b0c | 127 | } |