client

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2022 License: GPL-3.0 Imports: 12 Imported by: 1

Documentation

Overview

Package client provides a pico-cs go command station client.

Index

Examples

Constants

View Source
const DefaultTCPPort = "4242"

DefaultTCPPort is the default TCP Port used by Pico W.

Variables

View Source
var (
	ErrInvCmd    = errors.New("invalid command")
	ErrInvPrm    = errors.New("invalid parameter")
	ErrInvNumPrm = errors.New("invalid number of parameters")
	ErrNoData    = errors.New("no data")
	ErrNoChange  = errors.New("no change")
	ErrInvGPIO   = errors.New("invalid GPIO")
	ErrNotImpl   = errors.New("not implemented")
	ErrUnknown   = errors.New("unknown error")
)

Command station error definitions.

Functions

func SerialDefaultPortName

func SerialDefaultPortName() (string, error)

SerialDefaultPortName returns the default serial port name if a detection is possible and an error otherwise.

Types

type Board added in v0.1.5

type Board struct {
	Type BoardType
	ID   string
	MAC  string
}

Board hold information of the Pico board.

type BoardType added in v0.1.5

type BoardType byte

BoardType represents the type of a board.

const (
	BtUnknown BoardType = iota
	BtPico
	BtPicoW
)

Board types.

func (BoardType) String added in v0.1.5

func (t BoardType) String() string

type Client

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

Client represents a command station client instance.

Example

ExampleClient shows how to establish a pico-cs command station client.

package main

import (
	"log"

	"github.com/pico-cs/go-client/client"
)

func main() {

	defaultPortName, err := client.SerialDefaultPortName()
	if err != nil {
		log.Fatal(err)
	}

	conn, err := client.NewSerial(defaultPortName)
	if err != nil {
		log.Fatal(err)
	}

	client := client.New(conn, func(msg string) {
		// handle push messages
		log.Printf("push message: %s", msg)
	})
	defer client.Close()

	// read borad information.
	board, err := client.Board()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%s ID %s MAC %s", board.Type, board.ID, board.MAC)

	// read command station temperature.
	temp, err := client.Temp()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("temperature %f", temp)

}

func New added in v0.1.8

func New(conn Conn, handler func(msg string)) *Client

New returns a new client instance.

func (*Client) Board

func (c *Client) Board() (*Board, error)

Board returns bord information like controller type and unique id.

func (*Client) Close

func (c *Client) Close() error

Close closes the client connection.

func (*Client) DCCSyncBits

func (c *Client) DCCSyncBits() (uint, error)

DCCSyncBits returns the number of DCC sync bits.

func (*Client) DelLoco

func (c *Client) DelLoco(addr uint) (uint, error)

DelLoco deletes loco with address addr from loco buffer (debugging).

func (*Client) Enabled

func (c *Client) Enabled() (bool, error)

Enabled returns true if the DCC sigal generation is enabled, false otherwise.

func (*Client) Help

func (c *Client) Help() ([]string, error)

Help returns the help texts of the command station.

func (*Client) IOADC added in v0.2.0

func (c *Client) IOADC(input uint) (float64, error)

IOADC returns the 'raw' value of the ADC input.

func (*Client) IOCmdb added in v0.2.0

func (c *Client) IOCmdb(cmd, gpio uint) (bool, error)

IOCmdb returns the boolean result value of the binary GPIO command.

func (*Client) LocoCV1718

func (c *Client) LocoCV1718(addr uint) (byte, byte, error)

LocoCV1718 returns (calculates) the CV17 and CV18 values (long address) from a loco address.

func (*Client) LocoDir

func (c *Client) LocoDir(addr uint) (bool, error)

LocoDir returns the direction of a loco. true : forward direction false: backward direction

func (*Client) LocoFct

func (c *Client) LocoFct(addr, no uint) (bool, error)

LocoFct returns a function value of a loco.

func (*Client) LocoSpeed128

func (c *Client) LocoSpeed128(addr uint) (uint, error)

LocoSpeed128 returns the speed of a loco. 0 : stop 1 : emergency stop 2-127: 126 speed steps

func (*Client) RBuf

func (c *Client) RBuf() (*RBuf, error)

RBuf returns the command station refresh buffer (debugging).

func (*Client) SetDCCSyncBits

func (c *Client) SetDCCSyncBits(syncBits uint) (uint, error)

SetDCCSyncBits sets the number of DCC sync bits.

func (*Client) SetEnabled

func (c *Client) SetEnabled(enabled bool) (bool, error)

SetEnabled sets DCC sigal generation whether to enabled or disabled.

func (*Client) SetIOCmdb added in v0.2.0

func (c *Client) SetIOCmdb(cmd, gpio uint, value bool) (bool, error)

SetIOCmdb sets the boolean value of the binary GPIO command.

func (*Client) SetLocoCV29Bit5

func (c *Client) SetLocoCV29Bit5(addr uint, bit bool) (bool, error)

SetLocoCV29Bit5 sets the CV 29 bit 5 value of a loco.

func (*Client) SetLocoCVBit

func (c *Client) SetLocoCVBit(addr, idx uint, bit byte, val bool) (bool, error)

SetLocoCVBit sets the indexed CV bit value of a loco.

func (*Client) SetLocoCVByte

func (c *Client) SetLocoCVByte(addr, idx uint, val byte) (byte, error)

SetLocoCVByte sets the indexed CV byte value of a loco.

func (*Client) SetLocoDir

func (c *Client) SetLocoDir(addr uint, dir bool) (bool, error)

SetLocoDir sets the direction of a loco. true : forward direction false: backward direction

func (*Client) SetLocoFct

func (c *Client) SetLocoFct(addr, no uint, fct bool) (bool, error)

SetLocoFct sets a function value of a loco.

func (*Client) SetLocoLaddr

func (c *Client) SetLocoLaddr(addr, laddr uint) (uint, error)

SetLocoLaddr sets the long address of a loco.

func (*Client) SetLocoSpeed128

func (c *Client) SetLocoSpeed128(addr, speed uint) (uint, error)

SetLocoSpeed128 sets the speed of a loco. 0 : stop 1 : emergency stop 2-127: 126 speed steps

func (*Client) Temp

func (c *Client) Temp() (float64, error)

Temp returns the temperature of the command station.

func (*Client) ToggleIOCmdb added in v0.2.1

func (c *Client) ToggleIOCmdb(cmd, gpio uint) (bool, error)

ToggleIOCmdb toggles the value of the binary GPIO command.

func (*Client) ToggleLocoDir

func (c *Client) ToggleLocoDir(addr uint) (bool, error)

ToggleLocoDir toggles the direction of a loco.

func (*Client) ToggleLocoFct

func (c *Client) ToggleLocoFct(addr, no uint) (bool, error)

ToggleLocoFct toggles a function value of a loco.

type Conn

type Conn interface {
	io.ReadWriteCloser
}

Conn is a stream oriented connection to the pico board.

type RBuf

type RBuf struct {
	First, Next int
	Entries     []*RBufEntry
}

RBuf represents a command station refresh buffer.

func (*RBuf) String

func (buf *RBuf) String() string

type RBufEntry

type RBufEntry struct {
	Idx             int
	Addr            uint
	NumRefreshCycle byte
	RefreshCycle    byte
	DirSpeed        byte
	F0_4            byte
	F5_8            byte
	F9_12           byte //lint:ignore ST1003 complains about ALL_CAPS
	F5_12           byte //lint:ignore ST1003 complains about ALL_CAPS
	F13_20          byte //lint:ignore ST1003 complains about ALL_CAPS
	F21_28          byte //lint:ignore ST1003 complains about ALL_CAPS
	F29_36          byte //lint:ignore ST1003 complains about ALL_CAPS
	F37_44          byte //lint:ignore ST1003 complains about ALL_CAPS
	F45_52          byte //lint:ignore ST1003 complains about ALL_CAPS
	F53_60          byte //lint:ignore ST1003 complains about ALL_CAPS
	F61_68          byte //lint:ignore ST1003 complains about ALL_CAPS
	Prev            byte
	Next            byte
}

RBufEntry represents a command station refresh buffer entry.

func (*RBufEntry) String

func (e *RBufEntry) String() string

type Serial

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

Serial provides a serial connection to to the Raspberry Pi Pico.

func NewSerial

func NewSerial(portName string) (*Serial, error)

NewSerial returns a new serial connection instance.

func (*Serial) Close

func (s *Serial) Close() error

Close implements the Conn interface.

func (*Serial) Read

func (s *Serial) Read(p []byte) (n int, err error)

Read implements the Conn interface.

func (*Serial) Write

func (s *Serial) Write(p []byte) (n int, err error)

Write implements the Conn interface.

type TCPClient added in v0.1.10

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

TCPClient provides a TCP/IP connection to to the Raspberry Pi Pico W.

func NewTCPClient added in v0.1.10

func NewTCPClient(host, port string) (*TCPClient, error)

NewTCPClient returns a new TCP/IP connection instance.

func (*TCPClient) Close added in v0.1.10

func (c *TCPClient) Close() error

Close implements the Conn interface.

func (*TCPClient) Read added in v0.1.10

func (c *TCPClient) Read(p []byte) (n int, err error)

Read implements the Conn interface.

func (*TCPClient) Write added in v0.1.10

func (c *TCPClient) Write(p []byte) (n int, err error)

Write implements the Conn interface.

Jump to

Keyboard shortcuts

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