--- /dev/null
+#!/bin/sh
+
+CR="$(printf \\r)"
+
+i2cset -y 1 0x68 0x1b 0x08 # Set gyroscope to +/- 500º/sec (1º/s = 65.5)
+i2cset -y 1 0x68 0x1c 0x08 # Set accelerometer to +/- 4g (1g = 8192)
+
+read METHOD LOCATION PROTOCOL
+while read name value; do
+ [ ! "${name%$CR}" ] && break
+done
+
+printf 'HTTP/1.1 200 OK\r\n'
+printf '%s: %s\r\n' \
+ Connection close \
+ Access-Control-Allow-Origin '*' \
+ Content-Type text/event-stream
+printf '\r\n'
+
+da=0 db=0 dc=0 dn=0 dm=50
+aa=0 ab=0 ac=0
+
+while sleep .1; do
+ i2cdump -y 1 0x68 i || break
+done \
+| sed -uEn '
+ # pick Hex values, pair them and generate decimal calculations
+ /^[3]0:/{
+ N;
+ s;^.{37}(.{15}).{24}(.{26}).{41}$; \1\2;;
+ s;(..) (..);\1\2;g;
+ s; (.)(.)(.)(.); 4096*\1+256*\2+16*\3+\4;g;
+ s;a;10;g; s;b;11;g; s;c;12;g; s;d;13;g; s;e;14;g; s;f;15;g;
+ p;}' \
+| while read x y z t a b c; do
+ # convert to decimal
+ x=$(($x)) y=$(($y)) z=$(($z)) t=$(($t)) a=$(($a)) b=$(($b)) c=$(($c))
+ # convert from unsigned to signed
+ x=$((x - (x/32768 * 65536))) y=$((y - (y/32768 * 65536))) z=$((z - (z/32768 * 65536)))
+ a=$((a - (a/32768 * 65536))) b=$((b - (b/32768 * 65536))) c=$((c - (c/32768 * 65536)))
+ t=$((t - (t/32768 * 65536)))
+
+ # measure gyroscope drift
+ if [ $dn -lt $dm ]; then
+ dn=$((dn + 1))
+ da=$((da + a)) db=$((db + b)) dc=$((dc + c))
+ elif [ $dn -eq $dm ]; then
+ dn=$((dn + 1))
+ da=$((da / dm)) db=$((db / dm )) dc=$((dc / dm))
+ aa=0 ab=0 ac=0
+ fi
+
+ # scale to proper range
+ x=$((x * 9810 / 8192)) y=$((y * 9810 / 8192)) z=$((z * 9810 / 8192))
+ a=$(((a - da) * 125000 / 8192)) b=$(((b - db) * 125000 / 8192)) c=$(((c - dc) * 125000 / 8192))
+ t=$((t * 1000 / 256))
+
+ # derive bearing from rotation
+ aa=$((aa + a / 10)) ab=$((ab + b / 10)) ac=$((ac + c / 10))
+
+ printf 'event: motion
+ data: %04i %04i %04i
+
+ event: rotation
+ data: %04i %04i %04i
+
+ event: bearing
+ data: %04i %04i %04i
+
+ event: temperature
+ data: %04i
+ \n' $x $y $z \
+ $a $b $c \
+ $((aa * 12500 / 8192)) $((ab * 12500 / 8192)) $((ac * 12500 / 8192)) \
+ $t \
+ || break
+done \
+| sed -uE 's;^ +;;; s;[0-9]{3} ;.&;g; s;[0-9]{3}$;.&;; s;-\.;-0.;g;'
-PACKAGES := ${PACKAGES} i2c-tools
+PACKAGES := ${PACKAGES} i2c-tools openbsd-inetd
.PHONY: gyro
${IMGFILE}: gyro
-gyro: imgmount root_copy
+gyro: imgmount root_copy files_gyro/
+ cp -a files_gyro/. "$<"
printf 'i2c-dev\n' >>"$</etc/modules"
+ printf '127.0.0.1:314 stream tcp nowait root /opt/gyro/mpu6050.sh mpu6050\n' >>"$</etc/inetd.conf"
+