Documentation
¶
Overview ¶
Package mcp2221 drives the Microchip MCP2221 / MCP2221A USB-to-I2C bridge from pure Go (no cgo), on Linux, Windows and macOS.
It talks to the chip with raw 64-byte HID reports, following the same protocol steps as Adafruit Blinka's MCP2221 support.
Example ¶
package main
import (
"fmt"
"log"
mcp2221 "github.com/ApolloX939/mcp2221"
)
func main() {
dev, err := mcp2221.Open()
if err != nil {
log.Fatal(err)
}
defer dev.Close()
// List the devices on the I2C bus.
addrs, err := dev.Scan()
if err != nil {
log.Fatal(err)
}
for _, a := range addrs {
fmt.Printf("found device at 0x%02x\n", a)
}
// Turn on the oscillator of an HT16K33 LED driver at 0x70.
if err := dev.I2CWrite(0x70, []byte{0x21}); err != nil {
log.Fatal(err)
}
// Read two bytes from register 0x00 of the same device.
buf := make([]byte, 2)
if err := dev.I2CTx(0x70, []byte{0x00}, buf); err != nil {
log.Fatal(err)
}
}
Output:
Index ¶
Examples ¶
Constants ¶
const ( VendorID = 0x04D8 ProductID = 0x00DD )
USB IDs of the MCP2221 and MCP2221A.
Variables ¶
var ( // ErrNACK means no device acknowledged the I2C address. ErrNACK = errors.New("mcp2221: no I2C device acknowledged the address") // ErrTimeout means an I2C transfer did not finish in time. ErrTimeout = errors.New("mcp2221: I2C transfer timed out") )
Functions ¶
This section is empty.
Types ¶
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is an open MCP2221. It is safe for concurrent use.
func Open ¶
Open opens the MCP2221 attached to this computer, clears any stuck I2C transfer and sets the bus to 100 kHz. It fails if there is not exactly one MCP2221 connected.
The device is held exclusively until Close, because the chip answers commands in order and two programs sharing it would get each other's replies. Opening it while another program has it open fails with an error wrapping usbhid.ErrDeviceLocked.
On Linux the HID device must be readable and writable by the current user; see the README for a udev rule.
func (*Device) I2CTx ¶
I2CTx writes w and then reads into r. Either may be empty, but not both, and each may be up to 65535 bytes.
The write and read are separate transfers (stop, then start), not a repeated start: the MCP2221's repeated-start read returned corrupted data in testing. Devices that keep their register pointer between transfers, which is nearly all of them, work as expected.
func (*Device) Scan ¶
Scan returns the 7-bit addresses (0x08-0x77) of devices that answer a one-byte read.
func (*Device) SetI2CSpeed ¶
SetI2CSpeed sets the I2C clock in Hz, from 47 kHz to 400 kHz.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
claudematrix
command
claudematrix plays a "working" animation on the LED matrix until it is stopped, then blanks the display.
|
claudematrix plays a "working" animation on the LED matrix until it is stopped, then blanks the display. |
|
Package ht16k33 drives an Adafruit bicolor 8x8 LED matrix (HT16K33 backpack).
|
Package ht16k33 drives an Adafruit bicolor 8x8 LED matrix (HT16K33 backpack). |