Merged in digital-volume (pull request #458)

treatment: add support for digital volume control

Approved-by: Saxon Milton
This commit is contained in:
Trek Hopton 2021-03-04 01:21:45 +00:00
commit cd3d2388d6
1 changed files with 31 additions and 9 deletions

View File

@ -46,6 +46,7 @@ import (
"bitbucket.org/ausocean/iot/pi/netlogger"
"bitbucket.org/ausocean/iot/pi/netsender"
"bitbucket.org/ausocean/utils/logger"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/rpi"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
@ -71,6 +72,10 @@ const (
netSendRetryTime = 5 * time.Second
defaultSleepTime = 60 // Seconds
pkg = "rv: "
minAmpVolume = 0
maxAmpVolume = 63
volAddr = 0x4B
i2cPort = 1
)
// Treatment modes.
@ -116,14 +121,14 @@ func main() {
log.Debug("initialising netsender client")
ns, err := netsender.New(log, gpio.InitPin, nil, gpio.WritePin, varMap)
if err != nil {
log.Fatal("could not initialise netsender client", "error", err.Error())
log.Fatal("could not initialise netsender client", "error", err)
}
// Revid will handle the recording and sending of audio for sound checking.
log.Debug("initialising revid")
rv, err := revid.New(config.Config{Logger: log}, ns)
if err != nil {
log.Fatal("could not initialise revid", "error", err.Error())
log.Fatal("could not initialise revid", "error", err)
}
// Start the control loop.
@ -145,7 +150,7 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger,
l.Debug("running netsender")
err := ns.Run()
if err != nil {
l.Warning("run failed. Retrying...", "error", err.Error())
l.Warning("run failed. Retrying...", "error", err)
time.Sleep(netSendRetryTime)
continue
}
@ -153,7 +158,7 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger,
l.Debug("sending logs")
err = nl.Send(ns)
if err != nil {
l.Warning(pkg+"Logs could not be sent", "error", err.Error())
l.Warning(pkg+"Logs could not be sent", "error", err)
}
l.Debug("checking varsum")
@ -168,7 +173,7 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger,
l.Debug("getting new vars")
vars, err := ns.Vars()
if err != nil {
l.Error(pkg+"netSender failed to get vars", "error", err.Error())
l.Error(pkg+"netSender failed to get vars", "error", err)
time.Sleep(netSendRetryTime)
continue
}
@ -178,12 +183,29 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger,
l.Debug("updating revid configuration")
err = rv.Update(vars)
if err != nil {
l.Warning(pkg+"couldn't update revid", "error", err.Error())
l.Warning(pkg+"couldn't update revid", "error", err)
sleep(ns, l)
continue
}
l.Info("revid successfully reconfigured")
l.Debug("checking amplifier volume")
v := vars["AmpVolume"]
if v != "" {
vol, err := strconv.ParseInt(v, 10, 8)
if err != nil {
l.Error(pkg+"failed to parse amplifier volume", "error", err)
} else if vol < minAmpVolume || vol > maxAmpVolume {
l.Error(fmt.Sprintf("%s invalid amplifier volume, must be between %v and %v", pkg, minAmpVolume, maxAmpVolume), "volume", vol)
} else {
bus := embd.NewI2CBus(i2cPort)
err := bus.WriteByte(volAddr, byte(vol))
if err != nil {
l.Error(pkg+"failed to write amplifier volume", "error", err)
}
}
}
l.Debug("checking mode")
switch ns.Mode() {
case modePaused:
@ -211,7 +233,7 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger,
l.Info("sound checking")
err = rv.Start()
if err != nil {
l.Error("could not start revid", "error", err.Error())
l.Error("could not start revid", "error", err)
ns.SetMode(modePaused, &vs)
sleep(ns, l)
continue
@ -246,7 +268,7 @@ func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l *logger.L
// Start playback of the audio file.
err = cmd.Start()
if err != nil {
l.Error("start failed", "error", err.Error())
l.Error("start failed", "error", err)
continue
}
numPlays++
@ -273,7 +295,7 @@ func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l *logger.L
// Wait for playback to complete.
err = cmd.Wait()
if err != nil {
l.Error("failed to wait for execution finish", "error", err.Error())
l.Error("failed to wait for execution finish", "error", err)
}
l.Debug("stdout received", "stdout", string(outBuff.Bytes()))