Skip to content

Commit

Permalink
led: add duty command to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
deadprogram committed May 27, 2021
1 parent a72e02c commit b2a247b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
28 changes: 26 additions & 2 deletions cmd/kd6ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func main() {
leds := &ffcli.Command{
Name: "led",
ShortUsage: "kd6ctl led <A/B/AB> <on/off> [pulse]",
ShortHelp: "Sets LEDs on sensor on or off.",
ShortHelp: "Turn sensor LEDs on or off.",
Exec: func(_ context.Context, args []string) error {
if len(args) < 2 {
return fmt.Errorf("led command requires specific LEDs either 'a', 'b', 'ab'. You must also specify to set LEDs 'on' or 'off'")
Expand Down Expand Up @@ -250,11 +250,35 @@ func main() {
},
}

duty := &ffcli.Command{
Name: "duty",
ShortUsage: "kd6ctl duty <a/b> <duty>",
ShortHelp: "Set LED duty illumination period (in microseconds).",
Exec: func(_ context.Context, args []string) error {
if len(args) < 2 {
return fmt.Errorf("duty command requires specific LEDs either 'a' or 'b'. You must also specify the duty to set LEDs to")
}

led := args[0]
if led != "a" && led != "b" {
return fmt.Errorf("invalid led value. must be 'a' or 'b'")
}

duty, err := strconv.ParseFloat(args[1], 32)
if err != nil {
return err
}

cis := kd6rmx.Sensor{Port: *port}
return cis.LEDDutyCycle(led, float32(duty))
},
}

root := &ffcli.Command{
ShortUsage: "kd6ctl [flags] <subcommand>",
ShortHelp: "kd6ctl is a command line utility to change config on the KD6RMX contact image sensor.",
FlagSet: rootFlagSet,
Subcommands: []*ffcli.Command{load, save, outputfreq, outputfmt, interp, dark, white, leds},
Subcommands: []*ffcli.Command{load, save, outputfreq, outputfmt, interp, dark, white, leds, duty},
Exec: func(context.Context, []string) error {
return flag.ErrHelp
},
Expand Down
8 changes: 5 additions & 3 deletions kd6rmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ func (cis Sensor) LEDControl(leds string, on bool, pulsedivider int) error {
return checkError("LEDControl", result)
}

func (cis Sensor) LEDDutyCycle(led string, duty int) error {
// LEDDutyCycle sets the duty cycle for each LED separately.
// The value for duty represents the number of microseconds.
func (cis Sensor) LEDDutyCycle(led string, duty float32) error {
var ls string
switch led {
case "a", "A":
Expand All @@ -394,10 +396,10 @@ func (cis Sensor) LEDDutyCycle(led string, duty int) error {
return errors.New("invalid LED for duty cycle")
}

if duty < 0 || duty > 0xFFFF {
if duty <= 0 || duty >= 4096 {
return errors.New("invalid duty cycle value")
}
param := fmt.Sprintf("%s%04X", ls, duty)
param := fmt.Sprintf("%s%04X", ls, duty*16)

result, err := cis.sendCommand("LC", param)
if err != nil {
Expand Down

0 comments on commit b2a247b

Please sign in to comment.