Commit 5788a997 authored by barnslig's avatar barnslig
Browse files

Initial DMX commit

parents
package dmx
import (
"errors"
"fmt"
"github.com/tarm/goserial"
"io"
"strconv"
)
const (
MaxChannel int = 513
MaxValue int = 255
StartByte byte = 0x7E
EndByte byte = 0xE7
TxLabelByte byte = 0x06
)
type Config struct {
Device string
Baud int
AutoRender bool
}
type DMX struct {
config Config
dmx io.ReadWriteCloser
frame [MaxChannel]byte
}
func CreateDMX(config Config) (dmx *DMX, err error) {
dmx = &DMX{config: config}
dmx.dmx, err = serial.OpenPort(&serial.Config{
Name: dmx.config.Device,
Baud: dmx.config.Baud,
})
if err != nil {
return
}
return
}
func (dmx *DMX) SetChannel(channel int, value int) (err error) {
if channel > MaxChannel || channel < 1 {
err = errors.New(fmt.Sprintf("Invalid channel. Maximum channel: %v", MaxChannel))
return
}
if value > MaxValue || value < 0 {
err = errors.New(fmt.Sprintf("Invalid value. Maximum value: %v", MaxValue))
return
}
dmx.frame[channel] = byte(value)
if dmx.config.AutoRender {
dmx.Render()
}
return
}
func (dmx *DMX) Render() (packet []byte, err error) {
packet = make([]byte, MaxChannel+6)
size0 := []byte(strconv.QuoteRuneToASCII(rune(len(packet))))
size1 := []byte(strconv.QuoteRuneToASCII(rune(len(packet)) << 8))
packet[0] = StartByte
packet[1] = TxLabelByte
packet[2] = size0[0]
packet[3] = size1[0]
packet[4] = 0x00
for _, frame := range dmx.frame {
packet = append(packet, frame)
}
packet[len(packet)-1] = EndByte
return
}
package dmx
import (
"testing"
)
func TestSetChannel(t *testing.T) {
dmx := &DMX{}
err := dmx.SetChannel(256, 10)
if err != nil {
t.Errorf("SetChannel failed: %v", err)
}
if dmx.frame[256] != 10 {
t.Errorf("SetChannel failed: Frame not setted")
}
}
func TestRender(t *testing.T) {
dmx := &DMX{}
dmx.SetChannel(10, 246)
dmx.SetChannel(25, 42)
dmx.Render()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment