switchprocontroller

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2020 License: MIT Imports: 5 Imported by: 1

README

Nintendo Switch Pro Controller

PkgGoDev golangci-lint Go Report Card

This drivers allows to read inputs from a Nintendo Switch Pro Controller connected using Bluetooth

Requirements

  • a Nintendo Switch Pro Controller connected to a Raspberry Pi using Bluetooth

Documentation

For full documentation, please visit PkgGoDev

Quick start

import (
	"fmt"

	"github.com/raspberrypi-go-drivers/switchprocontroller"
)

func main() {
	controller := switchprocontroller.NewSwitchProController()
	controller.StartListener(0)
	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
		}
	}
}

Raspberry Pi compatibility

This driver has has only been tested on an Raspberry Pi Zero WH using integrated bluetooth but may work well on other Raspberry Pi having integrated Bluetooth

License

MIT License

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Button

type Button struct {
	Name  string
	State int
	// contains filtered or unexported fields
}

Button represent a physical button

There's two possible State 1: button pressed 0: button released

type Event

type Event struct {
	Stick  *Stick
	Button *Button
}

Event is an event

type Stick

type Stick struct {
	Name string
	X    float32
	Y    float32
	// contains filtered or unexported fields
}

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())
	}
}

Jump to

Keyboard shortcuts

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