Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active March 31, 2022 21:00
Show Gist options
  • Save Utopiah/d4a7aba620fdb64af2e06895c4b14491 to your computer and use it in GitHub Desktop.
Save Utopiah/d4a7aba620fdb64af2e06895c4b14491 to your computer and use it in GitHub Desktop.
// sensor via https://wiki.postmarketos.org/wiki/PINE64_PinePhone_(pine64-pinephone)#Sensors
const x_path = "/sys/bus/iio/devices/iio:device3/in_accel_x_raw"
const y_path = "/sys/bus/iio/devices/iio:device3/in_accel_y_raw"
const z_path = "/sys/bus/iio/devices/iio:device3/in_accel_z_raw"
var fs = require("fs");
var express = require('express');
var app = express();
// could be WSS but fast enough on LAN as-is
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', function (req, res) {
var x = Number( fs.readFileSync(x_path, 'utf-8') )
var y = Number( fs.readFileSync(y_path, 'utf-8') )
var z = Number( fs.readFileSync(z_path, 'utf-8') )
res.end( JSON.stringify({x,y,z}) )
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("IMU data server listening at http://%s:%s", host, port)
})
/*
// client side, could be hosted in the target page or executed in the console or as a WebExtension for example
yprev=0,zprev=0
min_squared_sum=30000000 // magic value, depends on your calibration
// phone is expected to be in your pocket with a slight forward angle
min_diff=100
as = setInterval( () => {
fetch('http://pinephone:8081/') // assuming you have this address in hosts
// if content is served over https you'll have to allow mixed security or tunnel through https
.then( response => { return response.json() } )
.then( res => {
var moved =
//res.y*res.y + res.z*res.z > min_squared_sum,
// ( Math.abs(yprev-res.y) > min_diff ),
// ( Math.abs(zprev-res.z) > min_diff ),
(res.y*res.y + res.z*res.z > min_squared_sum
&& ( Math.abs(yprev-res.y) > min_diff )
&& ( Math.abs(zprev-res.z) > min_diff )
)
console.log(res, moved)
if (moved) window.scrollBy(0, 1) // adjust based on reading and walking speed
yprev=res.y
zprev=res.z
})
} , 100 );
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment