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
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'
TestShell.cpp@2:7576d1327cf1, 2014-04-27 (annotated)
- Committer:
- non
- Date:
- Sun Apr 27 01:36:30 2014 +0000
- Revision:
- 2:7576d1327cf1
- Parent:
- 1:892f8922bdc4
- Child:
- 3:31fbce33c25b
Now it plays when you push SW connected to p21.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
radiojunkbox | 0:79620c558b0c | 1 | |
radiojunkbox | 0:79620c558b0c | 2 | /* |
radiojunkbox | 0:79620c558b0c | 3 | Copyright (c) 2010 Peter Barrett |
radiojunkbox | 0:79620c558b0c | 4 | |
radiojunkbox | 0:79620c558b0c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
radiojunkbox | 0:79620c558b0c | 6 | of this software and associated documentation files (the "Software"), to deal |
radiojunkbox | 0:79620c558b0c | 7 | in the Software without restriction, including without limitation the rights |
radiojunkbox | 0:79620c558b0c | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
radiojunkbox | 0:79620c558b0c | 9 | copies of the Software, and to permit persons to whom the Software is |
radiojunkbox | 0:79620c558b0c | 10 | furnished to do so, subject to the following conditions: |
radiojunkbox | 0:79620c558b0c | 11 | |
radiojunkbox | 0:79620c558b0c | 12 | The above copyright notice and this permission notice shall be included in |
radiojunkbox | 0:79620c558b0c | 13 | all copies or substantial portions of the Software. |
radiojunkbox | 0:79620c558b0c | 14 | |
radiojunkbox | 0:79620c558b0c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
radiojunkbox | 0:79620c558b0c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
radiojunkbox | 0:79620c558b0c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
radiojunkbox | 0:79620c558b0c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
radiojunkbox | 0:79620c558b0c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
radiojunkbox | 0:79620c558b0c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
radiojunkbox | 0:79620c558b0c | 21 | THE SOFTWARE. |
radiojunkbox | 0:79620c558b0c | 22 | */ |
radiojunkbox | 0:79620c558b0c | 23 | |
radiojunkbox | 0:79620c558b0c | 24 | #include <stdio.h> |
radiojunkbox | 0:79620c558b0c | 25 | #include <stdlib.h> |
radiojunkbox | 0:79620c558b0c | 26 | #include <stdio.h> |
radiojunkbox | 0:79620c558b0c | 27 | #include <string.h> |
radiojunkbox | 0:79620c558b0c | 28 | |
non | 1:892f8922bdc4 | 29 | #include "mbed.h" |
radiojunkbox | 0:79620c558b0c | 30 | #include "Utils.h" |
radiojunkbox | 0:79620c558b0c | 31 | #include "USBHost.h" |
radiojunkbox | 0:79620c558b0c | 32 | #include "hci.h" |
radiojunkbox | 0:79620c558b0c | 33 | |
non | 2:7576d1327cf1 | 34 | #include "common.h" |
non | 2:7576d1327cf1 | 35 | #include "TestShell.h" |
non | 2:7576d1327cf1 | 36 | |
non | 2:7576d1327cf1 | 37 | static DigitalOut myLED1(LED1); |
non | 2:7576d1327cf1 | 38 | static DigitalIn mySW(SWpin); |
non | 2:7576d1327cf1 | 39 | |
radiojunkbox | 0:79620c558b0c | 40 | void printf(const BD_ADDR* addr) |
radiojunkbox | 0:79620c558b0c | 41 | { |
radiojunkbox | 0:79620c558b0c | 42 | const u8* a = addr->addr; |
radiojunkbox | 0:79620c558b0c | 43 | printf("%02X:%02X:%02X:%02X:%02X:%02X",a[5],a[4],a[3],a[2],a[1],a[0]); |
radiojunkbox | 0:79620c558b0c | 44 | } |
radiojunkbox | 0:79620c558b0c | 45 | |
radiojunkbox | 0:79620c558b0c | 46 | #define MAX_HCL_SIZE 260 |
radiojunkbox | 0:79620c558b0c | 47 | #define MAX_ACL_SIZE 400 |
radiojunkbox | 0:79620c558b0c | 48 | |
radiojunkbox | 0:79620c558b0c | 49 | class HCITransportUSB : public HCITransport |
radiojunkbox | 0:79620c558b0c | 50 | { |
radiojunkbox | 0:79620c558b0c | 51 | int _device; |
radiojunkbox | 0:79620c558b0c | 52 | u8* _hciBuffer; |
radiojunkbox | 0:79620c558b0c | 53 | u8* _aclBuffer; |
radiojunkbox | 0:79620c558b0c | 54 | |
radiojunkbox | 0:79620c558b0c | 55 | public: |
radiojunkbox | 0:79620c558b0c | 56 | void Open(int device, u8* hciBuffer, u8* aclBuffer) |
radiojunkbox | 0:79620c558b0c | 57 | { |
radiojunkbox | 0:79620c558b0c | 58 | _device = device; |
radiojunkbox | 0:79620c558b0c | 59 | _hciBuffer = hciBuffer; |
radiojunkbox | 0:79620c558b0c | 60 | _aclBuffer = aclBuffer; |
radiojunkbox | 0:79620c558b0c | 61 | USBInterruptTransfer(_device,0x81,_hciBuffer,MAX_HCL_SIZE,HciCallback,this); |
radiojunkbox | 0:79620c558b0c | 62 | USBBulkTransfer(_device,0x82,_aclBuffer,MAX_ACL_SIZE,AclCallback,this); |
radiojunkbox | 0:79620c558b0c | 63 | } |
radiojunkbox | 0:79620c558b0c | 64 | |
radiojunkbox | 0:79620c558b0c | 65 | static void HciCallback(int device, int endpoint, int status, u8* data, int len, void* userData) |
radiojunkbox | 0:79620c558b0c | 66 | { |
radiojunkbox | 0:79620c558b0c | 67 | HCI* t = ((HCITransportUSB*)userData)->_target; |
radiojunkbox | 0:79620c558b0c | 68 | if (t) |
radiojunkbox | 0:79620c558b0c | 69 | t->HCIRecv(data,len); |
radiojunkbox | 0:79620c558b0c | 70 | USBInterruptTransfer(device,0x81,data,MAX_HCL_SIZE,HciCallback,userData); |
radiojunkbox | 0:79620c558b0c | 71 | } |
radiojunkbox | 0:79620c558b0c | 72 | |
radiojunkbox | 0:79620c558b0c | 73 | static void AclCallback(int device, int endpoint, int status, u8* data, int len, void* userData) |
radiojunkbox | 0:79620c558b0c | 74 | { |
radiojunkbox | 0:79620c558b0c | 75 | HCI* t = ((HCITransportUSB*)userData)->_target; |
radiojunkbox | 0:79620c558b0c | 76 | if (t) |
radiojunkbox | 0:79620c558b0c | 77 | t->ACLRecv(data,len); |
radiojunkbox | 0:79620c558b0c | 78 | USBBulkTransfer(device,0x82,data,MAX_ACL_SIZE,AclCallback,userData); |
radiojunkbox | 0:79620c558b0c | 79 | } |
radiojunkbox | 0:79620c558b0c | 80 | |
radiojunkbox | 0:79620c558b0c | 81 | virtual void HCISend(const u8* data, int len) |
radiojunkbox | 0:79620c558b0c | 82 | { |
radiojunkbox | 0:79620c558b0c | 83 | USBControlTransfer(_device,REQUEST_TYPE_CLASS, 0, 0, 0,(u8*)data,len); |
radiojunkbox | 0:79620c558b0c | 84 | } |
radiojunkbox | 0:79620c558b0c | 85 | |
radiojunkbox | 0:79620c558b0c | 86 | virtual void ACLSend(const u8* data, int len) |
radiojunkbox | 0:79620c558b0c | 87 | { |
radiojunkbox | 0:79620c558b0c | 88 | USBBulkTransfer(_device,0x02,(u8*)data,len); |
radiojunkbox | 0:79620c558b0c | 89 | } |
radiojunkbox | 0:79620c558b0c | 90 | }; |
radiojunkbox | 0:79620c558b0c | 91 | |
radiojunkbox | 0:79620c558b0c | 92 | |
radiojunkbox | 0:79620c558b0c | 93 | #define WII_REMOTE 0x042500 |
radiojunkbox | 0:79620c558b0c | 94 | |
non | 2:7576d1327cf1 | 95 | #define NSX39_device 1 |
non | 2:7576d1327cf1 | 96 | #define NSX39_endpoint 2 |
non | 2:7576d1327cf1 | 97 | |
non | 1:892f8922bdc4 | 98 | void NoteOn(u8 ch, u8 note, u8 velocity) |
non | 1:892f8922bdc4 | 99 | { |
non | 1:892f8922bdc4 | 100 | static u8 d[64] = {0x09}; |
non | 1:892f8922bdc4 | 101 | d[1] = 0x90 | (ch & 0x7f); |
non | 1:892f8922bdc4 | 102 | d[2] = note & 0x7f; |
non | 1:892f8922bdc4 | 103 | d[3] = velocity & 0x7f; |
non | 2:7576d1327cf1 | 104 | USBBulkTransfer(NSX39_device, NSX39_endpoint, d, 4, NULL, NULL); |
non | 1:892f8922bdc4 | 105 | } |
non | 1:892f8922bdc4 | 106 | |
non | 1:892f8922bdc4 | 107 | void NoteOff(u8 ch, u8 note, u8 velocity) |
non | 1:892f8922bdc4 | 108 | { |
non | 1:892f8922bdc4 | 109 | static u8 d[64] = {0x08}; |
non | 1:892f8922bdc4 | 110 | d[1] = 0x80 | (ch & 0xf); |
non | 1:892f8922bdc4 | 111 | d[2] = note & 0x7f; |
non | 1:892f8922bdc4 | 112 | d[3] = velocity & 0x7f; |
non | 2:7576d1327cf1 | 113 | USBBulkTransfer(NSX39_device, NSX39_endpoint, d, 4, NULL, NULL); |
non | 1:892f8922bdc4 | 114 | } |
non | 1:892f8922bdc4 | 115 | |
non | 1:892f8922bdc4 | 116 | void Miku(u8 chr) |
non | 1:892f8922bdc4 | 117 | { |
non | 1:892f8922bdc4 | 118 | static u8 d[64] = {0x04, 0xf0, 0x43, 0x79, |
non | 1:892f8922bdc4 | 119 | 0x04, 0x09, 0x11, 0x0a, |
non | 1:892f8922bdc4 | 120 | 0x07, 0x00, 0x00, 0xf7}; |
non | 1:892f8922bdc4 | 121 | d[10] = chr; |
non | 2:7576d1327cf1 | 122 | USBBulkTransfer(NSX39_device, NSX39_endpoint, d, 12, NULL, NULL); |
non | 1:892f8922bdc4 | 123 | } |
non | 1:892f8922bdc4 | 124 | |
non | 1:892f8922bdc4 | 125 | |
radiojunkbox | 0:79620c558b0c | 126 | class HIDBluetooth |
radiojunkbox | 0:79620c558b0c | 127 | { |
radiojunkbox | 0:79620c558b0c | 128 | int _control; // Sockets for control (out) and interrupt (in) |
radiojunkbox | 0:79620c558b0c | 129 | int _interrupt; |
radiojunkbox | 0:79620c558b0c | 130 | int _devClass; |
radiojunkbox | 0:79620c558b0c | 131 | BD_ADDR _addr; |
radiojunkbox | 0:79620c558b0c | 132 | u8 _pad[2]; // Struct align |
radiojunkbox | 0:79620c558b0c | 133 | |
radiojunkbox | 0:79620c558b0c | 134 | public: |
radiojunkbox | 0:79620c558b0c | 135 | HIDBluetooth() : _control(0),_interrupt(0),_devClass(0) {}; |
radiojunkbox | 0:79620c558b0c | 136 | |
radiojunkbox | 0:79620c558b0c | 137 | bool InUse() |
radiojunkbox | 0:79620c558b0c | 138 | { |
radiojunkbox | 0:79620c558b0c | 139 | return _control != 0; |
radiojunkbox | 0:79620c558b0c | 140 | } |
radiojunkbox | 0:79620c558b0c | 141 | |
radiojunkbox | 0:79620c558b0c | 142 | static void OnHidInterrupt(int socket, SocketState state, const u8* data, int len, void* userData) |
radiojunkbox | 0:79620c558b0c | 143 | { |
radiojunkbox | 0:79620c558b0c | 144 | HIDBluetooth* t = (HIDBluetooth*)userData; |
radiojunkbox | 0:79620c558b0c | 145 | if (data) |
radiojunkbox | 0:79620c558b0c | 146 | { |
radiojunkbox | 0:79620c558b0c | 147 | if (t->_devClass == WII_REMOTE && data[1] == 0x30) |
radiojunkbox | 0:79620c558b0c | 148 | { |
radiojunkbox | 0:79620c558b0c | 149 | printf("================wii====================\n"); |
radiojunkbox | 0:79620c558b0c | 150 | t->Led(); |
radiojunkbox | 0:79620c558b0c | 151 | t->Hid(); // ask for accelerometer |
radiojunkbox | 0:79620c558b0c | 152 | t->_devClass = 0; |
radiojunkbox | 0:79620c558b0c | 153 | } |
radiojunkbox | 0:79620c558b0c | 154 | |
radiojunkbox | 0:79620c558b0c | 155 | const u8* d = data; |
radiojunkbox | 0:79620c558b0c | 156 | switch (d[1]) |
radiojunkbox | 0:79620c558b0c | 157 | { |
radiojunkbox | 0:79620c558b0c | 158 | case 0x02: |
radiojunkbox | 0:79620c558b0c | 159 | { |
radiojunkbox | 0:79620c558b0c | 160 | int x = (signed char)d[3]; |
radiojunkbox | 0:79620c558b0c | 161 | int y = (signed char)d[4]; |
radiojunkbox | 0:79620c558b0c | 162 | printf("Mouse %2X dx:%d dy:%d\n",d[2],x,y); |
radiojunkbox | 0:79620c558b0c | 163 | } |
radiojunkbox | 0:79620c558b0c | 164 | break; |
radiojunkbox | 0:79620c558b0c | 165 | |
radiojunkbox | 0:79620c558b0c | 166 | case 0x37: // Accelerometer http://wiki.wiimoteproject.com/Reports |
radiojunkbox | 0:79620c558b0c | 167 | { |
radiojunkbox | 0:79620c558b0c | 168 | int pad = (d[2] & 0x9F) | ((d[3] & 0x9F) << 8); |
radiojunkbox | 0:79620c558b0c | 169 | int x = (d[2] & 0x60) >> 5 | d[4] << 2; |
radiojunkbox | 0:79620c558b0c | 170 | int y = (d[3] & 0x20) >> 4 | d[5] << 2; |
radiojunkbox | 0:79620c558b0c | 171 | int z = (d[3] & 0x40) >> 5 | d[6] << 2; |
radiojunkbox | 0:79620c558b0c | 172 | printf("WII %04X %d %d %d\n",pad,x,y,z); |
radiojunkbox | 0:79620c558b0c | 173 | } |
radiojunkbox | 0:79620c558b0c | 174 | break; |
radiojunkbox | 0:79620c558b0c | 175 | default: |
radiojunkbox | 0:79620c558b0c | 176 | printHex(data,len); |
radiojunkbox | 0:79620c558b0c | 177 | } |
radiojunkbox | 0:79620c558b0c | 178 | } |
radiojunkbox | 0:79620c558b0c | 179 | } |
radiojunkbox | 0:79620c558b0c | 180 | |
radiojunkbox | 0:79620c558b0c | 181 | static void OnHidControl(int socket, SocketState state, const u8* data, int len, void* userData) |
radiojunkbox | 0:79620c558b0c | 182 | { |
radiojunkbox | 0:79620c558b0c | 183 | printf("OnHidControl\n"); |
radiojunkbox | 0:79620c558b0c | 184 | if (data) |
radiojunkbox | 0:79620c558b0c | 185 | printHex(data,len); |
radiojunkbox | 0:79620c558b0c | 186 | } |
radiojunkbox | 0:79620c558b0c | 187 | |
radiojunkbox | 0:79620c558b0c | 188 | void Open(BD_ADDR* bdAddr, inquiry_info* info) |
radiojunkbox | 0:79620c558b0c | 189 | { |
radiojunkbox | 0:79620c558b0c | 190 | printf("L2CAPAddr size %d\n",sizeof(L2CAPAddr)); |
radiojunkbox | 0:79620c558b0c | 191 | _addr = *bdAddr; |
radiojunkbox | 0:79620c558b0c | 192 | L2CAPAddr sockAddr; |
radiojunkbox | 0:79620c558b0c | 193 | sockAddr.bdaddr = _addr; |
radiojunkbox | 0:79620c558b0c | 194 | sockAddr.psm = L2CAP_PSM_HID_INTR; |
radiojunkbox | 0:79620c558b0c | 195 | printf("Socket_Open size %d\n",sizeof(L2CAPAddr)); |
radiojunkbox | 0:79620c558b0c | 196 | _interrupt = Socket_Open(SOCKET_L2CAP,&sockAddr.hdr,OnHidInterrupt,this); |
radiojunkbox | 0:79620c558b0c | 197 | sockAddr.psm = L2CAP_PSM_HID_CNTL; |
radiojunkbox | 0:79620c558b0c | 198 | _control = Socket_Open(SOCKET_L2CAP,&sockAddr.hdr,OnHidControl,this); |
radiojunkbox | 0:79620c558b0c | 199 | |
radiojunkbox | 0:79620c558b0c | 200 | printfBytes("OPEN DEVICE CLASS",info->dev_class,3); |
radiojunkbox | 0:79620c558b0c | 201 | _devClass = (info->dev_class[0] << 16) | (info->dev_class[1] << 8) | info->dev_class[2]; |
radiojunkbox | 0:79620c558b0c | 202 | } |
radiojunkbox | 0:79620c558b0c | 203 | |
radiojunkbox | 0:79620c558b0c | 204 | void Close() |
radiojunkbox | 0:79620c558b0c | 205 | { |
radiojunkbox | 0:79620c558b0c | 206 | if (_control) |
radiojunkbox | 0:79620c558b0c | 207 | Socket_Close(_control); |
radiojunkbox | 0:79620c558b0c | 208 | if (_interrupt) |
radiojunkbox | 0:79620c558b0c | 209 | Socket_Close(_interrupt); |
radiojunkbox | 0:79620c558b0c | 210 | _control = _interrupt = 0; |
radiojunkbox | 0:79620c558b0c | 211 | } |
radiojunkbox | 0:79620c558b0c | 212 | |
radiojunkbox | 0:79620c558b0c | 213 | void Led(int id = 0x10) |
radiojunkbox | 0:79620c558b0c | 214 | { |
radiojunkbox | 0:79620c558b0c | 215 | u8 led[3] = {0x52, 0x11, id}; |
radiojunkbox | 0:79620c558b0c | 216 | if (_control) |
radiojunkbox | 0:79620c558b0c | 217 | Socket_Send(_control,led,3); |
radiojunkbox | 0:79620c558b0c | 218 | } |
radiojunkbox | 0:79620c558b0c | 219 | |
radiojunkbox | 0:79620c558b0c | 220 | void Hid(int report = 0x37) |
radiojunkbox | 0:79620c558b0c | 221 | { |
radiojunkbox | 0:79620c558b0c | 222 | u8 hid[4] = { 0x52, 0x12, 0x00, report }; |
radiojunkbox | 0:79620c558b0c | 223 | if (_control != -1) |
radiojunkbox | 0:79620c558b0c | 224 | Socket_Send(_control,hid,4); |
radiojunkbox | 0:79620c558b0c | 225 | } |
radiojunkbox | 0:79620c558b0c | 226 | }; |
radiojunkbox | 0:79620c558b0c | 227 | |
radiojunkbox | 0:79620c558b0c | 228 | |
radiojunkbox | 0:79620c558b0c | 229 | HCI* gHCI = 0; |
radiojunkbox | 0:79620c558b0c | 230 | |
radiojunkbox | 0:79620c558b0c | 231 | #define MAX_HID_DEVICES 8 |
radiojunkbox | 0:79620c558b0c | 232 | |
radiojunkbox | 0:79620c558b0c | 233 | class ShellApp |
radiojunkbox | 0:79620c558b0c | 234 | { |
radiojunkbox | 0:79620c558b0c | 235 | char _line[64]; |
radiojunkbox | 0:79620c558b0c | 236 | HIDBluetooth _hids[MAX_HID_DEVICES]; |
radiojunkbox | 0:79620c558b0c | 237 | |
radiojunkbox | 0:79620c558b0c | 238 | public: |
radiojunkbox | 0:79620c558b0c | 239 | void Ready() |
radiojunkbox | 0:79620c558b0c | 240 | { |
radiojunkbox | 0:79620c558b0c | 241 | printf("HIDBluetooth %d\n",sizeof(HIDBluetooth)); |
radiojunkbox | 0:79620c558b0c | 242 | memset(_hids,0,sizeof(_hids)); |
radiojunkbox | 0:79620c558b0c | 243 | Inquiry(); |
radiojunkbox | 0:79620c558b0c | 244 | |
radiojunkbox | 0:79620c558b0c | 245 | } |
radiojunkbox | 0:79620c558b0c | 246 | |
radiojunkbox | 0:79620c558b0c | 247 | // We have connected to a device |
radiojunkbox | 0:79620c558b0c | 248 | void ConnectionComplete(HCI* hci, connection_info* info) |
radiojunkbox | 0:79620c558b0c | 249 | { |
radiojunkbox | 0:79620c558b0c | 250 | printf("ConnectionComplete "); |
radiojunkbox | 0:79620c558b0c | 251 | BD_ADDR* a = &info->bdaddr; |
radiojunkbox | 0:79620c558b0c | 252 | printf(a); |
radiojunkbox | 0:79620c558b0c | 253 | BTDevice* bt = hci->Find(a); |
radiojunkbox | 0:79620c558b0c | 254 | HIDBluetooth* hid = NewHIDBluetooth(); |
radiojunkbox | 0:79620c558b0c | 255 | printf("%08x %08x\n",bt,hid); |
radiojunkbox | 0:79620c558b0c | 256 | if (hid) |
radiojunkbox | 0:79620c558b0c | 257 | hid->Open(a,&bt->_info); |
radiojunkbox | 0:79620c558b0c | 258 | } |
radiojunkbox | 0:79620c558b0c | 259 | |
radiojunkbox | 0:79620c558b0c | 260 | HIDBluetooth* NewHIDBluetooth() |
radiojunkbox | 0:79620c558b0c | 261 | { |
radiojunkbox | 0:79620c558b0c | 262 | for (int i = 0; i < MAX_HID_DEVICES; i++) |
radiojunkbox | 0:79620c558b0c | 263 | if (!_hids[i].InUse()) |
radiojunkbox | 0:79620c558b0c | 264 | return _hids+i; |
radiojunkbox | 0:79620c558b0c | 265 | return 0; |
radiojunkbox | 0:79620c558b0c | 266 | } |
radiojunkbox | 0:79620c558b0c | 267 | |
radiojunkbox | 0:79620c558b0c | 268 | void ConnectDevices() |
radiojunkbox | 0:79620c558b0c | 269 | { |
radiojunkbox | 0:79620c558b0c | 270 | BTDevice* devs[8]; |
radiojunkbox | 0:79620c558b0c | 271 | int count = gHCI->GetDevices(devs,8); |
radiojunkbox | 0:79620c558b0c | 272 | for (int i = 0; i < count; i++) |
radiojunkbox | 0:79620c558b0c | 273 | { |
radiojunkbox | 0:79620c558b0c | 274 | printfBytes("DEVICE CLASS",devs[i]->_info.dev_class,3); |
radiojunkbox | 0:79620c558b0c | 275 | if (devs[i]->_handle == 0) |
radiojunkbox | 0:79620c558b0c | 276 | { |
radiojunkbox | 0:79620c558b0c | 277 | BD_ADDR* bd = &devs[i]->_info.bdaddr; |
radiojunkbox | 0:79620c558b0c | 278 | printf("Connecting to "); |
radiojunkbox | 0:79620c558b0c | 279 | printf(bd); |
radiojunkbox | 0:79620c558b0c | 280 | printf("\n"); |
radiojunkbox | 0:79620c558b0c | 281 | gHCI->CreateConnection(bd); |
radiojunkbox | 0:79620c558b0c | 282 | } |
radiojunkbox | 0:79620c558b0c | 283 | } |
radiojunkbox | 0:79620c558b0c | 284 | } |
radiojunkbox | 0:79620c558b0c | 285 | |
radiojunkbox | 0:79620c558b0c | 286 | const char* ReadLine() |
radiojunkbox | 0:79620c558b0c | 287 | { |
radiojunkbox | 0:79620c558b0c | 288 | int i; |
radiojunkbox | 0:79620c558b0c | 289 | for (i = 0; i < 255; ) |
radiojunkbox | 0:79620c558b0c | 290 | { |
radiojunkbox | 0:79620c558b0c | 291 | USBLoop(); |
radiojunkbox | 0:79620c558b0c | 292 | int c = GetConsoleChar(); |
radiojunkbox | 0:79620c558b0c | 293 | if (c == -1) |
radiojunkbox | 0:79620c558b0c | 294 | continue; |
radiojunkbox | 0:79620c558b0c | 295 | if (c == '\n' || c == 13) |
radiojunkbox | 0:79620c558b0c | 296 | break; |
radiojunkbox | 0:79620c558b0c | 297 | _line[i++] = c; |
radiojunkbox | 0:79620c558b0c | 298 | } |
radiojunkbox | 0:79620c558b0c | 299 | _line[i] = 0; |
radiojunkbox | 0:79620c558b0c | 300 | return _line; |
radiojunkbox | 0:79620c558b0c | 301 | } |
radiojunkbox | 0:79620c558b0c | 302 | |
radiojunkbox | 0:79620c558b0c | 303 | void Inquiry() |
radiojunkbox | 0:79620c558b0c | 304 | { |
radiojunkbox | 0:79620c558b0c | 305 | printf("Inquiry..\n"); |
radiojunkbox | 0:79620c558b0c | 306 | gHCI->Inquiry(); |
radiojunkbox | 0:79620c558b0c | 307 | } |
radiojunkbox | 0:79620c558b0c | 308 | |
radiojunkbox | 0:79620c558b0c | 309 | void List() |
radiojunkbox | 0:79620c558b0c | 310 | { |
radiojunkbox | 0:79620c558b0c | 311 | #if 0 |
radiojunkbox | 0:79620c558b0c | 312 | printf("%d devices\n",_deviceCount); |
radiojunkbox | 0:79620c558b0c | 313 | for (int i = 0; i < _deviceCount; i++) |
radiojunkbox | 0:79620c558b0c | 314 | { |
radiojunkbox | 0:79620c558b0c | 315 | printf(&_devices[i].info.bdaddr); |
radiojunkbox | 0:79620c558b0c | 316 | printf("\n"); |
radiojunkbox | 0:79620c558b0c | 317 | } |
radiojunkbox | 0:79620c558b0c | 318 | #endif |
radiojunkbox | 0:79620c558b0c | 319 | } |
radiojunkbox | 0:79620c558b0c | 320 | |
radiojunkbox | 0:79620c558b0c | 321 | void Connect() |
radiojunkbox | 0:79620c558b0c | 322 | { |
radiojunkbox | 0:79620c558b0c | 323 | ConnectDevices(); |
radiojunkbox | 0:79620c558b0c | 324 | } |
radiojunkbox | 0:79620c558b0c | 325 | |
radiojunkbox | 0:79620c558b0c | 326 | void Disconnect() |
radiojunkbox | 0:79620c558b0c | 327 | { |
radiojunkbox | 0:79620c558b0c | 328 | gHCI->DisconnectAll(); |
radiojunkbox | 0:79620c558b0c | 329 | } |
radiojunkbox | 0:79620c558b0c | 330 | |
radiojunkbox | 0:79620c558b0c | 331 | void CloseMouse() |
radiojunkbox | 0:79620c558b0c | 332 | { |
radiojunkbox | 0:79620c558b0c | 333 | } |
radiojunkbox | 0:79620c558b0c | 334 | |
radiojunkbox | 0:79620c558b0c | 335 | void Quit() |
radiojunkbox | 0:79620c558b0c | 336 | { |
radiojunkbox | 0:79620c558b0c | 337 | CloseMouse(); |
radiojunkbox | 0:79620c558b0c | 338 | } |
radiojunkbox | 0:79620c558b0c | 339 | |
radiojunkbox | 0:79620c558b0c | 340 | void Run() |
radiojunkbox | 0:79620c558b0c | 341 | { |
radiojunkbox | 0:79620c558b0c | 342 | for(;;) |
radiojunkbox | 0:79620c558b0c | 343 | { |
non | 2:7576d1327cf1 | 344 | const char* cmd = ""; |
non | 2:7576d1327cf1 | 345 | USBLoop(); |
non | 2:7576d1327cf1 | 346 | if (IsConsoleReadable()) |
non | 2:7576d1327cf1 | 347 | cmd = ReadLine(); |
non | 2:7576d1327cf1 | 348 | |
non | 2:7576d1327cf1 | 349 | if ((strcmp(cmd,"s") == 0) || (mySW == 0)) { |
non | 2:7576d1327cf1 | 350 | myLED1 = 1; |
non | 2:7576d1327cf1 | 351 | Miku(3); |
non | 2:7576d1327cf1 | 352 | wait(0.001); |
non | 2:7576d1327cf1 | 353 | NoteOn(0, 72, 0x7f); |
non | 2:7576d1327cf1 | 354 | wait(0.8); |
non | 2:7576d1327cf1 | 355 | NoteOff(0, 72, 0x7f); |
non | 2:7576d1327cf1 | 356 | wait(0.001); |
non | 1:892f8922bdc4 | 357 | |
non | 2:7576d1327cf1 | 358 | Miku(124); |
non | 2:7576d1327cf1 | 359 | wait(0.001); |
non | 2:7576d1327cf1 | 360 | NoteOn(0, 74, 0x7f); |
non | 2:7576d1327cf1 | 361 | wait(0.5); |
non | 2:7576d1327cf1 | 362 | NoteOff(0, 74, 0x7f); |
non | 2:7576d1327cf1 | 363 | wait(0.001); |
non | 1:892f8922bdc4 | 364 | |
non | 2:7576d1327cf1 | 365 | Miku(79); |
non | 2:7576d1327cf1 | 366 | wait(0.001); |
non | 2:7576d1327cf1 | 367 | NoteOn(0, 76, 0x7f); |
non | 2:7576d1327cf1 | 368 | wait(0.5); |
non | 2:7576d1327cf1 | 369 | NoteOff(0, 76, 0x7f); |
non | 1:892f8922bdc4 | 370 | |
non | 2:7576d1327cf1 | 371 | wait(0.2); |
non | 1:892f8922bdc4 | 372 | |
non | 2:7576d1327cf1 | 373 | Miku(50); |
non | 2:7576d1327cf1 | 374 | wait(0.001); |
non | 2:7576d1327cf1 | 375 | NoteOn(0, 76, 0x7f); |
non | 2:7576d1327cf1 | 376 | wait(0.5); |
non | 2:7576d1327cf1 | 377 | NoteOff(0, 76, 0x7f); |
non | 2:7576d1327cf1 | 378 | myLED1 = 0; |
non | 1:892f8922bdc4 | 379 | } else |
radiojunkbox | 0:79620c558b0c | 380 | if (strcmp(cmd,"scan") == 0 || strcmp(cmd,"inquiry") == 0) |
radiojunkbox | 0:79620c558b0c | 381 | Inquiry(); |
radiojunkbox | 0:79620c558b0c | 382 | else if (strcmp(cmd,"ls") == 0) |
radiojunkbox | 0:79620c558b0c | 383 | List(); |
radiojunkbox | 0:79620c558b0c | 384 | else if (strcmp(cmd,"connect") == 0) |
radiojunkbox | 0:79620c558b0c | 385 | Connect(); |
radiojunkbox | 0:79620c558b0c | 386 | else if (strcmp(cmd,"disconnect") == 0) |
radiojunkbox | 0:79620c558b0c | 387 | Disconnect(); |
radiojunkbox | 0:79620c558b0c | 388 | else if (strcmp(cmd,"q")== 0) |
radiojunkbox | 0:79620c558b0c | 389 | { |
radiojunkbox | 0:79620c558b0c | 390 | Quit(); |
radiojunkbox | 0:79620c558b0c | 391 | break; |
non | 2:7576d1327cf1 | 392 | } else if (*cmd == 0) { |
non | 2:7576d1327cf1 | 393 | /* ignore */ |
radiojunkbox | 0:79620c558b0c | 394 | } else { |
radiojunkbox | 0:79620c558b0c | 395 | printf("eh? %s\n",cmd); |
radiojunkbox | 0:79620c558b0c | 396 | } |
radiojunkbox | 0:79620c558b0c | 397 | } |
radiojunkbox | 0:79620c558b0c | 398 | } |
radiojunkbox | 0:79620c558b0c | 399 | }; |
radiojunkbox | 0:79620c558b0c | 400 | |
radiojunkbox | 0:79620c558b0c | 401 | // Instance |
radiojunkbox | 0:79620c558b0c | 402 | ShellApp gApp; |
radiojunkbox | 0:79620c558b0c | 403 | |
radiojunkbox | 0:79620c558b0c | 404 | static int HciCallback(HCI* hci, HCI_CALLBACK_EVENT evt, const u8* data, int len) |
radiojunkbox | 0:79620c558b0c | 405 | { |
radiojunkbox | 0:79620c558b0c | 406 | switch (evt) |
radiojunkbox | 0:79620c558b0c | 407 | { |
radiojunkbox | 0:79620c558b0c | 408 | case CALLBACK_READY: |
radiojunkbox | 0:79620c558b0c | 409 | printf("CALLBACK_READY\n"); |
radiojunkbox | 0:79620c558b0c | 410 | gApp.Ready(); |
radiojunkbox | 0:79620c558b0c | 411 | break; |
radiojunkbox | 0:79620c558b0c | 412 | |
radiojunkbox | 0:79620c558b0c | 413 | case CALLBACK_INQUIRY_RESULT: |
radiojunkbox | 0:79620c558b0c | 414 | printf("CALLBACK_INQUIRY_RESULT "); |
radiojunkbox | 0:79620c558b0c | 415 | printf((BD_ADDR*)data); |
radiojunkbox | 0:79620c558b0c | 416 | printf("\n"); |
radiojunkbox | 0:79620c558b0c | 417 | break; |
radiojunkbox | 0:79620c558b0c | 418 | |
radiojunkbox | 0:79620c558b0c | 419 | case CALLBACK_INQUIRY_DONE: |
radiojunkbox | 0:79620c558b0c | 420 | printf("CALLBACK_INQUIRY_DONE\n"); |
radiojunkbox | 0:79620c558b0c | 421 | gApp.ConnectDevices(); |
radiojunkbox | 0:79620c558b0c | 422 | break; |
radiojunkbox | 0:79620c558b0c | 423 | |
radiojunkbox | 0:79620c558b0c | 424 | case CALLBACK_REMOTE_NAME: |
radiojunkbox | 0:79620c558b0c | 425 | { |
radiojunkbox | 0:79620c558b0c | 426 | BD_ADDR* addr = (BD_ADDR*)data; |
radiojunkbox | 0:79620c558b0c | 427 | const char* name = (const char*)(data + 6); |
radiojunkbox | 0:79620c558b0c | 428 | printf(addr); |
radiojunkbox | 0:79620c558b0c | 429 | printf(" % s\n",name); |
radiojunkbox | 0:79620c558b0c | 430 | } |
radiojunkbox | 0:79620c558b0c | 431 | break; |
radiojunkbox | 0:79620c558b0c | 432 | |
radiojunkbox | 0:79620c558b0c | 433 | case CALLBACK_CONNECTION_COMPLETE: |
radiojunkbox | 0:79620c558b0c | 434 | gApp.ConnectionComplete(hci,(connection_info*)data); |
radiojunkbox | 0:79620c558b0c | 435 | break; |
radiojunkbox | 0:79620c558b0c | 436 | }; |
radiojunkbox | 0:79620c558b0c | 437 | return 0; |
radiojunkbox | 0:79620c558b0c | 438 | } |
radiojunkbox | 0:79620c558b0c | 439 | |
radiojunkbox | 0:79620c558b0c | 440 | // these should be placed in the DMA SRAM |
radiojunkbox | 0:79620c558b0c | 441 | typedef struct |
radiojunkbox | 0:79620c558b0c | 442 | { |
radiojunkbox | 0:79620c558b0c | 443 | u8 _hciBuffer[MAX_HCL_SIZE]; |
radiojunkbox | 0:79620c558b0c | 444 | u8 _aclBuffer[MAX_ACL_SIZE]; |
radiojunkbox | 0:79620c558b0c | 445 | } SRAMPlacement; |
radiojunkbox | 0:79620c558b0c | 446 | |
radiojunkbox | 0:79620c558b0c | 447 | HCITransportUSB _HCITransportUSB; |
radiojunkbox | 0:79620c558b0c | 448 | HCI _HCI; |
radiojunkbox | 0:79620c558b0c | 449 | |
radiojunkbox | 0:79620c558b0c | 450 | u8* USBGetBuffer(u32* len); |
radiojunkbox | 0:79620c558b0c | 451 | int OnBluetoothInsert(int device) |
radiojunkbox | 0:79620c558b0c | 452 | { |
radiojunkbox | 0:79620c558b0c | 453 | printf("Bluetooth inserted of %d\n",device); |
radiojunkbox | 0:79620c558b0c | 454 | u32 sramLen; |
radiojunkbox | 0:79620c558b0c | 455 | u8* sram = USBGetBuffer(&sramLen); |
radiojunkbox | 0:79620c558b0c | 456 | sram = (u8*)(((u32)sram + 1023) & ~1023); |
radiojunkbox | 0:79620c558b0c | 457 | SRAMPlacement* s = (SRAMPlacement*)sram; |
radiojunkbox | 0:79620c558b0c | 458 | _HCITransportUSB.Open(device,s->_hciBuffer,s->_aclBuffer); |
radiojunkbox | 0:79620c558b0c | 459 | _HCI.Open(&_HCITransportUSB,HciCallback); |
radiojunkbox | 0:79620c558b0c | 460 | RegisterSocketHandler(SOCKET_L2CAP,&_HCI); |
radiojunkbox | 0:79620c558b0c | 461 | gHCI = &_HCI; |
radiojunkbox | 0:79620c558b0c | 462 | gApp.Inquiry(); |
radiojunkbox | 0:79620c558b0c | 463 | return 0; |
radiojunkbox | 0:79620c558b0c | 464 | } |
radiojunkbox | 0:79620c558b0c | 465 | |
radiojunkbox | 0:79620c558b0c | 466 | void TestShell() |
radiojunkbox | 0:79620c558b0c | 467 | { |
radiojunkbox | 0:79620c558b0c | 468 | USBInit(); |
radiojunkbox | 0:79620c558b0c | 469 | gApp.Run(); |
radiojunkbox | 0:79620c558b0c | 470 | } |