gobot_sim

package module
v0.0.0-...-f91051b Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

README

gobot-sim

Simulate GPIO for Gobot with your keyboard and your own code

github: https://github.com/24HOURSMEDIA/gobot-sim

  • Log keystrokes to send vitual pin inputs to your Gobot application
    (your keyboard replaces an IN pin)
  • Hook into OUT pins so they pass through your code instead of the hardware
    (your code replaces a device connected to an OUT pin)

View the example code.

Gobot-Sim can simulate Raspberry Pi GPIO pin inputs on a local development machine such as a Mac. It links specific keyboard input to GPIO inputs captured by GoBot.

It facilitates local development and testing of Gobot applications on non-raspberry machines.

For example, consider a push button which should be wired to GPIO 11.

With Gobot-sim, you can run your application in the terminal, and activate the button press by linking it to a keyboard shortcut.

Examples

Run this on your Mac or Linux machine. You can toggle the button by pressing the '1' key.

package main

import (
	"fmt"
	"github.com/24hoursmedia/gobot_sim"
	"github.com/24hoursmedia/gobot_sim/raspi_sim"
	"gobot.io/x/gobot"
	"gobot.io/x/gobot/drivers/gpio"
	"gobot.io/x/gobot/platforms/raspi"
	"time"
)

func main() {
  // set up rasberry pi gobot with a button attached to pin 11 (GPIO17)
  r := raspi.NewAdaptor()
  button := gpio.NewButtonDriver(r, "11", time.Millisecond*20)
  work := func() {
    button.On(gpio.ButtonPush, func(data interface{}) {
      log.Info().Msg("button pressed")
    })
    button.On(gpio.ButtonRelease, func(data interface{}) {
      log.Info().Msg("button released")
    })
  }

  robot := gobot.NewRobot("buttonBot",
    []gobot.Connection{r},
    []gobot.Device{button},
    work,
  )

  // hook in the simulator. It links keypress '1' to a simulation of a button press and release
  // on pin 11 (GPIO 17)
  sim := raspi_sim.NewGobotSimulator(r)
  sim.AddKeyPressPWAction('1', "11", gobot_sim.PW_ACTION_BUTTONPRESS)
  sim.Run()

  // start the 'real' robot
  robot.Start()
}

Example

Documentation

Index

Constants

View Source
const (
	PW_ACTION_UNDEFINED = iota
	PW_ACTION_ON
	PW_ACTION_OFF
	PW_ACTION_TOGGLE
	PW_ACTION_BUTTONPRESS
)
View Source
const PIN_OFF = 0x00
View Source
const PIN_ON = 0x01

Variables

This section is empty.

Functions

This section is empty.

Types

type PinChangedEvent

type PinChangedEvent struct {
	Pin       string
	LastValue int
	Value     int
	// contains filtered or unexported fields
}

func (PinChangedEvent) Source

func (p PinChangedEvent) Source() interface{}

type PinChangedFunc

type PinChangedFunc func(ev PinChangedEvent) error

type PinFuncs

type PinFuncs struct {
	Write PinWriteFunc
	Read  PinReadFunc
}

type PinReadFunc

type PinReadFunc func(pin string) (int, error)

type PinWatcher

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

func NewPinWatcher

func NewPinWatcher(pin string, watchFuncs *WatchFuncs) *PinWatcher

NewPinWatcher creates a new watcher for value changes of GPIO pins

func (*PinWatcher) Name

func (w *PinWatcher) Name() string

Name returns the name of the watcher and can be used in change handlers, for logging etc

func (*PinWatcher) Observe

func (w *PinWatcher) Observe() error

Observe must be called periodically by the owner and detects changes in state.

func (*PinWatcher) Pin

func (w *PinWatcher) Pin() string

Pin returns the pin number

func (*PinWatcher) SetName

func (w *PinWatcher) SetName(name string)

SetName sets the name of the watcher and can be used in change handlers, for logging etc

type PinWriteAction

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

func NewPinWriteAction

func NewPinWriteAction(pin string, action int, pinFuncs *PinFuncs) *PinWriteAction

func (PinWriteAction) Action

func (ac PinWriteAction) Action() int

Action returns the action constant (i.e. PWACTION_TOGGLE etc)

func (*PinWriteAction) Execute

func (ac *PinWriteAction) Execute() error

Execute is called by the owner when an acton on a pin must be executed

func (PinWriteAction) Pin

func (ac PinWriteAction) Pin() string

Pin returns the pin number

type PinWriteFunc

type PinWriteFunc func(pin string, val byte) error

type WatchFuncs

type WatchFuncs struct {
	Read    PinReadFunc
	Changed PinChangedFunc
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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