2016-08-30 20:51:32 +02:00
|
|
|
namespace pxsim.input {
|
2022-03-22 17:36:19 +01:00
|
|
|
export function onPinTouchEvent(pinId: number, pinEvent: number, handler: RefAction) {
|
2016-08-30 20:51:32 +02:00
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.isTouched();
|
2019-12-02 05:58:26 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2022-03-22 17:36:19 +01:00
|
|
|
pxtcore.registerWithDal(pin.id, pinEvent, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
export function onPinPressed(pinId: number, handler: RefAction) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.isTouched();
|
|
|
|
runtime.queueDisplayUpdate();
|
2016-09-01 14:06:03 +02:00
|
|
|
pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_CLICK, handler);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 17:36:19 +01:00
|
|
|
// Deprecated
|
2016-08-30 20:51:32 +02:00
|
|
|
export function onPinReleased(pinId: number, handler: RefAction) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.isTouched();
|
2022-03-22 17:36:19 +01:00
|
|
|
runtime.queueDisplayUpdate();
|
2016-09-01 14:06:03 +02:00
|
|
|
pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_UP, handler);
|
2016-11-01 16:16:03 +01:00
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
|
|
|
|
export function pinIsPressed(pinId: number): boolean {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return false;
|
|
|
|
return pin.isTouched();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace pxsim {
|
|
|
|
export function getPin(id: number) {
|
|
|
|
return board().edgeConnectorState.getPin(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace pxsim.pins {
|
|
|
|
export function digitalReadPin(pinId: number): number {
|
|
|
|
let pin = getPin(pinId);
|
2019-12-02 05:58:26 +01:00
|
|
|
if (!pin) return -1;
|
2016-08-30 20:51:32 +02:00
|
|
|
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 = 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;
|
2020-09-08 11:04:25 +02:00
|
|
|
pin.setPull(pull);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function analogReadPin(pinId: number): number {
|
|
|
|
let pin = getPin(pinId);
|
2019-12-02 05:58:26 +01:00
|
|
|
if (!pin) return -1;
|
2016-08-30 20:51:32 +02:00
|
|
|
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 = PinFlags.Analog | PinFlags.Output;
|
2020-09-08 11:04:25 +02:00
|
|
|
pin.value = value | 0;
|
2016-08-30 20:51:32 +02:00
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function analogSetPeriod(pinId: number, micros: number) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
pin.mode = PinFlags.Analog | PinFlags.Output;
|
|
|
|
pin.period = micros;
|
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function servoWritePin(pinId: number, value: number) {
|
2016-11-08 07:25:19 +01:00
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
|
2016-08-30 20:51:32 +02:00
|
|
|
analogSetPeriod(pinId, 20000);
|
2019-12-02 05:58:26 +01:00
|
|
|
pin.servoAngle = value;
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 22:16:34 +01:00
|
|
|
export function servoSetContinuous(pinId: number, value: boolean) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
|
|
|
|
pin.servoSetContinuous(value);
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:51:32 +02:00
|
|
|
export function servoSetPulse(pinId: number, micros: number) {
|
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
export function analogSetPitchPin(pinId: number) {
|
2022-03-22 17:36:19 +01:00
|
|
|
const b = board();
|
|
|
|
if (!b) return;
|
2016-08-30 20:51:32 +02:00
|
|
|
let pin = getPin(pinId);
|
|
|
|
if (!pin) return;
|
2022-03-22 17:36:19 +01:00
|
|
|
const ec = b.edgeConnectorState
|
|
|
|
ec.pins.filter(p => !!p).forEach(p => p.pitch = false);
|
2016-08-30 20:51:32 +02:00
|
|
|
pin.pitch = true;
|
|
|
|
}
|
|
|
|
|
2022-03-22 17:36:19 +01:00
|
|
|
export function setSoundOutputPinEnabled(enabled: boolean) {
|
|
|
|
const b = board();
|
|
|
|
if (!b) return;
|
|
|
|
const ec = b.edgeConnectorState
|
|
|
|
ec.pitchEnabled = !enabled;
|
|
|
|
}
|
|
|
|
|
2020-09-08 11:04:25 +02:00
|
|
|
export function analogSetPitchVolume(volume: number) {
|
|
|
|
const ec = board().edgeConnectorState;
|
|
|
|
ec.pitchVolume = Math.max(0, Math.min(0xff, volume | 0));
|
2022-03-22 17:36:19 +01:00
|
|
|
AudioContextManager.setCurrentToneGain((ec.pitchVolume / 0xff) / 10);
|
2020-09-08 11:04:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function analogPitchVolume() {
|
|
|
|
const ec = board().edgeConnectorState;
|
|
|
|
return ec.pitchVolume;
|
|
|
|
}
|
2022-03-22 17:36:19 +01:00
|
|
|
|
2016-08-30 20:51:32 +02:00
|
|
|
export function analogPitch(frequency: number, ms: number) {
|
|
|
|
// update analog output
|
2020-09-08 11:04:25 +02:00
|
|
|
const b = board();
|
|
|
|
if (!b) return;
|
|
|
|
const ec = b.edgeConnectorState;
|
|
|
|
const pins = ec.pins;
|
2022-03-22 17:36:19 +01:00
|
|
|
const pin = ec.pitchEnabled && (pins.filter(pin => !!pin && pin.pitch)[0] || pins[0]);
|
2020-09-08 11:04:25 +02:00
|
|
|
const pitchVolume = ec.pitchVolume | 0;
|
2022-03-22 17:36:19 +01:00
|
|
|
if (pin) {
|
|
|
|
pin.mode = PinFlags.Analog | PinFlags.Output;
|
|
|
|
if (frequency <= 0 || pitchVolume <= 0) {
|
|
|
|
pin.value = 0;
|
|
|
|
pin.period = 0;
|
|
|
|
} else {
|
|
|
|
const v = 1 << (pitchVolume >> 5);
|
|
|
|
pin.value = v;
|
|
|
|
pin.period = 1000000 / frequency;
|
|
|
|
}
|
|
|
|
runtime.queueDisplayUpdate();
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let cb = getResume();
|
2022-03-22 17:36:19 +01:00
|
|
|
if (pin) {
|
|
|
|
const v = pitchVolume / 0xff;
|
|
|
|
AudioContextManager.tone(frequency, v / 10);
|
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
if (ms <= 0) cb();
|
|
|
|
else {
|
|
|
|
setTimeout(() => {
|
|
|
|
AudioContextManager.stop();
|
2022-03-22 17:36:19 +01:00
|
|
|
if (pin) {
|
|
|
|
pin.value = 0;
|
|
|
|
pin.period = 0;
|
|
|
|
pin.mode = PinFlags.Unused;
|
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
runtime.queueDisplayUpdate();
|
|
|
|
cb()
|
|
|
|
}, ms);
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 11:04:25 +02:00
|
|
|
|
|
|
|
export function pushButton(pinId: number) {
|
|
|
|
const b = board();
|
|
|
|
if (!b) return;
|
|
|
|
const ec = b.edgeConnectorState;
|
|
|
|
// TODO support buttons here
|
|
|
|
}
|
2022-03-22 17:36:19 +01:00
|
|
|
}
|
|
|
|
namespace pxsim.music {
|
|
|
|
export function setVolume(volume: number): void {
|
|
|
|
pxsim.pins.analogSetPitchVolume(volume);
|
|
|
|
}
|
|
|
|
export function volume(): number {
|
|
|
|
return pxsim.pins.analogPitchVolume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace pxsim.pins {
|
|
|
|
export function setAudioPin(pinId: number) {
|
|
|
|
pxsim.pins.analogSetPitchPin(pinId);
|
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|