Skip to content

Commit 93d41f0

Browse files
authored
Change ports to be dropdown (wpilibsuite#217)
* Change ports to be dropdown * Address review comments
1 parent 521e2c5 commit 93d41f0

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/blocks/mrc_port.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { Order } from 'blockly/python';
2525
import { MRC_STYLE_PORTS } from '../themes/styles'
2626
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
2727
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
28+
import { createFieldNumberDropdown } from '../fields/field_number_dropdown';
2829

2930
export const BLOCK_NAME = 'mrc_port';
3031
export const OUTPUT_NAME = 'mrc_port';
@@ -99,7 +100,7 @@ const PORT = {
99100
const port = this.ports_[i];
100101
this.appendDummyInput('PORT_' + i)
101102
.appendField(createFieldNonEditableText(port.portType), 'TYPE_' + i)
102-
.appendField(new Blockly.FieldTextInput(port.portNumber.toString()), 'PORT_NUM_' + i)
103+
.appendField(createFieldDropdownForPortType(port.portType, port.portNumber), 'PORT_NUM_' + i)
103104
.setAlign(Blockly.inputs.Align.RIGHT);
104105
}
105106
},
@@ -156,10 +157,31 @@ export const pythonFromBlock = function (
156157
return [code, Order.ATOMIC];
157158
}
158159

159-
export function createPort(portType : string) {
160+
function createFieldDropdownForPortType(portType: string, defaultVal: number): Blockly.Field {
161+
switch (portType) {
162+
case 'can':
163+
return createFieldNumberDropdown(0, 4, defaultVal);
164+
case 'smartio':
165+
return createFieldNumberDropdown(0, 5, defaultVal);
166+
case 'MotionCore port':
167+
return createFieldNumberDropdown(1, 6, defaultVal);
168+
case 'i2c':
169+
return createFieldNumberDropdown(0, 1, defaultVal);
170+
case 'usb in':
171+
return createFieldNumberDropdown(0, 3, defaultVal);
172+
case 'motor':
173+
return createFieldNumberDropdown(1, 6, defaultVal);
174+
case 'servo':
175+
return createFieldNumberDropdown(1, 6, defaultVal);
176+
default:
177+
return createFieldNumberDropdown(0, 99, defaultVal);
178+
}
179+
}
180+
181+
export function createPort(portType: string) {
160182
// Based off of the port type, create the right number and type of ports
161-
const ports : MrcPortType[] = [];
162-
switch(portType){
183+
const ports: MrcPortType[] = [];
184+
switch (portType) {
163185
case 'CAN_PORT':
164186
ports.push({ portType: 'can', portNumber: 1 });
165187
break;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Porpoiseful LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @fileoverview Create a field that has a dropdown with a number range
20+
* @author [email protected] (Alan Smith)
21+
*/
22+
23+
import * as Blockly from 'blockly/core';
24+
import { createFieldDropdown } from './FieldDropdown';
25+
26+
export function createFieldNumberDropdown(min: number, max: number, defaultVal: number): Blockly.Field {
27+
const items: string[] = [];
28+
for (let i = min; i <= max; i++) {
29+
items.push(i.toString());
30+
}
31+
const field = createFieldDropdown(items);
32+
field.setValue(defaultVal.toString());
33+
return field;
34+
}

0 commit comments

Comments
 (0)