mcp2221

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

mcp2221

A Go library for the Microchip MCP2221 / MCP2221A USB-to-I2C bridge (for example the Adafruit MCP2221A breakout).

  • Pure Go with no cgo, so no C compiler is needed. Works on Linux, Windows and macOS.
  • Talks to the chip with raw HID reports, using the same protocol steps as Adafruit Blinka.
  • Covers I2C write, read, write-then-read and bus scan. Safe for concurrent use.

GPIO, ADC/DAC and UART are not implemented.

Also in this repo:

Path What it is
ht16k33/ Driver for the Adafruit bicolor 8x8 LED matrix (HT16K33 backpack)
cmd/claudematrix/ A "Claude is working" animation for that matrix
led-matrix/ Claude Code plugin that runs claudematrix while Claude works

Install

go get github.com/ApolloX939/mcp2221

Usage

import mcp2221 "github.com/ApolloX939/mcp2221"

dev, err := mcp2221.Open() // also sets the bus to 100 kHz
if err != nil {
	log.Fatal(err)
}
defer dev.Close()

addrs, _ := dev.Scan()                          // e.g. [0x70]
err = dev.I2CWrite(0x70, []byte{0x21})          // write
err = dev.I2CRead(0x70, buf)                    // read len(buf) bytes
err = dev.I2CTx(0x70, []byte{0x00}, buf)        // write register, then read
err = dev.SetI2CSpeed(400_000)                  // 47 kHz to 400 kHz

if errors.Is(err, mcp2221.ErrNACK) { /* nothing at that address */ }

Open expects exactly one MCP2221 to be connected. The adapter is held exclusively until Close; opening it while another program has it fails with an error wrapping usbhid.ErrDeviceLocked.

LED matrix (ht16k33)
import "github.com/ApolloX939/mcp2221/ht16k33"

grid, err := ht16k33.NewMatrix8x8x2(dev, 0x70, 0.1) // any I2CWrite-er, brightness 0-1
grid.Set(x, y, ht16k33.Green)                        // Off, Red, Green, Orange
grid.Fill(ht16k33.Off)
err = grid.Show()                                    // send the buffered pixels
Notes
  • I2CTx sends the write and the read as two transfers (stop, then start), not with a repeated start. The MCP2221's repeated-start read returned corrupted data in testing. Nearly all devices keep their register pointer between transfers, so this works as expected.
  • Scan probes 0x08–0x77 with a one-byte read.
  • Transfers are up to 65535 bytes each and time out after 250 ms.

claudematrix: a "Claude is working" light

claudematrix plays Claude Code's spinner on the LED matrix until it is stopped, then blanks the display: an orange dot that grows into a star (· ✢ ✳ ✶ ✻ ✽) and shrinks back again. The led-matrix Claude Code plugin runs it while Claude is working on a prompt and stops it when Claude finishes or is interrupted.

What the matrix shows, one frame every 120ms (● is a lit orange LED). It plays 0 to 5 and back down to 0, then repeats:

frame 0     frame 1     frame 2     frame 3     frame 4     frame 5
········    ········    ········    ········    ···●●···    ●··●●··●
········    ········    ···●●···    ·●·●●·●·    ·●·●●·●·    ·●·●●·●·
········    ···●●···    ···●●···    ··●●●●··    ··●●●●··    ··●●●●··
···●●···    ··●●●●··    ·●●●●●●·    ·●●●●●●·    ●●●●●●●●    ●●●●●●●●
···●●···    ··●●●●··    ·●●●●●●·    ·●●●●●●·    ●●●●●●●●    ●●●●●●●●
········    ···●●···    ···●●···    ··●●●●··    ··●●●●··    ··●●●●··
········    ········    ···●●···    ·●·●●·●·    ·●·●●·●·    ·●·●●·●·
········    ········    ········    ········    ···●●···    ●··●●··●

See it running on the matrix: claude-ing.mp4 (silent video).

Install

1. The program.

go install github.com/ApolloX939/mcp2221/cmd/claudematrix@latest

Or, from a checkout of this repo:

go install ./cmd/claudematrix

Either way it goes to ~/go/bin/claudematrix (or $GOBIN). On Linux, also add the udev rule from Linux permissions.

2. The Claude Code plugin, for every session. This repo is also a plugin marketplace. From a checkout:

claude plugin marketplace add /path/to/mcp2221
claude plugin install led-matrix@mcp2221 --scope user

Installed from a local folder like this, Claude Code reads the plugin straight from the checkout. After you edit it, run /reload-plugins in a session.

Without a checkout, add the marketplace from git instead: claude plugin marketplace add ApolloX939/mcp2221.

Then start a new session, or run /reload-plugins in an open one.

3. Settings (optional). In Claude Code, run /plugin configure led-matrix@mcp2221:

Setting Default Description
binary /home/apollo/go/bin/claudematrix Path to claudematrix. Change it if yours is elsewhere.
brightness 0.1 Matrix brightness, 0.0 to 1.0
How it behaves
  • A turn starts: the plugin starts claudematrix. Subagents don't start it again; they run inside Claude's turn.
  • The turn ends, or you press Esc: the plugin stops it and the display blanks within about half a second.
  • The adapter is unplugged, or another program has it open (such as another Claude session that is working): claudematrix keeps retrying quietly. Its errors go to Claude Code's debug log (claude --debug).
  • Claude Code exits or crashes: claudematrix notices within a second, blanks the display and exits.
Running it by hand
claudematrix              # until Ctrl+C
Flag Default Description
-brightness 0.1 Display brightness, 0.0 to 1.0
-addr 0x70 I2C address of the matrix
-frame 120ms Time per animation step
-heartbeat off Print a line this often, and stop if output is closed

It blanks the display and exits on SIGTERM or Ctrl+C, when the program that started it exits, or, with -heartbeat, when nothing reads its output any more. The plugin uses -heartbeat 500ms because Claude Code only stops a child after the child's next line of output.

Linux permissions

By default only root can open the HID device. To use it as a normal user, create /etc/udev/rules.d/99-mcp2221.rules:

SUBSYSTEM=="hidraw", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="00dd", MODE="0660", GROUP="plugdev", TAG+="uaccess"

Then run sudo udevadm control --reload-rules, replug the adapter, and make sure you are in the plugdev group.

The rule must match SUBSYSTEM=="hidraw". A rule matching SUBSYSTEM=="usb" (common for libusb-based tools such as Blinka) doesn't cover the HID device this library uses.

Windows and macOS need no setup.

Tested

Tested on Linux with an MCP2221A and an HT16K33 LED matrix backpack. Library:

  • Bus scan.
  • Write, then read back.
  • No-device errors on writes and reads.
  • Recovery after errors.
  • 100 kHz and 400 kHz.

claudematrix and the plugin:

  • Animation start and stop in a live session and a fresh headless session.
  • Blanking on SIGTERM, on parent exit, and on closed output.
  • Waiting while another program holds the adapter.
  • claude plugin test led-matrix passes.

License

MIT. See LICENSE.

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)
	}
}

Index

Examples

Constants

View Source
const (
	VendorID  = 0x04D8
	ProductID = 0x00DD
)

USB IDs of the MCP2221 and MCP2221A.

Variables

View Source
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

func Open() (*Device, error)

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) Close

func (d *Device) Close() error

Close releases the device.

func (*Device) I2CRead

func (d *Device) I2CRead(addr byte, buf []byte) error

I2CRead reads len(buf) bytes from the device at 7-bit address addr.

func (*Device) I2CTx

func (d *Device) I2CTx(addr byte, w, r []byte) error

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) I2CWrite

func (d *Device) I2CWrite(addr byte, data []byte) error

I2CWrite writes data to the device at 7-bit address addr.

func (*Device) Scan

func (d *Device) Scan() ([]byte, error)

Scan returns the 7-bit addresses (0x08-0x77) of devices that answer a one-byte read.

func (*Device) SetI2CSpeed

func (d *Device) SetI2CSpeed(hz int) error

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL