Initial sim implementation
This commit is contained in:
@ -68,6 +68,7 @@
|
||||
"motors|block": "motors",
|
||||
"output|block": "output",
|
||||
"screen|block": "screen",
|
||||
"sensors|block": "sensors",
|
||||
"serial|block": "serial",
|
||||
"{id:category}Brick": "Brick",
|
||||
"{id:category}Control": "Control",
|
||||
|
11
libs/core/input.cpp
Normal file
11
libs/core/input.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include "pxt.h"
|
||||
|
||||
namespace sensors {
|
||||
|
||||
/**
|
||||
* Mark a sensor as used
|
||||
*/
|
||||
//%
|
||||
void __sensorUsed(int port, int type) {
|
||||
}
|
||||
}
|
@ -142,6 +142,11 @@ namespace sensors.internal {
|
||||
this._port = port_ - 1
|
||||
init()
|
||||
sensorInfos[this._port].sensors.push(this)
|
||||
this.markUsed();
|
||||
}
|
||||
|
||||
markUsed() {
|
||||
sensors.__sensorUsed(this._port, this._deviceType());
|
||||
}
|
||||
|
||||
_activated() { }
|
||||
|
@ -20,3 +20,13 @@ void target_init() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace motors {
|
||||
|
||||
/**
|
||||
* Mark a motor as used
|
||||
*/
|
||||
//%
|
||||
void __motorUsed(int port, bool large) {
|
||||
}
|
||||
}
|
@ -236,6 +236,11 @@ namespace motors {
|
||||
constructor(port: Output, large: boolean) {
|
||||
super(port);
|
||||
this._large = large;
|
||||
this.markUsed();
|
||||
}
|
||||
|
||||
markUsed() {
|
||||
motors.__motorUsed(this._port, this._large);
|
||||
}
|
||||
|
||||
protected __init() {
|
||||
@ -347,6 +352,11 @@ namespace motors {
|
||||
|
||||
constructor(ports: Output) {
|
||||
super(ports);
|
||||
this.markUsed();
|
||||
}
|
||||
|
||||
markUsed() {
|
||||
motors.__motorUsed(this._port, true);
|
||||
}
|
||||
|
||||
protected __init() {
|
||||
|
@ -18,6 +18,7 @@
|
||||
"output.cpp",
|
||||
"output.ts",
|
||||
"core.ts",
|
||||
"input.cpp",
|
||||
"input.ts",
|
||||
"shims.d.ts",
|
||||
"enums.d.ts",
|
||||
|
16
libs/core/shims.d.ts
vendored
16
libs/core/shims.d.ts
vendored
@ -120,5 +120,21 @@ declare namespace output {
|
||||
//% shim=output::createBuffer
|
||||
function createBuffer(size: int32): Buffer;
|
||||
}
|
||||
declare namespace motors {
|
||||
|
||||
/**
|
||||
* Mark a motor as used
|
||||
*/
|
||||
//% shim=motors::__motorUsed
|
||||
function __motorUsed(port: int32, large: boolean): void;
|
||||
}
|
||||
declare namespace sensors {
|
||||
|
||||
/**
|
||||
* Mark a sensor as used
|
||||
*/
|
||||
//% shim=sensors::__sensorUsed
|
||||
function __sensorUsed(port: int32, type: int32): void;
|
||||
}
|
||||
|
||||
// Auto-generated. Do not edit. Really.
|
||||
|
@ -1,81 +0,0 @@
|
||||
namespace pxsim {
|
||||
enum ThresholdState {
|
||||
High,
|
||||
Low,
|
||||
Normal
|
||||
}
|
||||
|
||||
export class AnalogSensorState {
|
||||
public sensorUsed: boolean = false;
|
||||
|
||||
private level: number;
|
||||
private state = ThresholdState.Normal;
|
||||
|
||||
constructor(public id: number, private min = 0, private max = 255, private lowThreshold = 64, private highThreshold = 192) {
|
||||
this.level = Math.ceil((max - min) / 2);
|
||||
}
|
||||
|
||||
public setUsed() {
|
||||
if (!this.sensorUsed) {
|
||||
this.sensorUsed = true;
|
||||
runtime.queueDisplayUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public setLevel(level: number) {
|
||||
this.level = this.clampValue(level);
|
||||
|
||||
if (this.level >= this.highThreshold) {
|
||||
this.setState(ThresholdState.High);
|
||||
}
|
||||
else if (this.level <= this.lowThreshold) {
|
||||
this.setState(ThresholdState.Low);
|
||||
}
|
||||
else {
|
||||
this.setState(ThresholdState.Normal);
|
||||
}
|
||||
}
|
||||
|
||||
public getLevel(): number {
|
||||
return this.level;
|
||||
}
|
||||
|
||||
public setLowThreshold(value: number) {
|
||||
this.lowThreshold = this.clampValue(value);
|
||||
this.highThreshold = Math.max(this.lowThreshold + 1, this.highThreshold);
|
||||
}
|
||||
|
||||
public setHighThreshold(value: number) {
|
||||
this.highThreshold = this.clampValue(value);
|
||||
this.lowThreshold = Math.min(this.highThreshold - 1, this.lowThreshold);
|
||||
}
|
||||
|
||||
private clampValue(value: number) {
|
||||
if (value < this.min) {
|
||||
return this.min;
|
||||
}
|
||||
else if (value > this.max) {
|
||||
return this.max;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private setState(state: ThresholdState) {
|
||||
if (this.state === state) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.state = state;
|
||||
switch (state) {
|
||||
case ThresholdState.High:
|
||||
board().bus.queue(this.id, DAL.ANALOG_THRESHOLD_HIGH);
|
||||
break;
|
||||
case ThresholdState.Low:
|
||||
board().bus.queue(this.id, DAL.ANALOG_THRESHOLD_LOW);
|
||||
break;
|
||||
case ThresholdState.Normal:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,177 +0,0 @@
|
||||
|
||||
namespace pxsim.pins {
|
||||
export class CommonPin extends Pin {
|
||||
used: boolean;
|
||||
}
|
||||
|
||||
export class DigitalPin extends CommonPin {
|
||||
}
|
||||
|
||||
export class AnalogPin extends CommonPin {
|
||||
|
||||
}
|
||||
|
||||
export function markUsed(name: CommonPin) {
|
||||
if (!name.used) {
|
||||
name.used = true;
|
||||
runtime.queueDisplayUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace pxsim.DigitalPinMethods {
|
||||
export function digitalRead(name: pins.DigitalPin): number {
|
||||
return name.digitalReadPin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a pin or connector value to either 0 or 1.
|
||||
* @param value value to set on the pin, 1 eg,0
|
||||
*/
|
||||
export function digitalWrite(name: pins.DigitalPin, value: number): void {
|
||||
name.digitalWritePin(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures this pin to a digital input, and generates events where the timestamp is the duration
|
||||
* that this pin was either ``high`` or ``low``.
|
||||
*/
|
||||
export function onPulsed(name: pins.DigitalPin, pulse: number, body: RefAction): void {
|
||||
// TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the duration of a pulse in microseconds
|
||||
* @param value the value of the pulse (default high)
|
||||
* @param maximum duration in micro-seconds
|
||||
*/
|
||||
export function pulseIn(name: pins.DigitalPin, pulse: number, maxDuration = 2000000): number {
|
||||
// TODO
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the pull of this pin.
|
||||
* @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone
|
||||
*/
|
||||
export function setPull(name: pins.DigitalPin, pull: number): void {
|
||||
name.setPull(pull);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do something when a pin is pressed.
|
||||
* @param body the code to run when the pin is pressed
|
||||
*/
|
||||
export function onPressed(name: pins.DigitalPin, body: RefAction): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Do something when a pin is released.
|
||||
* @param body the code to run when the pin is released
|
||||
*/
|
||||
export function onReleased(name: pins.DigitalPin, body: RefAction): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pin state (pressed or not). Requires to hold the ground to close the circuit.
|
||||
* @param name pin used to detect the touch
|
||||
*/
|
||||
export function isPressed(name: pins.DigitalPin): boolean {
|
||||
return name.isTouched();
|
||||
}
|
||||
}
|
||||
|
||||
namespace pxsim.AnalogPinMethods {
|
||||
/**
|
||||
* Read the connector value as analog, that is, as a value comprised between 0 and 1023.
|
||||
*/
|
||||
export function analogRead(name: pins.AnalogPin): number {
|
||||
pins.markUsed(name);
|
||||
return name.analogReadPin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the connector value as analog. Value must be comprised between 0 and 1023.
|
||||
* @param value value to write to the pin between ``0`` and ``1023``. eg:1023,0
|
||||
*/
|
||||
export function analogWrite(name: pins.AnalogPin, value: number): void {
|
||||
pins.markUsed(name);
|
||||
name.analogWritePin(value);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the Pulse-width modulation (PWM) of the analog output to the given value in
|
||||
* **microseconds** or `1/1000` milliseconds.
|
||||
* If this pin is not configured as an analog output (using `analog write pin`), the operation has
|
||||
* no effect.
|
||||
* @param micros period in micro seconds. eg:20000
|
||||
*/
|
||||
export function analogSetPeriod(name: pins.AnalogPin, micros: number): void {
|
||||
pins.markUsed(name);
|
||||
name.analogSetPeriod(micros);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will
|
||||
* set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous
|
||||
* rotation servo, this will set the speed of the servo (with ``0`` being full-speed in one
|
||||
* direction, ``180`` being full speed in the other, and a value near ``90`` being no movement).
|
||||
* @param value angle or rotation speed, eg:180,90,0
|
||||
*/
|
||||
export function servoWrite(name: pins.AnalogPin, value: number): void {
|
||||
pins.markUsed(name);
|
||||
name.servoWritePin(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures this IO pin as an analog/pwm output, configures the period to be 20 ms, and sets the
|
||||
* pulse width, based on the value it is given **microseconds** or `1/1000` milliseconds.
|
||||
* @param micros pulse duration in micro seconds, eg:1500
|
||||
*/
|
||||
export function servoSetPulse(name: pins.AnalogPin, micros: number): void {
|
||||
pins.markUsed(name);
|
||||
// TODO fix pxt
|
||||
// name.servoSetPulse(micros);
|
||||
}
|
||||
}
|
||||
|
||||
namespace pxsim.PwmPinMethods {
|
||||
export function analogSetPeriod(name: pins.AnalogPin, micros: number): void {
|
||||
name.analogSetPeriod(micros);
|
||||
}
|
||||
|
||||
export function servoWrite(name: pins.AnalogPin, value: number): void {
|
||||
name.servoWritePin(value);
|
||||
}
|
||||
|
||||
export function servoSetPulse(name: pins.AnalogPin, micros: number): void {
|
||||
name.servoSetPulse(name.id, micros);
|
||||
}
|
||||
}
|
||||
|
||||
namespace pxsim.pins {
|
||||
export function pulseDuration(): number {
|
||||
// bus last event timestamp
|
||||
return 500;
|
||||
}
|
||||
|
||||
export function createBuffer(sz: number) {
|
||||
return pxsim.BufferMethods.createBuffer(sz)
|
||||
}
|
||||
|
||||
export function spiWrite(value: number): number {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
export function i2cReadBuffer(address: number, size: number, repeat?: boolean): RefBuffer {
|
||||
// fake reading zeros
|
||||
return createBuffer(size)
|
||||
}
|
||||
|
||||
export function i2cWriteBuffer(address: number, buf: RefBuffer, repeat?: boolean): void {
|
||||
// fake - noop
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user