Skip to content

Commit cf402d3

Browse files
committed
generate ok
1 parent 78fbd3a commit cf402d3

5 files changed

Lines changed: 359 additions & 166 deletions

File tree

internal/args/init.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ Example: "-pick err:wrn -pick default" results in suppressing all messages despi
176176
fsScLog.StringVar(&decoder.PackageFraming, "packageFraming", "TCOBSv1", `Use "none" or "COBS" as alternative. "COBS" needs "#define TRICE_FRAMING TRICE_FRAMING_COBS" inside "triceConfig.h".`)
177177
fsScLog.StringVar(&decoder.PackageFraming, "pf", "TCOBSv1", "Short for '-packageFraming'.")
178178
fsScLog.BoolVar(&trexDecoder.AddNewlineToEachTriceMessage, "addNL", false, `Add a newline char at trice messages end to use for example "hi" instead of "hi\n" in source code.`)
179-
fsScLog.BoolVar(&emitter.TagStatistics, "tagStat", false, `Print Trices occurances count on exit.`)
180-
fsScLog.BoolVar(&decoder.TriceStatistics, "triceStat", false, `Print Trices occurances count on exit.`)
179+
fsScLog.BoolVar(&emitter.TagStatistics, "tagStat", false, `Print Trices occurrences count on exit.`)
180+
fsScLog.BoolVar(&decoder.TriceStatistics, "triceStat", false, `Print Trices occurrences count on exit.`)
181181
fsScLog.BoolVar(&emitter.AllStatistics, "stat", false, `Print complete statistics on exit.`)
182182
}
183183

@@ -190,11 +190,12 @@ func generateInit() {
190190
fsScGenerate = flag.NewFlagSet("generate", flag.ExitOnError) // sub-command
191191
flagIDList(fsScGenerate)
192192
flagVerbosity(fsScGenerate)
193-
fsScGenerate.BoolVar(&id.GenerateHFile, "h", false, `Create a til.h file.`)
194-
fsScGenerate.BoolVar(&id.GenerateCFile, "c", false, `Create a til.c file.`)
195-
fsScGenerate.BoolVar(&id.GenerateCSFile, "cs", false, `Create a til.cs file.`)
193+
fsScGenerate.BoolVar(&id.GenerateTilH, "tilH", false, `Create a til.h file.`)
194+
fsScGenerate.BoolVar(&id.GenerateTilC, "tilC", false, `Create a til.c file.`)
195+
fsScGenerate.BoolVar(&id.GenerateTilCS, "tilCS", false, `Create a til.cs file.`)
196+
fsScGenerate.BoolVar(&id.GenerateRpcH, "rpcH", false, `Create a til_rpc.h file.`)
197+
fsScGenerate.BoolVar(&id.GenerateRpcC, "rpcC", false, `Create a til_rpc.c file.`)
196198
fsScGenerate.BoolVar(&id.WriteAllColors, "colors", false, `Write all possible colors.`)
197-
fsScGenerate.BoolVar(&id.IDToFunctionPointerList, "fpl", false, `Write ID to function pointer list.`)
198199
}
199200

200201
func insertIDsInit() {

internal/args/tricehelpall_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ sub-command 'l|log': For displaying trice logs coming from port. With "trice log
266266
-suffix string
267267
Append suffix to all lines, options: any string.
268268
-tagStat
269-
Print Trices occurances count on exit.
269+
Print Trices occurrences count on exit.
270270
-tcp string
271271
TCP address for an external log receiver like Putty. Example: 1st: "trice log -p COM1 -tcp localhost:64000", 2nd "putty". In "Terminal" enable "Implicit CR in every LF", In "Session" Connection type:"Other:Telnet", specify "hostname:port" here like "localhost:64000".
272272
-testTable
@@ -277,7 +277,7 @@ sub-command 'l|log': For displaying trice logs coming from port. With "trice log
277277
-triceEndianness string
278278
Target endianness trice data stream. Option: "bigEndian". (default "littleEndian")
279279
-triceStat
280-
Print Trices occurances count on exit.
280+
Print Trices occurrences count on exit.
281281
-ts string
282282
Target timestamp general format string at start of each line, if target timestamps existent (configured). Choose between "µs" (or "us") and "ms", use "" or 'off' or 'none' to suppress existing target timestamps. Sets ts0, ts16, ts32 if these not passed. If several trices form a log line only the timestamp of first trice ist displayed. (default "µs")
283283
-ts0 string
@@ -353,24 +353,28 @@ sub-command 'a|add': Use for adding library source files containing already tric
353353
For example "trice log -p JLINK -v" is the same as "trice log -p JLINK" but with more descriptive output what could be helpful during setup.
354354
This is a bool switch. It has no parameters. Its default value is false. If the switch is applied its value is true. You can also set it explicit: =false or =true.
355355
sub-command 'g|gen|generate': Use for creating colors or support files.
356-
-c Create a til.c file.
357356
-colors
358357
Write all possible colors.
359-
-cs
360-
Create a til.cs file.
361-
-fpl
362-
Write ID to function pointer list.
363-
-h Create a til.h file.
364358
-i string
365359
Short for '-idlist'.
366360
(default "til.json")
367361
-idlist string
368362
The trice ID list file.
369363
The specified JSON file is needed to display the ID coded trices during runtime and should be under version control.
370364
(default "til.json")
365+
-rpcC
366+
Create a til_rpc.c file.
367+
-rpcH
368+
Create a til_rpc.h file.
371369
-til string
372370
Short for '-idlist'.
373371
(default "til.json")
372+
-tilC
373+
Create a til.c file.
374+
-tilCS
375+
Create a til.cs file.
376+
-tilH
377+
Create a til.h file.
374378
-v short for verbose
375379
-verbose
376380
Gives more informal output if used. Can be helpful during setup.

internal/id/generate.go

Lines changed: 78 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -19,208 +19,93 @@ import (
1919
)
2020

2121
var (
22-
GenerateHFile bool
23-
GenerateCFile bool
24-
GenerateCSFile bool
25-
WriteAllColors bool
26-
IDToFunctionPointerList bool
22+
GenerateTilH bool
23+
GenerateTilC bool
24+
GenerateTilCS bool
25+
GenerateRpcH bool
26+
GenerateRpcC bool
27+
WriteAllColors bool
2728
)
2829

29-
// ConstructFullTriceInfo returns full TRICE info, if not exist in triceType string
30-
func ConstructFullTriceInfo(triceType string, paramCount int) string {
31-
origType := triceType
32-
for _, name := range []string{"TRICE", "TRice", "Trice", "trice"} {
33-
if strings.HasPrefix(origType, name+"_") { // when no bit width, insert it
34-
triceType = name + DefaultTriceBitWidth + "_" + origType[6:]
35-
}
36-
if origType == name { // when plain trice name
37-
if paramCount == 0 { // no parameters
38-
triceType = name + "0" // special case
39-
} else { // append bit width and count
40-
triceType = fmt.Sprintf(name+DefaultTriceBitWidth+"_%d", paramCount)
41-
}
42-
}
43-
if origType == name+"8" || origType == name+"16" || origType == name+"32" || origType == name+"64" { // when no count
44-
triceType = fmt.Sprintf(origType+"_%d", paramCount) // append count
45-
}
46-
}
47-
return triceType
48-
}
49-
5030
// SubCmdIdGenerate performs sub-command generate, creating support files/output.
5131
func SubCmdGenerate(w io.Writer, fSys *afero.Afero) (err error) {
32+
if !GenerateTilH && !GenerateTilC && !GenerateTilCS && !GenerateRpcH && !GenerateRpcC && !WriteAllColors {
33+
fmt.Fprintln(w, `The "trice generate" command needs at least one parameter. Check "trice help -generate".`)
34+
return nil
35+
}
36+
5237
if WriteAllColors {
5338
emitter.ShowAllColors()
5439
if Verbose {
5540
fmt.Fprintln(w, `Modify ansi.ColorFunc assignments in lineTransformerANSI.go to change Trice colors.`)
5641
}
5742
}
5843

59-
if !GenerateHFile && !GenerateCFile && !GenerateCSFile && !IDToFunctionPointerList {
60-
if !WriteAllColors {
61-
fmt.Fprintln(w, `The "trice generate" command needs at least one parameter. Check "trice help -generate".`)
62-
}
63-
return nil
64-
}
65-
6644
ilu := NewLut(w, fSys, FnJSON) // read til.json
6745
fn := strings.TrimSuffix(FnJSON, filepath.Ext(FnJSON))
6846
msg.FatalOnErr(err)
6947

70-
if GenerateHFile {
48+
if GenerateTilH {
7149
fnH := fn + ".h"
72-
msg.FatalOnErr(ToFileCHeader(fSys, fnH))
50+
msg.FatalOnErr(ToFileTilH(fSys, fnH))
7351
if Verbose {
7452
fmt.Fprintln(w, "generated", fnH)
7553
}
7654
}
7755

78-
if GenerateCFile {
56+
if GenerateRpcH {
57+
fnRPC := fn + "Rpc.h"
58+
msg.FatalOnErr(ilu.ToFileTriceRpcH(fSys, fnRPC))
59+
if Verbose {
60+
fmt.Fprintln(w, "generated", fnRPC)
61+
}
62+
}
63+
64+
if GenerateTilC {
7965
fnC := fn + ".c"
80-
msg.FatalOnErr(ilu.ToFileLangC(fSys, fnC))
66+
msg.FatalOnErr(ilu.ToFileTilC(fSys, fnC))
8167
if Verbose {
8268
fmt.Fprintln(w, "generated", fnC)
8369
}
8470
}
8571

86-
if GenerateCSFile {
72+
if GenerateTilCS {
8773
fnCS := fn + ".cs"
88-
msg.FatalOnErr(ilu.ToFileCSharp(fSys, fnCS))
74+
msg.FatalOnErr(ilu.ToFileTilCSharp(fSys, fnCS))
8975
if Verbose {
9076
fmt.Fprintln(w, "generated", fnCS)
9177
}
9278
}
9379

94-
if IDToFunctionPointerList {
95-
fmt.Fprintln(w, `CLI "-fpl" not implemented yet`)
80+
if GenerateRpcC {
81+
fnRPC := fn + "Rpc.c"
82+
msg.FatalOnErr(ilu.ToFileRpcC(fSys, fnRPC))
83+
if Verbose {
84+
fmt.Fprintln(w, "generated", fnRPC)
85+
}
9686
}
9787

9888
return nil
9989
}
10090

101-
// ToFileCHeader generates lang:C header file.
102-
func ToFileCHeader(fSys afero.Fs, fn string) (err error) {
91+
// ToFileTilCSharp generates C# helpers for a third party tool.
92+
func (ilu TriceIDLookUp) ToFileTilCSharp(fSys afero.Fs, fn string) (err error) {
10393
fh, err := fSys.Create(fn)
10494
msg.FatalOnErr(err)
10595
defer func() {
10696
err = fh.Close()
10797
msg.FatalOnErr(err)
10898
}()
10999

110-
h := []byte(`//! \file ` + fn + `
111-
//! ///////////////////////////////////////////////////////////////////////////
112-
113-
//! With Trice generated code - do not edit!
114-
115-
#include <stdint.h>
116-
117-
typedef struct{
118-
uint16_t id;
119-
uint8_t bitWidth;
120-
int16_t paramCount;
121-
char* formatString;
122-
} triceFormatStringList_t;
123-
124-
extern const triceFormatStringList_t triceFormatStringList[];
125-
extern const unsigned triceFormatStringListElements;
126-
`)
127-
_, err = fh.Write(h)
128-
msg.FatalOnErr(err)
129-
return
130-
}
131-
132-
// ToFileLangC generates lang:C helpers for a third party tool.
133-
func (ilu TriceIDLookUp) ToFileLangC(fSys afero.Fs, fn string) (err error) {
134-
fh, err := fSys.Create(fn)
135-
msg.FatalOnErr(err)
136-
137-
defer func() {
138-
err = fh.Close()
139-
msg.FatalOnErr(err)
140-
}()
141-
142-
c, err := ilu.toCFmtList(fn)
143-
msg.FatalOnErr(err)
144-
_, err = fh.Write(c)
145-
msg.FatalOnErr(err)
146-
return
147-
}
148-
149-
// toCFmtList converts lim into C-source byte slice in human-readable form.
150-
func (ilu TriceIDLookUp) toCFmtList(filename string) ([]byte, error) {
151-
var (
152-
fileNameBody = strings.TrimSuffix(filename, filepath.Ext(filename))
153-
text = []byte(`//! \file ` + filename + `
154-
//! ///////////////////////////////////////////////////////////////////////////
155-
156-
//! Trice generated code - do not edit!
157-
158-
#include "` + fileNameBody + `.h"
159-
160-
//! triceFormatStringList contains all trice format strings together with id and parameter information.
161-
//!
162-
//! The bitWidth value is not transmitted in the binary data stream and needed for its decoding.
163-
//! The paramCount is de-facto not needed. It is derivable from the received data, see docs/TriceUserManual.md/#binary-encoding.
164-
//! It is recommended to check if both values are matching. A negative paramCount indicates, that its value is unknown at compile time.
165-
const triceFormatStringList_t triceFormatStringList[] = {
166-
/* Trice type ( extended ) */ // id, bitWidth, paramCount, format-string
167-
`)
168-
tail = []byte(`};
169-
170-
//! triceFormatStringListElements holds the compile time computed count of list elements.
171-
const unsigned triceFormatStringListElements = sizeof(triceFormatStringList) / sizeof(triceFormatStringList_t);
172-
`)
173-
)
174-
defaultBitWidth, err := strconv.Atoi(DefaultTriceBitWidth)
175-
msg.FatalOnErr(err)
176-
177-
for id, t := range ilu {
178-
extType, bitWidth, paramCount := computeValues(t, defaultBitWidth)
179-
text = append(text, []byte(fmt.Sprintf(` /* %10s ( %10s ) */ { %5d, %3d, %2d, "%s" },`+"\n", t.Type, extType, id, bitWidth, paramCount, t.Strg))...)
180-
}
181-
182-
text = append(text, tail...)
183-
return text, nil
184-
}
185-
186-
func computeValues(t TriceFmt, defaultBitWidth int) (extType string, bitWidth, paramCount int) {
187-
switch t.Type[len(t.Type)-1:] {
188-
case "B", "F", "N", "S":
189-
paramCount = -1
190-
extType = t.Type
191-
default:
192-
paramCount = formatSpecifierCount(t.Strg)
193-
extType = ConstructFullTriceInfo(t.Type, paramCount)
194-
}
195-
bitWidth = defaultBitWidth
196-
for i, w := range []string{"8", "16", "32", "64"} {
197-
found := strings.Contains(extType, w)
198-
if found {
199-
bitWidth = 8 << i
200-
break
201-
}
202-
}
203-
return
204-
}
205-
206-
// ToFileCSharp generates C# helpers for a third party tool.
207-
func (ilu TriceIDLookUp) ToFileCSharp(fSys afero.Fs, fn string) (err error) {
208-
fh, err := fSys.Create(fn)
209-
msg.FatalOnErr(err)
210-
defer func() {
211-
err = fh.Close()
212-
msg.FatalOnErr(err)
213-
}()
214-
215-
cs, e := ilu.toCSFmtList(fn)
100+
cs, e := ilu.toListTilCS(fn)
216101
_, err = fh.Write(cs)
217102
msg.FatalOnErr(e)
218103
return
219104
}
220105

221-
// toCSFmtList converts ilu into CS-source byte slice in human-readable form.
222-
func (ilu TriceIDLookUp) toCSFmtList(filename string) ([]byte, error) {
223-
text := []byte(`//! \file til.cs
106+
// toListTilCS converts ilu into CS-source byte slice in human-readable form.
107+
func (ilu TriceIDLookUp) toListTilCS(filename string) ([]byte, error) {
108+
text := []byte(`//! \file ` + filename + `
224109
225110
// Trice generated code - do not edit!
226111
@@ -270,3 +155,44 @@ namespace TriceIDList;
270155
text = append(text, tail...)
271156
return text, nil
272157
}
158+
159+
func computeValues(t TriceFmt, defaultBitWidth int) (extType string, bitWidth, paramCount int) {
160+
switch t.Type[len(t.Type)-1:] {
161+
case "B", "F", "N", "S":
162+
paramCount = -1
163+
extType = t.Type
164+
default:
165+
paramCount = formatSpecifierCount(t.Strg)
166+
extType = ConstructFullTriceInfo(t.Type, paramCount)
167+
}
168+
bitWidth = defaultBitWidth
169+
for i, w := range []string{"8", "16", "32", "64"} {
170+
found := strings.Contains(extType, w)
171+
if found {
172+
bitWidth = 8 << i
173+
break
174+
}
175+
}
176+
return
177+
}
178+
179+
// ConstructFullTriceInfo returns full TRICE info, if not exist in triceType string
180+
func ConstructFullTriceInfo(triceType string, paramCount int) string {
181+
origType := triceType
182+
for _, name := range []string{"TRICE", "TRice", "Trice", "trice"} {
183+
if strings.HasPrefix(origType, name+"_") { // when no bit width, insert it
184+
triceType = name + DefaultTriceBitWidth + "_" + origType[6:]
185+
}
186+
if origType == name { // when plain trice name
187+
if paramCount == 0 { // no parameters
188+
triceType = name + "0" // special case
189+
} else { // append bit width and count
190+
triceType = fmt.Sprintf(name+DefaultTriceBitWidth+"_%d", paramCount)
191+
}
192+
}
193+
if origType == name+"8" || origType == name+"16" || origType == name+"32" || origType == name+"64" { // when no count
194+
triceType = fmt.Sprintf(origType+"_%d", paramCount) // append count
195+
}
196+
}
197+
return triceType
198+
}

0 commit comments

Comments
 (0)