u2if(USB to interfaces) is an attempt to implement some of the MicroPython "machine" module functionalities on a computer. The goal is to be able to communicate with breakout boards (sensors, lcd...) simply from a python program on x86 machine. It uses a Raspberry PICO microcontroller to make the interface between the computer (USB) and peripherals/protocols.
Python led swith on/off:
import time
from machine import u2if, Pin
# Initialize GPIO to output and set the value HIGH
led = Pin(u2if.GP_3, Pin.OUT, value=Pin.HIGH)
time.sleep(1)
# Switch off the led
led.value(Pin.LOW)
It is in experimental state and not everything has been tested. Use with caution. It is supposed to work on windows, linux and Mac. To work in Linux in non-root, an udev rules has to be added (See Firmware readme).
When I want to retrieve values from a sensor or even to play with a led or a button from the PC, I make an arduino program that communicate to the PC via the serial port. That umplies to define a serial "protocol" between the PC and the arduino and it is not necessarily reusable because it is specific.
Solutions already exist, for example Blinka from Adafruit via the FT2232H in CircuitPython or pyftdi in Python with the same IC.
Nevertheless I find it interesting to implement a majority of the functionalities of the machine module and add other protocols.
The following features are coded:
- machine.Pin: input (+irq, +debounced), output (+pull down/up).
- machine.Signal
- machine.ADC: read (12bits)
- machine.UART
- machine.I2C
- machine.SPI
- machine.PWM
- WS2812B led
- I2S
This repository is presented as the sources of a python project (License). But it also contains the following projects:
- firmware: PICO firmware, binary and C++ source License.
- examples: Python program examples.
- board: Example of a pico headers board for this project (License).
See Firmware README
Install python package from release file (u2if-..tar.gz) with python3 -m pip install dist/u2if-..tar.gz
To build package if wanted :
- If needed, install build: python3 -m pip install --upgrade build
- Build it: python3 -m build
There is no documentation but examples can help to use this library :
- Led On/Off
- Switch with or whithour irq and debouncing
- PWM controlling servo-motor
- Display testing: SSD1306 (I2C and SPI), GC9A01 (round lcd)
- Some sensors simple test: MPU9250 (IMU), VL53L0X (range), BMP280(Temp)
- UART read/Write
- Analog read.
- Rotrary encoder
- I2C scan
- Play 48khz 16bit .wav over I2S DAC (PAM5102)
- ...
For simplicity, the pins of the SPI, I2C and UART devices have been fixed. If a peripheral is not used, its pins can be used as a classic I/O.
The ustruct module is belongs by micropython. There is a micropython-cpython-ustruct compatibility module, but it doesn't seem to work for me. If necessary modify:
import ustruct
to:
import struct as ustruct
Install micropython-cpython-utime and micropython-cpython-micropython.