Skip to content

Commit f06f9b8

Browse files
committed
Merge branch 'dfluff-master'
2 parents ff02016 + 809755b commit f06f9b8

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

examples/bargraph.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
3+
from icm20948 import ICM20948
4+
import time
5+
import math
6+
import argparse
7+
import os
8+
import signal
9+
import sys
10+
11+
BAR_CHAR = u'\u2588' # Unicode FULL BLOCK
12+
13+
running = True
14+
mingraphval = 0
15+
maxgraphval = 360
16+
17+
18+
# Terminal bar-graph adapted from examples/graph.py file from
19+
# https://github.com/pimoroni/vl53l1x-python
20+
def graphValue(value):
21+
global cols, mingraphval, maxgraphval
22+
if value > maxgraphval:
23+
value = maxgraphval
24+
elif value < mingraphval:
25+
value = mingraphval
26+
27+
graphvalue = value - mingraphval
28+
29+
bar_size = int((graphvalue / float(maxgraphval - mingraphval)) * (cols - 10)) # Scale bar_size to our terminal width
30+
bar = BAR_CHAR * bar_size # Create a bar out of `bar_size` unicode FULL BLOCK characters
31+
bar = bar.ljust(cols - 7, u' ') # Pad the bar to the full with of the terminal, minus the value prefix
32+
sys.stdout.write("\r") # Return the cursor to the beginning of the current line
33+
sys.stdout.write(u"{:05.1f} {}".format(value, bar)) # Output our measurement and bar
34+
sys.stdout.flush() # Flush the output buffer, since we're overdrawing the last line
35+
36+
37+
def exit_handler(signal, frame):
38+
global running, args
39+
running = False
40+
if args.graph:
41+
# Clean up terminal after using --graph output
42+
sys.stdout.write("\n")
43+
sys.exit(0)
44+
45+
46+
signal.signal(signal.SIGINT, exit_handler)
47+
48+
parser = argparse.ArgumentParser()
49+
parser.add_argument('--axis', choices=['xy', 'yz', 'xz'], default='yz', help="Axis to measure (default: yz)")
50+
parser.add_argument('--graph', '-g', action="store_true", default=False, help="Display heading as terminal-graph")
51+
args = parser.parse_args()
52+
53+
54+
if args.graph:
55+
try:
56+
rows, cols = [int(c) for c in os.popen("stty size", "r").read().split()]
57+
except ValueError:
58+
print("Cannot get size of tty! Try running in Terminal.")
59+
sys.exit(1)
60+
61+
62+
print("""bargraph.py - Convert raw values to heading
63+
64+
Rotate the sensor through 360 degrees to calibrate.
65+
66+
Press Ctrl+C to exit!
67+
68+
""")
69+
70+
X = 0
71+
Y = 1
72+
Z = 2
73+
74+
if args.axis == 'xy':
75+
AXES = X, Y
76+
elif args.axis == 'yz':
77+
AXES = Y, Z
78+
elif args.axis == 'xz':
79+
AXES = X, Z
80+
81+
82+
if args.graph:
83+
sys.stdout.write("\n")
84+
85+
86+
imu = ICM20948()
87+
88+
amin = list(imu.read_magnetometer_data())
89+
amax = list(imu.read_magnetometer_data())
90+
91+
while running:
92+
mag = list(imu.read_magnetometer_data())
93+
for i in AXES:
94+
v = mag[i]
95+
if v < amin[i]:
96+
amin[i] = v
97+
if v > amax[i]:
98+
amax[i] = v
99+
mag[i] -= amin[i]
100+
try:
101+
mag[i] /= amax[i] - amin[i]
102+
except ZeroDivisionError:
103+
pass
104+
mag[i] -= 0.5
105+
106+
heading = math.atan2(mag[AXES[0]], mag[AXES[1]])
107+
108+
if heading < 0:
109+
heading += 2 * math.pi
110+
heading = math.degrees(heading)
111+
112+
if args.graph:
113+
# Display the heading as a bar-graph in the terminal
114+
graphValue(heading)
115+
else:
116+
# Round the heading value and print out directly
117+
heading = round(heading)
118+
print("Heading: {}".format(heading))
119+
120+
time.sleep(0.1)

examples/magnetometer.py

100644100755
File mode changed.

0 commit comments

Comments
 (0)