Split analog.ts

This commit is contained in:
Michal Moskal 2017-07-10 11:37:14 +01:00
parent c23bf44e35
commit 1e3bce6d44
5 changed files with 219 additions and 213 deletions

84
libs/core/core.ts Normal file
View File

@ -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,
}

View File

@ -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()
}

109
libs/core/ir.ts Normal file
View File

@ -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()
}

View File

@ -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",

21
libs/core/touch.ts Normal file
View File

@ -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()
}