mirror of https://bitbucket.org/ausocean/av.git
156 lines
3.3 KiB
Go
156 lines
3.3 KiB
Go
/*
|
|
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()
|
|
|
|
` |