forked from Blendman974/faceclaw
First iteration of glanceboard
This commit is contained in:
1 parent
a3660fac61
commit
8a340fc4ad
36 files changed
+2874
-357
No files matched your search
+50
@@ -0,0 +1,50 @@
|
||||
package com.faceclaw.app;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class RingBatteryProtocolTest {
|
||||
private static byte[] hex(String value) {
|
||||
byte[] result = new byte[value.length() / 2];
|
||||
for (int i = 0; i < result.length; ++i)
|
||||
result[i] = (byte)Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16);
|
||||
return result;
|
||||
}
|
||||
private static BleProtocol.RingBatterySnapshot parse(byte[] payload) {
|
||||
byte[] frame = BleProtocol.framePb(payload, 9, BleProtocol.FLAG_NOTIFY, 15).get(0);
|
||||
return BleProtocol.parseRingBattery(BleProtocol.parseFrame(frame).pb);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
// Same golden payload as g2flash/tests/ring_battery_test.c.
|
||||
byte[] packet = hex("08031000d206055242010752");
|
||||
BleProtocol.RingBatterySnapshot result = parse(packet);
|
||||
assert result.battery == 82 && result.charging == 1;
|
||||
packet[10] = 3;
|
||||
for (int level = 0; level <= 100; ++level) {
|
||||
packet[11] = (byte)level;
|
||||
result = parse(packet);
|
||||
assert result.battery == level && result.charging == 0;
|
||||
}
|
||||
for (int flags : new int[] {0, 1}) {
|
||||
packet[10] = (byte)flags; packet[11] = (byte)255;
|
||||
result = parse(packet);
|
||||
assert result.battery == -1 && result.charging == -1;
|
||||
}
|
||||
packet = hex("08031000d206055242010752");
|
||||
for (int n = 0; n < packet.length; ++n)
|
||||
assert parse(Arrays.copyOf(packet, n)) == null;
|
||||
for (int offset : new int[] {7, 8, 9, 10, 11}) {
|
||||
byte previous = packet[offset]; packet[offset] = (byte)255;
|
||||
assert parse(packet) == null;
|
||||
packet[offset] = previous;
|
||||
}
|
||||
assert parse(hex("08031000")) == null; // older firmware
|
||||
assert parse(hex("d206055242010252")) == null; // valid without connected
|
||||
assert parse(hex("d206055242010352")).battery == 82;
|
||||
assert parse(hex("d2060552420100ff")).battery == -1;
|
||||
// Invalid unknown value and charging-without-valid must be rejected.
|
||||
assert parse(hex("d206055242010052")) == null;
|
||||
assert parse(hex("d2060552420105ff")) == null;
|
||||
assert parse(hex("d206055242010364")).battery == 100;
|
||||
assert parse(hex("d206055242010365")) == null;
|
||||
assert BleProtocol.parseRingBattery(null) == null;
|
||||
}
|
||||
}
|
||||
Vendored
+59
@@ -0,0 +1,59 @@
|
||||
// Execute the production worker, replacing only NativeScript/platform services.
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
const root = path.resolve(__dirname, '../..');
|
||||
|
||||
function load(file, modules = {}, extra = '', globals = {}) {
|
||||
const context = { exports: {}, console, ...globals, require: (name) => {
|
||||
if (!(name in modules)) throw new Error(`Unexpected dependency: ${name}`);
|
||||
return modules[name];
|
||||
} };
|
||||
const source = fs.readFileSync(path.join(root, file), 'utf8');
|
||||
vm.runInNewContext(ts.transpileModule(source, {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||
}).outputText + extra, context, { filename: file });
|
||||
return context.exports;
|
||||
}
|
||||
|
||||
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
|
||||
const font = (size) => BdfFont.parse(fs.readFileSync(path.join(root, `app/fonts/terminus/ter-u${size}n.bdf`), 'utf8'));
|
||||
const fonts = { terminus24: font(24), terminus32: font(32) };
|
||||
const smallFont = font(18);
|
||||
const graphics = load('app/graphics/image.ts', { './textwrap': load('app/graphics/textwrap.ts') });
|
||||
|
||||
module.exports = function worker() {
|
||||
let now = 10000;
|
||||
let highScore = '0';
|
||||
const noop = () => {};
|
||||
const global = { postMessage: noop };
|
||||
const api = load('app/apps/pinball/pinball-app.worker.ts', {
|
||||
'@nativescript/core/globals': {},
|
||||
'../../graphics/image': graphics,
|
||||
'../../graphics/plane': {}, '../../graphics/glyph-wire': {},
|
||||
'../../graphics/bdffont': { getFont: (name) => fonts[name] },
|
||||
'../../graphics/ui-fonts': { getDefaultSmallFont: () => smallFont },
|
||||
'../../native/frame-timings': { finishFrame: noop, logFrame: noop },
|
||||
'../../native/active-display': {},
|
||||
'../../native/settings-store': { getStringSetting: () => highScore, setStringSetting: (_, value) => { highScore = value; } },
|
||||
'../../ui/sound-effects': {},
|
||||
'../../ui/window-menu': { WindowMenu: class { open() {} } },
|
||||
'../../ui/gestures': { directionalFallback: (event) => event, GESTURE_CLICK: '●', GESTURE_DOUBLE_CLICK: '●●', GESTURE_SCROLL: '↕', GESTURE_LONG_PRESS: '—' },
|
||||
'../../util/numeric-util': { clamp: (v, lo, hi) => Math.max(lo, Math.min(hi, v)) },
|
||||
}, `
|
||||
renderAndSubmit = () => {};
|
||||
playSfx = () => {};
|
||||
exports.api = { windows, launchBall, ballDrained, stepPhysics, tick, flip, nudge,
|
||||
checkTargets, checkRollovers, collideBumpers, resetGame, paintContent, TARGETS, ROLLOVERS };
|
||||
`, { global, Date: { now: () => now }, setInterval: () => 1, clearInterval: noop }).api;
|
||||
const message = (data) => global.onmessage({ data });
|
||||
message({ type: 'open-window', windowId: 'test', surfaceId: 'test', title: 'Pinball', viewport: { width: 576, height: 260 } });
|
||||
message({ type: 'foreground', windowId: 'test', foreground: true, focused: true });
|
||||
const w = api.windows.get('test');
|
||||
return { ...api, w, message, highScore: () => Number(highScore),
|
||||
input: (type) => message({ type: 'input', windowId: 'test', focused: true, frameId: 1, event: { type } }),
|
||||
advance: (seconds) => { for (let i = 0; i < Math.round(seconds * 120); i++) { now += 1000 / 120; api.stepPhysics(w); } },
|
||||
elapse: (seconds) => { now += seconds * 1000; },
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user