Browse Source

Fixed some issues

Robin de Jong 1 year ago
parent
commit
b8960167b1
2 changed files with 64 additions and 11 deletions
  1. 46 9
      controller/controller.go
  2. 18 2
      main.go

+ 46 - 9
controller/controller.go

@@ -4,6 +4,8 @@ import (
 	"github.com/karalabe/hid"
 	"strings"
 	"log"
+	"time"
+	"github.com/pkg/errors"
 )
 
 type Button int
@@ -51,6 +53,7 @@ func (c *Controller) GetButtons(bytes []byte) []Button {
 
 type Controller struct {
 	device *hid.Device
+	id string
 
 	press func([]Button)
 	release func([]Button)
@@ -59,17 +62,26 @@ type Controller struct {
 
 func NewController(id string) *Controller {
 	controller := new(Controller)
+	controller.id = id
 
-	info := controller.getDevice(id)
 	var err error
-	controller.device, err = info.Open()
+	controller.device, err = controller.readDevice(id)
 	if err != nil {
-		log.Fatal(err)
+		log.Println("ERROR:", err)
 	}
 
 	return controller
 }
 
+func (c *Controller) readDevice(id string) (*hid.Device, error){
+	info := c.getDevice(id)
+	if info == nil {
+		return nil, errors.New("device not found")
+	}
+	device, err := info.Open()
+	return device, err
+}
+
 func (c *Controller) getDevice(str string) *hid.DeviceInfo {
 	devices := hid.Enumerate(0, 0)
 	for _, device := range devices {
@@ -158,14 +170,32 @@ func (c *Controller) Changed(buttons []Button) bool {
 	return false
 }
 
-func (c *Controller) Run() {
-	for {
-		bytes := make([]byte, 1024)
-		_, err := c.device.Read(bytes)
-		if err != nil {
-			log.Fatal(err)
+// Attempt to read the device, if the device was disconnected it will produce an error.
+// Redo with a 100 ms timeout.
+func (c *Controller) getBytes() []byte {
+	//var err error
+	bytes := make([]byte, 1024)
+
+	var err error
+	if c.device != nil {
+		_, err = c.device.Read(bytes)
+	}
+
+	for err != nil || c.device == nil {
+		time.Sleep(100 * time.Millisecond)
+		c.device, err = c.readDevice(c.id)
+
+		if c.device != nil {
+			_, err = c.device.Read(bytes)
 		}
+	}
+
+	return bytes
+}
 
+func (c *Controller) Run() {
+	for {
+		bytes := c.getBytes()
 		buttons := c.GetButtons(bytes)
 
 		if c.buttons != nil {
@@ -175,6 +205,13 @@ func (c *Controller) Run() {
 			} else if len(buttons) < len(*c.buttons) {
 				diff := c.GetDifference(buttons, *c.buttons)
 				c.release(diff)
+			} else {
+				diff := c.GetDifference(buttons, *c.buttons)
+				if len(diff) > 0 {
+					c.release(*c.buttons)
+					c.press(buttons)
+					log.Println("diff:", diff, ", press: buttons:", buttons, "release; *c.buttons:",c.buttons)
+				}
 			}
 		}
 		c.buttons = &buttons

+ 18 - 2
main.go

@@ -12,8 +12,8 @@ var err error
 
 func main() {
 	// Socket to the Ledbanner NES server
-	//ws, err = gosocketio.Dial(gosocketio.GetUrl("100.64.0.72", 9998, false), transport.GetDefaultWebsocketTransport())
-	ws, err = gosocketio.Dial(gosocketio.GetUrl("127.0.0.1", 9998, false), transport.GetDefaultWebsocketTransport())
+	ws, err = gosocketio.Dial(gosocketio.GetUrl("100.64.0.72", 9998, false), transport.GetDefaultWebsocketTransport())
+	//ws, err = gosocketio.Dial(gosocketio.GetUrl("127.0.0.1", 9998, false), transport.GetDefaultWebsocketTransport())
 	if err != nil {
 		log.Fatal(err)
 	}
@@ -29,20 +29,28 @@ func press(btns []controller.Button) {
 	for _, btn := range btns {
 		switch btn {
 		case controller.ButtonA:
+			log.Println("Emit: button_down:a")
 			ws.Emit("button_down", "a")
 		case controller.ButtonB:
+			log.Println("Emit: button_down:b")
 			ws.Emit("button_down", "b")
 		case controller.ButtonSelect:
+			log.Println("Emit: button_down:select")
 			ws.Emit("button_down", "select")
 		case controller.ButtonStart:
+			log.Println("Emit: button_down:start")
 			ws.Emit("button_down", "start")
 		case controller.ButtonLeft:
+			log.Println("Emit: button_down:left")
 			ws.Emit("button_down", "left")
 		case controller.ButtonRight:
+			log.Println("Emit: button_down:right")
 			ws.Emit("button_down", "right")
 		case controller.ButtonUp:
+			log.Println("Emit: button_down:up")
 			ws.Emit("button_down", "up")
 		case controller.ButtonDown:
+			log.Println("Emit: button_down:down")
 			ws.Emit("button_down", "down")
 		}
 	}
@@ -52,20 +60,28 @@ func release(btns []controller.Button) {
 	for _, btn := range btns {
 		switch btn {
 		case controller.ButtonA:
+			log.Println("Emit: button_up:a")
 			ws.Emit("button_up", "a")
 		case controller.ButtonB:
+			log.Println("Emit: button_up:b")
 			ws.Emit("button_up", "b")
 		case controller.ButtonSelect:
+			log.Println("Emit: button_up:select")
 			ws.Emit("button_up", "select")
 		case controller.ButtonStart:
+			log.Println("Emit: button_up:start")
 			ws.Emit("button_up", "start")
 		case controller.ButtonLeft:
+			log.Println("Emit: button_up:left")
 			ws.Emit("button_up", "left")
 		case controller.ButtonRight:
+			log.Println("Emit: button_up:right")
 			ws.Emit("button_up", "right")
 		case controller.ButtonUp:
+			log.Println("Emit: button_up:up")
 			ws.Emit("button_up", "up")
 		case controller.ButtonDown:
+			log.Println("Emit: button_up:down")
 			ws.Emit("button_up", "down")
 		}
 	}