diff --git a/canbaudrate.go b/canbaudrate.go index 68dca4d..402889d 100644 --- a/canbaudrate.go +++ b/canbaudrate.go @@ -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 + } +} diff --git a/canbaudrate_string.go b/canbaudrate_string.go deleted file mode 100644 index f100a0a..0000000 --- a/canbaudrate_string.go +++ /dev/null @@ -1,35 +0,0 @@ -// Code generated by "stringer -type CANBaudRate -trimprefix CANBaudRate"; DO NOT EDIT. - -package xsens - -import "strconv" - -func _() { - // An "invalid array index" compiler error signifies that the constant values have changed. - // Re-run the stringer command to generate them again. - var x [1]struct{} - _ = x[CANBaudRate1M-12] - _ = x[CANBaudRate800k-11] - _ = x[CANBaudRate500k-10] - _ = x[CANBaudRate250k-0] - _ = x[CANBaudRate125k-1] - _ = x[CANBaudRate100k-2] - _ = x[CANBaudRate83k3-3] - _ = x[CANBaudRate62k5-4] - _ = x[CANBaudRate50k-5] - _ = x[CANBaudRate33k3-6] - _ = x[CANBaudRate20k-7] - _ = x[CANBaudRate10k-8] - _ = x[CANBaudRate5k-9] -} - -const _CANBaudRate_name = "250k125k100k83k362k550k33k320k10k5k500k800k1M" - -var _CANBaudRate_index = [...]uint8{0, 4, 8, 12, 16, 20, 23, 27, 30, 33, 35, 39, 43, 45} - -func (i CANBaudRate) String() string { - if i < 0 || i >= CANBaudRate(len(_CANBaudRate_index)-1) { - return "CANBaudRate(" + strconv.FormatInt(int64(i), 10) + ")" - } - return _CANBaudRate_name[_CANBaudRate_index[i]:_CANBaudRate_index[i+1]] -} diff --git a/canbaudrateid_string.go b/canbaudrateid_string.go new file mode 100644 index 0000000..adcfdf8 --- /dev/null +++ b/canbaudrateid_string.go @@ -0,0 +1,35 @@ +// Code generated by "stringer -type CANBaudRateID -trimprefix CANBaudRateID"; DO NOT EDIT. + +package xsens + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[CANBaudRate1M-12] + _ = x[CANBaudRate800k-11] + _ = x[CANBaudRate500k-10] + _ = x[CANBaudRate250k-0] + _ = x[CANBaudRate125k-1] + _ = x[CANBaudRate100k-2] + _ = x[CANBaudRate83k3-3] + _ = x[CANBaudRate62k5-4] + _ = x[CANBaudRate50k-5] + _ = x[CANBaudRate33k3-6] + _ = x[CANBaudRate20k-7] + _ = x[CANBaudRate10k-8] + _ = x[CANBaudRate5k-9] +} + +const _CANBaudRateID_name = "CANBaudRate250kCANBaudRate125kCANBaudRate100kCANBaudRate83k3CANBaudRate62k5CANBaudRate50kCANBaudRate33k3CANBaudRate20kCANBaudRate10kCANBaudRate5kCANBaudRate500kCANBaudRate800kCANBaudRate1M" + +var _CANBaudRateID_index = [...]uint8{0, 15, 30, 45, 60, 75, 89, 104, 118, 132, 145, 160, 175, 188} + +func (i CANBaudRateID) String() string { + if i < 0 || i >= CANBaudRateID(len(_CANBaudRateID_index)-1) { + return "CANBaudRateID(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _CANBaudRateID_name[_CANBaudRateID_index[i]:_CANBaudRateID_index[i+1]] +} diff --git a/canconfig.go b/canconfig.go index 17d9e1e..145a832 100644 --- a/canconfig.go +++ b/canconfig.go @@ -6,7 +6,7 @@ import ( type CANConfig struct { Enable bool - BaudRate CANBaudRate + BaudRate CANBaudRateID } const ( @@ -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 }