Skip to content

Commit

Permalink
Refactor: separate hardware initialization from koebiten
Browse files Browse the repository at this point in the history
  • Loading branch information
sago35 committed Nov 8, 2024
1 parent f6daf9a commit b7268ef
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 40 deletions.
3 changes: 3 additions & 0 deletions games/all/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (
"github.com/sago35/koebiten/games/blocks/blocks"
"github.com/sago35/koebiten/games/flappygopher/flappygopher"
"github.com/sago35/koebiten/games/jumpingopher/jumpingopher"
"github.com/sago35/koebiten/hardware"
)

func main() {
koebiten.SetHardware(hardware.Device)

menu := NewMenu()

menu.AddGames([]Game{
Expand Down
2 changes: 2 additions & 0 deletions games/blocks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (

"github.com/sago35/koebiten"
"github.com/sago35/koebiten/games/blocks/blocks"
"github.com/sago35/koebiten/hardware"
)

func main() {
koebiten.SetHardware(hardware.Device)
koebiten.SetRotation(koebiten.Rotation90)
koebiten.SetWindowSize(64, 128)
koebiten.SetWindowTitle("Tetris in Go")
Expand Down
2 changes: 2 additions & 0 deletions games/flappygopher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (

"github.com/sago35/koebiten"
"github.com/sago35/koebiten/games/flappygopher/flappygopher"
"github.com/sago35/koebiten/hardware"
)

func main() {
koebiten.SetHardware(hardware.Device)
koebiten.SetWindowSize(128, 64)
koebiten.SetWindowTitle("Flappy Gopher")

Expand Down
2 changes: 2 additions & 0 deletions games/jumpingopher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (

"github.com/sago35/koebiten"
"github.com/sago35/koebiten/games/jumpingopher/jumpingopher"
"github.com/sago35/koebiten/hardware"
)

func main() {
koebiten.SetHardware(hardware.Device)
koebiten.SetWindowSize(128, 64)
koebiten.SetWindowTitle("Jumpin Gopher")
game := jumpingopher.NewGame()
Expand Down
7 changes: 7 additions & 0 deletions hardware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package koebiten

type Hardware interface {
Init() error
GetDisplay() Displayer
KeyUpdate() error
}
58 changes: 41 additions & 17 deletions machine.go → hardware/zero-kb02.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
//go:build tinygo && !macropad_rp2040
//go:build tinygo

package koebiten
package hardware

import (
"machine"

"github.com/sago35/koebiten"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/encoders"
"tinygo.org/x/drivers/ssd1306"
)

var Device = ZERO_KB02{}

type ZERO_KB02 struct {
}

func (z ZERO_KB02) Init() error {
return Init()
}

func (z ZERO_KB02) GetDisplay() koebiten.Displayer {
return Display
}

func (z ZERO_KB02) KeyUpdate() error {
return keyUpdate()
}

var (
Display *ssd1306.Device
)

var (
colPins []machine.Pin
rowPins []machine.Pin
Expand Down Expand Up @@ -46,7 +68,7 @@ func (a ADCDevice) Get() bool {
return a.PressedFunc()
}

func init() {
func Init() error {
machine.InitADC()
ax := machine.ADC{Pin: machine.GPIO29}
ay := machine.ADC{Pin: machine.GPIO28}
Expand Down Expand Up @@ -75,7 +97,7 @@ func init() {
})
d.SetRotation(drivers.Rotation180)
d.ClearDisplay()
display = &d
Display = &d

gpioPins = []machine.Pin{
machine.GPIO2, // rotary
Expand Down Expand Up @@ -129,13 +151,15 @@ func init() {
cycle = make([]int, len(colPins)*len(rowPins)+len(gpioPins)+len(rotaryPins)+len(adcPins))
duration = make([]int, len(colPins)*len(rowPins)+len(gpioPins)+len(rotaryPins)+len(adcPins))

return nil
}

func keyUpdate() {
func keyUpdate() error {
keyGpioUpdate()
keyRotaryUpdate()
keyMatrixUpdate()
keyJoystickUpdate()
return nil
}

func keyGpioUpdate() {
Expand All @@ -157,9 +181,9 @@ func keyGpioUpdate() {
}
case NoneToPress:
state[idx] = Press
AppendJustPressedKeys([]Key{Key(idx)})
koebiten.AppendJustPressedKeys([]koebiten.Key{koebiten.Key(idx)})
case Press:
AppendPressedKeys([]Key{Key(idx)})
koebiten.AppendPressedKeys([]koebiten.Key{koebiten.Key(idx)})
if current {
cycle[idx] = 0
duration[idx]++
Expand All @@ -174,7 +198,7 @@ func keyGpioUpdate() {
}
case PressToRelease:
state[idx] = None
AppendJustReleasedKeys([]Key{Key(idx)})
koebiten.AppendJustReleasedKeys([]koebiten.Key{koebiten.Key(idx)})
}
}
}
Expand Down Expand Up @@ -204,9 +228,9 @@ func keyRotaryUpdate() {
} else {
state[idx] = PressToRelease
}
AppendJustPressedKeys([]Key{Key(idx)})
koebiten.AppendJustPressedKeys([]koebiten.Key{koebiten.Key(idx)})
case Press:
AppendPressedKeys([]Key{Key(idx)})
koebiten.AppendPressedKeys([]koebiten.Key{koebiten.Key(idx)})
if current {
} else {
state[idx] = PressToRelease
Expand All @@ -217,7 +241,7 @@ func keyRotaryUpdate() {
} else {
state[idx] = None
}
AppendJustReleasedKeys([]Key{Key(idx)})
koebiten.AppendJustReleasedKeys([]koebiten.Key{koebiten.Key(idx)})
}
}
}
Expand All @@ -244,9 +268,9 @@ func keyMatrixUpdate() {
}
case NoneToPress:
state[idx] = Press
AppendJustPressedKeys([]Key{Key(idx)})
koebiten.AppendJustPressedKeys([]koebiten.Key{koebiten.Key(idx)})
case Press:
AppendPressedKeys([]Key{Key(idx)})
koebiten.AppendPressedKeys([]koebiten.Key{koebiten.Key(idx)})
if current {
cycle[idx] = 0
duration[idx]++
Expand All @@ -261,7 +285,7 @@ func keyMatrixUpdate() {
}
case PressToRelease:
state[idx] = None
AppendJustReleasedKeys([]Key{Key(idx)})
koebiten.AppendJustReleasedKeys([]koebiten.Key{koebiten.Key(idx)})
}

colPins[c].Low()
Expand Down Expand Up @@ -289,9 +313,9 @@ func keyJoystickUpdate() {
}
case NoneToPress:
state[idx] = Press
AppendJustPressedKeys([]Key{Key(idx)})
koebiten.AppendJustPressedKeys([]koebiten.Key{koebiten.Key(idx)})
case Press:
AppendPressedKeys([]Key{Key(idx)})
koebiten.AppendPressedKeys([]koebiten.Key{koebiten.Key(idx)})
if current {
cycle[idx] = 0
duration[idx]++
Expand All @@ -306,7 +330,7 @@ func keyJoystickUpdate() {
}
case PressToRelease:
state[idx] = None
AppendJustReleasedKeys([]Key{Key(idx)})
koebiten.AppendJustReleasedKeys([]koebiten.Key{koebiten.Key(idx)})
}
}
}
12 changes: 12 additions & 0 deletions koebiten.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ var (
black = color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xFF}
)

var keyUpdate = func() error { return nil }

func init() {
pngBuffer = map[string]pixel.Image[pixel.Monochrome]{}
}
Expand Down Expand Up @@ -104,6 +106,16 @@ func SetWindowSize(w, h int) {
func SetWindowTitle(title string) {
}

func SetHardware(h Hardware) error {
err := h.Init()
if err != nil {
return err
}
display = h.GetDisplay()
keyUpdate = h.KeyUpdate
return nil
}

// SetRotation sets the display rotation mode.
// If the display is already a RotatedDisplay, it updates the mode.
// Otherwise, it wraps the existing display in a new RotatedDisplay with the specified mode.
Expand Down
23 changes: 0 additions & 23 deletions machine_macropad_rp2040.go

This file was deleted.

0 comments on commit b7268ef

Please sign in to comment.