pxt-calliope/sim/state/edgeconnector.ts
Amerlander 3e0c9b43a2
V4 updates for beta testing (#147)
* change simulator svg

* change radio image

* Remove google fonts cdn

* change color of 'advanced' button

* font fix

* font fix 2

* display fix

* change fullsceen simulator bg

* Continuous servo

* handle continuous state

* adding shims

* update rendering for continuous servos

* fixing sim

* fix sig

* typo

* fix sim

* bump pxt

* bump pxt

* rerun travis

* Input blocks revision

- add Button and Pin event types
- merge onPinPressed & onPinReleased in new onPinEvent function
- create new onButtonEvent function

* update input blocks in docs and tests

* remove device_pin_release block

* Hide DAL.x behind Enum

* bring back deprecated blocks, but hide them

* shims and locales files

* fix input.input. typing

* remove buildpr

* bump V3

* update simulator aspect ratio

* add Loudness Block

* revoke loudness block

* Adds soundLevel

To be replaced by pxt-common-packages when DAL is updated.

* Remove P0 & P3 from AnalogPin

* Fix Sound and replace AnalogPin.P0

* remove approved extensions

* V4 Updates from remote Repo

* locales

* add storage functions

* fix storage functions

* fix int/float values

* decrease decimal precision

* reorder blocks

* Update BLE Settings and Storage Blocks

* Fetch MicroBit changes up to v4.0.18

* Update timing for LED Matrix usage

* use 32kb ram (mini v2)

* resize gatt table

* Revert "use 32kb ram (mini v2)"

This reverts commit 4b15592f0f.

* fix missleading indentation

* add support for 32kb and 16kb ram

* only MIT extensions in preferredRepos

* remove extensions without MIT License file

* add updated extensions

* add extensions with MIT license

Co-authored-by: Juri <gitkraken@juriwolf.de>
Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 09:36:19 -07:00

196 lines
5.7 KiB
TypeScript

namespace pxsim.input {
export function onPinTouchEvent(pinId: number, pinEvent: number, handler: RefAction) {
let pin = getPin(pinId);
if (!pin) return;
pin.isTouched();
runtime.queueDisplayUpdate();
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();
pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_CLICK, handler);
}
// Deprecated
export function onPinReleased(pinId: number, handler: RefAction) {
let pin = getPin(pinId);
if (!pin) return;
pin.isTouched();
runtime.queueDisplayUpdate();
pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_UP, handler);
}
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);
if (!pin) return -1;
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;
pin.setPull(pull);
}
export function analogReadPin(pinId: number): number {
let pin = getPin(pinId);
if (!pin) return -1;
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;
pin.value = value | 0;
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) {
let pin = getPin(pinId);
if (!pin) return;
analogSetPeriod(pinId, 20000);
pin.servoAngle = value;
}
export function servoSetContinuous(pinId: number, value: boolean) {
let pin = getPin(pinId);
if (!pin) return;
pin.servoSetContinuous(value);
}
export function servoSetPulse(pinId: number, micros: number) {
let pin = getPin(pinId);
if (!pin) return;
// TODO
}
export function analogSetPitchPin(pinId: number) {
const b = board();
if (!b) return;
let pin = getPin(pinId);
if (!pin) return;
const ec = b.edgeConnectorState
ec.pins.filter(p => !!p).forEach(p => p.pitch = false);
pin.pitch = true;
}
export function setSoundOutputPinEnabled(enabled: boolean) {
const b = board();
if (!b) return;
const ec = b.edgeConnectorState
ec.pitchEnabled = !enabled;
}
export function analogSetPitchVolume(volume: number) {
const ec = board().edgeConnectorState;
ec.pitchVolume = Math.max(0, Math.min(0xff, volume | 0));
AudioContextManager.setCurrentToneGain((ec.pitchVolume / 0xff) / 10);
}
export function analogPitchVolume() {
const ec = board().edgeConnectorState;
return ec.pitchVolume;
}
export function analogPitch(frequency: number, ms: number) {
// update analog output
const b = board();
if (!b) return;
const ec = b.edgeConnectorState;
const pins = ec.pins;
const pin = ec.pitchEnabled && (pins.filter(pin => !!pin && pin.pitch)[0] || pins[0]);
const pitchVolume = ec.pitchVolume | 0;
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();
}
let cb = getResume();
if (pin) {
const v = pitchVolume / 0xff;
AudioContextManager.tone(frequency, v / 10);
}
if (ms <= 0) cb();
else {
setTimeout(() => {
AudioContextManager.stop();
if (pin) {
pin.value = 0;
pin.period = 0;
pin.mode = PinFlags.Unused;
}
runtime.queueDisplayUpdate();
cb()
}, ms);
}
}
export function pushButton(pinId: number) {
const b = board();
if (!b) return;
const ec = b.edgeConnectorState;
// TODO support buttons here
}
}
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);
}
}