-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up code into modules, add actions file
- Loading branch information
Showing
6 changed files
with
223 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: GitHub Actions Demo | ||
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 | ||
on: [push] | ||
jobs: | ||
Explore-GitHub-Actions: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." | ||
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" | ||
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." | ||
- name: Check out repository code | ||
uses: actions/checkout@v3 | ||
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." | ||
- run: echo "🖥️ The workflow is now ready to test your code on the runner." | ||
- name: List files in the repository | ||
run: | | ||
ls ${{ github.workspace }} | ||
- run: echo "🍏 This job's status is ${{ job.status }}." |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package joystick | ||
|
||
import ( | ||
messages "../messages" | ||
"fmt" | ||
"github.com/donomii/goof" | ||
"github.com/go-gl/glfw/v3.3/glfw" | ||
) | ||
|
||
// Global flag to switch on steam deck features, such as correct button mapping | ||
var steamDeck = false | ||
|
||
// Default to a games controller | ||
var joystick_type = "F310" | ||
var latches = make(map[int]bool) | ||
var joystickHistory [][]float64 | ||
var joystickHistoryIndex int | ||
|
||
func Setup_joystick() { | ||
|
||
//Attempt to detect steam deck | ||
if goof.Exists("/sys/devices/virtual/dmi/id/board_vendor") { | ||
|
||
if goof.FileContains("/sys/devices/virtual/dmi/id/board_vendor", "Valve") || | ||
goof.FileContains("/sys/devices/virtual/dmi/id/board_vendor", "valve") { | ||
steamDeck = true | ||
joystick_type = "steamdeck" | ||
} | ||
} | ||
|
||
} | ||
|
||
func outOfDeadZone(deadZone, xdeflect, ydeflect float64) bool { | ||
|
||
return xdeflect > deadZone || xdeflect < -deadZone || ydeflect > deadZone || ydeflect < -deadZone | ||
} | ||
func DoJoystick() { | ||
deadZone := 0.2 | ||
joy := glfw.Joystick(0) | ||
if joy.Present() { | ||
|
||
//fmt.Println("Joystick 1 present. Axes: ", joy.GetAxes(), " Buttons: ", joy.GetButtons(), " Hats: ", joy.GetHats(), "Latches: ", latches) | ||
|
||
//Fetch button states | ||
buttons := joy.GetButtons() | ||
|
||
//Check for button presses | ||
for i := 0; i < len(buttons); i++ { | ||
|
||
butt := buttons[i] | ||
|
||
//If the button is available | ||
|
||
//If the button is pressed, and it wasn't pressed last time | ||
if butt == glfw.Press { | ||
|
||
if !latches[i] { | ||
latches[i] = true | ||
messages.SendMessage("Button", i) | ||
|
||
} | ||
} else { | ||
//Button is released, so reset the latch | ||
latches[i] = false | ||
} | ||
|
||
} | ||
if joystick_type == "steamdeck" { | ||
for i := 0; i < len(joy.GetAxes()); i = i + 3 { | ||
var ydeflect float64 = 0 | ||
var trigger float64 = 0 | ||
xdeflect := float64(joy.GetAxes()[i]) | ||
if i+1 < len(joy.GetAxes()) { | ||
ydeflect = float64(joy.GetAxes()[i+1]) | ||
} | ||
if i+2 < len(joy.GetAxes()) { | ||
trigger = float64(joy.GetAxes()[i+2]) | ||
} | ||
if outOfDeadZone(deadZone, xdeflect, ydeflect) { | ||
messages.SendMessage("JoystickY", ydeflect/-100) | ||
messages.SendMessage("JoystickX", xdeflect/-100) | ||
messages.SendMessage("JoystickTrigger", trigger) | ||
} | ||
/* | ||
if xdeflect > deadZone || xdeflect < -deadZone || ydeflect > deadZone || ydeflect < -deadZone { | ||
b.X = b.X + (xdeflect)*10.0 | ||
b.Y = b.Y + (ydeflect)*10.0 | ||
} | ||
*/ | ||
|
||
} | ||
|
||
//fmt.Printf("Axes: %+v, hats: %+v, buttons: %+v\n", joy.GetAxes(), joy.GetButtons(), joy.GetHats()) | ||
} | ||
|
||
if joystick_type == "F310" { | ||
for i := 0; i < len(joy.GetAxes()); i = i + 2 { | ||
var ydeflect float64 = 0 | ||
var trigger float64 = 0 | ||
xdeflect := float64(joy.GetAxes()[i]) | ||
if i+1 < len(joy.GetAxes()) { | ||
ydeflect = float64(joy.GetAxes()[i+1]) | ||
} | ||
if i+2 < len(joy.GetAxes()) { | ||
trigger = float64(joy.GetAxes()[i+2]) | ||
} | ||
if outOfDeadZone(deadZone, xdeflect, ydeflect) { | ||
messages.SendMessage("JoystickY", ydeflect/-100) | ||
messages.SendMessage("JoystickX", xdeflect/-100) | ||
messages.SendMessage("JoystickTrigger", trigger) | ||
} | ||
|
||
/* | ||
if xdeflect > deadZone || xdeflect < -deadZone || ydeflect > deadZone || ydeflect < -deadZone { | ||
b.X = b.X + (xdeflect)*10.0 | ||
b.Y = b.Y + (ydeflect)*10.0 | ||
} | ||
*/ | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package messages | ||
|
||
import ( | ||
"github.com/cornelk/hashmap" | ||
) | ||
|
||
//The message registry allows communication between modules. It supports dynamic and changing message handles, and is a solution to the common problems with import cycles and dependencies that other systems have. | ||
|
||
//The use is simple. You register a handler for a message, and then send the message. The handler will be called with the message name, an id, and the arguments. You provide the id when you register. The id is used to identify the handler, so you can remove it later. Registering another handler with the same id will replace the old handler. | ||
|
||
// The message registry is thread-safe, and can be used from multiple goroutines. However the handler will run in the same routine as the caller, so be careful about calling graphics functions from the handler. Many graphics libraries, such as OpenGL, are not thread-safe. | ||
|
||
//The message system is designed for notifications of events, less for passing large amounts of data, or for large numbers of events, or for complex execution flow. It works well for simple things like "the user has clicked on this button", or "the user has pressed this key", or "the user has moved the mouse". | ||
|
||
//Messages are never queued, they are delivered immediately. | ||
|
||
var MessageRegistry *hashmap.Map[string, *hashmap.Map[string, func(name , id string, args interface{})]] = hashmap.New[string, *hashmap.Map[string, func(name , id string, args interface{})]]() | ||
|
||
//Register your message handler with the message system. Name is the lookup name for the message, id is a free text field | ||
//that you can use to identify your handler, and handler is the function that will be called when the message is sent. | ||
// | ||
//There can be as many handlers as you want for a message. The id is used to identify the handler, so you can remove it later. | ||
//Registering another handler with the same id will replace the old handler. | ||
func Register( name string, id string, handler func(name , id string, args interface{})) { | ||
key_name := name | ||
ids,_ := MessageRegistry.Get(key_name) | ||
if ids == nil { | ||
ids = hashmap.New[string, func(name , id string, args interface{})]() | ||
MessageRegistry.Set(key_name, ids) | ||
} | ||
f,_ := ids.Get(id) | ||
if f == nil { | ||
ids := hashmap.New[string, func(name , id string, args interface{})]() | ||
MessageRegistry.Set(key_name, ids) | ||
} | ||
ids.Set(id, handler) | ||
MessageRegistry.Set(key_name, ids) | ||
} | ||
|
||
//Delete a handler. The name and id must be the same as the ones used to register the handler. | ||
func Unregister(name string, id string) { | ||
key_name := name | ||
ids,_ := MessageRegistry.Get(key_name) | ||
ids.Del(id) | ||
} | ||
|
||
//Send a message. The name is the lookup name for the message, and args is the data to be sent to the handler. Sendmessage calls the handler immediately (i.e. synchronously) | ||
func SendMessage( name string, args interface{}) { | ||
|
||
//log.Printf("SendMessage: %s, %v", name, args) | ||
ids, _ := MessageRegistry.Get(name) | ||
if ids == nil { | ||
//TODO some kind of warning that you are sending messages to a non existant message handler? | ||
return | ||
} | ||
ids.Range(func(key string, handler func(name , id string, args interface{})) bool { | ||
handler(name, key, args) | ||
return true | ||
}) | ||
|
||
} |