diff options
author | jet tsang zeon-git <zeon-git@jettsang.com> | 2020-08-05 15:38:23 +0800 |
---|---|---|
committer | jet tsang zeon-git <zeon-git@jettsang.com> | 2020-08-05 15:38:23 +0800 |
commit | afde6c3829656022f7dc738870178bf9e48b0119 (patch) | |
tree | 3657759df29522455eb717acbfe2ef43faa57368 /utils | |
download | lobot-afde6c3829656022f7dc738870178bf9e48b0119.tar.gz lobot-afde6c3829656022f7dc738870178bf9e48b0119.tar.bz2 lobot-afde6c3829656022f7dc738870178bf9e48b0119.zip |
Initial scaffolding
Signed-off-by: jet tsang zeon-git <zeon-git@jettsang.com>
Diffstat (limited to 'utils')
-rw-r--r-- | utils/utils.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..4e16ba3 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,43 @@ +package utils + +import ( + "fmt" +) + +// BytesToInt converts a slice of bytes to an int. Only 8- and 16-bit uints are +// supported, because those are the only thing the control tables contain. +func BytesToInt(b []byte) (int, error) { + + switch len(b) { + case 1: + return int(b[0]), nil + + case 2: + return int(b[0]) | int(b[1])<<8, nil + + default: + return 0, fmt.Errorf("invalid read length %d", len(b)) + + } +} + +// BoolToInt converts a bool to an int. +func BoolToInt(b bool) int { + if b { + return 1 + } + return 0 +} + +// IntToBool converts an int to a bool. +func IntToBool(v int) bool { + return (v != 0) +} + +func Low(i uint16) byte { + return byte(i & 0xFF) +} + +func High(i uint16) byte { + return Low(i >> 8) +} |