Implement button mmap
This commit is contained in:
parent
46c18af461
commit
4f6941e6cf
@ -1,10 +1,28 @@
|
|||||||
namespace input.internal {
|
namespace input.internal {
|
||||||
//% shim=pxt::unsafePollForChanges
|
//% shim=pxt::unsafePollForChanges
|
||||||
export function unsafePollForChanges(
|
export function unsafePollForChanges(
|
||||||
periodMs: int32,
|
periodMs: number,
|
||||||
query: () => int32,
|
query: () => number,
|
||||||
changeHandler: (prev: int32, curr: int32) => void
|
changeHandler: (prev: number, curr: number) => void
|
||||||
) { }
|
) {
|
||||||
|
// This is implemented in C++ without blocking the regular JS when query() is runnning
|
||||||
|
// which is generally unsafe. Query should not update globally visible state, and cannot
|
||||||
|
// call any yielding functions, like sleep().
|
||||||
|
|
||||||
|
// This is implementation for the simulator.
|
||||||
|
|
||||||
|
control.runInBackground(() => {
|
||||||
|
let prev = query()
|
||||||
|
while (true) {
|
||||||
|
loops.pause(periodMs)
|
||||||
|
let curr = query()
|
||||||
|
if (prev !== curr) {
|
||||||
|
changeHandler(prev, curr)
|
||||||
|
prev = curr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let analogMM: MMap
|
let analogMM: MMap
|
||||||
let uartMM: MMap
|
let uartMM: MMap
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
namespace pxsim {
|
namespace pxsim {
|
||||||
|
|
||||||
export class EV3ButtonState extends CommonButtonState{
|
export class EV3ButtonState extends CommonButtonState {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
@ -13,6 +13,24 @@ namespace pxsim {
|
|||||||
new CommonButton(DAL.BUTTON_ID_ESCAPE),
|
new CommonButton(DAL.BUTTON_ID_ESCAPE),
|
||||||
new CommonButton(DAL.BUTTON_ID_ALL)
|
new CommonButton(DAL.BUTTON_ID_ALL)
|
||||||
];
|
];
|
||||||
|
let data = new Uint8Array(this.buttons.length)
|
||||||
|
MMapMethods.register("/dev/lms_ui", {
|
||||||
|
data,
|
||||||
|
beforeMemRead: () => {
|
||||||
|
for (let i = 0; i < this.buttons.length; ++i)
|
||||||
|
data[i] = this.buttons[i].isPressed() ? 1 : 0
|
||||||
|
},
|
||||||
|
read: buf => {
|
||||||
|
let v = "vSIM"
|
||||||
|
for (let i = 0; i < buf.data.length; ++i)
|
||||||
|
buf.data[i] = v.charCodeAt(i) || 0
|
||||||
|
return buf.data.length
|
||||||
|
},
|
||||||
|
write: buf => {
|
||||||
|
pxsim.output.setLights(buf.data[0] - 48)
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,92 @@
|
|||||||
/// <reference path="../../libs/core/enums.d.ts"/>
|
/// <reference path="../../libs/core/enums.d.ts"/>
|
||||||
|
|
||||||
namespace pxsim.control {
|
namespace pxsim.MMapMethods {
|
||||||
|
export interface MMapImpl {
|
||||||
|
data?: Uint8Array;
|
||||||
|
afterMemWrite?: () => void;
|
||||||
|
beforeMemRead?: () => void;
|
||||||
|
read?: (d: Buffer) => number;
|
||||||
|
write?: (d: Buffer) => number;
|
||||||
|
ioctl?: (id: number, d: Buffer) => number;
|
||||||
|
}
|
||||||
|
|
||||||
export function mmap(filename: string, size: number, offset: number): void {
|
import BM = pxsim.BufferMethods
|
||||||
|
import NumberFormat = BM.NumberFormat
|
||||||
|
import Buffer = pxsim.RefBuffer
|
||||||
|
|
||||||
|
export class MMap extends pxsim.RefObject {
|
||||||
|
constructor(public impl: MMapImpl, public len: number) {
|
||||||
|
super()
|
||||||
|
if (!impl.data) impl.data = new Uint8Array(this.len)
|
||||||
|
if (!impl.afterMemWrite) impl.afterMemWrite = () => { }
|
||||||
|
if (!impl.beforeMemRead) impl.beforeMemRead = () => { }
|
||||||
|
if (!impl.read) impl.read = () => 0
|
||||||
|
if (!impl.write) impl.write = () => 0
|
||||||
|
if (!impl.ioctl) impl.ioctl = () => -1
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
}
|
||||||
|
buf(): Buffer {
|
||||||
|
return { data: this.impl.data } as any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const mmapRegistry: pxt.Map<MMapImpl> = {}
|
||||||
|
|
||||||
|
export function register(filename: string, impl: MMapImpl) {
|
||||||
|
mmapRegistry[filename] = impl
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setNumber(m: MMap, format: NumberFormat, offset: number, value: number): void {
|
||||||
|
BM.setNumber(m.buf(), format, offset, value)
|
||||||
|
m.impl.afterMemWrite();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNumber(m: MMap, format: NumberFormat, offset: number): number {
|
||||||
|
m.impl.beforeMemRead()
|
||||||
|
return BM.getNumber(m.buf(), format, offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function slice(m: MMap, offset?: number, length?: number): Buffer {
|
||||||
|
m.impl.beforeMemRead()
|
||||||
|
return BM.slice(m.buf(), offset, length)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function length(m: MMap): number {
|
||||||
|
m.impl.beforeMemRead()
|
||||||
|
return m.buf().data.length
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ioctl(m: MMap, id: number, data: Buffer): number {
|
||||||
|
return m.impl.ioctl(id, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function write(m: MMap, data: Buffer): number {
|
||||||
|
return m.impl.write(data)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function read(m: MMap, data: Buffer): number {
|
||||||
|
return m.impl.read(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace pxsim.control {
|
||||||
|
|
||||||
|
export function mmap(filename: string, size: number, offset: number): MMapMethods.MMap {
|
||||||
|
let impl = MMapMethods.mmapRegistry[filename]
|
||||||
|
if (!impl) impl = {}
|
||||||
|
return new MMapMethods.MMap(impl, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function dmesg(s: string) {
|
||||||
|
console.log("DMESG: " + s)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace pxsim.output {
|
||||||
|
export function createBuffer(size: number) {
|
||||||
|
return BufferMethods.createBuffer(size)
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user