cmd/treatment: main.go updated to exactly what's on macquarie speaker

This commit is contained in:
trekhopton 2024-04-29 14:07:28 +09:30
parent b55961b089
commit 88ae00ede1
1 changed files with 11 additions and 5 deletions

View File

@ -154,7 +154,7 @@ func main() {
log.Debug("got cached config", "config", cfg) log.Debug("got cached config", "config", cfg)
// Set the volume from the cached value. // Set the volume from the cached value.
err = setVolume(cfg.Volume, bus) err = setVolume(cfg.Volume, bus, log)
if err != nil { if err != nil {
log.Error("failed to set cached volume", "error", err) log.Error("failed to set cached volume", "error", err)
} else { } else {
@ -181,6 +181,11 @@ func main() {
// Start the control loop. // Start the control loop.
log.Debug("starting control loop") log.Debug("starting control loop")
setChannels(modeStereo, log)
if err != nil {
log.Error("could not set to stereo", err)
}
run(rv, ns, &cfg.Path, bus, log, netLog) run(rv, ns, &cfg.Path, bus, log, netLog)
} }
@ -248,7 +253,7 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, bus embd.I2CBus, l
l.Debug("checking amplifier volume") l.Debug("checking amplifier volume")
v := vars["AmpVolume"] v := vars["AmpVolume"]
if v != "" { if v != "" {
err = setVolume(v, bus) err = setVolume(v, bus, l)
if err != nil { if err != nil {
l.Error("unable to set requested volume", "volume", v, "error", err) l.Error("unable to set requested volume", "volume", v, "error", err)
continue continue
@ -275,7 +280,7 @@ func setChannels(mode string, l logging.Logger) error {
modeMute: "0%,0%", modeMute: "0%,0%",
}[mode] }[mode]
if vols == "" { if vols == "" {
l.Warning("invalid SpeakeMode", "SpeakerMode", mode) l.Warning("invalid SpeakerMode", "SpeakerMode", mode)
return fmt.Errorf("invalid SpeakerMode: %s", mode) return fmt.Errorf("invalid SpeakerMode: %s", mode)
} }
@ -327,11 +332,12 @@ func setChannels(mode string, l logging.Logger) error {
} }
// setVolume sends i2c commands to the amplifier in order to set the volume of the amplifier. // setVolume sends i2c commands to the amplifier in order to set the volume of the amplifier.
func setVolume(hexVol string, bus embd.I2CBus) error { func setVolume(hexVol string, bus embd.I2CBus, l logging.Logger) error {
vol, err := strconv.ParseInt(hexVol, 16, 8) vol, err := strconv.Atoi(hexVol)
if err != nil { if err != nil {
return fmt.Errorf("could not parse hex volume: %w", err) return fmt.Errorf("could not parse hex volume: %w", err)
} }
l.Debug("volume log: ", "hexStr", hexVol, "intVol", vol, "byteVol", byte(vol))
if vol < minAmpVolume || vol > maxAmpVolume { if vol < minAmpVolume || vol > maxAmpVolume {
return fmt.Errorf("volume %d, out of range: [%d, %d]", vol, minAmpVolume, maxAmpVolume) return fmt.Errorf("volume %d, out of range: [%d, %d]", vol, minAmpVolume, maxAmpVolume)
} }