Skip to content

Commit d5ff607

Browse files
committed
Change ports to be dropdown
1 parent ce30750 commit d5ff607

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/blocks/mrc_port.ts

Lines changed: 23 additions & 1 deletion
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_' + i)
103104
.setAlign(Blockly.inputs.Align.RIGHT);
104105
}
105106
},
@@ -156,6 +157,27 @@ export const pythonFromBlock = function (
156157
return [code, Order.ATOMIC];
157158
}
158159

160+
function createFieldDropdownForPortType( portType : string, defaultVal : number) : Blockly.Field {
161+
switch(portType){
162+
case 'can':
163+
return createFieldNumberDropdown(0, 4, 1, defaultVal);
164+
case 'smartio':
165+
return createFieldNumberDropdown(0, 5, 1, defaultVal);
166+
case 'MotionCore port':
167+
return createFieldNumberDropdown(1, 6, 1, defaultVal);
168+
case 'i2c':
169+
return createFieldNumberDropdown(0, 1, 1, defaultVal);
170+
case 'usb in':
171+
return createFieldNumberDropdown(0, 3, 1, defaultVal);
172+
case 'motor':
173+
return createFieldNumberDropdown(1, 6, 1, defaultVal);
174+
case 'servo':
175+
return createFieldNumberDropdown(1, 6, 1, defaultVal);
176+
default:
177+
return createFieldNumberDropdown(0, 99, 1, defaultVal);
178+
}
179+
}
180+
159181
export function createPort(portType : string) {
160182
// Based off of the port type, create the right number and type of ports
161183
const ports : MrcPortType[] = [];
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, precision : number, defaultVal : number): Blockly.Field {
27+
const items: string[] = [];
28+
for (let i = min; i <= max; i += precision) {
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)