Skip to content

Commit

Permalink
add new pin shadow blocks and field editor
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Aug 26, 2024
1 parent b4962a3 commit 90f09db
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 170 deletions.
4 changes: 2 additions & 2 deletions editor/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export function patchBlocks(pkgTargetVersion: string, dom: Element) {
let pinBlockType;
switch (oldPinNode.textContent.split(".")[0]) {
case "DigitalPin":
pinBlockType = "digital_pin";
pinBlockType = "digital_pin_shadow";
break;
case "AnalogPin":
pinBlockType = "analog_pin";
pinBlockType = "analog_pin_shadow";
break;
}
if (!pinBlockType) return;
Expand Down
15 changes: 11 additions & 4 deletions fieldeditors/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>

import { FieldGestures } from "./field_gestures";
import { FieldPinPicker } from "./field_pinPicker";

pxt.editor.initFieldExtensionsAsync = function (opts: pxt.editor.FieldExtensionOptions): Promise<pxt.editor.FieldExtensionResult> {
pxt.debug('loading pxt-microbit field editors...')
const res: pxt.editor.FieldExtensionResult = {
fieldEditors: [{
selector: "gestures",
editor: FieldGestures
}]
fieldEditors: [
{
selector: "gestures",
editor: FieldGestures
},
{
selector: "pinpicker",
editor: FieldPinPicker
}
]
};
return Promise.resolve<pxt.editor.FieldExtensionResult>(res);
}
93 changes: 93 additions & 0 deletions fieldeditors/field_pinPicker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/// <reference path="../node_modules/pxt-core/localtypings/pxtblockly.d.ts"/>

const pxtblockly = pxt.blocks.requirePxtBlockly()
const Blockly = pxt.blocks.requireBlockly();

const WARNING_ID = "pinpicker_warning";

export class FieldPinPicker extends pxtblockly.FieldGridPicker {
protected warningVisible: boolean;

override init() {
super.init();

const sourceBlock = this.sourceBlock_;
if (sourceBlock.isShadow() || sourceBlock.isInFlyout) {
return;
}

sourceBlock.workspace.addChangeListener(this.changeListener);
}

private changeListener = (e: any) => {
if (e.type === Blockly.Events.BLOCK_MOVE && e.blockId === this.sourceBlock_.id) {
this.updateWarning();
}
}

protected override doValueUpdate_(newValue: string): void {
super.doValueUpdate_(newValue);
this.updateWarning();
}

protected updateWarning() {
this.hideWarning();
const sourceBlock = this.sourceBlock_;

if (!sourceBlock || !this.value_ || sourceBlock.isShadow() || sourceBlock.isInFlyout) {
return;
}

const pin = this.value_.split(".")[1];

if (!isAnalogWriteOnlyPin(pin)) {
return;
}

const parent = sourceBlock.outputConnection.targetBlock();

if (!parent || parent.type !== "device_get_analog_pin") {
return;
}

this.showWarning(pin);
}

protected showWarning(pin: string) {
if (!this.sourceBlock_) {
return;
}
this.sourceBlock_.setWarningText(pxt.U.lf("{0} is a write only analog pin", pin), WARNING_ID);
}

protected hideWarning() {
if (!this.sourceBlock_) {
return;
}
this.sourceBlock_.setWarningText(null, WARNING_ID)
}

override dispose(): void {
super.dispose();
this.sourceBlock_?.workspace?.removeChangeListener(this.changeListener);
}
}

function isAnalogWriteOnlyPin(pin: string) {
switch (pin) {
case "P5":
case "P6":
case "P7":
case "P8":
case "P9":
case "P11":
case "P12":
case "P13":
case "P14":
case "P15":
case "P16":
return true;
default:
return false;
}
}
15 changes: 12 additions & 3 deletions libs/core/_locales/core-jsdoc-strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"Array.reduce": "Call the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.",
"Array.reduce|param|callbackfn": "A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the array.",
"Array.reduce|param|initialValue": "Initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.",
"Array.removeAt": "Remove the element at a certain index.",
"Array.removeAt": "Remove and return the element at a certain index.",
"Array.removeElement": "Remove the first occurence of an object. Returns true if removed.",
"Array.reverse": "Reverse the elements in an array. The first array element becomes the last, and the last array element becomes the first.",
"Array.set": "Store a value at a particular index",
Expand Down Expand Up @@ -554,6 +554,7 @@
"music.noteFrequency": "Gets the frequency of a note.",
"music.noteFrequency|param|name": "the note name",
"music.onEvent": "Registers code to run on various melody events",
"music.play": "Play a song, melody, or other sound. The music plays until finished or can play as a\nbackground task.",
"music.playMelody": "Play a melody from the melody editor.",
"music.playMelody|param|melody": "string of up to eight notes [C D E F G A B C5] or rests [-] separated by spaces, which will be played one at a time, ex: \"E D G F B A C5 B \"",
"music.playMelody|param|tempo": "number in beats per minute (bpm), dictating how long each note will play for",
Expand All @@ -563,6 +564,8 @@
"music.playTone": "Plays a tone through pin ``P0`` for the given duration.",
"music.playTone|param|frequency": "pitch of the tone to play in Hertz (Hz), eg: Note.C",
"music.playTone|param|ms": "tone duration in milliseconds (ms)",
"music.play|param|playbackMode": "play the song or melody until it's finished or as background task",
"music.play|param|toPlay": "the song or melody to play",
"music.rest": "Rests (plays nothing) for a specified time through pin ``P0``.",
"music.rest|param|ms": "rest duration in milliseconds (ms)",
"music.ringTone": "Plays a tone through pin ``P0``.",
Expand All @@ -581,6 +584,9 @@
"music.stopAllSounds": "Stop all sounds and melodies currently playing.",
"music.stopMelody": "Stops the melodies",
"music.stopMelody|param|options": "which melody to stop",
"music.stringPlayable": "Play a melody from the melody editor",
"music.stringPlayable|param|bpm": "number in beats per minute dictating how long each note will play",
"music.stringPlayable|param|melody": "string of up to eight notes [C D E F G A B C5] or rests [-] separated by spaces, which will be played one at a time, ex: \"E D G F B A C5 B \"",
"music.tempo": "Returns the tempo in beats per minute. Tempo is the speed (bpm = beats per minute) at which notes play. The larger the tempo value, the faster the notes will play.",
"music.tonePlayable": "Plays a tone through pin ``P0`` for the given duration.",
"music.tonePlayable|param|duration": "tone duration in milliseconds (ms)",
Expand Down Expand Up @@ -615,7 +621,11 @@
"pins.P7": "Pin P7",
"pins.P8": "Pin P8",
"pins.P9": "Pin P9",
"pins.analogPin": "Returns the value of a C++ runtime constant",
"pins._analogPin": "Returns the value of a C++ runtime constant",
"pins._analogPinShadow": "Returns the value of a C++ runtime constant",
"pins._analogReadWritePinShadow": "Returns the value of a C++ runtime constant",
"pins._digitalPin": "Returns the value of a C++ runtime constant",
"pins._digitalPinShadow": "Returns the value of a C++ runtime constant",
"pins.analogPitch": "Send a pulse-width modulation (PWM) signal to the current pitch pin. Use `analog set pitch pin` to define the pitch pin.",
"pins.analogPitchVolume": "Gets the volume the pitch pin from 0..255",
"pins.analogPitch|param|frequency": "frequency to modulate in Hz.",
Expand All @@ -634,7 +644,6 @@
"pins.analogWritePin|param|value": "value to write to the pin between ``0`` and ``1023``. eg:1023,0",
"pins.createBuffer": "Create a new zero-initialized buffer.",
"pins.createBuffer|param|size": "number of bytes in the buffer",
"pins.digitalPin": "Returns the value of a C++ runtime constant",
"pins.digitalReadPin": "Read the specified pin or connector as either 0 or 1",
"pins.digitalReadPin|param|name": "pin to read from, eg: DigitalPin.P0",
"pins.digitalWritePin": "Set a pin or connector value to either 0 or 1.",
Expand Down
34 changes: 19 additions & 15 deletions libs/core/_locales/core-strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
"AcceleratorRange.OneG|block": "1g",
"AcceleratorRange.TwoG": "The accelerator measures forces up to 2 gravity",
"AcceleratorRange.TwoG|block": "2g",
"AnalogPin.P11|block": "P11 (write only)",
"AnalogPin.P12|block": "P12 (write only)",
"AnalogPin.P13|block": "P13 (write only)",
"AnalogPin.P14|block": "P14 (write only)",
"AnalogPin.P15|block": "P15 (write only)",
"AnalogPin.P16|block": "P16 (write only)",
"AnalogPin.P19|block": "P19 (write only)",
"AnalogPin.P20|block": "P20 (write only)",
"AnalogPin.P5|block": "P5 (write only)",
"AnalogPin.P6|block": "P6 (write only)",
"AnalogPin.P7|block": "P7 (write only)",
"AnalogPin.P8|block": "P8 (write only)",
"AnalogPin.P9|block": "P9 (write only)",
"AnalogPin.P11|block": "P11",
"AnalogPin.P12|block": "P12",
"AnalogPin.P13|block": "P13",
"AnalogPin.P14|block": "P14",
"AnalogPin.P15|block": "P15",
"AnalogPin.P16|block": "P16",
"AnalogPin.P19|block": "P19",
"AnalogPin.P20|block": "P20",
"AnalogPin.P5|block": "P5",
"AnalogPin.P6|block": "P6",
"AnalogPin.P7|block": "P7",
"AnalogPin.P8|block": "P8",
"AnalogPin.P9|block": "P9",
"Array._pickRandom|block": "get random value from %list",
"Array._popStatement|block": "remove last value from %list",
"Array._removeAtStatement|block": "%list| remove value at %index",
Expand Down Expand Up @@ -254,6 +254,7 @@
"SoundExpressionPlayMode.InBackground|block": "in background",
"SoundExpressionPlayMode.UntilDone|block": "until done",
"String.charAt|block": "char from %this=text|at %pos",
"String.charCodeAt|block": "char code from $this=text|at $index",
"String.compare|block": "compare %this=text| to %that",
"String.fromCharCode|block": "text from char code %code",
"String.includes|block": "%this=text|includes %searchValue",
Expand Down Expand Up @@ -407,15 +408,18 @@
"music|block": "music",
"parseFloat|block": "parse to number %text",
"parseInt|block": "parse to integer %text",
"pins.analogPin|block": "analog pin %pin",
"pins._analogPinShadow|block": "$pin",
"pins._analogPin|block": "analog pin $pin",
"pins._analogReadWritePinShadow|block": "$pin",
"pins._digitalPinShadow|block": "$pin",
"pins._digitalPin|block": "digital pin $pin",
"pins.analogPitchVolume|block": "analog pitch volume",
"pins.analogPitch|block": "analog pitch %frequency|for (ms) %ms",
"pins.analogReadPin|block": "analog read|pin %name",
"pins.analogSetPeriod|block": "analog set period|pin %pin|to (µs)%micros",
"pins.analogSetPitchPin|block": "analog set pitch pin %name",
"pins.analogSetPitchVolume|block": "analog set pitch volume $volume",
"pins.analogWritePin|block": "analog write|pin %name|to %value",
"pins.digitalPin|block": "digital pin %pin",
"pins.digitalReadPin|block": "digital read|pin %name",
"pins.digitalWritePin|block": "digital write|pin %name|to %value",
"pins.i2cReadNumber|block": "i2c read number|at address %address|of format %format|repeated %repeat",
Expand Down
Loading

0 comments on commit 90f09db

Please sign in to comment.