Skip to content

Commit

Permalink
feat: add conversion CAN baudrate to CAN baudrate ID
Browse files Browse the repository at this point in the history
  • Loading branch information
tvkn committed Mar 3, 2022
1 parent c35a1d8 commit 97ba7d8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 52 deletions.
66 changes: 51 additions & 15 deletions canbaudrate.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
package xsens

//go:generate stringer -type CANBaudRate -trimprefix CANBaudRate
//go:generate stringer -type CANBaudRateID -trimprefix CANBaudRateID

type CANBaudRate int8
type (
CANBaudRateID int8
CANBaudRate int
)

const (
CANBaudRate1M CANBaudRate = 0x0C
CANBaudRate800k CANBaudRate = 0x0B
CANBaudRate500k CANBaudRate = 0x0A
CANBaudRate250k CANBaudRate = 0x00
CANBaudRate125k CANBaudRate = 0x01
CANBaudRate100k CANBaudRate = 0x02
CANBaudRate83k3 CANBaudRate = 0x03
CANBaudRate62k5 CANBaudRate = 0x04
CANBaudRate50k CANBaudRate = 0x05
CANBaudRate33k3 CANBaudRate = 0x06
CANBaudRate20k CANBaudRate = 0x07
CANBaudRate10k CANBaudRate = 0x08
CANBaudRate5k CANBaudRate = 0x09
CANBaudRate1M CANBaudRateID = 0x0C
CANBaudRate800k CANBaudRateID = 0x0B
CANBaudRate500k CANBaudRateID = 0x0A
CANBaudRate250k CANBaudRateID = 0x00
CANBaudRate125k CANBaudRateID = 0x01
CANBaudRate100k CANBaudRateID = 0x02
CANBaudRate83k3 CANBaudRateID = 0x03
CANBaudRate62k5 CANBaudRateID = 0x04
CANBaudRate50k CANBaudRateID = 0x05
CANBaudRate33k3 CANBaudRateID = 0x06
CANBaudRate20k CANBaudRateID = 0x07
CANBaudRate10k CANBaudRateID = 0x08
CANBaudRate5k CANBaudRateID = 0x09
)

func (c CANBaudRate) ID() (CANBaudRateID, error) {
switch c {
case 5000:
return CANBaudRate5k, nil
case 10000:
return CANBaudRate10k, nil
case 20000:
return CANBaudRate20k, nil
case 33300:
return CANBaudRate33k3, nil
case 50000:
return CANBaudRate50k, nil
case 62500:
return CANBaudRate62k5, nil
case 83300:
return CANBaudRate83k3, nil
case 100000:
return CANBaudRate100k, nil
case 125000:
return CANBaudRate125k, nil
case 250000:
return CANBaudRate250k, nil
case 500000:
return CANBaudRate500k, nil
case 800000:
return CANBaudRate800k, nil
case 1000000:
return CANBaudRate1M, nil
default:
return -1, nil
}
}
35 changes: 0 additions & 35 deletions canbaudrate_string.go

This file was deleted.

35 changes: 35 additions & 0 deletions canbaudrateid_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions canconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type CANConfig struct {
Enable bool
BaudRate CANBaudRate
BaudRate CANBaudRateID
}

const (
Expand Down Expand Up @@ -38,6 +38,6 @@ func (o *CANConfig) UnmarshalBinary(data []byte) error {
return fmt.Errorf("cannot unmarshal to a nil pointer")
}
o.Enable = (data[canCfgEnableOffset] & canCfgEnableMask) == 1
o.BaudRate = CANBaudRate(data[canCfgBaudrateOffset] & canCfgBaudrateMask)
o.BaudRate = CANBaudRateID(data[canCfgBaudrateOffset] & canCfgBaudrateMask)
return nil
}

0 comments on commit 97ba7d8

Please sign in to comment.