Initial github commit
This commit is contained in:
258
noolite.go
Normal file
258
noolite.go
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 Denis V. Dedkov (denis.v.dedkov@gmail.com)
|
||||||
|
|
||||||
|
This file is part of Noolite Go bindings.
|
||||||
|
|
||||||
|
Noolite Go bindings is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Noolite Go bindings is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with Noolite Go bindings. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Package noolite provide class for control Noolite Adapters PC11xx.
|
||||||
|
// Protocol described on url:
|
||||||
|
// http://www.noo.com.by/assets/files/software/PC11xx_HID_API.pdf
|
||||||
|
package noolite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/tonymagro/usb"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
VID = 5824 // Vendor ID
|
||||||
|
PID = 1503 // Product ID
|
||||||
|
)
|
||||||
|
|
||||||
|
// Available commands
|
||||||
|
type command byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
off command = iota
|
||||||
|
decBrightnes
|
||||||
|
on
|
||||||
|
incBrightnes
|
||||||
|
cSwitch
|
||||||
|
invertBrightnes
|
||||||
|
set
|
||||||
|
callScenario
|
||||||
|
saveScenario
|
||||||
|
unbind
|
||||||
|
stopColorSelection
|
||||||
|
bind = iota + 0x04
|
||||||
|
// Commands for SD111-180 only
|
||||||
|
colorSelection
|
||||||
|
colorSwitch
|
||||||
|
modeSwitch
|
||||||
|
effectSpeed
|
||||||
|
)
|
||||||
|
|
||||||
|
// Noolite Adapter class as USB HID
|
||||||
|
type NooliteAdapter struct {
|
||||||
|
*usb.Device
|
||||||
|
mode byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return NooliteAdapter object.
|
||||||
|
//
|
||||||
|
// work mode: range 0..7
|
||||||
|
// bitrate: range 0..3
|
||||||
|
// command repeats: range 0..7
|
||||||
|
func NewNooliteAdapter(mode, bitrate, repeats uint) (*NooliteAdapter, error) {
|
||||||
|
usb.Init()
|
||||||
|
|
||||||
|
d := usb.Open(VID, PID)
|
||||||
|
|
||||||
|
if d == nil {
|
||||||
|
return nil, errors.New("Device not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
if mode > 7 {
|
||||||
|
return nil, errors.New("Mode must be in 0..7 range")
|
||||||
|
}
|
||||||
|
|
||||||
|
if bitrate > 3 {
|
||||||
|
return nil, errors.New("Bitrate must be in 0..3 range")
|
||||||
|
}
|
||||||
|
|
||||||
|
if repeats > 7 {
|
||||||
|
return nil, errors.New("Repeats must be in 0..7 range")
|
||||||
|
}
|
||||||
|
|
||||||
|
m := (byte(repeats) << 5) | (byte(bitrate) << 3) | byte(mode)
|
||||||
|
|
||||||
|
return &NooliteAdapter{d, m}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return NooliteAdapter object with default work mode values:
|
||||||
|
//
|
||||||
|
// work mode: 0
|
||||||
|
// bitrate: 2 (for 1000 bit/sec)
|
||||||
|
// command repeats: 2
|
||||||
|
func DefaultNooliteAdapter() (*NooliteAdapter, error) { // Default constructor
|
||||||
|
return NewNooliteAdapter(0, 2, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NooliteAdapter) composeCommand(cmd command, channel int, args ...int) []byte {
|
||||||
|
c := make([]byte, 8)
|
||||||
|
|
||||||
|
c[0] = n.mode
|
||||||
|
c[1] = byte(cmd)
|
||||||
|
c[4] = byte(channel)
|
||||||
|
|
||||||
|
if cmd == set {
|
||||||
|
l := len(args)
|
||||||
|
switch l {
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
c[2] = 0x01
|
||||||
|
c[5] = byte(args[0])
|
||||||
|
}
|
||||||
|
case 3:
|
||||||
|
{
|
||||||
|
c[2] = 0x03
|
||||||
|
for i, v := range args {
|
||||||
|
c[5+i] = byte(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("Bad arguments for SET command")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NooliteAdapter) sendCommand(command []byte) {
|
||||||
|
n.Configuration(1)
|
||||||
|
n.Interface(0)
|
||||||
|
n.ControlMsg(0x21, 0x09, 0x300, 0, command)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Turn power OFF for specified channel
|
||||||
|
func (n *NooliteAdapter) Off(channel int) {
|
||||||
|
cmd := n.composeCommand(off, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Smooth brightnes decrase for specified channel
|
||||||
|
func (n *NooliteAdapter) DecraseBrightnes(channel int) {
|
||||||
|
cmd := n.composeCommand(decBrightnes, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Turn power ON for specified channel
|
||||||
|
func (n *NooliteAdapter) On(channel int) {
|
||||||
|
cmd := n.composeCommand(on, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Smooth brightnes incrase for specified channel
|
||||||
|
func (n *NooliteAdapter) IncraseBrightnes(channel int) {
|
||||||
|
cmd := n.composeCommand(incBrightnes, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch power state between off and on for specified channel
|
||||||
|
func (n *NooliteAdapter) Switch(channel int) {
|
||||||
|
cmd := n.composeCommand(cSwitch, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Smooth brightnes incrase or decrase for specified channel
|
||||||
|
func (n *NooliteAdapter) InvertBrightnes(channel int) {
|
||||||
|
cmd := n.composeCommand(invertBrightnes, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set brightnes value for specified channel
|
||||||
|
//
|
||||||
|
// Value must be in range 35..155.
|
||||||
|
// When value == 0 lights off.
|
||||||
|
// When value > 155 lights on for full brightness.
|
||||||
|
func (n *NooliteAdapter) SetBrightnesValue(channel, value int) {
|
||||||
|
cmd := n.composeCommand(set, channel, value)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set brightnes values for independens channels
|
||||||
|
//
|
||||||
|
// Available for SD111-180 only
|
||||||
|
func (n *NooliteAdapter) SetBrightnesValues(channel, val1, val2, val3 int) {
|
||||||
|
cmd := n.composeCommand(set, channel, val1, val2, val3)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call scenario for specified channel
|
||||||
|
func (n *NooliteAdapter) CallScenario(channel int) {
|
||||||
|
cmd := n.composeCommand(callScenario, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save scenario for specified channel
|
||||||
|
func (n *NooliteAdapter) SaveScenario(channel int) {
|
||||||
|
cmd := n.composeCommand(saveScenario, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unbind signal for specified channel
|
||||||
|
func (n *NooliteAdapter) UnbindChannel(channel int) {
|
||||||
|
cmd := n.composeCommand(unbind, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop color selection for specified channel
|
||||||
|
//
|
||||||
|
// Available for SD111-180 only
|
||||||
|
func (n *NooliteAdapter) StopColorSelection(channel int) {
|
||||||
|
cmd := n.composeCommand(stopColorSelection, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set binding for specified channel
|
||||||
|
func (n *NooliteAdapter) BindChannel(channel int) {
|
||||||
|
cmd := n.composeCommand(bind, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Smooth color changing for specified channel
|
||||||
|
//
|
||||||
|
// Stop with StopColorSelection method
|
||||||
|
//
|
||||||
|
// Avialable for SD111-180 only
|
||||||
|
func (n *NooliteAdapter) ColorSelection(channel int) {
|
||||||
|
cmd := n.composeCommand(colorSelection, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch color for specified channel
|
||||||
|
//
|
||||||
|
// Avialable for SD111-180 only
|
||||||
|
func (n *NooliteAdapter) ColorSwitch(channel int) {
|
||||||
|
cmd := n.composeCommand(colorSwitch, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch work mode for specified channel
|
||||||
|
//
|
||||||
|
// Avialable for SD111-180 only
|
||||||
|
func (n *NooliteAdapter) ModeSwitch(channel int) {
|
||||||
|
cmd := n.composeCommand(modeSwitch, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set change color speed for specified channel
|
||||||
|
//
|
||||||
|
// Avialable for SD111-180 only
|
||||||
|
func (n *NooliteAdapter) EffectSpeed(channel int) {
|
||||||
|
cmd := n.composeCommand(effectSpeed, channel)
|
||||||
|
n.sendCommand(cmd)
|
||||||
|
}
|
||||||
110
noolite_test.go
Normal file
110
noolite_test.go
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 Denis V. Dedkov (denis.v.dedkov@gmail.com)
|
||||||
|
|
||||||
|
This file is part of Noolite Go bindings.
|
||||||
|
|
||||||
|
Noolite Go bindings is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Noolite Go bindings is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with Noolite Go bindings. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package noolite
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestDefaultNooliteAdapter(t *testing.T) {
|
||||||
|
n, err := DefaultNooliteAdapter()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
cmd := n.composeCommand(0x00, 0x00)
|
||||||
|
|
||||||
|
if cmd[0] != 0x50 {
|
||||||
|
t.Error("Adapter mode (first byte): expected 0x50, got", cmd[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewNooliteAdapter(t *testing.T) {
|
||||||
|
n, err := NewNooliteAdapter(0, 2, 2)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
n.Close()
|
||||||
|
|
||||||
|
n, err = NewNooliteAdapter(8, 2, 2)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Bad value for mode. Must be in range 0..7")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err = NewNooliteAdapter(0, 4, 2)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Bad value for bitrate. Must be in range 0..3")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err = NewNooliteAdapter(0, 2, 8)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Bad value for mode. Must be in range 0..7")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type teststr struct {
|
||||||
|
values []byte
|
||||||
|
cmd command
|
||||||
|
args []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComposeCommand(t *testing.T) {
|
||||||
|
n, err := DefaultNooliteAdapter()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
var tests = []teststr{
|
||||||
|
{[]byte{0x50, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, off, []int{}},
|
||||||
|
{[]byte{0x50, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, decBrightnes, []int{}},
|
||||||
|
{[]byte{0x50, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, on, []int{}},
|
||||||
|
{[]byte{0x50, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, incBrightnes, []int{}},
|
||||||
|
{[]byte{0x50, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, cSwitch, []int{}},
|
||||||
|
{[]byte{0x50, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, invertBrightnes, []int{}},
|
||||||
|
{[]byte{0x50, 0x06, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00}, set, []int{1}},
|
||||||
|
{[]byte{0x50, 0x06, 0x03, 0x00, 0x01, 0x01, 0x01, 0x01}, set, []int{1, 1, 1}},
|
||||||
|
{[]byte{0x50, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, callScenario, []int{}},
|
||||||
|
{[]byte{0x50, 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, saveScenario, []int{}},
|
||||||
|
{[]byte{0x50, 0x09, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, unbind, []int{}},
|
||||||
|
{[]byte{0x50, 0x0a, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, stopColorSelection, []int{}},
|
||||||
|
{[]byte{0x50, 0x0f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, bind, []int{}},
|
||||||
|
{[]byte{0x50, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, colorSelection, []int{}},
|
||||||
|
{[]byte{0x50, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, colorSwitch, []int{}},
|
||||||
|
{[]byte{0x50, 0x12, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, modeSwitch, []int{}},
|
||||||
|
{[]byte{0x50, 0x13, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, effectSpeed, []int{}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tstr := range tests {
|
||||||
|
cmd := n.composeCommand(tstr.cmd, 1, tstr.args...)
|
||||||
|
for i, v := range cmd {
|
||||||
|
if v != tstr.values[i] {
|
||||||
|
t.Error("For command",
|
||||||
|
tstr.cmd,
|
||||||
|
"expected",
|
||||||
|
tstr.values[i],
|
||||||
|
"got", v,
|
||||||
|
"in position", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user