-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] cmd: execute, title, read, write (#7)
* 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
Showing
10 changed files
with
391 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.