full support for setPull
This commit is contained in:
@ -524,29 +524,35 @@ namespace pxsim.pins {
|
||||
export function digitalReadPin(pinId: number): number {
|
||||
let pin = getPin(pinId);
|
||||
if (!pin) return;
|
||||
pin.mode = PinMode.Digital | PinMode.Input;
|
||||
pin.mode = PinFlags.Digital | PinFlags.Input;
|
||||
return pin.value > 100 ? 1 : 0;
|
||||
}
|
||||
|
||||
export function digitalWritePin(pinId: number, value: number) {
|
||||
let pin = getPin(pinId);
|
||||
if (!pin) return;
|
||||
pin.mode = PinMode.Digital | PinMode.Output;
|
||||
pin.mode = PinFlags.Digital | PinFlags.Output;
|
||||
pin.value = value > 0 ? 1023 : 0;
|
||||
runtime.queueDisplayUpdate();
|
||||
}
|
||||
|
||||
export function setPull(pinId: number, pull: number) {
|
||||
let pin = getPin(pinId);
|
||||
if (!pin) return;
|
||||
pin.pull = pull;
|
||||
}
|
||||
|
||||
export function analogReadPin(pinId: number): number {
|
||||
let pin = getPin(pinId);
|
||||
if (!pin) return;
|
||||
pin.mode = PinMode.Analog | PinMode.Input;
|
||||
pin.mode = PinFlags.Analog | PinFlags.Input;
|
||||
return pin.value || 0;
|
||||
}
|
||||
|
||||
export function analogWritePin(pinId: number, value: number) {
|
||||
let pin = getPin(pinId);
|
||||
if (!pin) return;
|
||||
pin.mode = PinMode.Analog | PinMode.Output;
|
||||
pin.mode = PinFlags.Analog | PinFlags.Output;
|
||||
pin.value = value ? 1 : 0;
|
||||
runtime.queueDisplayUpdate();
|
||||
}
|
||||
@ -554,7 +560,7 @@ namespace pxsim.pins {
|
||||
export function analogSetPeriod(pinId: number, micros: number) {
|
||||
let pin = getPin(pinId);
|
||||
if (!pin) return;
|
||||
pin.mode = PinMode.Analog | PinMode.Output;
|
||||
pin.mode = PinFlags.Analog | PinFlags.Output;
|
||||
pin.period = micros;
|
||||
runtime.queueDisplayUpdate();
|
||||
}
|
||||
@ -580,7 +586,7 @@ namespace pxsim.pins {
|
||||
export function analogPitch(frequency: number, ms: number) {
|
||||
// update analog output
|
||||
let pin = board().pins.filter(pin => !!pin && pin.pitch)[0] || board().pins[0];
|
||||
pin.mode = PinMode.Analog | PinMode.Output;
|
||||
pin.mode = PinFlags.Analog | PinFlags.Output;
|
||||
if (frequency <= 0) {
|
||||
pin.value = 0;
|
||||
pin.period = 0;
|
||||
@ -598,7 +604,7 @@ namespace pxsim.pins {
|
||||
AudioContextManager.stop();
|
||||
pin.value = 0;
|
||||
pin.period = 0;
|
||||
pin.mode = PinMode.Unused;
|
||||
pin.mode = PinFlags.Unused;
|
||||
runtime.queueDisplayUpdate();
|
||||
cb()
|
||||
}, ms);
|
||||
|
@ -183,20 +183,20 @@ namespace pxsim.micro_bit {
|
||||
if (!pin) return;
|
||||
let text = this.pinTexts[index];
|
||||
let v = "";
|
||||
if (pin.mode & PinMode.Analog) {
|
||||
if (pin.mode & PinFlags.Analog) {
|
||||
v = Math.floor(100 - (pin.value || 0) / 1023 * 100) + "%";
|
||||
if (text) text.textContent = (pin.period ? "~" : "") + (pin.value || 0) + "";
|
||||
}
|
||||
else if (pin.mode & PinMode.Digital) {
|
||||
else if (pin.mode & PinFlags.Digital) {
|
||||
v = pin.value > 0 ? '0%' : '100%';
|
||||
if (text) text.textContent = pin.value > 0 ? "1" : "0";
|
||||
}
|
||||
else if (pin.mode & PinMode.Touch) {
|
||||
v = pin.touched ? '0%' : '100%';
|
||||
else if (pin.mode & PinFlags.Touch) {
|
||||
v = pin.touched ? "0%" : "100%";
|
||||
if (text) text.textContent = "";
|
||||
} else {
|
||||
v = '100%';
|
||||
if (text) text.textContent = '';
|
||||
v = "100%";
|
||||
if (text) text.textContent = "";
|
||||
}
|
||||
if (v) svg.setGradientValue(this.pinGradients[index], v);
|
||||
}
|
||||
@ -651,7 +651,7 @@ svg.sim.grayscale {
|
||||
let state = this.board;
|
||||
let pin = state.pins[index];
|
||||
let svgpin = this.pins[index];
|
||||
if (pin.mode & PinMode.Input) {
|
||||
if (pin.mode & PinFlags.Input) {
|
||||
let cursor = svg.cursorPoint(pt, this.element, ev);
|
||||
let v = (400 - cursor.y) / 40 * 1023
|
||||
pin.value = Math.max(0, Math.min(1023, Math.floor(v)));
|
||||
@ -664,7 +664,7 @@ svg.sim.grayscale {
|
||||
let pin = state.pins[index];
|
||||
let svgpin = this.pins[index];
|
||||
svg.addClass(svgpin, "touched");
|
||||
if (pin.mode & PinMode.Input) {
|
||||
if (pin.mode & PinFlags.Input) {
|
||||
let cursor = svg.cursorPoint(pt, this.element, ev);
|
||||
let v = (400 - cursor.y) / 40 * 1023
|
||||
pin.value = Math.max(0, Math.min(1023, Math.floor(v)));
|
||||
|
@ -8,7 +8,7 @@ namespace pxsim {
|
||||
greyscale
|
||||
}
|
||||
|
||||
export enum PinMode {
|
||||
export enum PinFlags {
|
||||
Unused = 0,
|
||||
Digital = 0x0001,
|
||||
Analog = 0x0002,
|
||||
@ -22,11 +22,12 @@ namespace pxsim {
|
||||
touched = false;
|
||||
value = 0;
|
||||
period = 0;
|
||||
mode = PinMode.Unused;
|
||||
mode = PinFlags.Unused;
|
||||
pitch = false;
|
||||
pull = 0; // PullDown
|
||||
|
||||
isTouched(): boolean {
|
||||
this.mode = PinMode.Touch;
|
||||
this.mode = PinFlags.Touch;
|
||||
return this.touched;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user