Skip to content

Commit

Permalink
[feat] cmd: execute, title, read, write (#7)
Browse files Browse the repository at this point in the history
* For cmd: tickle

* Write and save

* Ready to do more

* Shared args

* More args for new cmd

* cmd: read state

* Save Text & JSON

* read playing
  • Loading branch information
hyorigo authored Oct 24, 2023
1 parent b880a82 commit 68fdd57
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// executeCmd represents the execute command
var executeCmd = &cobra.Command{
Use: "execute",
Use: "execute <file>",
Aliases: aliasesExecute,
Short: "Execute pattern files",
Long: hdoc(`
Expand Down
19 changes: 10 additions & 9 deletions cmd/read.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// readCmd represents the read command
var readCmd = &cobra.Command{
Use: "read",
Aliases: aliasesRead,
Short: "Read blink(1) into a given color",
Short: "Read from a blink(1) device",
Long: hdoc(`
Perform a specific color changing action on a blink(1) device.
// TODO:
`),
Args: cobra.MinimumNArgs(1),
PersistentPreRunE: openBlink1Device,
RunE: func(cmd *cobra.Command, args []string) error {
// TODO:
return fmt.Errorf("not implemented")
},
PersistentPreRunE: openBlink1Device,
PersistentPostRunE: saveResultData,
}

var (
readPreviewResult bool
)

func init() {
rootCmd.AddCommand(readCmd)

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command and all subcommands, e.g.:
// readCmd.PersistentFlags().String("foo", "", "A help for foo")
readCmd.PersistentFlags().BoolVarP(&readPreviewResult, "preview", "p", false, "whether to preview the result")
readCmd.PersistentFlags().StringVarP(&outputJSONPath, "json", "j", "", "output JSON file path")
readCmd.PersistentFlags().StringVarP(&outputTextPath, "text", "t", "", "output Text file path")

// Cobra supports local flags which will only run when this command is called directly, e.g.:
// readCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
Expand Down
62 changes: 62 additions & 0 deletions cmd/read_pattern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"github.com/b1ug/nb1/hdwr"
"github.com/b1ug/nb1/schema"
"github.com/b1ug/nb1/util"
"github.com/spf13/cobra"
)

// readPatternCmd represents the pattern command
var readPatternCmd = &cobra.Command{
Use: "pattern",
Aliases: aliasesPattern,
Short: "Read all patterns from the RAM",
Long: hdoc(`
Read all patterns from the RAM of a blink(1) device.
`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
seq, err := hdwr.ReadOnChipSequence()
if err != nil {
return err
}

// preview
if readPreviewResult {
_ = util.PrintStateSequence(seq)
}

// save json result
ps := schema.PatternSet{
Name: "from_device",
RepeatTimes: 1,
Sequence: seq,
}
ps.AutoFill()
saveJSONData = ps

// save text result
saveTextLine = make([]string, len(seq))
for i, s := range seq {
b, _ := s.MarshalText()
saveTextLine[i] = string(b)
}

return errNotImplemented
},
}

func init() {
readCmd.AddCommand(readPatternCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// readPatternCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// readPatternCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
58 changes: 58 additions & 0 deletions cmd/read_play.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"fmt"

"github.com/b1ug/nb1/hdwr"
"github.com/spf13/cobra"
)

// readPlayCmd represents the play command
var readPlayCmd = &cobra.Command{
Use: "play",
Aliases: aliasesPlay,
Short: "Read the playing state of pattern",
Long: hdoc(`
Read the pattern playing state from a blink(1) device.
`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
pls, err := hdwr.ReadPlayingState()
if err != nil {
return err
}

// preview
if readPreviewResult {
fmt.Println("Playing:", pls.IsPlaying)
fmt.Println("Start:", pls.StartPosition)
fmt.Println("End:", pls.EndPosition)
fmt.Println("Repeat:", pls.RepeatTimes)
}

// save result
saveJSONData = pls
saveTextLine = []string{
fmt.Sprintf("Playing: %v", pls.IsPlaying),
fmt.Sprintf("Start: %d", pls.StartPosition),
fmt.Sprintf("End: %d", pls.EndPosition),
fmt.Sprintf("Repeat: %d", pls.RepeatTimes),
}

return nil
},
}

func init() {
readCmd.AddCommand(readPlayCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// readPlayCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// readPlayCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
88 changes: 88 additions & 0 deletions cmd/read_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cmd

import (
"fmt"
"image/color"

"github.com/b1ug/nb1/hdwr"
"github.com/b1ug/nb1/util"
"github.com/spf13/cobra"
)

// readStateCmd represents the state command
var readStateCmd = &cobra.Command{
Use: "state",
Aliases: aliasesState,
Short: "Read the state of LED",
Long: hdoc(`
Read the current state of LED from a blink(1) device.
`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
// read
var (
lc1 color.Color
lc2 color.Color
err error
)
switch readStateLedNum {
case 0:
log.Debugw("read all led state")
if lc1, err = hdwr.ReadLEDColor(1); err != nil {
return err
}
if lc2, err = hdwr.ReadLEDColor(2); err != nil {
return err
}
case 1:
log.Debugw("read top led state")
if lc1, err = hdwr.ReadLEDColor(1); err != nil {
return err
}
case 2:
log.Debugw("read bottom led state")
if lc2, err = hdwr.ReadLEDColor(2); err != nil {
return err
}
}

// handle result
jm := make(map[string]interface{})
saveTextLine = make([]string, 0)
outputLEDColor := func(ledNum uint, lc color.Color) {
if lc != nil {
ln := fmt.Sprintf("LED%d", ledNum)
cn := util.ConvColorToHex(lc)
jm[ln] = cn
saveTextLine = append(saveTextLine, ln+": "+cn)
if readPreviewResult {
fmt.Println(ln+":", util.FormatNamedColor(lc))
}
}
}
outputLEDColor(1, lc1)
outputLEDColor(2, lc2)
saveJSONData = jm

return nil
},
}

var (
readStateLedNum uint
)

func init() {
readCmd.AddCommand(readStateCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// readStateCmd.PersistentFlags().String("foo", "", "A help for foo")
readStateCmd.PersistentFlags().UintVarP(&readStateLedNum, "led", "l", 0, "which led number to read, 0=all/1=top/2=bottom (mk2+)")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// readStateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
26 changes: 19 additions & 7 deletions cmd/tickle.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
package cmd

import (
"fmt"
"time"

"github.com/b1ug/nb1/hdwr"
"github.com/spf13/cobra"
)

// tickleCmd represents the tickle command
var tickleCmd = &cobra.Command{
Use: "tickle",
Aliases: aliasesTickle,
Short: "Tickle blink(1) into a given color",
Short: "Tickle blink(1) device not to play before the timeout",
Long: hdoc(`
Perform a specific color changing action on a blink(1) device.
// TODO:
Send a command to blink(1) to tickle the device not to play the pattern before the timeout.
If the next command is sent before the timeout, the current tickle command is cancelled.
If the next command is not sent before the timeout, the device will play the given pattern.
`),
Args: cobra.MinimumNArgs(1),
PersistentPreRunE: openBlink1Device,
RunE: func(cmd *cobra.Command, args []string) error {
// TODO:
return fmt.Errorf("not implemented")
// parse sub-pattern range
if err := getPatternPosArgs(cmd, args); err != nil {
return err
}

// let's tickle
log.Infow("tickle server mode on blink(1) device", "start", patternStartPos, "end", patternEndPos, "timeout", tickleTimeout)
return hdwr.TickleOnChipPattern(patternStartPos, patternEndPos, tickleTimeout)
},
}

var (
tickleTimeout time.Duration
)

func init() {
rootCmd.AddCommand(tickleCmd)

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command and all subcommands, e.g.:
// tickleCmd.PersistentFlags().String("foo", "", "A help for foo")
tickleCmd.PersistentFlags().DurationVarP(&tickleTimeout, "timeout", "t", 5*time.Second, "Timeout before the device plays the pattern")

// Cobra supports local flags which will only run when this command is called directly, e.g.:
// tickleCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
Expand Down
Loading

0 comments on commit 68fdd57

Please sign in to comment.