client

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: GPL-3.0 Imports: 14 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")
	ErrIO        = errors.New("io error")
	ErrNotExec   = errors.New("not executed")
	ErrUnknown   = errors.New("unknown error")
)

Command station error definitions.

View Source
var (
	ErrSerialDefaultPortPathMissing = fmt.Errorf("missing default serial port path for %s", runtime.GOOS)
	ErrSerialDefaultPortNotFound    = fmt.Errorf("default port could not be detected for %s", defaultSerialPortPath)
)

Serial default port errors.

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 CVIdx added in v0.5.0

type CVIdx byte

CVIdx represents a command station CV index.

const (
	CVMT           CVIdx = iota // Main track configuration flags
	CVNumSyncBit                // Number of DCC synchronization bits
	CVNumRepeat                 // DCC command repetitions
	CVNumRepeatCV               // DCC command repetitions for main track CV programming
	CVNumRepeatAcc              // DCC command repetitions for accessory decoders
	CVBidiTS                    // BiDi (microseconds until power off after end bit)
	CVBidiTE                    // BiDi (microseconds to power on before start of 5th sync bit)
)

Main track CV index constants.

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"
	"time"

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

func main() {

	time.Sleep(2 * time.Second)

	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 board 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 board information like controller type and unique id.

func (*Client) CV added in v0.5.0

func (c *Client) CV(idx CVIdx) (byte, error)

CV returns the value of a command station CV.

func (*Client) Close

func (c *Client) Close() error

Close closes the client connection.

func (*Client) Flash added in v0.5.0

func (c *Client) Flash() (*flash.Flash, error)

Flash returns the command station flash data (debugging).

func (*Client) FlashFormat added in v0.5.0

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

FlashFormat formats the command station flash (debugging).

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) IsSerialConn added in v0.5.1

func (c *Client) IsSerialConn() bool

IsSerialConn returns true if the connection is serial, false otherwise.

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) 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) Reboot added in v0.5.0

func (c *Client) Reboot() error

Reboot reboots the command station (debugging).

func (*Client) Reconnect added in v0.5.0

func (c *Client) Reconnect() error

Reconnect reconnects the client.

func (*Client) RefreshBuffer added in v0.5.0

func (c *Client) RefreshBuffer() (*rbuf.Buffer, error)

RefreshBuffer returns the command station refresh buffer (debugging).

func (*Client) RefreshBufferDelete added in v0.5.0

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

RefreshBufferDelete deletes address addr from refresh buffer (debugging).

func (*Client) RefreshBufferReset added in v0.5.0

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

RefreshBufferReset resets the refresh buffer (debugging).

func (*Client) SetAccFct added in v0.5.0

func (c *Client) SetAccFct(addr uint, out byte, fct bool) (bool, error)

SetAccFct sets the function value of an accessory decoder on output out.

func (*Client) SetAccStatus added in v0.5.0

func (c *Client) SetAccStatus(addr uint, status byte) (bool, error)

SetAccStatus sets the status byte of an extended accessory decoder.

func (*Client) SetAccTime added in v0.5.0

func (c *Client) SetAccTime(addr uint, out, time byte) (bool, error)

SetAccTime sets the activation time of an accessory decoder on output out.

func (*Client) SetCV added in v0.5.0

func (c *Client) SetCV(idx CVIdx, val byte) (byte, error)

SetCV sets the value of a command station CV.

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) 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) Store added in v0.5.0

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

Store stores the command station CVs on flash.

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 {
	Connect() error
	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 Msg added in v0.2.2

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

A Msg represents a push message.

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) Connect added in v0.5.1

func (s *Serial) Connect() error

Connect connect the serial port.s

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) Connect added in v0.5.1

func (c *TCPClient) Connect() (err error)

Connect connects to the tcp address.

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

Directories

Path Synopsis
Package flash contains flash types, methods and functions.
Package flash contains flash types, methods and functions.
Package rbuf contains refresh buffer types, methods and functions.
Package rbuf contains refresh buffer types, methods and functions.

Jump to

Keyboard shortcuts

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