diff --git a/libs/core/core.ts b/libs/core/core.ts new file mode 100644 index 00000000..6587e2d8 --- /dev/null +++ b/libs/core/core.ts @@ -0,0 +1,84 @@ +namespace core { + let nextComponentId = 20000; + + export class Component { + protected _id: number; + constructor(id = 0) { + if (!id) id = ++nextComponentId + this._id = id + } + + getId() { + return this._id; + } + } +} + +const enum LMS { + NUM_INPUTS = 4, + LCD_WIDTH = 178, + LCD_HEIGHT = 128, + + DEVICE_TYPE_NXT_TOUCH = 1, + DEVICE_TYPE_NXT_LIGHT = 2, + DEVICE_TYPE_NXT_SOUND = 3, + DEVICE_TYPE_NXT_COLOR = 4, + DEVICE_TYPE_TACHO = 7, + DEVICE_TYPE_MINITACHO = 8, + DEVICE_TYPE_NEWTACHO = 9, + DEVICE_TYPE_TOUCH = 16, + DEVICE_TYPE_THIRD_PARTY_START = 50, + DEVICE_TYPE_THIRD_PARTY_END = 99, + DEVICE_TYPE_IIC_UNKNOWN = 100, + DEVICE_TYPE_NXT_TEST = 101, + DEVICE_TYPE_NXT_IIC = 123, + DEVICE_TYPE_TERMINAL = 124, + DEVICE_TYPE_UNKNOWN = 125, + DEVICE_TYPE_NONE = 126, + DEVICE_TYPE_ERROR = 127, + MAX_DEVICE_DATALENGTH = 32, + MAX_DEVICE_MODES = 8, + UART_BUFFER_SIZE = 64, + TYPE_NAME_LENGTH = 11, + SYMBOL_LENGTH = 4, + DEVICE_LOGBUF_SIZE = 300, + IIC_NAME_LENGTH = 8, + CONN_UNKNOWN = 111, + CONN_DAISYCHAIN = 117, + CONN_NXT_COLOR = 118, + CONN_NXT_DUMB = 119, + CONN_NXT_IIC = 120, + CONN_INPUT_DUMB = 121, + CONN_INPUT_UART = 122, + CONN_OUTPUT_DUMB = 123, + CONN_OUTPUT_INTELLIGENT = 124, + CONN_OUTPUT_TACHO = 125, + CONN_NONE = 126, + CONN_ERROR = 127, + opOutputGetType = 0xA0, + opOutputSetType = 0xA1, + opOutputReset = 0xA2, + opOutputStop = 0xA3, + opOutputPower = 0xA4, + opOutputSpeed = 0xA5, + opOutputStart = 0xA6, + opOutputPolarity = 0xA7, + opOutputRead = 0xA8, + opOutputTest = 0xA9, + opOutputReady = 0xAA, + opOutputPosition = 0xAB, + opOutputStepPower = 0xAC, + opOutputTimePower = 0xAD, + opOutputStepSpeed = 0xAE, + opOutputTimeSpeed = 0xAF, + opOutputStepSync = 0xB0, + opOutputTimeSync = 0xB1, + opOutputClearCount = 0xB2, + opOutputGetCount = 0xB3, + opOutputProgramStop = 0xB4, + + DEVICE_EVT_ANY = 0, + DEVICE_ID_NOTIFY = 10000, + DEVICE_ID_NOTIFY_ONE = 10001, +} + diff --git a/libs/core/analog.ts b/libs/core/input.ts similarity index 70% rename from libs/core/analog.ts rename to libs/core/input.ts index 7cdf2f2f..d5de41a2 100644 --- a/libs/core/analog.ts +++ b/libs/core/input.ts @@ -1,110 +1,3 @@ -const enum IrSensorMode { - None = -1, - Proximity = 0, - Seek = 1, - RemoteControl = 2, -} - -const enum IrRemoteChannel { - Ch0 = 0, // top - Ch1 = 1, - Ch2 = 2, - Ch3 = 3, -} - -const enum IrRemoteButton { - None = 0x00, - CenterBeacon = 0x01, - TopLeft = 0x02, - BottomLeft = 0x04, - TopRight = 0x08, - BottomRight = 0x10, -} - -namespace core { - let nextComponentId = 20000; - - export class Component { - protected _id: number; - constructor(id = 0) { - if (!id) id = ++nextComponentId - this._id = id - } - - getId() { - return this._id; - } - } -} - -const enum LMS { - NUM_INPUTS = 4, - LCD_WIDTH = 178, - LCD_HEIGHT = 128, - - DEVICE_TYPE_NXT_TOUCH = 1, - DEVICE_TYPE_NXT_LIGHT = 2, - DEVICE_TYPE_NXT_SOUND = 3, - DEVICE_TYPE_NXT_COLOR = 4, - DEVICE_TYPE_TACHO = 7, - DEVICE_TYPE_MINITACHO = 8, - DEVICE_TYPE_NEWTACHO = 9, - DEVICE_TYPE_TOUCH = 16, - DEVICE_TYPE_THIRD_PARTY_START = 50, - DEVICE_TYPE_THIRD_PARTY_END = 99, - DEVICE_TYPE_IIC_UNKNOWN = 100, - DEVICE_TYPE_NXT_TEST = 101, - DEVICE_TYPE_NXT_IIC = 123, - DEVICE_TYPE_TERMINAL = 124, - DEVICE_TYPE_UNKNOWN = 125, - DEVICE_TYPE_NONE = 126, - DEVICE_TYPE_ERROR = 127, - MAX_DEVICE_DATALENGTH = 32, - MAX_DEVICE_MODES = 8, - UART_BUFFER_SIZE = 64, - TYPE_NAME_LENGTH = 11, - SYMBOL_LENGTH = 4, - DEVICE_LOGBUF_SIZE = 300, - IIC_NAME_LENGTH = 8, - CONN_UNKNOWN = 111, - CONN_DAISYCHAIN = 117, - CONN_NXT_COLOR = 118, - CONN_NXT_DUMB = 119, - CONN_NXT_IIC = 120, - CONN_INPUT_DUMB = 121, - CONN_INPUT_UART = 122, - CONN_OUTPUT_DUMB = 123, - CONN_OUTPUT_INTELLIGENT = 124, - CONN_OUTPUT_TACHO = 125, - CONN_NONE = 126, - CONN_ERROR = 127, - opOutputGetType = 0xA0, - opOutputSetType = 0xA1, - opOutputReset = 0xA2, - opOutputStop = 0xA3, - opOutputPower = 0xA4, - opOutputSpeed = 0xA5, - opOutputStart = 0xA6, - opOutputPolarity = 0xA7, - opOutputRead = 0xA8, - opOutputTest = 0xA9, - opOutputReady = 0xAA, - opOutputPosition = 0xAB, - opOutputStepPower = 0xAC, - opOutputTimePower = 0xAD, - opOutputStepSpeed = 0xAE, - opOutputTimeSpeed = 0xAF, - opOutputStepSync = 0xB0, - opOutputTimeSync = 0xB1, - opOutputClearCount = 0xB2, - opOutputGetCount = 0xB3, - opOutputProgramStop = 0xB4, - - DEVICE_EVT_ANY = 0, - DEVICE_ID_NOTIFY = 10000, - DEVICE_ID_NOTIFY_ONE = 10001, -} - namespace input.internal { //% shim=pxt::unsafePollForChanges function unsafePollForChanges( @@ -514,23 +407,6 @@ namespace input.internal { } namespace input { - export class TouchSensor extends internal.AnalogSensor { - button: ButtonWrapper; - - constructor() { - super() - this.button = new ButtonWrapper() - } - - _query() { - return this._readPin6() > 2500 ? 1 : 0 - } - - _update(prev: number, curr: number) { - this.button.update(curr > 0) - } - } - export class ButtonWrapper extends core.Component { private downTime: number; private _isPressed: boolean; @@ -583,91 +459,4 @@ namespace input { control.onEvent(this._id, ev, body) } } - - function mapButton(v: number) { - switch (v) { - case 0: return IrRemoteButton.None - case 1: return IrRemoteButton.TopLeft - case 2: return IrRemoteButton.BottomLeft - case 3: return IrRemoteButton.TopRight - case 4: return IrRemoteButton.TopRight | IrRemoteButton.BottomRight - case 5: return IrRemoteButton.TopLeft | IrRemoteButton.TopRight - case 6: return IrRemoteButton.TopLeft | IrRemoteButton.BottomRight - case 7: return IrRemoteButton.BottomLeft | IrRemoteButton.TopRight - case 8: return IrRemoteButton.BottomLeft | IrRemoteButton.BottomRight - case 9: return IrRemoteButton.CenterBeacon - case 10: return IrRemoteButton.BottomLeft | IrRemoteButton.TopLeft - case 11: return IrRemoteButton.TopRight | IrRemoteButton.BottomRight - default: return IrRemoteButton.None - } - } - - export class IrSensor extends internal.UartSensor { - private channel: IrRemoteChannel - private pollRunning: boolean - private buttons: ButtonWrapper[]; - - constructor() { - super() - this.channel = IrRemoteChannel.Ch0 - this.buttons = [] - for (let i = 0; i < 5; ++i) { - this.buttons.push(new ButtonWrapper()) - } - } - - button(id: IrRemoteButton) { - let num = -1 - while (id) { - id >>= 1; - num++; - } - num = Math.clamp(0, this.buttons.length - 1, num) - return this.buttons[num] - } - - _query() { - if (this.mode == IrSensorMode.RemoteControl) - return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel)) - return 0 - } - - _update(prev: number, curr: number) { - for (let i = 0; i < this.buttons.length; ++i) { - let v = !!(curr & (1 << i)) - this.buttons[i].update(v) - } - } - - setRemoteChannel(c: IrRemoteChannel) { - c = Math.clamp(0, 3, c | 0) - this.channel = c - this.setMode(IrSensorMode.RemoteControl) - } - - setMode(m: IrSensorMode) { - this._setMode(m) - } - - getDistance() { - this.setMode(IrSensorMode.Proximity) - return this.getNumber(NumberFormat.UInt8LE, 0) - } - - getRemoteCommand() { - this.setMode(IrSensorMode.RemoteControl) - return this.getNumber(NumberFormat.UInt8LE, this.channel) - } - - getDirectionAndDistance() { - this.setMode(IrSensorMode.Seek) - return this.getNumber(NumberFormat.UInt16LE, this.channel * 2) - } - } - - - //% whenUsed - export const touch: TouchSensor = new TouchSensor() - //% whenUsed - export const ir: IrSensor = new IrSensor() -} \ No newline at end of file +} diff --git a/libs/core/ir.ts b/libs/core/ir.ts new file mode 100644 index 00000000..dcfaac12 --- /dev/null +++ b/libs/core/ir.ts @@ -0,0 +1,109 @@ +const enum IrSensorMode { + None = -1, + Proximity = 0, + Seek = 1, + RemoteControl = 2, +} + +const enum IrRemoteChannel { + Ch0 = 0, // top + Ch1 = 1, + Ch2 = 2, + Ch3 = 3, +} + +const enum IrRemoteButton { + None = 0x00, + CenterBeacon = 0x01, + TopLeft = 0x02, + BottomLeft = 0x04, + TopRight = 0x08, + BottomRight = 0x10, +} + +namespace input { + function mapButton(v: number) { + switch (v) { + case 0: return IrRemoteButton.None + case 1: return IrRemoteButton.TopLeft + case 2: return IrRemoteButton.BottomLeft + case 3: return IrRemoteButton.TopRight + case 4: return IrRemoteButton.TopRight | IrRemoteButton.BottomRight + case 5: return IrRemoteButton.TopLeft | IrRemoteButton.TopRight + case 6: return IrRemoteButton.TopLeft | IrRemoteButton.BottomRight + case 7: return IrRemoteButton.BottomLeft | IrRemoteButton.TopRight + case 8: return IrRemoteButton.BottomLeft | IrRemoteButton.BottomRight + case 9: return IrRemoteButton.CenterBeacon + case 10: return IrRemoteButton.BottomLeft | IrRemoteButton.TopLeft + case 11: return IrRemoteButton.TopRight | IrRemoteButton.BottomRight + default: return IrRemoteButton.None + } + } + + export class IrSensor extends internal.UartSensor { + private channel: IrRemoteChannel + private pollRunning: boolean + private buttons: ButtonWrapper[]; + + constructor() { + super() + this.channel = IrRemoteChannel.Ch0 + this.buttons = [] + for (let i = 0; i < 5; ++i) { + this.buttons.push(new ButtonWrapper()) + } + } + + button(id: IrRemoteButton) { + let num = -1 + while (id) { + id >>= 1; + num++; + } + num = Math.clamp(0, this.buttons.length - 1, num) + return this.buttons[num] + } + + _query() { + if (this.mode == IrSensorMode.RemoteControl) + return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel)) + return 0 + } + + _update(prev: number, curr: number) { + for (let i = 0; i < this.buttons.length; ++i) { + let v = !!(curr & (1 << i)) + this.buttons[i].update(v) + } + } + + setRemoteChannel(c: IrRemoteChannel) { + c = Math.clamp(0, 3, c | 0) + this.channel = c + this.setMode(IrSensorMode.RemoteControl) + } + + setMode(m: IrSensorMode) { + this._setMode(m) + } + + getDistance() { + this.setMode(IrSensorMode.Proximity) + return this.getNumber(NumberFormat.UInt8LE, 0) + } + + getRemoteCommand() { + this.setMode(IrSensorMode.RemoteControl) + return this.getNumber(NumberFormat.UInt8LE, this.channel) + } + + getDirectionAndDistance() { + this.setMode(IrSensorMode.Seek) + return this.getNumber(NumberFormat.UInt16LE, this.channel * 2) + } + } + + + //% whenUsed + export const ir: IrSensor = new IrSensor() +} diff --git a/libs/core/pxt.json b/libs/core/pxt.json index 3ebcf0fb..a9b7ed57 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -14,7 +14,10 @@ "screen.ts", "output.cpp", "output.ts", - "analog.ts", + "core.ts", + "input.ts", + "ir.ts", + "touch.ts", "shims.d.ts", "enums.d.ts", "dal.d.ts", diff --git a/libs/core/touch.ts b/libs/core/touch.ts new file mode 100644 index 00000000..7a552e1b --- /dev/null +++ b/libs/core/touch.ts @@ -0,0 +1,21 @@ +namespace input { + export class TouchSensor extends internal.AnalogSensor { + button: ButtonWrapper; + + constructor() { + super() + this.button = new ButtonWrapper() + } + + _query() { + return this._readPin6() > 2500 ? 1 : 0 + } + + _update(prev: number, curr: number) { + this.button.update(curr > 0) + } + } + + //% whenUsed + export const touch: TouchSensor = new TouchSensor() +}