Created
January 11, 2019 02:42
-
-
Save riaqn/d28e711cfe54e33a38dde3d8f117cdfe to your computer and use it in GitHub Desktop.
cardboard profile generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var ProtoBuf = require("protobufjs") | |
// to and from URL-safe variant of base64 encoding | |
function base64ToUrl(s) { | |
return s.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''); | |
} | |
function base64FromUrl(s) { | |
s = s + '==='.slice(0, [0, 3, 2, 1][s.length % 4]); | |
return s.replace(/-/g, '+').replace(/_/g, '/'); | |
} | |
var PARAMS_URI_PREFIX = 'http://www.sitesinvr.com/qrcode/index.html?'; | |
ProtoBuf.load('CardboardDevice.proto').then(function(root) { | |
var DeviceParams = root.lookupType("DeviceParams"); | |
function paramsToUri(params) { | |
var msg = DeviceParams.create(params); | |
var buf = DeviceParams.encode(msg).finish() | |
var dream = new Buffer("9a9160100801120a0d0000000015f4fdd43c1800", "hex") | |
var fin = Buffer.concat([buf, dream]) | |
return PARAMS_URI_PREFIX + base64ToUrl(fin.toString("base64")); | |
} | |
// TODO: support Cardboard v1 URI (i.e. default params) | |
function uriToParamsProto(uri) { | |
if (uri.substring(0, PARAMS_URI_PREFIX.length) !== PARAMS_URI_PREFIX) { | |
return; | |
} | |
var base64_msg = base64FromUrl(uri.substring(PARAMS_URI_PREFIX.length)); | |
// TODO: round numeric values | |
let buf = new Buffer(base64_msg, "base64") | |
return DeviceParams.decode(buf); | |
} | |
// Returns plain object having only properties of interest. | |
function uriToParams(uri) { | |
var source = uriToParamsProto(uri), dest = {}, k; | |
for (k in source) { | |
if (source.hasOwnProperty(k)) { | |
dest[k] = source[k]; | |
} | |
} | |
return dest; | |
} | |
var cfg = {} | |
cfg.vendor = "Baofeng" | |
cfg.model = "Mengjing" | |
cfg.screenToLensDistance = "0.041" | |
cfg.interLensDistance = 0.062 | |
cfg.leftEyeFieldOfViewAngles = | |
[ 48.85536575317383, | |
48.85536575317383, | |
48.85536575317383, | |
48.85536575317383 ] | |
cfg.trayToLensDistance = 0.037 | |
cfg.distortionCoefficients = [ 0.24498000741004944, 0.4219900071620941 ] | |
cfg.hasMagnet= false | |
cfg.verticalAlignment = 0 | |
cfg.primaryButton = 0 | |
console.info(cfg) | |
console.info(paramsToUri(cfg)) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment