79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
|
package rgbled
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
"github.com/go-flucky/flucky/pkg/config"
|
||
|
"github.com/go-flucky/flucky/pkg/types"
|
||
|
"github.com/spf13/cobra"
|
||
|
)
|
||
|
|
||
|
var enabled bool
|
||
|
var location, wireID, wirePath string
|
||
|
|
||
|
var addRgbLedCmd = &cobra.Command{
|
||
|
Use: "add",
|
||
|
Short: "Add a RGB-LED",
|
||
|
Aliases: []string{"append"},
|
||
|
Args: cobra.ExactArgs(4),
|
||
|
Example: fmt.Sprintf(`flucky rgb-led add <name> <gpio-for-blue> <gpio-for-green> <gpio-for-red>
|
||
|
flucky rgb-led add my-led GPIO13 GPIO17 GPIO26`),
|
||
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
// read configuration
|
||
|
cnf, err := config.Read(configFile)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
// determine gpio port
|
||
|
gpioRed, err := types.StringToGPIO(args[1])
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
gpioGreen, err := types.StringToGPIO(args[2])
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
gpioBlue, err := types.StringToGPIO(args[3])
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
// create new sensor struct
|
||
|
rgbLED := &types.RGBLED{
|
||
|
RGBLEDName: args[0],
|
||
|
RGBLEDLocation: location,
|
||
|
RGBLEDEnabled: enabled,
|
||
|
RGBLEDColorToGPIO: map[types.RGBColor]*types.GPIO{
|
||
|
types.RGBLEDBlue: &gpioBlue,
|
||
|
types.RGBLEDGreen: &gpioGreen,
|
||
|
types.RGBLEDRed: &gpioRed,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
// // add sensor entry to list
|
||
|
err = cnf.AddRGBLED(rgbLED)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
// save new configuration
|
||
|
err = config.Write(cnf, configFile)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
rgbLedCmd.AddCommand(addRgbLedCmd)
|
||
|
|
||
|
addRgbLedCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
|
||
|
addRgbLedCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location")
|
||
|
addRgbLedCmd.Flags().StringVarP(&wireID, "wire-id", "i", "", "Wire-ID")
|
||
|
addRgbLedCmd.Flags().StringVarP(&wirePath, "wire-path", "w", "/sys/bus/w1/devices", "Wire device path")
|
||
|
}
|