Skip to content

Commit

Permalink
ts switch added, go code refactored, life.gif changed
Browse files Browse the repository at this point in the history
  • Loading branch information
rokath committed Mar 12, 2020
1 parent 667dcf7 commit 99d64e9
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 32 deletions.
20 changes: 11 additions & 9 deletions cmd/trice/handleArgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (i *arrayFlag) Set(value string) error {

var srcs arrayFlag // gets multiple files or directories


// HandleArgs evaluates the arguments slice of strings und uses wd as working directory
func HandleArgs(wd string, args []string) error {
list := make(id.List, 0, 65536) // for 16 bit IDs enough
Expand All @@ -47,10 +48,11 @@ func HandleArgs(wd string, args []string) error {
lCmd := flag.NewFlagSet("log", flag.ExitOnError) // subcommand
pPort := lCmd.String("port", "", "subcommand (required, try COMscan)") // flag
pBaud := lCmd.Int("baud", 38400, "COM baudrate (optional, default is 38400") // flag
pL := lCmd.String("list", "til.json", "trice ID list path (optional)") // flag
pJSON := lCmd.String("list", "til.json", "trice ID list path (optional)") // flag
pCol := lCmd.String("color", "default", "color set (optional), off, alternate") // flag
pKey := lCmd.String("key", "none", "decrypt passphrase, (optional)") // flag
pShow := lCmd.Bool("show", false, "show passphrase (optional)") // flag
pTs := lCmd.String("ts", "LOCmicro", "timestamp (optional), off, UTCmicro") // flag

cCmd := flag.NewFlagSet("check", flag.ExitOnError) // subcommand
pSet := cCmd.String("dataset", "position", "parameters (optional), negative") // flag
Expand Down Expand Up @@ -139,7 +141,9 @@ func HandleArgs(wd string, args []string) error {
return checkList(*pC, *pSet, pList, *pPal)
}
if lCmd.Parsed() {
return logTrices(lCmd, *pPort, *pBaud, *pL, pList, *pCol, *pKey, *pShow)
emit.TimeStampFormat = *pTs
emit.ColorPalette = *pCol
return logTrices(lCmd, *pPort, *pBaud, *pJSON, pList, *pKey, *pShow)
}
if zCmd.Parsed() {
return zeroIds(*pRunZ, *pSrcZ, zCmd)
Expand Down Expand Up @@ -203,7 +207,7 @@ func checkList(fn, dataset string, p *id.List, palette string) error {
fmt.Println("ID list " + fn + " not found, exit")
return nil
}
emit.Check(*p, dataset, palette)
emit.Check(*p, dataset)
return nil
}

Expand Down Expand Up @@ -231,7 +235,7 @@ func createCipher(password string, show bool) (*xtea.Cipher, bool, error) {
}

// connect to port and display traces
func logTrices(cmd *flag.FlagSet, port string, baud int, fn string, p *id.List, palette, password string, show bool) error {
func logTrices(cmd *flag.FlagSet, port string, baud int, fn string, p *id.List, password string, show bool) error {
if "" == port {
cmd.PrintDefaults()
return nil
Expand Down Expand Up @@ -276,10 +280,10 @@ func logTrices(cmd *flag.FlagSet, port string, baud int, fn string, p *id.List,
}
}
fmt.Println("id list file", fn, "with", len(*p), "items")
return doSerialReceive(port, baud, p, palette)
return doSerialReceive(port, baud, p)
}

func doSerialReceive(port string, baud int, p *id.List, palette string) error {
func doSerialReceive(port string, baud int, p *id.List) error {
serialReceiver := receiver.NewSerialReceiver(port, baud)

if serialReceiver.SetUp() == false {
Expand All @@ -301,11 +305,9 @@ func doSerialReceive(port string, baud int, p *id.List, palette string) error {
//log.Println("from buffer channel:", c)
b = append(b, c...)
}
//fmt.Println("b is:", b)
case t = <-(*serialReceiver.GetTriceChannel()):
//log.Println("from trice channel:", t)
//log.Println("emit.Trice", t, len(b))
b, err = emit.Trice(t, b, *p, palette)
b, err = emit.Trice(t, b, *p)
if nil != err {
log.Println("trice.Log error", err, t, b)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/trice/handleArgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func ExampleHandleArgs_logListNotFound() {
func ExampleHandleArgs_logCOM0() {
HandleArgs("./", []string{"trice", "log", "-list", "none", "-port", "COM0"})
// Output:
// id list file none with 0 items
// Error: Could not open serial port: Serial port not found
// Could not set up serial port COM0
// try -port COMscan
Expand Down
Binary file modified doc/README.media/life.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 18 additions & 1 deletion examples/triceDemoF030R8/MDK-ARM/triceDemoF030R8.uvoptx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,24 @@
<Name>-U0671FF514948897767194535 -O2254 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC800 -FN1 -FF0STM32F0xx_64.FLM -FS08000000 -FL010000 -FP0($$Device:STM32F030R8Tx$CMSIS\Flash\STM32F0xx_64.FLM)</Name>
</SetRegEntry>
</TargetDriverDllRegistry>
<Breakpoint/>
<Breakpoint>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>31</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>134218050</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>C:\GitRepos\trice\src.C\xteaCrypto.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\triceDemoF030R8\../../../src.C/xteaCrypto.c\31</Expression>
</Bp>
</Breakpoint>
<WatchWindow1>
<Ww>
<count>0</count>
Expand Down
4 changes: 2 additions & 2 deletions examples/triceDemoF030R8/MDK-ARM/triceDemoF030R8.uvprojx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<BeforeMake>
<RunUserProg1>1</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name>trice u -src .. -list ../../til.json -v</UserProg1Name>
<UserProg1Name>trice u -src ..</UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
Expand All @@ -82,7 +82,7 @@
<AfterMake>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name></UserProg1Name>
<UserProg1Name>trice log -port COM26 -baud 115200</UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
Expand Down
43 changes: 26 additions & 17 deletions pkg/emit/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import (
"fmt"
"runtime"
"strings"
"time"
"unicode"

"github.com/fatih/color"
"github.com/rokath/trice/pkg/id"
)

var ColorPalette string
var TimeStampFormat string = "off"

// checkValuePosition is a consistency check for positive values and their position
func checkValuePosition(l id.List, palette string) error {
func checkValuePosition(l id.List) error {
var b []byte // check data
for i := range l {
it := l[i]
Expand Down Expand Up @@ -65,7 +69,7 @@ func checkValuePosition(l id.List, palette string) error {
return err
}
b = b[:0] // empty d for next trice
err = visualize(s, palette)
err = visualize(s)
if nil != err {
return err
}
Expand All @@ -74,7 +78,7 @@ func checkValuePosition(l id.List, palette string) error {
}

// checkNegativeValues is a consistency check for negative values
func checkNegativeValues(l id.List, palette string) error {
func checkNegativeValues(l id.List) error {
var b []byte // check data
for i := range l {
it := l[i]
Expand Down Expand Up @@ -123,15 +127,15 @@ func checkNegativeValues(l id.List, palette string) error {
return err
}
b = b[:0] // empty d for next trice
err = visualize(s, palette)
err = visualize(s)
if nil != err {
return err
}
}
return nil
}

func checkFix(l id.List, palette string) error {
func checkFix(l id.List) error {
b := []byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'} // dummy data
for i := range l {
it := l[i]
Expand Down Expand Up @@ -164,7 +168,7 @@ func checkFix(l id.List, palette string) error {
return err
}
b = b[:0] // empty d for next trice
err = visualize(s, palette)
err = visualize(s)
if nil != err {
return err
}
Expand All @@ -173,14 +177,14 @@ func checkFix(l id.List, palette string) error {
}

// Check evaluates the l output with specifiesd dataSet
func Check(l id.List, dataSet, palette string) error {
func Check(l id.List, dataSet string) error {
if "position" == dataSet {
return checkValuePosition(l, palette)
return checkValuePosition(l)
}
if "negative" == dataSet {
return checkNegativeValues(l, palette)
return checkNegativeValues(l)
}
return checkFix(l, palette)
return checkFix(l)
}

var d = make([]byte, 0, 32) // param collector, usually not more than 16 bytes
Expand All @@ -191,7 +195,7 @@ var buffer []byte // container for buffer data (strings)
// The ID can be 0, in that case only the data payload is saved
// If b is not empty, the start byte in buffer b is 0xc0. The byte slice b contains a 8 byte header, a 2 byte len-1 value and and len buffer bytes.
// b is used for long runtime strings, when the format string is "%s".
func Trice(t, b []byte, l id.List, palette string) ([]byte, error) {
func Trice(t, b []byte, l id.List) ([]byte, error) {
d = append(d, t[6:8]...)
i := int(binary.LittleEndian.Uint16(t[4:6]))
if 0 == i {
Expand All @@ -213,7 +217,7 @@ func Trice(t, b []byte, l id.List, palette string) ([]byte, error) {
}
it := l[x]
s, b, _ := emitter(it, d, b)
err = visualize(s, palette)
err = visualize(s)
d = d[:0] // empty d for next trice
return b, err
}
Expand Down Expand Up @@ -352,7 +356,7 @@ func colorSetAlternate(channel string) (*color.Color, error) {
// If lower case match it returns *color.Color and s without starting pattern "col:"
// col options are: err, wrn, msg, ...
// COL options are: ERR, WRN, MSG, ...
func colorChannel(s, palette string) (*color.Color, string) {
func colorChannel(s string) (*color.Color, string) {
var c *color.Color
var err error
sc := strings.SplitN(s, ":", 2)
Expand All @@ -365,7 +369,7 @@ func colorChannel(s, palette string) (*color.Color, string) {
} else {
r = s // keep channel info
}
switch palette {
switch ColorPalette {
case "off":
color.NoColor = true // disables colorized output
case "default":
Expand Down Expand Up @@ -399,18 +403,23 @@ func trimBackslashes(s string) string {
}

// set color and linebreaks
func visualize(s, palette string) error {
func visualize(s string) error {
var err error
s = trimBackslashes(s)
c, s := colorChannel(s, palette)
c, s := colorChannel(s)

// When a carriage return is executed, the whole next line gets the current background color.
// Therefore detect this case and set the color to a default value before the carriage return.
if strings.HasSuffix(s, "\n") {
s := strings.TrimSuffix(s, "\n")
printIt(s, c)
b := color.New(color.FgWhite).Add(color.BgBlack) // assuming this as the default terminal color
_, err = b.Println()
_, _ = b.Println()
switch(TimeStampFormat){
case "off":
case "UTCmicro": _, err = b.Print(time.Now().UTC().Format(time.StampMicro),": ")
case "LOCmicro": _, err = b.Print(time.Now().Format(time.StampMicro),": ")
}
} else {
_, err = printIt(s, c)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/emit/emit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestPosition(t *testing.T) {
r, w, _ := os.Pipe()
os.Stdout = w

Check(list, "position", "off") // color "off"
ColorPalette = "off"
Check(list, "position") // color "off"

outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
Expand Down Expand Up @@ -89,8 +90,8 @@ func TestNegative(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w

Check(list, "negative", "off") // color "off"
ColorPalette = "off"
Check(list, "negative") // color "off"

outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
Expand Down

0 comments on commit 99d64e9

Please sign in to comment.