Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

chore: implement pin field #246

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@
},
"cli": {
"analytics": "6d8c3839-2848-4328-b7fb-a10f9273fdfc",
"schematicCollections": ["@angular-eslint/schematics"]
"schematicCollections": ["@angular-eslint/schematics"],
"cache": {
"enabled": false
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@fortawesome/fontawesome-free": "^6.1.1",
"@leaphy-robotics/avrdude-webassembly": "^1.6.1",
"@leaphy-robotics/dfu-util-wasm": "1.0.2",
"@leaphy-robotics/leaphy-blocks": "^2.1.0",
"@leaphy-robotics/leaphy-blocks": "3.0.0",
"@leaphy-robotics/picotool-wasm": "1.0.2",
"@leaphy-robotics/webusb-ftdi": "^1.0.0",
"@ngx-translate/core": "^14.0.0",
Expand Down
101 changes: 101 additions & 0 deletions src/app/domain/blockly-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { FieldConfig, FieldDropdown, MenuOption } from "blockly/core";
import { PinMapping, RobotType } from "./robot.type";

interface PinSelectorOptions extends FieldConfig {
mode: "digital" | "analog" | "pwm";
}

export default class PinSelectorField extends FieldDropdown {
static digitalPinOptions: MenuOption[];
static analogPinOptions: MenuOption[];
static pwmPinOptions: MenuOption[];

static processPinMappings(board: RobotType) {
let digitalPinRange: [number, number];
let analogPinRange: [number, number];

switch (board.mapping) {
case PinMapping.UNO: {
digitalPinRange = [2, 13];
analogPinRange = [0, 5];
this.pwmPinOptions = [
["3", "3"],
["5", "5"],
["6", "6"],
["9", "9"],
["10", "10"],
["11", "11"],
];
break;
}

case PinMapping.NANO: {
digitalPinRange = [2, 13];
analogPinRange = [0, 7];
this.pwmPinOptions = [
["3", "3"],
["5", "5"],
["6", "6"],
["9", "9"],
["10", "10"],
["11", "11"],
];
break;
}

case PinMapping.MEGA: {
digitalPinRange = [2, 53];
analogPinRange = [0, 15];
this.pwmPinOptions = [
["2", "2"],
["3", "3"],
["4", "4"],
["5", "5"],
["6", "6"],
["7", "7"],
["8", "8"],
["9", "9"],
["10", "10"],
["11", "11"],
["12", "12"],
["13", "13"],
];
break;
}
}

this.digitalPinOptions = [];
for (let pin = digitalPinRange[0]; pin <= digitalPinRange[1]; pin++) {
this.digitalPinOptions.push([pin.toString(), pin.toString()]);
}

this.analogPinOptions = [];
for (let pin = analogPinRange[0]; pin <= analogPinRange[1]; pin++) {
this.analogPinOptions.push([
`A${pin.toString()}`,
`A${pin.toString()}`,
]);
}
}

constructor(options: PinSelectorOptions) {
switch (options.mode) {
case "digital": {
super(PinSelectorField.digitalPinOptions, undefined, options);
break;
}
case "analog": {
super(PinSelectorField.analogPinOptions, undefined, options);
break;
}
case "pwm": {
super(PinSelectorField.pwmPinOptions, undefined, options);
break;
}
}
}

static fromJson(options: PinSelectorOptions) {
return new this(options);
}
}
11 changes: 11 additions & 0 deletions src/app/domain/robot.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ interface ProtocolInformation {
microcontroller?: string;
}

export enum PinMapping {
UNO,
NANO,
MEGA,
}

export class RobotType {
public features: Features;

constructor(
public id: string,
public protocol: ProtocolInformation,
public mapping: PinMapping,
public name: string,
public svgname: string,
public background: string,
Expand Down Expand Up @@ -65,6 +72,7 @@ export class BaseUno extends RobotType {
super(
id,
{ protocol: Avrdude, microcontroller: "atmega328p" },
PinMapping.UNO,
name,
svgname,
background,
Expand All @@ -88,6 +96,7 @@ export class BaseNano extends RobotType {
super(
id,
{ protocol: Avrdude, microcontroller: "atmega328p" },
PinMapping.NANO,
name,
svgname,
background,
Expand All @@ -111,6 +120,7 @@ export class BaseNanoESP32 extends RobotType {
super(
id,
{ protocol: DFU },
PinMapping.NANO,
name,
svgname,
background,
Expand All @@ -134,6 +144,7 @@ export class BaseNanoRP2040 extends RobotType {
super(
id,
{ protocol: Pico },
PinMapping.NANO,
name,
svgname,
background,
Expand Down
15 changes: 5 additions & 10 deletions src/app/domain/robots.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { arduino } from "@leaphy-robotics/leaphy-blocks";
import Avrdude from "../services/arduino-uploader/protocols/avrdude";
import {
BaseNano,
BaseNanoESP32,
BaseNanoRP2040,
BaseUno,
RobotType,
} from "./robot.type";
import DFU from "../services/arduino-uploader/protocols/dfu";
import Pico from "../services/arduino-uploader/protocols/pico";
import {BaseNano, BaseNanoESP32, BaseNanoRP2040, BaseUno, PinMapping, RobotType,} from "./robot.type";

const defaultLibraries = [
"Leaphy Extensions",
Expand Down Expand Up @@ -163,6 +154,7 @@ export const arduinoNanoRP2040RobotType = new BaseNanoRP2040(
export const microPythonRobotType = new RobotType(
"l_micropython",
{ protocol: Avrdude },
PinMapping.NANO,
"MicroPython",
"micropython.svg",
null,
Expand All @@ -178,6 +170,7 @@ export const microPythonRobotType = new RobotType(
export const arduinoMegaRobotType = new RobotType(
"l_mega",
{ protocol: Avrdude, microcontroller: "atmega2560" },
PinMapping.MEGA,
"Arduino Mega",
"mega.svg",
null,
Expand Down Expand Up @@ -211,6 +204,7 @@ export const arduinoUnoRobotTypeGeneric = new BaseUno(
export const arduinoMegaRobotTypeGeneric = new RobotType(
"l_mega",
{ protocol: Avrdude, microcontroller: "atmega2560" },
PinMapping.MEGA,
"Arduino Mega",
"mega.svg",
null,
Expand Down Expand Up @@ -238,6 +232,7 @@ export const arduinoNanoESP32RobotTypeGeneric = new BaseNanoESP32(
export const arduinoNanoEveryRobotTypeGeneric = new RobotType(
"l_nano_every",
{ protocol: Avrdude, microcontroller: "megaavr" },
PinMapping.NANO,
"Arduino Nano Every",
"nano.svg",
null,
Expand Down
43 changes: 23 additions & 20 deletions src/app/effects/blockly-editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ import * as Blockly from "blockly";
import "@blockly/field-bitmap";

import {
arduino,
blocks,
CATEGORIES,
THEME,
EXTENSIONS,
THEME,
translations,
arduino,
getBlocks,
constantBlocks,
} from "@leaphy-robotics/leaphy-blocks";
import { LeaphyCategory } from "../services/toolbox/category";
import { LeaphyToolbox } from "../services/toolbox/toolbox";
import { CodeEditorState } from "../state/code-editor.state";
import { genericRobotType, microPythonRobotType } from "../domain/robots";
import {
genericRobotType,
leaphyFlitzNanoRobotType,
microPythonRobotType,
} from "../domain/robots";
import { RobotType } from "../domain/robot.type";
import { WorkspaceService } from "../services/workspace.service";
import { LocalStorageService } from "../services/localstorage.service";
import PinSelectorField from "../domain/blockly-fields";

@Injectable({
providedIn: "root",
Expand All @@ -41,6 +45,8 @@ export class BlocklyEditorEffects {
private workspaceService: WorkspaceService,
private localStorage: LocalStorageService,
) {
Blockly.defineBlocksWithJsonArray(blocks);
Blockly.fieldRegistry.register("field_pin_selector", PinSelectorField);
Blockly.registry.register(
Blockly.registry.Type.TOOLBOX_ITEM,
Blockly.ToolboxCategory.registrationName,
Expand Down Expand Up @@ -74,13 +80,6 @@ export class BlocklyEditorEffects {
["controls_if_elseif", "controls_if_else"],
);

// When the current language is set: Find and set the blockly translations
this.appState.currentLanguage$
.pipe(filter((language) => !!language))
.subscribe(async (language) => {
Blockly.setLocale(translations[language.code]);
});

// When the language is changed, save the workspace temporarily
this.appState.changedLanguage$
.pipe(filter((language) => !!language))
Expand Down Expand Up @@ -130,13 +129,18 @@ export class BlocklyEditorEffects {
leaphyToolboxXml,
startWorkspaceXml,
]) => {
let allBlocks = getBlocks(robotType.id).block;
if (this.firstRun) {
this.firstRun = false;
allBlocks = allBlocks.concat(constantBlocks);
}
const translation =
translations[this.appState.currentLanguageCode];
if (robotType === leaphyFlitzNanoRobotType)
translation.ARD_SERVO_WRITE =
translation.ARD_SERVO_ARM_WRITE;
else
translation.ARD_SERVO_WRITE =
translation.ARD_SERVO_REGULAR_WRITE;

Blockly.defineBlocksWithJsonArray(allBlocks);
Blockly.setLocale(translation);

PinSelectorField.processPinMappings(robotType);
config.theme = Blockly.Theme.defineTheme("leaphy", {
blockStyles: THEME.defaultBlockStyles,
categoryStyles: THEME.categoryStyles,
Expand Down Expand Up @@ -205,12 +209,11 @@ export class BlocklyEditorEffects {
leaphyToolboxXml,
startWorkspaceXml,
]) => {
const toolboxXmlString = this.loadToolBox(
this.blocklyState.toolboxXml = this.loadToolBox(
baseToolboxXml,
leaphyToolboxXml,
robotType,
);
this.blocklyState.toolboxXml = toolboxXmlString;

workspace.clear();
const xml = Blockly.utils.xml.textToDom(startWorkspaceXml);
Expand Down
39 changes: 32 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1841,10 +1841,10 @@
resolved "https://registry.yarnpkg.com/@leaphy-robotics/dfu-util-wasm/-/dfu-util-wasm-1.0.2.tgz#29afcc01266655e1c7b5da0eeceb0ead48c86fd7"
integrity sha512-lvS+tJFO049b4ERYKX0HcoUtnQv76A1z9sZrAvbs7s3bvdMW0BObKmktZeJ7emcp3lU/+OJWsD2h0EY5rnkiNg==

"@leaphy-robotics/leaphy-blocks@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@leaphy-robotics/leaphy-blocks/-/leaphy-blocks-2.1.0.tgz#9f24ac65322634e808f6dd1ff74e1e0553ab1965"
integrity sha512-u9AlHl8I8njzqM+/yG39sWgeqFHlVuPXQK59uFdyStsSBk9IpF+e7Ogo13WdXV+ORB831NvyNlSOKNZQEFY/ag==
"@leaphy-robotics/leaphy-blocks@3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@leaphy-robotics/leaphy-blocks/-/leaphy-blocks-3.0.0.tgz#08ed2832646035686f8fc80d1c2c189f0c6e8884"
integrity sha512-+IcyDq3WLkbh758f+X103ysmd8D8PpPzinFHmoyEveYIUknGks5T2uSdTRKlI/7Dvv9Xp1+s5rX3/DD68CzEcg==
dependencies:
blockly "^10.4.3"

Expand Down Expand Up @@ -8542,7 +8542,16 @@ streamroller@^3.1.5:
debug "^4.3.4"
fs-extra "^8.1.0"

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -8574,7 +8583,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand All @@ -8588,6 +8597,13 @@ strip-ansi@^3.0.0:
dependencies:
ansi-regex "^2.0.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
Expand Down Expand Up @@ -9314,7 +9330,7 @@ wildcard@^2.0.0:
resolved "https://registry.yarnpkg.com/wildcard/-/wildcard-2.0.1.tgz#5ab10d02487198954836b6349f74fff961e10f67"
integrity sha512-CC1bOL87PIWSBhDcTrdeLo6eGT7mCFtrg0uIJtqJUFyK+eJnzl8A1niH56uu7KMa5XFrtiV+AQuHO3n7DsHnLQ==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -9332,6 +9348,15 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down
Loading