monome

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2018 License: MIT Imports: 8 Imported by: 0

README

monome

Go library to program the monome

Documentation

Installation

It is recommended to use Go 1.11 with module support ($GO111MODULE=on).

go get -d github.com/gomonome/monome/...

Example

package main

import (
	"fmt"
	"os"
	"os/signal"

	"github.com/gomonome/monome"
)

func setup(conn monome.Connection) {
	monome.Greeter(conn)
	conn.SetHandler(monome.HandlerFunc(func(c monome.Connection, x, y uint8, down bool) {
		action := "released"
		if down {
			action = "pressed"
		}
		fmt.Printf("%s %s key %v/%v\n", c, action, x, y)

		// switch the lights
		c.Switch(x, y, down)
	}))
	conn.StartListening(func(err error) {
		// aborting on io error
		sigchan <- os.Interrupt
	})
}

var sigchan = make(chan os.Signal, 10)

func main() {
	if os.Getenv("USER") != "root" {
		fmt.Fprintln(os.Stderr, "please run as root")
		os.Exit(1)
	}

	conns, _ := monome.Connections()

	if len(conns) < 1 {
		fmt.Fprintln(os.Stderr, "no monome device found")
		os.Exit(1)
	}

	for _, conn := range conns {
		setup(conn)
	}

	// listen for ctrl+c
	go signal.Notify(sigchan, os.Interrupt)

	// interrupt has happend
	<-sigchan

	fmt.Fprint(os.Stdout, "\ninterrupted, cleaning up...")

	for _, conn := range conns {
		conn.Close()
	}

	fmt.Fprintln(os.Stdout, "done")
	os.Exit(0)
}

License

MIT (see LICENSE file)

Documentation

Index

Constants

View Source
const (
	VENDOR_ID  = "0403"
	PRODUCT_ID = "6001"
)

Variables

View Source
var LetterWidth = map[rune]int{}
View Source
var Letters = map[rune]map[[2]uint8]bool{}

add your own letters as you like

View Source
var USBAccessError = fmt.Errorf("USB stack could not be opened. Probably missing rights. Try to run as admin.")

Functions

func Connect added in v0.0.7

func Connect(dev *usb.Device, options ...Option) (d *connection, err error)

Connect returns a new Connection to the given usb.Device. Normally New should not be called directly, but Devices instead (which make use of New).

func Greeter added in v0.0.7

func Greeter(dev Device)

Greeter prints the name of the device on the device, followed by a flash

func Marquee added in v0.0.7

func Marquee(m Device, s string, dur time.Duration) error

Marquee shows the given string in a marquee-like manner (from left to right)

func NumButtons added in v0.0.7

func NumButtons(dev Device) uint8

NumButtons returns the available number of buttons

func Print added in v0.0.7

func Print(m Device, s string, dur time.Duration) error

Print prints the string one letter after another

func PrintUSBDevice added in v0.0.2

func PrintUSBDevice(dev *usb.Device)

func SwitchAll added in v0.0.7

func SwitchAll(m Device, on bool) error

SwitchAll switches all lights on or off

func USBDevices added in v0.0.2

func USBDevices(vendor_id, product_id string) ([]*usb.Device, error)

USBDevices returns all USB devices for the given vendor and product id. If they are empty strins, the defaults are used, which is VENDOR_ID and PRODUCT_ID

Types

type CloseError added in v0.0.2

type CloseError struct {
	Device       string
	WrappedError error
}

func (CloseError) Error added in v0.0.2

func (e CloseError) Error() string

type ConnectError added in v0.0.2

type ConnectError struct {
	USBDevice   *usb.Device
	USBEndPoint struct {
		Purpose   string
		Number    int
		Config    uint8
		Interface uint8
		Setup     uint8
		Info      usb.EndpointInfo
	}
	WrappedError error
}

func (*ConnectError) Error added in v0.0.2

func (m *ConnectError) Error() string

type Connection added in v0.0.7

type Connection interface {
	// Close closes the connection to the monome
	Close() error

	// IsClosed returns wether the connection is closed
	IsClosed() bool

	// SetHandler set the active handler for the device
	SetHandler(Handler)

	// StartListering starts listening for button events. For errors the given errHandler is called
	StartListening(errHandler func(error))

	// StopListening stops listening for button events
	StopListening()

	Device
}

Connection is a connection to a monome device

func Connections added in v0.0.7

func Connections(options ...Option) ([]Connection, error)

Connections returns all connections that could be made to attached monome devices.

func RowConnection added in v0.0.7

func RowConnection(name string, connections ...Connection) Connection

RowConnection creates a unified connection out of a row of connections. The order is from left to right. The number of columns is the sum of the columns of the devices. The number of rows is the smallest number of rows of any device.

func TestDevice added in v0.0.2

func TestDevice(tester Tester, options ...Option) Connection

TestDevice returns a new (fake) monome device, based on the given tester

type ConnectionClosedError added in v0.0.3

type ConnectionClosedError string

func (ConnectionClosedError) Error added in v0.0.3

func (c ConnectionClosedError) Error() string

type Device

type Device interface {
	// Rows returns the number of rows
	Rows() uint8

	// Cols returns the number of cols
	Cols() uint8

	// Set sets the button at position x,y to the given brightness
	// If the connection has been closed, nothing is sent
	// From the monome docs about brightness levels:
	// [0, 3] - off
	// [4, 7] - low intensity
	// [8, 11] - medium intensity
	// [12, 15] - high intensity
	// June 2012 devices allow the full 16 intensity levels.
	Set(x, y, brightness uint8) error

	// Switches the light at x,y on or off
	// Switches the light at x,y on or off
	// If on is true, it is a shortcut for  Set(x,y,15).
	// If on is false it is a shortcut for Set(x,y,0)
	Switch(x, y uint8, on bool) error

	// String returns an identifier as a string (name)
	String() string

	// ReadMessage reads a message from the device and calls the handler if necessary
	// It should normally not be called and is just there to allow external implementations of Device
	ReadMessage() error
}

type Error added in v0.0.2

type Error struct {
	X            uint8
	Y            uint8
	Device       string
	WrappedError error
	Task         string
}

func (Error) Error added in v0.0.2

func (e Error) Error() string

type Errors added in v0.0.2

type Errors struct {
	Task   string
	Errors []error
}

func (*Errors) Add added in v0.0.2

func (m *Errors) Add(err error)

func (*Errors) Error added in v0.0.2

func (m *Errors) Error() string

func (*Errors) Len added in v0.0.2

func (m *Errors) Len() int

type Handler

type Handler interface {

	// Handle is the callback that is called if a button is pressed (down=true)
	// or released (down=false)
	Handle(d Connection, x, y uint8, down bool)
}

Handler responds to a pressing or releasing action on a button

type HandlerFunc

type HandlerFunc func(d Connection, x, y uint8, down bool)

HandlerFunc is a function that acts as a Handler

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(d Connection, x, y uint8, down bool)

type Option

type Option func(*connection)

func PollInterval

func PollInterval(interval time.Duration) Option

type ReadError added in v0.0.2

type ReadError struct {
	Device       string
	WrappedError error
}

func (ReadError) Error added in v0.0.2

func (e ReadError) Error() string

type Tester added in v0.0.2

type Tester interface {
	Get() (x, y uint8, down bool, err error)
	Set(x, y, brightness uint8) error
	Name() string
	Cols() uint8
	Rows() uint8
	io.Closer
}

func CloseTester added in v0.0.2

func CloseTester(cols, rows uint8, clos func() error) Tester

CloseTester returns a Tester tailored for testing the closing of the device

func GetTester added in v0.0.2

func GetTester(cols, rows uint8, get func() (x, y uint8, down bool, err error)) Tester

GetTester returns a Tester tailored for testing the getting of the device

func SetTester added in v0.0.2

func SetTester(cols, rows uint8, set func(x, y, brightness uint8) error) Tester

SetTester returns a Tester tailored for testing the setting of the device

type UnknownMonomeError added in v0.0.2

type UnknownMonomeError struct {
	Response          []byte
	USBDevice         *usb.Device
	USBWriterEndPoint usb.EndpointInfo
	USBReaderEndPoint usb.EndpointInfo
}

func (*UnknownMonomeError) Error added in v0.0.2

func (e *UnknownMonomeError) Error() string

Directories

Path Synopsis
cmd
monome command
demo command

Jump to

Keyboard shortcuts

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