client

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2023 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 (
	MkUnknown = iota
	MkWifi
	MkTCP
	MkIOIE
)

Message kind.

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 client.Msg, err error) {
		// handle push messages
		if err != nil {
			log.Printf("push message: %s", msg)
		} else {
			log.Printf("push message error: %s", err)
		}
	})
	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 Msg, err error)) *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) 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) IODir added in v0.3.0

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

IODir returns the direction of the GPIO. false: in true: out

func (*Client) IODown added in v0.3.0

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

IODown returns the pull-down status of the GPIO.

func (*Client) IOUp added in v0.3.0

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

IOUp returns the pull-up status of the GPIO.

func (*Client) IOVal added in v0.3.0

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

IOVal returns the boolean value of the GPIO.

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) MTCV added in v0.3.0

func (c *Client) MTCV(idx MTCVIdx) (byte, error)

MTCV returns the value of a main track CV.

func (*Client) MTE added in v0.4.0

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

MTE returns true if the main track DCC sigal generation is enabled, false otherwise.

func (*Client) RBuf

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

RBuf returns the command station refresh buffer (debugging).

func (*Client) RBufDel added in v0.3.0

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

RBufDel deletes address addr from refresh buffer (debugging).

func (*Client) RBufReset added in v0.3.0

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

RBufReset resets the refresh buffer (debugging).

func (*Client) SetIODir added in v0.3.0

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

SetIODir sets the direction of the GPIO. false: in true: out

func (*Client) SetIODown added in v0.3.0

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

SetIODown sets the pull-down status of the GPIO.

func (*Client) SetIOUp added in v0.3.0

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

SetIOUp sets the pull-up status of the GPIO.

func (*Client) SetIOVal added in v0.3.1

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

SetIOVal sets the boolean value of the GPIO.

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) SetMTCV added in v0.3.0

func (c *Client) SetMTCV(idx MTCVIdx, val byte) (byte, error)

SetMTCV sets the value of a main track CV.

func (*Client) SetMTE added in v0.4.0

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

SetMTE sets main track DCC sigal generation whether to enabled or disabled.

func (*Client) Temp

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

Temp returns the temperature of the command station.

func (*Client) ToggleIODir added in v0.3.0

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

ToggleIODir toggles the direction of the GPIO.

func (*Client) ToggleIODown added in v0.3.0

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

ToggleIODown toggles the pull-down status of the GPIO.

func (*Client) ToggleIOUp added in v0.3.0

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

ToggleIOUp toggles the pull-up status of the GPIO.

func (*Client) ToggleIOVal added in v0.3.1

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

ToggleIOVal toggles the value of the GPIO.

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 IOIEMsg added in v0.2.2

type IOIEMsg struct {
	GPIO  uint
	State bool
}

IOIEMsg represents a GPIO input event message.

func (*IOIEMsg) Kind added in v0.2.2

func (m *IOIEMsg) Kind() int

Kind implements the push message interface.

func (*IOIEMsg) String added in v0.2.2

func (m *IOIEMsg) String() string

type MTCVIdx added in v0.3.0

type MTCVIdx byte

MTCVIdx represents a main track CV index.

const (
	MTCVNumSyncBit   MTCVIdx = iota // Number of DCC synchronization bits
	MTCVNumRepeat                   // DCC command repetitions
	MTCVNumRepeatCV                 // DCC command repetitions for main track CV programming
	MTCVNumRepeatAcc                // DCC command repetitions for accessory decoders
)

Main track CV index constants.

type Msg added in v0.2.2

type Msg interface {
	fmt.Stringer
	Kind() int
}

A Msg represents a push message.

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           byte
	Addr          uint
	MaxRefreshCmd byte
	RefreshCmd    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.

type TCPMsg added in v0.2.3

type TCPMsg struct {
	Text string
}

TCPMsg represents a tcp info message.

func (*TCPMsg) Kind added in v0.2.3

func (m *TCPMsg) Kind() int

Kind implements the push message interface.

func (*TCPMsg) String added in v0.2.3

func (m *TCPMsg) String() string

type WifiMsg added in v0.2.3

type WifiMsg struct {
	Text string
}

WifiMsg represents a Wifi info message.

func (*WifiMsg) Kind added in v0.2.3

func (m *WifiMsg) Kind() int

Kind implements the push message interface.

func (*WifiMsg) String added in v0.2.3

func (m *WifiMsg) String() string

Jump to

Keyboard shortcuts

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