This blog will show you how to setup snapcraft.yaml to access USB devices connected to ctrlX CORE. In addition to that, it shows an example of a Python snap, which uses the PyUSB module [wrapper for libusb] to read in data from a USB mouse.
You can also download the full project, which includes the providing of the mouse data on the ctrlX Data Layer, by clicking
here
.
The Vendor ID and the Product ID of the connected USB device are needed to be able to access the specific device you want to read in data from. The easiest way to find these is to connect the USB device to your PC and look it up according to your OS.
On Linux: Type in the following command in a terminal window.
usb-devices
This will show you the information about all of your connected USB devices. Just look for the corresponding device. For this example:
Here, the setup of the snapcraft.yaml file is done before the implementation of the Python script. But you can do it the other way around as well.
Snapcraft YAML File Setup
Open a terminal window, change the working directory to where you want to build the snap and type in the following command:
snapcraft init
This should give a YAML file similiar to this:
name: my-snap-name # you probably want to 'snapcraft register <name>'
base: core20 # the base snap is the execution environment for this snap
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |
This is my-snap's description. You have a paragraph or two to tell the
most important story about your snap. Keep it under 100 words though,
we live in tweetspace and your description wants to look good in the snap
store.
grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots
parts:
my-part:
# See 'snapcraft plugins'
plugin: nil
Change grade and confinement of the snap:
grade: stable
confinement: strict
Define the parts from which the snap is built and make sure to include the following packages:
Define the apps provided by the snap and make sure to include at least these three plugs in order to give this snap the permissions needed to access the USB device:
In addition to the snapcraft.yaml file, you need to create a setup.py script for the Python plugin to work properly and define in it which Python script is supposed to be used for this project. Simple setup.py for this example:
from setuptools import setup
setup(scripts = ['main.py'])
Loop to read in data from the USB device more than once:
while True:
data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
print(data)
# put your own code here
# for example datalayer access to present the data
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed out',):
continue