Documentation
¶
Overview ¶
Package switchprocontroller is a driver allowing to read inputs from a Nintendo Switch Pro controller
This package assumes you already have your Nintendo Switch Pro controller connected using bluetooth
Example (Blocking) ¶
package main import ( "fmt" "time" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } for { select { case event := <-controller.Events: if event.Button != nil { fmt.Printf("%s:%d\n", event.Button.Name, event.Button.State) } else if event.Stick != nil { fmt.Printf("%s - Y:%f X:%f\n", event.Stick.Name, event.Stick.Y, event.Stick.X) } case <-time.After(60 * time.Second): fmt.Println("timeout") return } } }
Example (NonBlocking) ¶
package main import ( "fmt" "time" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } for { // display button A state aState, _ := controller.GetButtonState("a") fmt.Printf("A:%d\n", aState) // display left stick position leftStick, _ := controller.GetStick("left") fmt.Printf("x:%f - y:%f\n", leftStick.X, leftStick.Y) time.Sleep(100 * time.Millisecond) } }
Output: A:0 x:0.000000 - y:0.000000 [...]
Index ¶
- type Button
- type Event
- type Stick
- type SwitchProController
- func (controller *SwitchProController) Display()
- func (controller *SwitchProController) GetButton(name string) (*Button, error)
- func (controller *SwitchProController) GetButtonState(name string) (int, error)
- func (controller *SwitchProController) GetStick(name string) (*Stick, error)
- func (controller *SwitchProController) StartListener(joystickID int) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Button ¶
Button represent a physical button
There's two possible State 1: button pressed 0: button released
type Stick ¶
Stick represent a physical stick
It contains a value (in %) for each axis (x and y), this value is contained between -100 and 100
0 is the default value when the stick is in the default position
type SwitchProController ¶
type SwitchProController struct { // each time a new event is received, true is sent to this channel Events chan *Event `json:"-"` // Sticks list Sticks []*Stick // Buttons list Buttons []*Button // contains filtered or unexported fields }
SwitchProController represent the physical controller
func NewSwitchProController ¶
func NewSwitchProController() *SwitchProController
NewSwitchProController creates and initialize a SwitchProController instance
Example ¶
package main import ( "fmt" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } }
func (*SwitchProController) Display ¶
func (controller *SwitchProController) Display()
Display pprint the current controller status
func (*SwitchProController) GetButton ¶
func (controller *SwitchProController) GetButton(name string) (*Button, error)
GetButton returns a pointer to a Button instance with specified name
err not nil if button name not found
Example (Pressed) ¶
package main import ( "fmt" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } aButton, _ := controller.GetButton("a") fmt.Printf("name:%s - state:%d\n", aButton.Name, aButton.State) }
Output: name:a - state:1
Example (Released) ¶
package main import ( "fmt" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } aButton, _ := controller.GetButton("a") fmt.Printf("name:%s - state:%d\n", aButton.Name, aButton.State) }
Output: name:a - state:0
func (*SwitchProController) GetButtonState ¶
func (controller *SwitchProController) GetButtonState(name string) (int, error)
GetButtonState returns the state of the button with specified name
returns: 0: button released 1: button pressed
err not nil if button name not found
Example (Pressed) ¶
package main import ( "fmt" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } aState, _ := controller.GetButtonState("a") fmt.Printf("state:%d\n", aState) }
Output: state:1
Example (Released) ¶
package main import ( "fmt" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } aState, _ := controller.GetButtonState("a") fmt.Printf("state:%d\n", aState) }
Output: state:0
func (*SwitchProController) GetStick ¶
func (controller *SwitchProController) GetStick(name string) (*Stick, error)
GetStick returns a pointer to a Stick instance with specified name
err not nil if stick name not found
Example ¶
package main import ( "fmt" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } leftStick, _ := controller.GetStick("left") fmt.Printf("x:%f - y:%f\n", leftStick.X, leftStick.Y) }
Output: x:0.000000 - y:0.000000
func (*SwitchProController) StartListener ¶
func (controller *SwitchProController) StartListener(joystickID int) error
StartListener starts listening for controller inputs and keep the controller instance up to date
Example ¶
package main import ( "fmt" "github.com/bbayszczak/raspberrypi-go-drivers/switchprocontroller" ) func main() { controller := switchprocontroller.NewSwitchProController() if err := controller.StartListener(0); err != nil { fmt.Printf("impossible to start listener: %s", err.Error()) } }