updated breadth of use for led node

This commit is contained in:
2022-07-05 00:01:28 -04:00
parent 247470dc49
commit 49630474f0
2 changed files with 156 additions and 81 deletions
+142 -75
View File
@@ -1,7 +1,7 @@
// ============================================================================= // =============================================================================
// Auth: Alex Celani // Auth: Alex Celani
// File: led.go // File: led.go
// Revn: 06-27-2022 1.3 // Revn: 07-04-2022 2.0
// Func: receive data as an endpoint, send response back to middleman // Func: receive data as an endpoint, send response back to middleman
// //
// TODO: // TODO:
@@ -19,6 +19,15 @@
// 06-23-2022: updated to produce three different colors // 06-23-2022: updated to produce three different colors
// 06-26-2022: commented // 06-26-2022: commented
// 06-27-2022: added flag package // 06-27-2022: added flag package
// began work on wrapper functions for led
// 06-28-2022: rewrote wrapper function for color, tell()
// 06-29-2022: wrote wrapper function for status, ask()
// added /list/ to options in ask()
//*07-04-2022: rewrote /color/ in tell() just a little bit, to work
// common CATHODE ( so mad ) RGB leds
// added support for white, cyan, yellow, and magenta
// finished comments for tell() and ask() and even simple
// parsing stuff in handleClient()
// //
// ============================================================================= // =============================================================================
@@ -32,6 +41,7 @@ import (
"strings" // ToLower "strings" // ToLower
"github.com/stianeikeland/go-rpio" // talk to RasPi pins "github.com/stianeikeland/go-rpio" // talk to RasPi pins
"flag" // Parse, String, Int, Bool "flag" // Parse, String, Int, Bool
"io/ioutil" // ReadFile
) )
@@ -46,11 +56,121 @@ func check( err error ) {
} }
// parse incoming message, keep only second word // quick function to return LED fields
func parse( recv string ) string { func ask( comm []string ) string {
// split message over spaces
keywords := strings.Split( recv, " " ) // declare response variable
return keywords[1] // return the second word var response string
// XXX this could be done with a map instead
// what is the first word?
switch comm[0] {
case "color": // if user asks for color, get color
response = color
case "status": // if user asks for status, get status
response = status
case "list": // if user wants a list of fields
// open file containing fields
// XXX considering using this file to populate future map
file, err := ioutil.ReadFile( os.Args[0] + "list" )
check( err ) // check error
// cast entire file to string and make returnable
response = string( file )
default: // if user asks for something unknown
response = "field unknown"
}
// return
return response
}
/*
// quick function to make a rainbow
func rainbow() {
for {
for r := 255; r >= 0; r-- {
}
for g := 255; g >= 0; g-- {
}
for b := 255; b >= 0; b-- {
}
}
}
*/
// quick function to change LED colors
func tell( comm []string ) string {
// initiate all pins as off
pinR.High()
pinG.High()
pinB.High()
// declare response variable
var response string
// XXX this could be done with a map instead
// what is the first word?
switch comm[0] {
case "color": // if user trying to change color
color = comm[1] // set current color to input
// FIXME on failure, this is wrong
// is that ok?
response = color // preemptively set response to color
status = "on" // preemptively set status
switch comm[1] {
case "red": // user wants red, turn red on
pinR.Low()
case "green": // user wants green, turn green on
pinG.Low()
case "blue": // user wants blue, turn blue on
pinB.Low()
case "magenta": // user wants blue and red
pinR.Low()
pinB.Low()
case "yellow": // user wants red and green
pinR.Low()
pinG.Low()
case "cyan": // user wants blue and green
pinB.Low()
pinG.Low()
case "white": // user wants all leds
pinR.Low()
pinG.Low()
pinB.Low()
case "rainbow": // user wants a loop
// go rainbow()
response = "not yet implemented"
default: // user asks for else, bail out
response = "color unknown"
status = "off"
}
case "status": // if user trying to change status
status = comm[1] // preemptively set status
switch comm[1] {
case "on": // if trying to turn led on
// re enter function as if trying to
// set current color
response = tell( []string{ "color", color } )
case "off": // leave leds off, set response to off
response = comm[1]
default: // user asks for else, bail out
response = "status unknown"
}
case "kill": // user wants to stop running
os.Exit( 2 ) // FIXME
default: // user enters incorrect command
response = "command unknown"
}
return response
} }
@@ -81,76 +201,23 @@ func handleClient( conn net.Conn ) {
os.Exit( 2 ) // if not, exit os.Exit( 2 ) // if not, exit
} }
var resp string // declare response variable // TODO differentiate ask commands from tell commands
// initiate response variable
var resp string
// TODO convert to switches? switch command[0] { // is user getting or setting?
// len is 3 for "asK" commands case "ask": // if getting
if len( command ) == 3 { // get, store response in variable to be sent back
if command[2] == "color" { resp = ask( command[2:] )
// if user wants color, return color case "tell": // if setting
resp = color // set, store response in variable to be sent back
} else if command[2] == "status" { resp = tell( command[2:] )
// if user wants status, return status default: // if neither, bail out
resp = status resp = "command form unknown"
} else {
// there are only those two fields
resp = "usage unknown"
}
// len is 4 for tell commands
} else if len( command ) == 4 {
// if user wants to set color
if command[2] == "color" {
color = command[3] // set current color
resp = color // return current color
switch color {
case "blue": // does user want blue?
// turn blue on, turn rest off
pinB.High()
pinR.Low()
pinG.Low()
case "red": // does user want red?
// turn red on, turn rest off
pinB.Low()
pinR.High()
pinG.Low()
case "green": // does user want green?
// turn green on, turn rest off
pinB.Low()
pinR.Low()
pinG.High()
default: // color unknown
// turn off
pinB.Low()
pinR.Low()
pinG.Low()
}
// if user wants to set status
} else if command[2] == "status" {
status = command[3] // set current status
resp = status // return current status
switch status {
case "on": // does user want on?
// default to red
pinB.Low()
pinR.High()
pinG.Low()
default: // anything else, like off
// default to off
pinB.Low()
pinR.Low()
pinG.Low()
}
// user entered something else entirely
} else {
resp = "usage unknown"
}
// no other lengths are valid
} else {
os.Exit( 2 ) // exit in this case
} }
if *verbose { if *verbose { // XXX verbose print
fmt.Println( "answer: ", resp ) // print response fmt.Println( "answer: ", resp ) // print response
} }
@@ -160,7 +227,7 @@ func handleClient( conn net.Conn ) {
return // function so it can start again later return // function so it can start again later
} }
if *verbose { if *verbose { // XXX identical verbose print
fmt.Println( "sent: ", resp ) // print response fmt.Println( "sent: ", resp ) // print response
} }
} }
@@ -172,8 +239,8 @@ func handleClient( conn net.Conn ) {
var pinR rpio.Pin // declare red led pin var pinR rpio.Pin // declare red led pin
var pinG rpio.Pin // declare green led pin var pinG rpio.Pin // declare green led pin
var pinB rpio.Pin // declare blue led pin var pinB rpio.Pin // declare blue led pin
var color string // declare color variable var color string = "red" // declare color variable
var status string // declare status variable var status string = "off" // declare status variable
var ( // declare flag variables var ( // declare flag variables
lr *int lr *int
+8
View File
@@ -0,0 +1,8 @@
color ^v
red
green
blue
status ^v
on
off
list v