-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathb35t-reader.rb
140 lines (129 loc) · 2.48 KB
/
b35t-reader.rb
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env nix-shell
#nix-shell -p ruby bluez -i ruby
mac_address = "98:84:E3:CD:BF:C9"
#i found mac address of my b35t by running `sudo hcitool lescan`
#and it showed up as '<MAC ADDRESS> BDM'
IO.popen("gatttool -b #{mac_address} --char-read --handle 0x2d --listen").each do |line|
line.chomp
if ! /Notification handle = 0x002e/.match(line) then
next
end
output = /Notification handle = 0x002e value: (.*)/
.match(line)[1]
.split(/ /)
.map {|x| x.hex }
digits = output[1..4].map {|x| x.chr}.join("").to_f
case output[6]
when 51
reading = digits/10.0
when 50
reading = digits/100.0
when 49
reading = digits/1000.0
else
reading = digits/1.0
end
case output[8]
when 16
maxmin = "min"
when 32
maxmin = "max"
else
maxmin = ""
end
case output[7]
when 0
mode = ""
when 1
mode = "Ohmmanual"
when 8
mode = "ACminmax"
if output[6] == 50 then
reading = reading* 10.0
end
when 9
mode = "ACmanual"
when 16
mode = "DCminmax"
if output[6] == 50 then
reading = reading* 10.0
end
when 17
mode = "DCmanual"
if output[6] == 50 then
reading = reading* 10.0
end
when 20
mode = "delta"
when 32
#this is hz, no ranging.
mode = ""
when 33
#this is hz, no ranging.
mode = "Ohmauto"
when 41
mode = "ACauto"
when 49
mode = "DCauto"
if output[6] == 50 then
reading = reading* 10.0
end
when 51
mode = "HOLD"
else
mode = "#{output[7]} mode unknown"
end
case output[9]
when 0
if output[10] == 4 then
reading = reading/10.0
unitPre = "n"
else
unitPre = ""
end
when 2
#hz duty cycle %
unitPre = "duty"
when 4
unitPre = "diode"
when 8
unitPre = ""
when 16
unitPre = "M"
when 32
unitPre = "K"
when 64
unitPre = "m"
if output[10] == 128 then
reading /= 10.0
end
when 128
unitPre = "u"
else
unitPre = "-#{output[9]} prefix unknown- "
end
case output[10]
when 0
unitType = "%"
when 1
unitType = "F"
when 2
unitType = "C"
when 4
unitType = "F"
when 8
unitType = "HZ"
when 16
unitType = "hFE"
when 32
unitType = "Ohm"
when 64
unitType = "A"
when 128
unitType = "V"
else
unitType = "-#{output[10]} unit unknown- "
end
bars = output[11]
puts ("%s%5.4g %s%s %s %s %s" % [ output[0].chr, reading, unitPre, unitType, mode, maxmin, "|" * bars])
end