implimenting LED netreciever colour control file

This commit is contained in:
Saxon Nelson-Milton 2022-02-23 03:52:10 +00:00
parent 3db7d1b1f3
commit 0f35b619d7
2 changed files with 166 additions and 0 deletions

View File

@ -67,10 +67,12 @@ import (
"bitbucket.org/ausocean/av/container/mts/meta" "bitbucket.org/ausocean/av/container/mts/meta"
"bitbucket.org/ausocean/av/revid" "bitbucket.org/ausocean/av/revid"
"bitbucket.org/ausocean/av/revid/config" "bitbucket.org/ausocean/av/revid/config"
"bitbucket.org/ausocean/av/led"
"bitbucket.org/ausocean/iot/pi/netlogger" "bitbucket.org/ausocean/iot/pi/netlogger"
"bitbucket.org/ausocean/iot/pi/netsender" "bitbucket.org/ausocean/iot/pi/netsender"
"bitbucket.org/ausocean/iot/pi/sds" "bitbucket.org/ausocean/iot/pi/sds"
"bitbucket.org/ausocean/utils/logger" "bitbucket.org/ausocean/utils/logger"
) )
// Copyright information prefixed to all metadata. // Copyright information prefixed to all metadata.
@ -178,6 +180,10 @@ func main() {
// run starts the main loop. This will run netsender on every pass of the loop // run starts the main loop. This will run netsender on every pass of the loop
// (sleeping inbetween), check vars, and if changed, update revid as appropriate. // (sleeping inbetween), check vars, and if changed, update revid as appropriate.
func run(rv *revid.Revid, ns *netsender.Sender, l *logger.Logger, nl *netlogger.Logger) { func run(rv *revid.Revid, ns *netsender.Sender, l *logger.Logger, nl *netlogger.Logger) {
led_, err := led.NewLED(*l)
if err != nil {
l.Log(logger.Error, pkg+"could not create LED", "error", err.Error())
}
var vs int var vs int
for { for {
l.Log(logger.Debug, "running netsender") l.Log(logger.Debug, "running netsender")
@ -212,6 +218,10 @@ func run(rv *revid.Revid, ns *netsender.Sender, l *logger.Logger, nl *netlogger.
} }
l.Log(logger.Debug, "got new vars", "vars", vars) l.Log(logger.Debug, "got new vars", "vars", vars)
err = led_.Update(vars["ledcolours"])
if err != nil {
l.Log(logger.Error, pkg+"could not update led", "error", err.Error())
}
// Configure revid based on the vars. // Configure revid based on the vars.
l.Log(logger.Debug, "updating revid's configuration") l.Log(logger.Debug, "updating revid's configuration")
err = rv.Update(vars) err = rv.Update(vars)

156
led/led.go Normal file
View File

@ -0,0 +1,156 @@
/*
DESCRIPTION
LED.go provides an implementation of the LED interface for a basic
multicolour LED using a background python process responsible for the
hardware interfacing.
AUTHORS
Alex Mastersson <alexm@ausoean.org>
Saxon Nelson-Milton <saxon@ausocean.org>
LICENSE
Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package led
import (
"fmt"
"io"
"os/exec"
"time"
"bitbucket.org/ausocean/utils/logger"
)
// Background process constants.
const processStartWait = 5 * time.Second
// LED is an implementation of the LED interface for a standard Multicolour LED.
type LED struct {
cmd *exec.Cmd
stdin io.Writer
stdout io.Reader
r,g,b int
log logger.Logger
}
const pin = 5
// NewLED returns a new LED.
func NewLED(l logger.Logger) (*LED, error) {
s := &LED{log: l}
s.cmd = exec.Command("python3", "-c", LEDScript)
var err error
s.stdin, err = s.cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("could not pipe stdin of process: %w", err)
}
s.stdout, err = s.cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("could not pipe stdout of process: %w", err)
}
s.log.Debug("starting LEDCommand process")
err = s.cmd.Start()
if err != nil {
return nil, fmt.Errorf("could not start servCommand process: %w", err)
}
time.Sleep(processStartWait)
return s, nil
}
func (s *LED) Update(v string) error {
_, err := s.stdin.Write([]byte(v + "\r\n"))
if err != nil {
return fmt.Errorf("could not write rgb to LEDCommand process: %w", err)
}
return nil
}
// Shutdown kills the LED command background process.
func (s *LED) Shutdown() error {
s.log.Debug("shutting down")
err := s.cmd.Process.Kill()
if err != nil {
return fmt.Errorf("could not kill LEDCommand process: %w", err)
}
return nil
}
func main(){
var l logger.Logger
_, err := NewLED(l)
fmt.Println(err)
}
const LEDScript = `
import sys
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
red = GPIO.PWM(17,120)
green = GPIO.PWM(18,120)
blue = GPIO.PWM(27,120)
redbr=50
greenbr=50
bluebr=50
red.start(0)
green.start(0)
blue.start(0)
def main():
rgb = [0,0,0]
while True: # one loop per line
rgb[2] = 0
next = sys.stdin.readline()
if next == "":
time.sleep(3)
else:
string = next.split(",")
rgb[0] = int(string[0])
rgb[1] = int(string[1])
rgb[2] = int(string[2].strip("\n"))
print(rgb)
rgblight(rgb[0],rgb[1],rgb[2])
time.sleep(3)
print("loop break")
def rgblight(rpwm,gpwm,bpwm):
red.ChangeDutyCycle(rpwm)
green.ChangeDutyCycle(gpwm)
blue.ChangeDutyCycle(bpwm)
main()
`