Skip to content

Commit

Permalink
Var map (#22)
Browse files Browse the repository at this point in the history
* 先改类型

* 能够删除和添加变量

* Solved #21 #19 #4

* 通过测试
  • Loading branch information
BigeYoung authored Apr 27, 2022
1 parent 31dbd66 commit a483813
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 202 deletions.
98 changes: 47 additions & 51 deletions datautil/datautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ func FindValidPart(data []byte) (int, int) {

// 从茫茫 data 中,寻找我所挂念的列表 y ,记录在列表 x 中。
// 所有的 add 我都难以忘记,所有的 del 我都不愿提起
func MakeChartPack(x *variable.ListChartT, add *variable.ListT, del *variable.ListT, y *variable.ListT, data []byte) {
var dataList variable.ListT

OUTER1:
func MakeChartPack(y *variable.ListT, data []byte) (x variable.ListChartT, add variable.ListT, del variable.ListT) {
x = variable.ListChartT{}
add = variable.ListT{} // 有些变量,我难以忘记
del = variable.ListT{} // 有些变量,我不愿提起
dataList := variable.ListT{}
for i := 0; i < len(data)/20; i++ {
// 变量的板子、心跳和地址
dataVar := variable.T{
Expand All @@ -79,58 +80,53 @@ OUTER1:
Tick: variable.BytesToUint32(data[i*20+15 : i*20+19])}

// 它是我要找的那个变量吗?
for _, v := range y.Variables {
if v.Addr == dataVar.Addr { // 是的,我还挂念着它
dataVar.Name = v.Name
dataVar.Type = v.Type
switch v.Type {
case "uint8_t":
dataVar.Data = float64(variable.BytesToUint8(data[i*20+7 : i*20+15]))
case "uint16_t":
dataVar.Data = float64(variable.BytesToUint16(data[i*20+7 : i*20+15]))
case "uint32_t":
dataVar.Data = float64(variable.BytesToUint32(data[i*20+7 : i*20+15]))
case "uint64_t":
dataVar.Data = float64(variable.BytesToUint64(data[i*20+7 : i*20+15]))
case "int8_t":
dataVar.Data = float64(variable.BytesToInt8(data[i*20+7 : i*20+15]))
case "int16_t":
dataVar.Data = float64(variable.BytesToInt16(data[i*20+7 : i*20+15]))
case "int32_t", "int":
dataVar.Data = float64(variable.BytesToInt32(data[i*20+7 : i*20+15]))
case "int64_t":
dataVar.Data = float64(variable.BytesToInt64(data[i*20+7 : i*20+15]))
case "float":
dataVar.Data = float64(variable.BytesToFloat32(data[i*20+7 : i*20+15]))
case "double":
dataVar.Data = float64(variable.BytesToFloat64(data[i*20+7 : i*20+15]))
default:
dataVar.Data = 0
}
dataList.Variables = append(dataList.Variables, dataVar)

chartVar := variable.ToChartT{
Board: dataVar.Board,
Name: dataVar.Name,
Data: dataVar.Data,
Tick: dataVar.Tick}
x.Variables = append(x.Variables, chartVar)
continue OUTER1
if v, ok := (*y)[dataVar.Addr]; ok { // 是的,我还挂念着它
dataVar.Name = v.Name
dataVar.Type = v.Type
switch v.Type {
case "uint8_t":
dataVar.Data = float64(variable.BytesToUint8(data[i*20+7 : i*20+15]))
case "uint16_t":
dataVar.Data = float64(variable.BytesToUint16(data[i*20+7 : i*20+15]))
case "uint32_t":
dataVar.Data = float64(variable.BytesToUint32(data[i*20+7 : i*20+15]))
case "uint64_t":
dataVar.Data = float64(variable.BytesToUint64(data[i*20+7 : i*20+15]))
case "int8_t":
dataVar.Data = float64(variable.BytesToInt8(data[i*20+7 : i*20+15]))
case "int16_t":
dataVar.Data = float64(variable.BytesToInt16(data[i*20+7 : i*20+15]))
case "int32_t", "int":
dataVar.Data = float64(variable.BytesToInt32(data[i*20+7 : i*20+15]))
case "int64_t":
dataVar.Data = float64(variable.BytesToInt64(data[i*20+7 : i*20+15]))
case "float":
dataVar.Data = float64(variable.BytesToFloat32(data[i*20+7 : i*20+15]))
case "double":
dataVar.Data = float64(variable.BytesToFloat64(data[i*20+7 : i*20+15]))
default:
dataVar.Data = 0
}
dataList[dataVar.Addr] = dataVar

chartVar := variable.ToChartT{
Board: dataVar.Board,
Name: dataVar.Name,
Data: dataVar.Data,
Tick: dataVar.Tick}
x = append(x, chartVar)
} else {
// 有些变量,我已不愿提起
del[dataVar.Addr] = dataVar
}
// 有些变量,我已不愿提起
del.Variables = append(del.Variables, dataVar)
}

// 我所挂念的,它们都还在吗
OUTER2:
for _, v := range y.Variables {
for _, dv := range dataList.Variables {
if dv.Addr == v.Addr {
continue OUTER2
}
for _, v := range *y {
if _, ok := dataList[v.Addr]; !ok {
// 我很想它,下次请别忘记
add[v.Addr] = v
}
// 我很想它,下次请别忘记
add.Variables = append(add.Variables, v)
}
return
}
97 changes: 41 additions & 56 deletions datautil/datautil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,39 +195,33 @@ func TestMakeChartPack(t *testing.T) {
0x0a,
},
listV: variable.ListT{
Variables: []variable.T{
{
Board: 1,
Name: "a",
Type: "float",
Addr: 0x80123456,
},
{
Board: 1,
Name: "b",
Type: "int",
Addr: 0x80654321,
},
0x80123456: {
Board: 1,
Name: "a",
Type: "float",
Addr: 0x80123456,
},
0x80654321: {
Board: 1,
Name: "b",
Type: "int",
Addr: 0x80654321,
},
},
wantChart: variable.ListChartT{
Variables: []variable.ToChartT{
{
Board: 1,
Name: "a",
Data: -8.25,
Tick: 1,
},
{
Board: 1,
Name: "a",
Data: -8.25,
Tick: 1,
},
},
wantAdd: variable.ListT{
Variables: []variable.T{
{
Board: 1,
Name: "b",
Type: "int",
Addr: 0x80654321,
},
0x80654321: {
Board: 1,
Name: "b",
Type: "int",
Addr: 0x80654321,
},
},
wantDel: variable.ListT{},
Expand All @@ -247,33 +241,27 @@ func TestMakeChartPack(t *testing.T) {
0x0a,
},
listV: variable.ListT{
Variables: []variable.T{
{
Board: 1,
Name: "a",
Type: "float",
Addr: 0x80123456,
},
0x80123456: {
Board: 1,
Name: "a",
Type: "float",
Addr: 0x80123456,
},
},
wantChart: variable.ListChartT{
Variables: []variable.ToChartT{
{
Board: 1,
Name: "a",
Data: -8.25,
Tick: 1,
},
{
Board: 1,
Name: "a",
Data: -8.25,
Tick: 1,
},
},
wantAdd: variable.ListT{},
wantDel: variable.ListT{
Variables: []variable.T{
{
Board: 1,
Type: "uint32_t",
Addr: 0x80654321,
},
0x80654321: {
Board: 1,
Type: "uint32_t",
Addr: 0x80654321,
},
},
},
Expand All @@ -286,17 +274,17 @@ func TestMakeChartPack(t *testing.T) {
return sa == sb, "Chart Not Same: " + sa + ", " + sb
}
isSame := func(a variable.ListT, b variable.ListT) (bool, string) {
if len(a.Variables) != len(b.Variables) {
if len(a) != len(b) {
return false, ""
}
for i := range a.Variables {
if a.Variables[i].Board != b.Variables[i].Board {
for i := range a {
if a[i].Board != b[i].Board {
return false, ""
}
if a.Variables[i].Addr != b.Variables[i].Addr {
if a[i].Addr != b[i].Addr {
return false, ""
}
if variable.TypeLen[a.Variables[i].Type] != variable.TypeLen[b.Variables[i].Type] {
if variable.TypeLen[a[i].Type] != variable.TypeLen[b[i].Type] {
return false, ""
}
}
Expand All @@ -308,10 +296,7 @@ func TestMakeChartPack(t *testing.T) {
return true, "List Not Same: " + sa + ", " + sb
}
for _, c := range cases {
var gotChart variable.ListChartT
var gotAdd variable.ListT
var gotDel variable.ListT
MakeChartPack(&gotChart, &gotAdd, &gotDel, &c.listV, c.in)
gotChart, gotAdd, gotDel := MakeChartPack(&c.listV, c.in)
if ok, msg := isSameChart(gotChart, c.wantChart); !ok {
t.Errorf("MakeChartPack, chart: " + msg)
}
Expand Down
16 changes: 9 additions & 7 deletions fromelf/fromelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Check(f *os.File) (*elf.File, error) {
func ReadVariable(x *variable.ListProjectT, f *elf.File) error {

// 排除一切杂念,听你娓娓道来
x.Variables = nil
x = &variable.ListProjectT{}

// 故事很长,从那一天说起吧
dwarfData, err := f.DWARF()
Expand Down Expand Up @@ -127,7 +127,7 @@ func ReadVariable(x *variable.ListProjectT, f *elf.File) error {
}

// 却总有些事,难以忘记
x.Variables = append(x.Variables, y)
(*x)[y.Addr] = y
}
}
}
Expand Down Expand Up @@ -171,11 +171,12 @@ func dfsStruct(namePrefix []string, addrPrefix uint32, x *variable.ListProjectT,
}

// 道出心底的秘密
x.Variables = append(x.Variables, variable.ToProjectT{
addr := fmt.Sprintf("0x%08x", a)
(*x)[addr] = variable.ToProjectT{
Name: strings.Join(namePrefix, ".") + "." + v.Name,
Addr: fmt.Sprintf("0x%08x", a),
Addr: addr,
Type: v.Type.String(),
})
}
}
}
}
Expand Down Expand Up @@ -219,11 +220,12 @@ func dfsArray(namePrefix []string, addrPrefix uint32, x *variable.ListProjectT,
if _, ok := variable.TypeLen[t]; !ok {
continue
}
x.Variables = append(x.Variables, variable.ToProjectT{
addr := fmt.Sprintf("0x%08x", a)
(*x)[addr] = variable.ToProjectT{
Name: strings.Join(namePrefix, ".") + ".[" + strconv.FormatInt(i, 10) + "]",
Addr: fmt.Sprintf("0x%08x", a),
Type: t,
})
}
}
addrPrefix = addrPrefix + uint32(t.Size())
}
Expand Down
14 changes: 5 additions & 9 deletions serial/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func GrReceive() {
buff := make([]byte, 200)
for {
<-chOp
for _, v := range variable.ToRead.Variables {
for _, v := range variable.ToRead {
SendCmd(datautil.ActModeSubscribe, v)
time.Sleep(10 * time.Millisecond)
}
Expand Down Expand Up @@ -163,7 +163,6 @@ func GrTransmit() {

func GrRxPrase(c chan string) {
var rxBuff []byte
var x variable.ListChartT
for {
rx := <-chRx // 收到你的来信
rxBuff = append(rxBuff, rx...) // 深藏我的心底
Expand All @@ -177,17 +176,14 @@ func GrRxPrase(c chan string) {
buff := rxBuff[startIdx:endIdx] // 撷取甜蜜的片段

// 拼凑出完整的清单
x.Variables = nil
var add variable.ListT // 有些变量,我难以忘记
var del variable.ListT // 有些变量,我不愿提起
datautil.MakeChartPack(&x, &add, &del, &variable.ToRead, buff)
if len(x.Variables) != 0 {
x, add, del := datautil.MakeChartPack(&variable.ToRead, buff)
if len(x) != 0 {
b, _ := json.Marshal(x)
c <- string(b)
}

// 挂念的变量,还望顺问近祺
for _, v := range add.Variables {
for _, v := range add {
err := SendCmd(datautil.ActModeSubscribe, v)
if err != nil {
logger.Log.Println("SendCmd error:", err)
Expand All @@ -196,7 +192,7 @@ func GrRxPrase(c chan string) {
}

// 无缘的变量,就请随风逝去
for _, v := range del.Variables {
for _, v := range del {
err := SendCmd(datautil.ActModeUnSubscribe, v)
if err != nil {
logger.Log.Println("SendCmd error:", err)
Expand Down
5 changes: 4 additions & 1 deletion serial/testport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ type testPort struct {
createdTime time.Time
}

var BoardSysTime time.Time = time.Now() // 虚拟电路板的系统时间

func newTestPort() serial.Port {
log.Println(BoardSysTime)
return &testPort{
readingAddresses: []uint32{},
createdTime: time.Now(),
createdTime: BoardSysTime,
}
}

Expand Down
Loading

0 comments on commit a483813

Please sign in to comment.