summaryrefslogtreecommitdiff
path: root/cmd/dolmetschctld/statemachine.go
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2019-02-09 22:03:15 +0100
committerChristian Pointner <equinox@spreadspace.org>2019-02-09 22:03:15 +0100
commit7672e8d60586129e8f4f9c718d1d62f3c9c0b729 (patch)
tree1245e9010d245d83cbb27eb2e63fe88b62d544e6 /cmd/dolmetschctld/statemachine.go
parentadded telnet control interface (diff)
language selection now returns result
Diffstat (limited to 'cmd/dolmetschctld/statemachine.go')
-rw-r--r--cmd/dolmetschctld/statemachine.go92
1 files changed, 79 insertions, 13 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go
index c388d24..2aba4bb 100644
--- a/cmd/dolmetschctld/statemachine.go
+++ b/cmd/dolmetschctld/statemachine.go
@@ -23,34 +23,92 @@
package main
import (
- "time"
+ "errors"
+ "log"
"spreadspace.org/dolmetschctl/pkg/mixer"
)
+type State int
+
+const (
+ StateNew = iota
+ StateSettling
+ StateSettled
+)
+
+func (s State) String() string {
+ switch s {
+ case StateNew:
+ return "new"
+ case StateSettling:
+ return "settling"
+ case StateSettled:
+ return "settled"
+ default:
+ return "unknown"
+ }
+}
+
type Language string
+type LanguageChannels struct {
+ main mixer.Channel
+ voice mixer.Channel
+}
+
+type selectLangRequest struct {
+ l Language
+ resultCh chan error
+}
+
type StateMachine struct {
mixer *mixer.Mixer
- selectLangCh chan Language
+ selectLangCh chan selectLangRequest
quitCh chan bool
exitedCh chan struct{}
+
+ languages map[Language]LanguageChannels
+
+ currentState State
+ currentLang Language
+ targetLang Language
}
func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) {
sm := &StateMachine{mixer: m}
- sm.selectLangCh = make(chan Language, 10)
+ sm.selectLangCh = make(chan selectLangRequest, 10)
sm.quitCh = make(chan bool, 1)
sm.exitedCh = make(chan struct{})
+
+ sm.languages = make(map[Language]LanguageChannels)
+ sm.languages["en"] = LanguageChannels{0x20, 0x24}
+ sm.languages["de"] = LanguageChannels{0x22, 0x25}
+
+ sm.currentState = StateNew
+ sm.currentLang = ""
+ sm.targetLang = ""
+
return sm, nil
}
-func (sm *StateMachine) selectLang(l Language) {
- sm.mixer.UnMute(0x24)
- sm.mixer.SetLevel(0x24, 42)
- time.Sleep(2 * time.Second)
- sm.mixer.Mute(0x24)
- sm.mixer.SetLevel(0x24, mixer.FaderLevel0db)
+func (sm *StateMachine) selectLang(l Language) error {
+ if l != "" {
+ if _, exists := sm.languages[l]; !exists {
+ return errors.New("language '" + string(l) + "' does not exist")
+ }
+ }
+ sm.targetLang = l
+ log.Printf("new target language: '%s'", sm.targetLang)
+ return nil
+}
+
+func (sm *StateMachine) reconcile() {
+ if sm.targetLang == sm.currentLang {
+ sm.currentState = StateSettled
+ return
+ }
+ sm.currentState = StateSettling
}
func (sm *StateMachine) run() {
@@ -58,11 +116,17 @@ func (sm *StateMachine) run() {
for {
select {
- case l := <-sm.selectLangCh:
- sm.selectLang(l)
+ case req := <-sm.selectLangCh:
+ req.resultCh <- sm.selectLang(req.l)
case <-sm.quitCh:
return
}
+
+ oldState := sm.currentState
+ sm.reconcile()
+ if oldState != sm.currentState {
+ log.Printf("new state: %s", sm.currentState)
+ }
}
}
@@ -70,8 +134,10 @@ func (sm *StateMachine) Start() {
go sm.run()
}
-func (sm *StateMachine) SelectLanguage(l Language) {
- sm.selectLangCh <- l
+func (sm *StateMachine) SelectLanguage(l Language) error {
+ resultCh := make(chan error)
+ sm.selectLangCh <- selectLangRequest{l, resultCh}
+ return <-resultCh
}
func (sm *StateMachine) Shutdown() {