Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 26 additions & 4 deletions src/blocks/mrc_port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
},
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions src/fields/field_number_dropdown.ts
Original file line number Diff line number Diff line change
@@ -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 [email protected] (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;
}