diff --git a/src/blocks/mrc_port.ts b/src/blocks/mrc_port.ts index 2c355171..eeb2ad0f 100644 --- a/src/blocks/mrc_port.ts +++ b/src/blocks/mrc_port.ts @@ -25,6 +25,7 @@ import { Order } from 'blockly/python'; import { MRC_STYLE_PORTS } from '../themes/styles' import { createFieldNonEditableText } from '../fields/FieldNonEditableText'; import { ExtendedPythonGenerator } from '../editor/extended_python_generator'; +import { createFieldNumberDropdown } from '../fields/field_number_dropdown'; export const BLOCK_NAME = 'mrc_port'; export const OUTPUT_NAME = 'mrc_port'; @@ -99,7 +100,7 @@ const PORT = { const port = this.ports_[i]; this.appendDummyInput('PORT_' + i) .appendField(createFieldNonEditableText(port.portType), 'TYPE_' + i) - .appendField(new Blockly.FieldTextInput(port.portNumber.toString()), 'PORT_NUM_' + i) + .appendField(createFieldDropdownForPortType(port.portType, port.portNumber), 'PORT_NUM_' + i) .setAlign(Blockly.inputs.Align.RIGHT); } }, @@ -156,10 +157,31 @@ export const pythonFromBlock = function ( return [code, Order.ATOMIC]; } -export function createPort(portType : string) { +function createFieldDropdownForPortType(portType: string, defaultVal: number): Blockly.Field { + switch (portType) { + case 'can': + return createFieldNumberDropdown(0, 4, defaultVal); + case 'smartio': + return createFieldNumberDropdown(0, 5, defaultVal); + case 'MotionCore port': + return createFieldNumberDropdown(1, 6, defaultVal); + case 'i2c': + return createFieldNumberDropdown(0, 1, defaultVal); + case 'usb in': + return createFieldNumberDropdown(0, 3, defaultVal); + case 'motor': + return createFieldNumberDropdown(1, 6, defaultVal); + case 'servo': + return createFieldNumberDropdown(1, 6, defaultVal); + default: + return createFieldNumberDropdown(0, 99, defaultVal); + } +} + +export function createPort(portType: string) { // Based off of the port type, create the right number and type of ports - const ports : MrcPortType[] = []; - switch(portType){ + const ports: MrcPortType[] = []; + switch (portType) { case 'CAN_PORT': ports.push({ portType: 'can', portNumber: 1 }); break; diff --git a/src/fields/field_number_dropdown.ts b/src/fields/field_number_dropdown.ts new file mode 100644 index 00000000..1fe3bea1 --- /dev/null +++ b/src/fields/field_number_dropdown.ts @@ -0,0 +1,34 @@ +/** + * @license + * Copyright 2025 Porpoiseful LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Create a field that has a dropdown with a number range + * @author alan@porpoiseful.com (Alan Smith) + */ + +import * as Blockly from 'blockly/core'; +import { createFieldDropdown } from './FieldDropdown'; + +export function createFieldNumberDropdown(min: number, max: number, defaultVal: number): Blockly.Field { + const items: string[] = []; + for (let i = min; i <= max; i++) { + items.push(i.toString()); + } + const field = createFieldDropdown(items); + field.setValue(defaultVal.toString()); + return field; +} \ No newline at end of file