opc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2014 License: MIT Imports: 2 Imported by: 8

README

   __     ___              ___   _____     ___   
 /'_ `\  / __`\  _______  / __`\/\ '__`\  /'___\ 
/\ \L\ \/\ \L\ \/\______\/\ \L\ \ \ \L\ \/\ \__/ 
\ \____ \ \____/\/______/\ \____/\ \ ,__/\ \____\
 \/___L\ \/___/           \/___/  \ \ \/  \/____/
   /\____/                         \ \_\         
   \_/__/                           \/_/         

Build Status

what

A golang implementation of the Open Pixel Control protocol.

open pixel control

Open Pixel Control is a protocol that is used to control arrays of RGB lights like Total Control Lighting (http://www.coolneon.com/) and Fadecandy devices (https://github.com/scanlime/fadecandy).

documentation

You can read the documentation here: (http://godoc.org/github.com/kellydunn/go-opc)

usage

package main

import("github.com/kellydunn/go-opc")

func main {
     // Setup a new server
     s := opc.NewServer()

     // Register your devices (where r is an implementation of opc.Device)
     s.RegisterDevice(r)

     // Listen for incoming messages and serve them accordingly
     go s.ListenOnPort("tcp", "7890")
     go s.Process()

     // Create a client
     c := opc.NewClient("tcp", "localhost:7890")

     // Make a message!
     // This creates a message to send on channel 0
     // Or according to the OPC spec, a Broadcast Message.
     m := opc.NewMessage(0)  

     // Set pixel #1 to white.
     m.SetPixelColors(1, 255, 255, 255)
     
     // Send the message!
     c.Send(m)

     // The first pixel of all registered devices should be white!
}    

design

The applications of OPC are not currently tied to any single communication model, and it is currently unclear if there is any canonical method of dispatching OPC messages. So, when using this library, it is encouraged to implement the opc.Device interface such that you can further define the details of your devices and how they should be written to.

A very simple implementation of the opc.Device interface could be the following:

type DummyDevice struct {
     conn net.Conn
     channel uint8
}

// Simple write behavior.  Write the OPC Message over a network connection.
func (d *DummyDevice) Write(m *opc.Message) error {
     _, err := conn.Write(m.byteArray)
     if err != nil {
        return err
     }
     
     return nil
}

// Simple Channel getter.  Return the channel in which to associate this device.
func (d *DummyDevice) Channel() uint8 {
     return channel
}

Here's a video of go-opc interacting with the default OPC client provided by openpixelcontrol.org : (https://vine.co/v/hIqiZewthIh)

Documentation

Index

Constants

View Source
const (
	SET_PIXEL_COLORS  = 0x00
	SYSTEM_EXCLUSIVE  = 0xFF
	HEADER_BYTES      = 4
	BROADCAST_CHANNEL = 0
	MAX_MESSAGE_SIZE  = 0xFFFF
)
View Source
const (
	DEFAULT_OPC_PORT = "7890"
)

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe()

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

This struct represents an OPC client which is used to send OPC messages to an OPC server.

func NewClient

func NewClient() *Client

Creates and returns a new Client

func (*Client) Connect

func (c *Client) Connect(protocol string, host string) error

Connects the client to a server specified by the protocol string of either 'tcp' or 'udp', and the host location, which is a single string in the `url:port` format.

func (*Client) Send

func (c *Client) Send(m *Message) error

Sends an OPC message from the Client to the Server connection.

type Device

type Device interface {
	Write(*Message) error
	Channel() uint8
}

This interface describes the behavior of an OPC device. OPC devices should be able to write OPC messages to themselves as well as be able to announce a Channel in which they are listening on.

type Message

type Message struct {
	// contains filtered or unexported fields
}

This struct describes a single message that follows the OPC protocol

func NewMessage

func NewMessage(channel uint8) *Message

Creates and returns a pointer to a new message that is to be sent to the passed in channel

func (*Message) ByteArray

func (m *Message) ByteArray() []byte

Returns a byte array representation of this message.

func (*Message) IsBroadcast

func (m *Message) IsBroadcast() bool

Returns whether or not this message is a Broadcast message.

func (*Message) IsValid

func (m *Message) IsValid() bool

Returns whether or not this message is valid or not. Validity is determined as whether or not the Length of the message corresponds with the number of data bytes in the message

func (*Message) Length

func (m *Message) Length() uint16

Returns the length of the message. The length of the message is respresented by combining the high and low length bytes of this message.

func (*Message) SetLength

func (m *Message) SetLength(length uint16)

Sets the length of this message by splitting the passed in length into high and low length bytes.

func (*Message) SetPixelColor

func (m *Message) SetPixelColor(pixel int, r uint8, g uint8, b uint8)

Sets the pixel color of the passed in pixel to the passed in red, green, and blue colors, respectively for this message

func (*Message) SystemExclusive

func (m *Message) SystemExclusive(systemId []byte, data []byte)

Specifies that this message is a System Exclusive Message and populates data accordingly

type Server

type Server struct {
	// contains filtered or unexported fields
}

This struct describes an OPC server, which keeps track of all connected OPC devices as well as a channel of incoming messages from all connected clients

func NewServer

func NewServer() *Server

Creates and returns a new opc.Server. Accepts a list of usb product IDs in which to send opc messages to.

func (*Server) ListenOnPort

func (s *Server) ListenOnPort(protocol string, port string)

Listens on the passed in port with the passed in protocol, which in turn accepts incoming connections and handles them by issuing individual goroutines.

func (*Server) Process

func (s *Server) Process()

Processes all pending messages indefinitely

func (*Server) RegisterDevice

func (s *Server) RegisterDevice(dev Device)

Registers the passed in device to the OPC server

func (*Server) UnregisterDevice

func (s *Server) UnregisterDevice(dev Device)

Unregisters the passed in device from the OPC server

Jump to

Keyboard shortcuts

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