-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdetect_tablet_mode
executable file
·69 lines (55 loc) · 1.66 KB
/
detect_tablet_mode
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
#!/usr/bin/env ruby
trap('INT'){}
def magnitude(v)
Math.sqrt v.map{|x| x*x}.reduce(:+)
end
def vector_product(v1,v2)
v1.zip(v2).map{|i1,i2| i1*i2}.reduce(:+)
end
def vector_angle(v1, v2)
Math.acos(vector_product(v1,v2) / (magnitude(v1)*magnitude(v2)))
end
def scale_vector(v, c)
v.map{|i| i*c}
end
def sign((y1,z1),(y2,z2))
val = z1*y2 - y1*z2
if val == 0 then 1 else val / val.abs end
end
def read_in(dev, in_name)
File.read("#{dev}/in_#{in_name}")
end
def read_float_in(dev, property_name, in_name)
read_in(dev, "#{property_name}_#{in_name}").to_f
end
def device(device_id)
"/sys/bus/iio/devices/iio:device#{device_id}"
end
def read_3d_sensor(device_id, property_name)
dev = device device_id
offset = read_float_in(dev, property_name, "offset")
scale = read_float_in(dev, property_name, "scale")
%w(x y z).map{|axis| (read_float_in(dev, property_name, "#{axis}_raw") + offset) * scale}
end
def read_angle(display_accel_id, keyboard_accel_id)
accel_3d_display = read_3d_sensor(display_accel_id, "accel")
accel_3d_keyboard = read_3d_sensor(keyboard_accel_id, "accel")
accel_2d_display = accel_3d_display[1..2]
accel_2d_keyboard = accel_3d_keyboard[1..2]
return sign(accel_2d_keyboard, accel_2d_display) *
vector_angle(accel_2d_keyboard, accel_2d_display)
end
def read_mode(display_accel_id, keyboard_accel_id)
angle = read_angle(display_accel_id, keyboard_accel_id)
if angle.abs > 2.8 or angle < -0.5
:tablet
else
:laptop
end
end
if ARGV.count != 2
puts "Usage: #{File.basename($0)} <display-accelerometer-id> <keyboard-accelerometer-id>"
puts "Example: #{File.basename($0)} 1 6"
else
puts read_mode(ARGV[0],ARGV[1])
end