5 i2cset -y 1 0x68 0x1b 0x08 # Set gyroscope to +/- 500º/sec (1º/s = 65.5)
6 i2cset -y 1 0x68 0x1c 0x08 # Set accelerometer to +/- 4g (1g = 8192)
8 read METHOD LOCATION PROTOCOL
9 while read name value; do
10 [ ! "${name%$CR}" ] && break
13 printf 'HTTP/1.1 200 OK\r\n'
16 Access-Control-Allow-Origin '*' \
17 Content-Type text/event-stream
20 da=0 db=0 dc=0 dn=0 dm=50
24 i2cdump -y 1 0x68 i || break
27 # pick Hex values, pair them and generate decimal calculations
30 s;^.{37}(.{15}).{24}(.{26}).{41}$; \1\2;;
32 s; (.)(.)(.)(.); 4096*\1+256*\2+16*\3+\4;g;
33 s;a;10;g; s;b;11;g; s;c;12;g; s;d;13;g; s;e;14;g; s;f;15;g;
35 | while read x y z t a b c; do
37 x=$(($x)) y=$(($y)) z=$(($z)) t=$(($t)) a=$(($a)) b=$(($b)) c=$(($c))
38 # convert from unsigned to signed
39 x=$((x - (x/32768 * 65536))) y=$((y - (y/32768 * 65536))) z=$((z - (z/32768 * 65536)))
40 a=$((a - (a/32768 * 65536))) b=$((b - (b/32768 * 65536))) c=$((c - (c/32768 * 65536)))
41 t=$((t - (t/32768 * 65536)))
43 # measure gyroscope drift
44 if [ $dn -lt $dm ]; then
46 da=$((da + a)) db=$((db + b)) dc=$((dc + c))
47 elif [ $dn -eq $dm ]; then
49 da=$((da / dm)) db=$((db / dm )) dc=$((dc / dm))
53 # scale to proper range
54 x=$((x * 9810 / 8192)) y=$((y * 9810 / 8192)) z=$((z * 9810 / 8192))
55 a=$(((a - da) * 125000 / 8192)) b=$(((b - db) * 125000 / 8192)) c=$(((c - dc) * 125000 / 8192))
58 # derive bearing from rotation
59 aa=$((aa + a / 10)) ab=$((ab + b / 10)) ac=$((ac + c / 10))
61 # print data and swap axis
62 # axis swap dependent on individual device layout
74 \n' $x $((-y)) $((-z)) \
76 $((aa * 12500 / 8192)) $((ab * 12500 / 8192)) $((ac * 12500 / 8192)) \
80 | sed -uE 's;^ +;;; s;[0-9]{3} ;.&;g; s;[0-9]{3}$;.&;; s;-\.;-0.;g;'