gobee

package module
v0.0.0-...-6270c53 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2019 License: MIT Imports: 3 Imported by: 1

README

gobee - A Go XBee Library

Build Status Coverage Status Go Report Card codebeat badge

gobee, Golang a library for enabling support of XBee series 2 and series 3 low power radios to your Go project.


Usage

Implement the XBeeTransmitter and XBeeReceiver interfaces, instantiate an XBee, and start communicating, almost... It is up to the gobee user to configure and own the serial port the XBee is connected to and marshal data to/from it.

XBeeTransmitter

gobee uses the XBeeTransmitter interface to request a byte slice be sent to the serial communications port the XBee is connected to.

type XBeeTransmitter interface {
	Transmit([]byte) (int, error)
}
XBeeReceiver

gobee uses the XBeeReceiver interface to report received API frames.

type XBeeReceiver interface {
	Receive(rx.RxFrame) error
}
XBee

This is the XBee widget used to communicate with the physical XBee.

transmitter := &Transmitter{...}	// your XBeeTransmitter
receiver    := &Receiver{...}		// your XBeeReceiver
xbee        := gobee.New(transmitter, receiver)
Building and Transmitting an API Frame

To send an API frame, construct the frame using an appropriate constructor and option functions to set frame parameters and then transmit the frame.

// build the frame
frame := tx.NewZB(
			tx.FrameID(frameID),
			tx.Addr64(api.BroadcastAddr64),
			tx.Addr16(api.BroadcastAddr16),
			tx.Data([]byte("Hello World!")))
			
// transmit the frame
_, err := xbee.TX(frame)
if err != nil {
	// handle transmit error
}

gobee will call the supplied Transmitters Transmit function to send a fully formed API Frame to the UART the XBee is connected to.

Sending API Frame to the UART

When a frame is transmitted, gobee forms an appropriate API frame (see Building and Transmitting an API Frame) and sends it to your XBeeTransmitter for writing to the serial UART the XBee is connected to.

func (tx *Transmitter) Transmit(buffer []byte) (n int, err error) {
	i, err := port.Write(buffer)
	if err != nil {
		fmt.Printf("Failed to write buffer to xbee comms: %v\n", err)
	}

	return i, err
}
Receiving Data from the UART

When data is received from the serial UART the XBee is connected to, send it to gobee.

n, err := port.Read(buffer)
if err != nil {
	// handle receive error
}

for i := 0; i < n; i++ {
	err = xbee.RX(buffer[i])
}
Receiving Data Frames from gobee

Your implemented XBeeReceiver will get called when completed API frames are received. gobee validates the received API frame and reports the data frame via the XBeeReceiver interface.

func (r *Receiver) Receive(f rx.Frame) error {
	switch f.(type) {
	case *rx.ZB:
		// do something with received ZB frame
	}

	return nil
}
License

gobee is licensed under the MIT License. See the LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIEscapeMode

func APIEscapeMode(mode api.EscapeMode) func(interface{})

APIEscapeMode helper option function to gobee.New

Types

type XBee

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

XBee all the things

func New

func New(transmitter XBeeTransmitter, receiver XBeeReceiver, options ...func(interface{})) *XBee

New constructor of XBee's

func (*XBee) RX

func (x *XBee) RX(b byte) error

RX bytes received from the serial communications port are sent here

func (*XBee) SetAPIEscapeMode

func (x *XBee) SetAPIEscapeMode(mode api.EscapeMode)

SetAPIEscapeMode satisfy APIEscapeModeSetter interface

func (*XBee) TX

func (x *XBee) TX(frame tx.Frame) (int, error)

TX transmit a frame to the XBee, forms an appropriate API frame for the frame being sent, uses the XBeeTransmitter to send the API frame bytes to the serial communications port

type XBeeReceiver

type XBeeReceiver interface {
	Receive(rx.Frame) error
}

XBeeReceiver used to report frames received by the XBee

type XBeeTransmitter

type XBeeTransmitter interface {
	Transmit([]byte) (int, error)
}

XBeeTransmitter used to transmit API frame bytes to serial communications port

Directories

Path Synopsis
_examples
echo command
api
rx
tx

Jump to

Keyboard shortcuts

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