HID via Leonardo testing
Added by J. Simmons over 6 years ago
Home of J's work to implement HID through Leonardo style boards controlled from CHIP.
Tasked under DEV-07: Test Keyboard USB HID over Arduino, with all sub-PBIs tasked onto 2017 Sprint 2.
Replies (9)
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
This week I committed to documenting my initial research into powering CHIP Pro from a Feather (our preferred Leonardo style board) and providing I2C between CHIP Pro and Feather (to enable command and control of the Feather from the CHIP Pro).
Powering CHIP Pro from a Feather- https://docs.getchip.com/chip_pro.html#power-c-h-i-p-pro
There are several power options listed in the CHIP Pro docs. Given we may later use a battery to provide power for graceful shutdown, I am going to go with the "Connect 5 - 5.5 volts of power to CHG-IN (and GND)" on the CHIP Pro side. - https://learn.adafruit.com/assets/30918
We can get 5v (up to 500mA) from the USB pin on the Feather and there are mutliple GND pins we can use. - Plan: connect Feather.USB pin to CHIP.CHG-IN pin and connect a Feather.GND pin to a CHIP.GND pin to provide power
- Feather I2C Pins - from https://learn.adafruit.com/adafruit-feather-32u4-basic-proto/pinouts
See Arduino Master Writer and Arduino Master Reader tutorials for background- I2C Data Pin = #2/SDA
- I2C Clock Pin = #3/SCL
- Johnny-Five and I2C
- Utilize backpacks (see also Johnny five i2c backpack ws2812 node-pixel)
- Looking around the Node Pixel example
- I2C Backpack Installation
To follow the above, you need to know the data and clock pins for both the slave board (Feather) and master board (CHIP Pro) - From https://docs.getchip.com/chip_pro.html#twi1
- I2C Data Pin = 12/TWI1-SDA
- I2C Clock Pin = 11/TWI1-SCK
- Example Arduino Backpack Code
- I2C Backpack Installation
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
Today I am planning to test implementing the Holoseat controller (and specifically the required connections for HID through Arduino-like devices) with the CHIP Pro/Python. My test plan is as follows.
- Prep CHIP Pro
- Install antenna
- Install Debian os
- Setup wifi
- Setup ssh
- Test ssh (need IP address)
- Test power from feather
- Power down CHIP Pro
- Wire up proposed power pinout from feather to CHIP Pro
- Connect feather to power supply
- Observe CHIP Pro
- If it starts, try to ssh into it
- Setup python IO chiptainer
- Continue getting power from feather
- Install Docker
- Pull python IO image
- Launch python IO container with i2c and mem access
- Verify devices are present
- Install git in chiptainer
- Shutdown chiptainer and chip Pro
- Prep feather i2c
- Disconnect power pins for chip pro
- Install sample firmware (ensure code references built in led)
- Disconnect feather from pc
- Wire up power and i2c between feather and chip Pro
- Test i2c
- Plug feather into pc
- Verify feather and chip Pro boot
- Ssh into chip Pro
- Start python IO container
- Get python demo source code and required libraries (note, there are two implementations, will test adafruit pure python first)
- Modify for wired i2c bus
- Run python test code
Here goes...
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
- Install antenna - check
- Install Debian os - check
- Setup wifi - check (see this post for full details for this step
- Setup ssh - already configured in Debian OS for CHIP Pro
- Test ssh (need IP address) - check
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
- Power down CHIP Pro - check
- Wire up proposed power pinout from feather to CHIP Pro
- 5V from Arduino to pin 4 (CHG-IN)
- GND from Arduino to pin 6 (GND) - Connect feather to power supply - check
- Observe CHIP Pro - power led came on and ping to ip address successful after waiting for it to boot
- If it starts, try to ssh into it - check
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
- Continue getting power from feather - check
- Install Docker
- the CHIP Pro kept shutting down during the installation process
- it turns out I am short a feather, so I am trying these steps out a Leonardo I have_)
- I think the CHIP Pro is shutting down because I am drawing too much power, ntc recommends 5v and 1 amp for power supplies
- looking at the feather docs it looks like it will have the same problem
- I think we will need to pull power directly from the USB cable
- going to install Docker with power from the USB port on the CHIP Pro and go back to Leonardo power for i2c tests - Pull python IO image
- docker pull modelb/chiptainer_python_io:master - Launch python IO container with i2c and mem access
- docker run -v /sys:/sys --cap-add SYS_RAWIO --device /dev/mem --device /dev/i2c-0 --device /dev/i2c-1 -it modelb/chiptainer_python_io:master - Verify devices are present - check
- Install git in chiptainer
- apk update
- apk add git - Shutdown chiptainer and chip Pro - check
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
- Disconnect power pins for chip pro - check
- Install sample firmware (ensure code references built in led)
- see https://oscarliang.com/raspberry-pi-arduino-connected-i2c/
- check - Disconnect feather from pc - check
- Wire up power and i2c between feather and chip Pro - check
Connections for i2c testing
Purpose | CHIP Pro | Leonardo |
5v | Pin 4 | 5v |
GND | Pin 6 | GND |
i2c clock | Pin 11 | SCL |
i2c data | Pin 12 | SDA |
Note, we are connecting to i2c-1 on the CHIP Pro.
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
- Plug feather into pc - check
- Verify feather and chip Pro boot - check
- Ssh into chip Pro
- the CHIP Pro is very slow to respond - Start python IO container -check
- Get python demo source code and required libraries (note, there are two implementations, will test adafruit pure python first)
- first lib to check: https://github.com/fordsfords/Adafruit_Python_PureIO
- example: https://oscarliang.com/raspberry-pi-arduino-connected-i2c/ - Modify for wired i2c bus
import Adafruit_PureIO.smbus as smbus import time # for RPI version 1, use "bus = smbus.SMBus(0)" bus = smbus.SMBus(1) # This is the address we setup in the Arduino Program address = 0x04 def writeNumber(value): bus.write_byte(address, value) # bus.write_byte_data(address, 0, value) return -1 def readNumber(): number = bus.read_byte(address) # number = bus.read_byte_data(address, 1) return number while True: var = input("Enter 1 - 9: ") if not var: continue writeNumber(var) print "CHIP Pro: Hi Arduino, I sent you ", var # sleep one second time.sleep(1) number = readNumber() print "Arduino: Hey CHIP Pro, I received a digit ", number print
- Run python test code - check
And it worked! I can see the Arduino receiving messages in the Serial Monitor and I can see the CHIP Pro receiving messages in the ssh session to the CHIP Pro!!!
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
Note, I have done some earlier testing demonstrating that the Leonardo style boards (including the Feather) can dynamically turn on and off their HID modes inside of loop(). These tests plus the testing above clearly demonstrates we can implement HID using Arduino. We just need to resolve the power issue (which note, was not a problem when I was not trying to install large packages) and implement a control protocol over i2c to send HID commands.
I also need to verify the issues with responsiveness (which were not present when I started) are not a chronic issue. I will start working on a Holoseat HID over i2c protocol so we can get this thing fully prototyped.
RE: HID via Leonardo testing - Added by J. Simmons over 6 years ago
I think the responsiveness issue is something to do with my laptop. Everything is starting to feel sluggish.
I made a couple of modifications to the source code for the Leonardo firmware and the Python demo script to move us to HID testing.
Leonard Firmware
#include <Wire.h> #include <Keyboard.h> #define SLAVE_ADDRESS 0x04 int number = 0; int state = 0; void setup() { Keyboard.begin(); pinMode(13, OUTPUT); Serial.begin(9600); // start serial for output // initialize i2c as slave Wire.begin(SLAVE_ADDRESS); // define callbacks for i2c communication Wire.onReceive(receiveData); Wire.onRequest(sendData); Serial.println("Ready!"); } void loop() { delay(100); } // callback for received data void receiveData(int byteCount) { while (Wire.available()) { number = Wire.read(); Serial.print("data received: "); Serial.write(number); Serial.println(""); Keyboard.write(number); if (number == 1) { if (state == 0) { digitalWrite(13, HIGH); // set the LED on state = 1; } else { digitalWrite(13, LOW); // set the LED off state = 0; } } } } // callback for sending data void sendData() { Wire.write(number); }
Python Demo
import Adafruit_PureIO.smbus as smbus import time # for RPI version 1, use "bus = smbus.SMBus(0)" bus = smbus.SMBus(1) # This is the address we setup in the Arduino Program address = 0x04 def writeNumber(value): bus.write_byte(address, value) # bus.write_byte_data(address, 0, value) return -1 def readNumber(): number = bus.read_byte(address) # number = bus.read_byte_data(address, 1) return number while True: var = input("Enter a message: ") if not var: continue for c in var: writeNumber(ord(c))
Using this code, I can remotely type messages from a machine running ssh into the CHIP Pro to a machine with the Leonardo plugged into it. Note, the Keyboard.write() calls are good for sending what you are typing, but not for game play. We will still need to use Keyboard.press() function for game play. With this last test, I feel we can claim done on the following PBIs.
- DEV-26: Determine wiring to power CHIP Pro from Feather - with the lesson learned that we should look into drawing power straight off of the USB line
- DEV-27: Determine wiring to provide I2C between CHIP Pro and Feather
- DEV-28: Develop Feather code to be I2C slave
- DEV-29: Develop Johnny-Five code for CHIP Pro to be I2C master - with the understanding that we are now moving to Python 2.7 and CHIP-IO
- DEV-30: Test master to slave and slave to master events between Feather and CHIP Pro
Part of the implementation of the CHIP Pro version of Holoseat will need to include developing a protocol between the CHIP Pro and the Feather. This protocol should support operation messages from the CHIP Pro to the Feather (e.g. press a key, disable keyboard, release keys, etc) and messages from the Feather to the CHIP Pro (e.g. passing along messages from host machine to the Holoseat over the Holoseat Serial Protocol).
Still, not a bad day's work. *
(1-9/9)