tranx2

package module
v0.0.0-...-9208da2 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2018 License: MIT Imports: 9 Imported by: 2

README

AMB TranX-2 golang implementation

Build Status GoDoc codecov Go Report Card

This package provides Marshal and Unmarshal functionalities for TranX-2 encoded data and some tools for debugging and testing AMB TranX-2 decoder devices and software.

Tools
  • tranx2sim - generate tranx2 encoded data
  • tranx2dump - show tranx2 encoded data in human readable format

Examples

Client
package main

import (
	"log"

	"github.com/bitbrewers/tranx2"
)

type Handler struct{}

func (h *Handler) OnPassing(rec tranx2.Passing) { log.Println(rec) }
func (h *Handler) OnNoise(noise uint16)         { log.Println(noise) }
func (h *Handler) OnError(err error)            { log.Println(err) }

func main() {
	c := tranx2.NewClient("/dev/USBtty0", &Handler{})
	log.Fatal(c.Listen())
}

Reader
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/bitbrewers/tranx2"
)

func main() {
	r := tranx2.NewReader(os.Stdin)
	for {
		record, err := r.ReadPassing()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("%+v\n", record)
	}
}
Writer
package main

import (
	"log"
	"math/rand"
	"os"
	"time"

	"github.com/bitbrewers/tranx2"
)

func main() {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	ticker := time.NewTicker(time.Second)
	started := time.Now()

	w := tranx2.NewWriter(os.Stdout)
	for t := range ticker.C {
		record := tranx2.Passing{
			TransponderID: 12335,
			PassingTicks:  uint32(t.Sub(started).Nanoseconds() / int64(time.Millisecond)),
			Hits:          uint8(r.Uint32()%10) + 5,
			Strength:      uint8(r.Uint32()%50) + 80,
			Prefix:        uint16(r.Uint32()),
			Trailing:      uint8(r.Uint32()),
		}
		_, err := w.WritePassing(record)
		if err != nil {
			log.Fatal(err)
		}
	}
}

Documentation

Index

Constants

View Source
const (
	NoisePrefix      byte   = '#'
	PassingPrefix    byte   = '$'
	MaxTransponderID uint32 = ^uint32(0) >> 8
)

Variables

View Source
var TransponderIDOverflow = errors.New("transponder id overflow")

TransponderIDOverflow is returned when transponder id is bigger than MaxUint24.

Functions

func MarshalNoise

func MarshalNoise(noise uint16) ([]byte, error)

MarshalNoise marshals uint16 noise value to byte array

func MarshalPassing

func MarshalPassing(rec Passing) ([]byte, error)

MarshalPassing marshals Passing struct to byte array

func UnmarshalNoise

func UnmarshalNoise(msg []byte) (uint16, error)

UnmarshalNoise parses noise level from TranX2 encoded data. TranX2 lines are terminated with "\r\n". Unmarshaller allows you to pass lines with or without "\r\n".

Types

type Client

type Client struct {
	Opts    SerialOptions
	Handler Tranx2Handler
	Conn    io.ReadCloser
}

Client provides high level callback API for TranX-2 serial connection.

func NewClient

func NewClient(portName string, handler Tranx2Handler) *Client

NewClient returns TranX2 client with default configuration. Configuration can be modified before calling the Listen receiver.

func (*Client) Close

func (c *Client) Close() error

func (*Client) Listen

func (c *Client) Listen() (err error)

Listen opens serial connections usign options in Client.

func (*Client) Serve

func (c *Client) Serve() (err error)

Serve starts reading from connection and calling handler callbacks.

type Passing

type Passing struct {
	TransponderID uint32 // ID of passing device
	PassingTicks  uint32 // milliseconds elapsed since the device was started.
	Hits          uint8  // number of reads while transponder passed the loop
	Strength      uint8  // signal strenght
	Prefix        uint16 // TODO: figure what is the meaning of this
	Trailing      uint8  // TODO: figure what is the meaning of this
}

Passing struct contains fields present in passing recordsend by TranX2 devices. Note that TransponderID is actually uint24 but is presented as uint32 so Marshalling message with something bigger than MaxTransponderID will result in TransponderIDOverflow error.

func UnmarshalPassing

func UnmarshalPassing(msg []byte) (Passing, error)

UnmarshalPassing creates Passing struct from TranX2 encoded data. TranX2 lines are terminated with "\r\n". Unmarshaller allows you to pass lines with or without "\r\n".

type Reader

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

Reader provides API for reading tranx2 encoded data

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) ReadNoise

func (r *Reader) ReadNoise() (noise uint16, err error)

ReadNoise is high level API for reading only noise levels and discarding everything else. It is suitable for applications that are only interested on noise levels and do not care about transponder data.

func (*Reader) ReadPassing

func (r *Reader) ReadPassing() (rec Passing, err error)

ReadPassing is high level API for reading only Passing records and discarding everything else. It is suitable for applications that are only interested on passings and do not care about noise level records.

type SerialOptions

type SerialOptions serial.OpenOptions

type Tranx2Handler

type Tranx2Handler interface {
	OnPassing(rec Passing)
	OnNoise(noise uint16)
	OnError(err error)
}

Tranx2Handler interface requiresall callback functions that are needed for handling tranx2 events.

type UnmarshalError

type UnmarshalError struct {
	Msg    []byte
	Detail string
	Err    error
}

func (*UnmarshalError) Error

func (e *UnmarshalError) Error() string

type Writer

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

Writer provides API for writing tranx2 encoded data

func NewWriter

func NewWriter(w io.Writer) *Writer

func (*Writer) WriteNoise

func (w *Writer) WriteNoise(noise uint16) (n int, err error)

WriteNoise writes noise level and returns the number of bytes writen or error.

func (*Writer) WritePassing

func (w *Writer) WritePassing(rec Passing) (n int, err error)

WritePassing writes given Passing record and returns the number of bytes writen or error.

Directories

Path Synopsis
cmd
tranx2dump command
tranx2sim command

Jump to

Keyboard shortcuts

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