summaryrefslogtreecommitdiff
path: root/cmd/dolmetschctld/statemachine.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/dolmetschctld/statemachine.go')
-rw-r--r--cmd/dolmetschctld/statemachine.go34
1 files changed, 24 insertions, 10 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go
index 5e27359..c7f4ac5 100644
--- a/cmd/dolmetschctld/statemachine.go
+++ b/cmd/dolmetschctld/statemachine.go
@@ -94,6 +94,10 @@ type getMain2VoiceRatioRequest struct {
resultCh chan float32
}
+type getStateRequest struct {
+ resultCh chan State
+}
+
type StateMachine struct {
mixer *mixer.Mixer
@@ -101,6 +105,7 @@ type StateMachine struct {
getLanguageCh chan getLanguageRequest
setMain2VoiceRatioCh chan setMain2VoiceRatioRequest
getMain2VoiceRatioCh chan getMain2VoiceRatioRequest
+ getStateCh chan getStateRequest
quitCh chan bool
exitedCh chan struct{}
mixerEventCh chan mixer.Event
@@ -110,8 +115,8 @@ type StateMachine struct {
main2VoiceRatio float32
- currentState State
- language Language
+ state State
+ language Language
}
func (sm *StateMachine) handleMixerEvent(ev mixer.Event) {
@@ -157,7 +162,7 @@ func (sm *StateMachine) initMixer() {
mcs.voice.target.level = mixer.FaderLevelOff
}
sm.language = ""
- sm.currentState = StateSettled
+ sm.state = StateSettled
}
// the "current language" is what is currently spoken on stage
@@ -207,18 +212,18 @@ func (sm *StateMachine) reconcile(ticker bool) {
}
}
- if sm.currentState != StateSettled && !ticker {
+ if sm.state != StateSettled && !ticker {
return
}
- sm.currentState = StateSettled
+ sm.state = StateSettled
for _, mcs := range sm.languages {
if mcs.main.target.level != mcs.main.current.level {
sm.mixer.SetLevel(mcs.main.num, calcNextLevel(mcs.main.target.level, mcs.main.current.level))
- sm.currentState = StateSettling
+ sm.state = StateSettling
}
if mcs.voice.target.level != mcs.voice.current.level {
sm.mixer.SetLevel(mcs.voice.num, calcNextLevel(mcs.voice.target.level, mcs.voice.current.level))
- sm.currentState = StateSettling
+ sm.state = StateSettling
}
}
}
@@ -231,7 +236,7 @@ func (sm *StateMachine) run() {
for {
select {
case <-t.C:
- if sm.currentState == StateSettling {
+ if sm.state == StateSettling {
sm.reconcile(true)
}
case req := <-sm.setLanguageCh:
@@ -244,6 +249,8 @@ func (sm *StateMachine) run() {
sm.reconcile(false)
case req := <-sm.getMain2VoiceRatioCh:
req.resultCh <- sm.main2VoiceRatio
+ case req := <-sm.getStateCh:
+ req.resultCh <- sm.state
case ev := <-sm.mixerEventCh:
sm.handleMixerEvent(ev)
sm.reconcile(false)
@@ -259,6 +266,7 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) {
sm.getLanguageCh = make(chan getLanguageRequest, 10)
sm.setMain2VoiceRatioCh = make(chan setMain2VoiceRatioRequest, 10)
sm.getMain2VoiceRatioCh = make(chan getMain2VoiceRatioRequest, 10)
+ sm.getStateCh = make(chan getStateRequest, 10)
sm.quitCh = make(chan bool, 1)
sm.exitedCh = make(chan struct{})
sm.mixerEventCh = make(chan mixer.Event, 1000)
@@ -266,14 +274,14 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) {
sm.languages = make(map[Language]*MixerChannels)
sm.channel2lang = make(map[mixer.Channel]Language)
- sm.currentState = StateNew
+ sm.state = StateNew
sm.language = ""
return sm, nil
}
// TODO: currently we can only deal with 2 languages...
func (sm *StateMachine) AddLanguage(name Language, main, voice mixer.Channel) error {
- if sm.currentState != StateNew {
+ if sm.state != StateNew {
return fmt.Errorf("adding languages is only allowed during startup")
}
if name == "none" {
@@ -331,6 +339,12 @@ func (sm *StateMachine) GetMain2VoiceRatio() float32 {
return <-resultCh
}
+func (sm *StateMachine) GetState() State {
+ resultCh := make(chan State)
+ sm.getStateCh <- getStateRequest{resultCh}
+ return <-resultCh
+}
+
func (sm *StateMachine) Shutdown() {
select {
case sm.quitCh <- true: