The document discusses using an Android phone to control hardware through USB. It describes connecting an LED keyboard to the phone using an OTG adapter and writing code to control the LEDs. It then explores adding galvanic isolation and using an ATX power supply for higher power functions. The document provides code snippets and discusses limitations but also shows controlling motors is possible through the keyboard interface.
6. Carlo Pescio @CarloPescio http://eptacom.net
Your ____mission
Should you choose to accept it…
- Your phone
(no rooting)
- Bunch of scraps
- No Firmware
12V
7. Carlo Pescio @CarloPescio http://eptacom.net
Smart > Hero
Don’t do industrial controls
using scraps
It’s OK at home
Great learning opportunity
11. Carlo Pescio @CarloPescio http://eptacom.net
LEDs in common USB scraps
Flash drive
Mouse
Keyboard
Printer
12. Carlo Pescio @CarloPescio http://eptacom.net
And the winner is…
LEDs are host-controlled
Standard protocol
LEDs outlive the keys
Easy to find
13. Carlo Pescio @CarloPescio http://eptacom.net
Phone – Keyboard connection
Your phone is normally a device (gadget) for a
computer (host).
A keyboard is a gadget
=> your phone must turn into a host
=> you need an OTG adapter
16. Carlo Pescio @CarloPescio http://eptacom.net
The software side
Don’t write any code yet
Interface #0
Class: Human Interaction Device (0x3)
Endpoint: #0
Address : 129 (10000001)
Number : 1
Direction : Inbound (0x80)
Type : Intrrupt (0x3)
Poll Interval : 10
Max Packet Size: 8
Attributes : 000000011
Interface #1
Class: Human Interaction Device (0x3)
Endpoint: #0
Address : 130 (10000010)
Number : 2
Direction : Inbound (0x80)
Type : Intrrupt (0x3)
Poll Interval : 10
Max Packet Size: 3
Attributes : 000000011
17. Carlo Pescio @CarloPescio http://eptacom.net
The missing endpoint
Source: USB Device Class Definition for Human
Interface Devices Firmware Specification, Section 4.4
If no Interrupt Out endpoint is declared then Output
reports are transmitted to a device through the
Control endpoint.
Endpoint 0 is a Control pipe always present in USB
devices. Therefore, only the Interrupt In pipe is
described for the Interface descriptor using an
Endpoint descriptor.
18. Carlo Pescio @CarloPescio http://eptacom.net
Know thy APIs
You just can’t get hold of endpoint 0. But:
UsbDeviceConnection.
controlTransfer( gazillion parameters )
Performs a control transaction on endpoint zero for
this device. The direction of the transfer is
determined by the request type. If requestType &
USB_ENDPOINT_DIR_MASK is USB_DIR_OUT, then
the transfer is a write […]
19. Carlo Pescio @CarloPescio http://eptacom.net
7.2.2 SET_REPORT
Part Description
bmRequestType 00100001 (0x21)
bRequest SET_REPORT
wValue Report Type and Report ID
wIndex Interface
wLength Report Length
Data Report
SET_REPORT = 0x09 (Paragraph 7.2)
20. Carlo Pescio @CarloPescio http://eptacom.net
For LEDs / Keyboards
Report Type: 02 = Output (Paragraph 7.2.1)
Report ID: 0 = Not Used (Paragraph 7.2.1)
Interface = 0 (irrelevant here)
Report Length = 1 (1 byte, Appendix B.1)
Data: bitmask where bit 0 = NUM LOCK
bit 1 = CAPS LOCK
bit 2 = SCROLL LOCK
21. Carlo Pescio @CarloPescio http://eptacom.net
Sending the report
int controlTransfer(
int requestType,
int request,
int value,
int index,
byte[] buffer,
int length,
int timeout)
byte[] buf = new byte[] { b };
connection.controlTransfer(
0x21, 0x09, 0x0200, 0x0000, buf, 1, 1000);
// data [as array] -> bitmask
// wValue -> 0x0200
// wIndex -> 0x0000
// wLength -> 1
// 1 sec ok -> 1000
// bmRequestType -> 0x21
// bRequest -> 0x09
22. Sketch of the code
usbManager = (UsbManager)
getSystemService(Context.USB_SERVICE);
// just get the first usb device –
// should get the keyboard instead
UsbDevice device = (UsbDevice)
usbManager.getDeviceList().values().
toArray()[0];
usbManager.requestPermission(
device, permissionIntent);
23. Carlo Pescio @CarloPescio http://eptacom.net
… build the Keyboard
// … receive intent
UsbDevice device = (UsbDevice)
intent.getParcelableExtra(
UsbManager.EXTRA_DEVICE);
keyboard = new
UsbKeyboard(usbManager, device);
24. Keyboard
public UsbKeyboard(UsbManager usbManager,
UsbDevice device)
{
requestQueue = new LinkedBlockingQueue<Byte>();
connection = usbManager.openDevice(device);
// WHY AM I DOING THIS? GUESS
ifc0 = device.getInterface(0);
connection.claimInterface(ifc0, true);
ioThread = new Thread(usbWriteLoop);
ioThread.start();
}
25. private Runnable usbWriteLoop = new Runnable()
{
// …
while( !stop )
{
Byte b = requestQueue.take();
if( b < 0 )
{
stop = true;
}
else
{
byte[] buf = new byte[] { b };
connection.controlTransfer(
0x21, 0x09, 0x0200,0x0000, buf, 1, 1000);
}
}
// …
};
28. Carlo Pescio @CarloPescio http://eptacom.net
Detour: how fast can we go?
Just use an infinite loop / no sleeping or waiting:
byte[] buf0 = new byte[] { 0 };
byte[] buf1 = new byte[] { 4 };
while( true )
{
connection.controlTransfer(
0x21, 0x09, 0x0200, 0x0000, buf0, 1, 1000);
connection.controlTransfer(
0x21, 0x09, 0x0200, 0x0000, buf1, 1, 1000);
}
29. Carlo Pescio @CarloPescio http://eptacom.net
Hollywood loves CRTs
Fantastic Four Iron Man
Star Trek Enterprise
The Big Bang Theory
35. Carlo Pescio @CarloPescio http://eptacom.net
Conclusions from detour
Useful: we killed the noise
Things you cannot do:
- “high” frequency / low latency / low jitter stuff
- PWM
- Infrared remotes
- … etc. …
Things you can do:
- Turning on and off some stuff at low freq
36. Carlo Pescio @CarloPescio http://eptacom.net
No joy yet
- Different voltage (LED powered from your phone USB)
- High current (same as above)
- Generally speaking, no galvanic isolation
(whatever happens there, happens to your phone)
You can’t just put a motor where the LED is
39. Carlo Pescio @CarloPescio http://eptacom.net
A small step forward…
But can’t power a
motor with that
40. Carlo Pescio @CarloPescio http://eptacom.net
A level of indirection…
… brings new problems
1) Who is providing the current
going through the phototransistor?
2) The phototransistor can’t handle much
current anyway
Phone
[usb]
???
41. Carlo Pescio @CarloPescio http://eptacom.net
Step 3: ATX is your best friend
Will solve all your problems at once
50. Carlo Pescio @CarloPescio http://eptacom.net
Charging while in host mode
Usually the host (phone) provides power.
OTG specification:
36.5 kΩ between pin 4 and 5
[…] The OTG device is allowed
to charge and enter host mode
51. Carlo Pescio @CarloPescio http://eptacom.net
May or may not work for you
If it works, ATX might be even more of a friend!
5V here also
in standby
52. Carlo Pescio @CarloPescio http://eptacom.net
If it doesn’t work…
- Fiddle with resistor values
- Remove battery and provide power from there
- Also useful when battery is dead
- That’s a story for another time
53. Carlo Pescio @CarloPescio http://eptacom.net
Get in touch
carlo.pescio@gmail.com
@CarloPescio
http://eptacom.net