Skip to content

Commit

Permalink
working GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatOneRuffian committed Jan 15, 2024
1 parent ea21f97 commit 5a92640
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 105 deletions.
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Go parameters
GOCMD = go
GOBUILD = $(GOCMD) build
GOCLEAN = $(GOCMD) clean
GOTEST = $(GOCMD) test
GOGET = $(GOCMD) get
RESOURCES = "resources"
BINARY_NAME = monalisa
MAIN_PATH = .

all: test build

build:
fyne bundle -o bundled.go $(RESOURCES)
CGO_ENABLED=1 $(GOBUILD) -o $(BINARY_NAME) $(MAIN_PATH)

test:
$(GOTEST) -v ./...

clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)

run:
$(GOBUILD) -o $(BINARY_NAME) $(MAIN_PATH)
./$(BINARY_NAME)

.PHONY: all build test clean bundle run
36 changes: 6 additions & 30 deletions gui/window.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gui

import (
"fmt"
"image/color"
"strconv"
"tempgo/util"
Expand All @@ -19,7 +18,7 @@ import (

type metronomeGUI struct {
fyneTitle string
InputChanTime chan time.Time // input for GUI tempo button
InputChanTime chan time.Time
PlayMetronomeChan chan bool
PauseMetronomeChan chan bool
UpdateMetronomeChan chan bool
Expand Down Expand Up @@ -51,8 +50,8 @@ type metronomeStats struct {
var TempgoFyneApp metronomeGUI
var TempgoStatData metronomeStats

func init() {
//init tempgo window elements
func InitWindowReources(qnoteIcon *fyne.StaticResource, playIcon *fyne.StaticResource, pauseIcon *fyne.StaticResource) {
// init tempgo window elements
TempgoFyneApp.fyneTitle = "Tempgo"
TempgoFyneApp.InputChanTime = make(chan time.Time)
TempgoFyneApp.PlayMetronomeChan = make(chan bool)
Expand All @@ -64,12 +63,7 @@ func init() {

// create taskbar icon
if desk, ok := TempgoFyneApp.fyneApp.(desktop.App); ok {
trayIcon, err := fyne.LoadResourceFromPath("./resources/quarter_note.png")
if err != nil {
fmt.Println("Could not load tray icon")
} else {
TempgoFyneApp.fyneApp.SetIcon(trayIcon)
}
TempgoFyneApp.fyneApp.SetIcon(qnoteIcon)

m := fyne.NewMenu("Tempgo",
fyne.NewMenuItem("Show", func() {
Expand All @@ -82,34 +76,18 @@ func init() {
TempgoFyneApp.FyneWindow.Hide()
})

//todo need to query capture devices to populate this list and then add a selection event
availableInputDevs, err := util.GetInputDevices()
if err != nil {
fmt.Println("Could Not Query Input Devices.")
util.Log("Could Not Query Input Devices.")
}

inputDevSelect := widget.NewSelect(availableInputDevs, func(value string) {
TempgoFyneApp.UpdateInputDevChan <- value
})

// allocate button resources
btnIcon, err := fyne.LoadResourceFromPath("./resources/quarter_note.png")
if err != nil {
fmt.Println("Unable to load button image resource.")
}

playIcon, err := fyne.LoadResourceFromPath("./resources/play_icon.png")
if err != nil {
fmt.Println("Unable to load button image resource.")
}

pauseIcon, err := fyne.LoadResourceFromPath("./resources/pause_icon.png")
if err != nil {
fmt.Println("Unable to load button image resource.")
}

// init gui buttons
TempgoFyneApp.tempoInputBtn = widget.NewButtonWithIcon("", btnIcon, func() {
TempgoFyneApp.tempoInputBtn = widget.NewButtonWithIcon("", qnoteIcon, func() {
TempgoFyneApp.InputChanTime <- time.Now()
})

Expand Down Expand Up @@ -217,12 +195,10 @@ func IntArrayToString(intArray [10]int) string {
// Concatenate the string representations
result := "["
for index, str := range strArray {

result += str
if index != len(strArray)-1 {
result += ", "
}

}
result = result + "]"

Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import (
func init() {

//InputFile = CurrentCapDevice.PromptCMDInputSelect() // CLI mode
gui.InitWindowReources(resourceQuarternotePng, resourcePlayiconPng, resourcePauseiconPng)

Check failure on line 11 in main.go

View workflow job for this annotation

GitHub Actions / build

undefined: resourceQuarternotePng

Check failure on line 11 in main.go

View workflow job for this annotation

GitHub Actions / build

undefined: resourcePlayiconPng

Check failure on line 11 in main.go

View workflow job for this annotation

GitHub Actions / build

undefined: resourcePauseiconPng

// initial metronome core
go tempo.MainMetronome.StartMetronome()

}

func main() {
// start input select monitor
tempo.CreateNewDevMonitor()
// show and run fyne app
gui.TempgoFyneApp.FyneWindow.ShowAndRun()

// time consuming but possible features
// todo need another way to give user permission to input without allowing all userspace to access

// todo
// need to embed images into app

// notes
// make note that only non-zero averages are used
}
103 changes: 51 additions & 52 deletions tempo/captureDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type BpmCaptureDevice struct {
FirstRun bool
}

func CreateNewCaptureDev() *BpmCaptureDevice {
func CreateNewDevMonitor() *BpmCaptureDevice {
var newCapDev BpmCaptureDevice
newCapDev.FirstRun = true
go newCapDev.StartInputEventListeners()
go newCapDev.AttachInputStream()
go newCapDev.attachInputStream()
return &newCapDev
}

Expand Down Expand Up @@ -62,26 +62,27 @@ func (cap *BpmCaptureDevice) StartInputEventListeners() {
}()
}

func (cap *BpmCaptureDevice) AttachInputStream() {
func (cap *BpmCaptureDevice) attachInputStream() {

var monitoring bool
var capDevFile *os.File
stopSignal := make(chan bool)
stopSignal := make(chan bool, 1)
go func() {
for {
select {
case inputDevice := <-gui.TempgoFyneApp.UpdateInputDevChan:
fmt.Println("got channel info", inputDevice)

// close current device if open and not same
if capDevFile != nil {
if inputDevice != capDevFile.Name() {
// stop current monitoring loops
if monitoring {
monitoring = false
stopSignal <- true
capDevFile.Close()
} else {
//reprompt for input key?
capDevFile = nil
}
}

// if nil capFile then init provided device
if capDevFile == nil {
// open new provided device
var err error
Expand All @@ -91,55 +92,52 @@ func (cap *BpmCaptureDevice) AttachInputStream() {
// this should prob send a dialog box or something
continue
}
// dialog here
form := dialog.NewForm("Test Dialog", "This is my confirm message", "dismiss", nil, func(a bool) {}, gui.TempgoFyneApp.FyneWindow)

// show informational dialog to end-user
form := dialog.NewInformation("Set Input Key", "Press Any Key on the Select Device to Monitor", gui.TempgoFyneApp.FyneWindow)

// set input monitor button
// todo should have a time out or something here - revert to nil
fmt.Println("Press Button to Monitor")
go form.Show()
form.Show()
for {
cap.CurrentCaptureBtn = 255
_, eventCode, _ := readEventData(capDevFile)
eventType, eventCode, eventValue := readEventData(capDevFile)
if cap.CurrentCaptureBtn == 255 && eventCode != 4 {
cap.CurrentCaptureBtn = eventCode
form.Hide()
if monitoring {
stopSignal <- true
}
break
//util.ClearTerminal()
// enter metronome monitor loop
} else if eventType == 0 && eventValue == 0 { // filter key release events
//fmt.Printf("Key Release: Code=%d\n", eventCode)
} else {
fmt.Println("Oh")
fmt.Println("Issue determining key event?")
fmt.Println(eventType, eventCode, eventValue)
}
}
}

// spawn new go-thread for monitoring this specific input device
go func(stopSig chan bool) {
monitoring = true
for {
// break out of current loop to create a new non-blocking loop
if len(stopSig) > 0 {
<-stopSig
monitoring = false
break
}

eventType, eventCode, eventValue := readEventData(capDevFile)
if eventCode == cap.CurrentCaptureBtn {
// check for key press and release events
if eventType == 1 && eventValue == 1 { // key press event
// send input timestamp to metronome
MainMetronome.inputCompare.inputSignalTime <- time.Now()
go cap.printStats()

} else if eventType == 0 && eventValue == 0 { // key release event
//fmt.Printf("Key Release: Code=%d\n", eventCode)
if capDevFile != nil {
go func() {
monitoring = true
for {
// break out of current loop on sig or nil cap
if len(stopSignal) > 0 || capDevFile == nil {
<-stopSignal
break
}
// monitor key presses live
eventType, eventCode, eventValue := readEventData(capDevFile)
if eventCode == cap.CurrentCaptureBtn {
// check for key press and release events
if eventType == 1 && eventValue == 1 { // key press event
// send input timestamp to metronome
MainMetronome.inputCompare.inputSignalTime <- time.Now()
go cap.printStats()
}
}
}
}
}(stopSignal)
}()
}
}
}
}()
Expand Down Expand Up @@ -205,7 +203,14 @@ func (cap *BpmCaptureDevice) printStats() {
} else {
inputOffset = "Start Metronome to Begin"
}
gui.TempgoStatData.RawInputArrayCV.Set(fmt.Sprintf("%.3f", cov))
// format front-end vars
covText := fmt.Sprintf("%.3f", cov)
if covText == "NaN" {
covText = "0"
}

// set front-end vars
gui.TempgoStatData.RawInputArrayCV.Set(covText)
gui.TempgoStatData.MetronomeInputOffset.Set(inputOffset)
gui.TempgoStatData.RawInputArray.Set(gui.IntArrayToString(cap.bpmSamples))
gui.TempgoStatData.AverageBPM.Set(fmt.Sprintf("%d BPM", avgBpm))
Expand All @@ -226,7 +231,6 @@ func readEventData(capDevFile *os.File) (eventType uint16, eventCode uint16, eve
var eventBytes [eventSize]byte
_, err := capDevFile.Read(eventBytes[:])
if err != nil {
fmt.Println("Error reading from input event device:", err)
return
}

Expand Down Expand Up @@ -255,8 +259,7 @@ func (cap *BpmCaptureDevice) PromptCMDInputSelect() *os.File {
availableDevices, enumErr := util.GetInputDevices()

if enumErr != nil {
fmt.Println("Could Not Enumerate Input Devices.")
os.Exit(0)
util.LogFatal("Could Not Enumerate Input Devices.")
}
util.ClearTerminal()

Expand Down Expand Up @@ -289,17 +292,13 @@ func (cap *BpmCaptureDevice) PromptCMDInputSelect() *os.File {
}
}
} else {
fmt.Println("No Input Capture Devices Found, Exiting.")
os.Exit(0)
util.LogFatal("No Input Capture Devices Found, Exiting.")
}
// open selected input device
fmt.Println(cap.CurrentCaptureDevice)
fmt.Println("hello")

capDevFile, err := os.Open(cap.CurrentCaptureDevice)

if err != nil {
fmt.Println("Error opening input event device:", err)
util.Log("Error opening input event device:", err)
return nil
}

Expand Down
Loading

0 comments on commit 5a92640

Please sign in to comment.