forked from willy77/go-pigpio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.go
More file actions
118 lines (103 loc) · 2.63 KB
/
Copy pathbinary.go
File metadata and controls
118 lines (103 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package pigpio
import (
"bytes"
)
// convertToInt16 converts binary data to int16
func convertToInt16(data []byte) int16 {
switch len(data) {
case 0:
return 0
case 1:
return int16(data[0])
default:
return int16(data[0]) | (int16(data[1]) << 8)
}
}
// convertToUint16 converts binary data to uint16
func convertToUint16(data []byte) uint16 {
switch len(data) {
case 0:
return 0
case 1:
return uint16(data[0])
default:
return uint16(data[0]) | (uint16(data[1]) << 8)
}
}
// convertToInt32 converts binary data to int32
func convertToInt32(data []byte) int {
switch len(data) {
case 0:
return 0
case 1:
return int(data[0])
case 2:
return int(int32(data[0]) | (int32(data[1]) << 8))
case 3:
return int(int32(data[0]) | (int32(data[1]) << 8) |
(int32(data[2]) << 16))
default:
return int(int32(data[0]) | (int32(data[1]) << 8) |
(int32(data[2]) << 16) | (int32(data[3]) << 24))
}
}
// convertToUint32 converts binary data to uint32
func convertToUint32(data []byte) uint {
switch len(data) {
case 0:
return 0
case 1:
return uint(data[0])
case 2:
return uint(uint32(data[0]) | (uint32(data[1]) << 8))
case 3:
return uint(uint32(data[0]) | (uint32(data[1]) << 8) |
(uint32(data[2]) << 16))
default:
return uint(uint32(data[0]) | (uint32(data[1]) << 8) |
(uint32(data[2]) << 16) | (uint32(data[3]) << 24))
}
}
// convertToInt16Array converts binary data to []int16
func convertToInt16Array(data []byte) []int16 {
arr := make([]int16, 0)
for offset := 0; offset < len(data); offset += 2 {
arr = append(arr, convertToInt16(data[offset:]))
}
return arr
}
// convertToUint16Array converts binary data to []uint16
func convertToUint16Array(data []byte) []uint16 {
arr := make([]uint16, 0)
for offset := 0; offset < len(data); offset += 2 {
arr = append(arr, convertToUint16(data[offset:]))
}
return arr
}
// convertToInt32Array converts binary data to []int32
func convertToInt32Array(data []byte) []int {
arr := make([]int, 0)
for offset := 0; offset < len(data); offset += 4 {
arr = append(arr, convertToInt32(data[offset:]))
}
return arr
}
// convertToUint32Array converts binary data to []uint32
func convertToUint32Array(data []byte) []uint {
arr := make([]uint, 0)
for offset := 0; offset < len(data); offset += 4 {
arr = append(arr, convertToUint32(data[offset:]))
}
return arr
}
// convertToBytes converts int values to bytes (using int32 as base)
func convertToBytes(v ...int) []byte {
buf := new(bytes.Buffer)
for _, val := range v {
buf.WriteByte(byte(val))
buf.WriteByte(byte(val >> 8))
buf.WriteByte(byte(val >> 16))
buf.WriteByte(byte(val >> 24))
}
return buf.Bytes()
}