Skip to content

Commit

Permalink
Class-ify touchscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgodbolt committed Dec 24, 2023
1 parent 027c0cf commit 42079a0
Showing 1 changed file with 55 additions and 48 deletions.
103 changes: 55 additions & 48 deletions touchscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,63 @@

import * as utils from "./utils.js";

export function TouchScreen(scheduler) {
const self = this;
const PollHz = 8; // Made up
const PollCycles = (2 * 1000 * 1000) / PollHz;
this.scheduler = scheduler;
this.mouse = [];
this.outBuffer = new utils.Fifo(16);
this.delay = 0;
this.mode = 0;
this.onMouse = function (x, y, button) {
this.mouse = { x: x, y: y, button: button };
};
this.poll = function () {
const PollHz = 8; // Made up
const PollCycles = (2 * 1000 * 1000) / PollHz;

function doScale(val, scale, margin) {
val = (val - margin) / (1 - 2 * margin);
return val * scale;
}

export class TouchScreen {
constructor(scheduler) {
this.scheduler = scheduler;
this.mouse = [];
this.outBuffer = new utils.Fifo(16);
this.delay = 0;
this.mode = 0;
this.pollTask = this.scheduler.newTask(this.poll);
}

tryReceive(rts) {
if (self.outBuffer.size && rts) return self.outBuffer.get();
return -1;
}

doRead() {
const scaleX = 120,
marginX = 0.13;
const scaleY = 100,
marginY = 0.03;
const scaledX = doScale(self.mouse.x, scaleX, marginX);
const scaledY = doScale(1 - self.mouse.y, scaleY, marginY);
const toSend = [0x4f, 0x4f, 0x4f, 0x4f];
const x = Math.min(255, Math.max(0, scaledX)) | 0;
const y = Math.min(255, Math.max(0, scaledY)) | 0;
if (self.mouse.button) {
toSend[0] = 0x40 | ((x & 0xf0) >>> 4);
toSend[1] = 0x40 | (x & 0x0f);
toSend[2] = 0x40 | ((y & 0xf0) >>> 4);
toSend[3] = 0x40 | (y & 0x0f);
}
for (let i = 0; i < 4; ++i) self.store(toSend[i]);
self.store(".".charCodeAt(0));
}

poll() {
self.doRead();
self.pollTask.reschedule(PollCycles);
};
this.pollTask = this.scheduler.newTask(this.poll);
this.onTransmit = function (val) {
}

store(byte) {
self.outBuffer.put(byte);
}

onMouse(x, y, button) {
this.mouse = { x: x, y: y, button: button };
}

onTransmit(val) {
switch (String.fromCharCode(val)) {
case "M":
self.mode = 0;
Expand All @@ -43,37 +82,5 @@ export function TouchScreen(scheduler) {
break;
}
self.pollTask.ensureScheduled(self.mode === 129 || self.mode === 130, PollCycles);
};
this.tryReceive = function (rts) {
if (self.outBuffer.size && rts) return self.outBuffer.get();
return -1;
};
this.store = function (byte) {
self.outBuffer.put(byte);
};

function doScale(val, scale, margin) {
val = (val - margin) / (1 - 2 * margin);
return val * scale;
}

this.doRead = function () {
const scaleX = 120,
marginX = 0.13;
const scaleY = 100,
marginY = 0.03;
const scaledX = doScale(self.mouse.x, scaleX, marginX);
const scaledY = doScale(1 - self.mouse.y, scaleY, marginY);
const toSend = [0x4f, 0x4f, 0x4f, 0x4f];
const x = Math.min(255, Math.max(0, scaledX)) | 0;
const y = Math.min(255, Math.max(0, scaledY)) | 0;
if (self.mouse.button) {
toSend[0] = 0x40 | ((x & 0xf0) >>> 4);
toSend[1] = 0x40 | (x & 0x0f);
toSend[2] = 0x40 | ((y & 0xf0) >>> 4);
toSend[3] = 0x40 | (y & 0x0f);
}
for (let i = 0; i < 4; ++i) self.store(toSend[i]);
self.store(".".charCodeAt(0));
};
}

0 comments on commit 42079a0

Please sign in to comment.