Documentation
¶
Overview ¶
Package octokeyz provides support for interacting with an octokeyz USB macropad.
Index ¶
- Constants
- Variables
- type Button
- type ButtonHandler
- type ButtonHandlerError
- type ButtonID
- type Device
- func (d *Device) AddHandler(button ButtonID, fn ButtonHandler) error
- func (d *Device) Close() error
- func (d *Device) DisplayClear() error
- func (d *Device) DisplayClearLine(line DisplayLine) error
- func (d *Device) DisplayClearWithDelay(delay time.Duration) error
- func (d *Device) DisplayLine(line DisplayLine, str string, align DisplayLineAlign) error
- func (d *Device) GetDisplayCharsPerLine() byte
- func (d *Device) Led(state LedState) error
- func (d *Device) Listen(errCh chan error) error
- func (d *Device) Open() error
- func (d *Device) SerialNumber() string
- type DisplayLine
- type DisplayLineAlign
- type LedState
- type Modifier
Examples ¶
Constants ¶
const ( // USB vendor identifier reported by octokeyz USBVendorId uint16 = 0x1d50 // USB product identifier reported by octokeyz USBProductId uint16 = 0x6184 // Major USB version number reported by octokeyz firmware that is supported by this package // (this is the interface version, not a USB protocol or product/firmware version) USBVersion byte = 0x01 // USB manufacturer name reported by octokeyz USBManufacturer = "rgm.io" // USB product name reported by octokeyz USBProduct = "octokeyz" )
Variables ¶
var ( ErrButtonInvalid = errors.New("button is not valid") ErrButtonHandlerInvalid = errors.New("button handler is not valid") ErrDeviceDisplayClearWithDelayNotSupported = errors.New("device firmware does not supports display clear with delay") ErrDeviceDisplayNumberOfLinesInvalid = errors.New("device firmware reported an incompatible number of display lines") ErrDeviceDisplayNotSupported = errors.New("device hardware does not includes a display") ErrDeviceEnumerationFailed = usbhid.ErrDeviceEnumerationFailed ErrDeviceFailedToClose = usbhid.ErrDeviceFailedToClose ErrDeviceFailedToOpen = usbhid.ErrDeviceFailedToOpen ErrDeviceFirmwareVersionIncompatible = errors.New("device firmware version is not compatible") ErrDeviceIsClosed = usbhid.ErrDeviceIsClosed ErrDeviceIsOpen = usbhid.ErrDeviceIsOpen ErrDeviceLocked = usbhid.ErrDeviceLocked ErrDeviceMoreThanOne = errors.New("more than one device found") ErrDeviceNotFound = errors.New("device not found") ErrGetFeatureReportFailed = usbhid.ErrGetFeatureReportFailed ErrGetInputReportFailed = usbhid.ErrGetInputReportFailed ErrReportBufferOverflow = usbhid.ErrReportBufferOverflow ErrSetFeatureReportFailed = usbhid.ErrSetFeatureReportFailed ErrSetOutputReportFailed = usbhid.ErrSetOutputReportFailed )
Errors returned from octokeyz package may be tested against these errors with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Button ¶
type Button struct {
// contains filtered or unexported fields
}
Button is an opaque structure that represents an octokeyz USB macropad button.
func (*Button) WaitForRelease ¶
WaitForRelease blocks a button handler until the button that was pressed to activated the handler is released. It returns the duration of the button press.
This function should not be called outside a ButtonHandler. It may cause undefined behavior.
type ButtonHandler ¶
ButtonHandler is a function prototype that helps defining a callback function to handle button events.
type ButtonHandlerError ¶
ButtonHandlerError represents the error returned by a button handler including the button identifier.
func (ButtonHandlerError) Error ¶
func (b ButtonHandlerError) Error() string
Error returns a string representation of a button handler error.
func (ButtonHandlerError) Unwrap ¶
func (b ButtonHandlerError) Unwrap() error
Unwrap returns the underlying button handler error.
type ButtonID ¶
type ButtonID uint8
ButtonID represents the identifier of a button.
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is an opaque structure that represents an octokeyz USB macropad device connected to the computer.
Example ¶
package main import ( "fmt" "log" "time" "rafaelmartins.com/p/octokeyz" ) func main() { dev, err := octokeyz.GetDevice("") if err != nil { log.Fatal(err) } if err := dev.Open(); err != nil { log.Fatal(err) } defer dev.Close() for i := 0; i < 3; i++ { dev.Led(octokeyz.LedFlash) time.Sleep(100 * time.Millisecond) } dev.AddHandler(octokeyz.BUTTON_1, func(b *octokeyz.Button) error { fmt.Println("pressed") duration := b.WaitForRelease() fmt.Printf("released. pressed for %s\n", duration) return nil }) if err := dev.Listen(nil); err != nil { log.Fatal(err) } }
Output:
func GetDevice ¶
GetDevice returns an octokeyz USB macropad found connected to the machine that matches the provided serial number. If serial number is empty and only one device is connected, this device is returned, otherwise an error is returned.
func (*Device) AddHandler ¶
func (d *Device) AddHandler(button ButtonID, fn ButtonHandler) error
AddHandler registers a ButtonHandler callback to be called whenever the given button is pressed.
func (*Device) DisplayClear ¶
DisplayClear erases the whole display. An error may be returned.
func (*Device) DisplayClearLine ¶
func (d *Device) DisplayClearLine(line DisplayLine) error
DisplayClearLine erases the provided display line. An error may be returned.
func (*Device) DisplayClearWithDelay ¶
DisplayClearWithDelay erases the whole display after the provided delay (milliseconds resolution). An error may be returned if the display does not support this feature. If a new line is drawn while the delay is ongoing the display is cleared immediately.
func (*Device) DisplayLine ¶
func (d *Device) DisplayLine(line DisplayLine, str string, align DisplayLineAlign) error
DisplayLine draws the provided string to the provided display line with the provided horizontal alignment. An error may be returned.
func (*Device) GetDisplayCharsPerLine ¶
GetDisplayCharsPerLine returns how many characters can fit in a display line without overflowing.
func (*Device) Listen ¶
Listen listens to button press events from the macropad and calls ButtonHandler callbacks as required.
errCh is an error channel to receive errors from the button handlers. If set to a nil channel, errors are sent to standard logger. Errors are sent non-blocking.
func (*Device) SerialNumber ¶
SerialNumber returns the serial number of the octokeyz USB macropad.
type DisplayLine ¶
type DisplayLine byte
DisplayLine represents the identifier of a display line number.
const ( DisplayLine1 DisplayLine = iota + 1 DisplayLine2 DisplayLine3 DisplayLine4 DisplayLine5 DisplayLine6 DisplayLine7 DisplayLine8 )
An octokeyz USB macropad may contain up to 8 lines
type DisplayLineAlign ¶
type DisplayLineAlign byte
DisplayLine represents the identifier of a display line horizontal alignment.
const ( DisplayLineAlignLeft DisplayLineAlign = iota + 1 DisplayLineAlignRight DisplayLineAlignCenter )
A display line may have its content horizontally aligned left, right or center.
type LedState ¶
type LedState byte
LedState represents a state to set the octokeyz USB macropad led to.
type Modifier ¶
type Modifier struct {
// contains filtered or unexported fields
}
Modifier is an opaque structure that represent a modifier button.
Modifier buttons allow the implementation of secondary functions for buttons, by checking if the modifier button is pressed or not in a button handler callback.