@@ -30,15 +30,28 @@ import { createFieldNumberDropdown } from '../fields/field_number_dropdown';
3030export const BLOCK_NAME = 'mrc_port' ;
3131export const OUTPUT_NAME = 'mrc_port' ;
3232
33- export type MrcPortType = {
33+ const FIELD_PREFIX_TYPE = 'TYPE_' ;
34+ const FIELD_PREFIX_PORT_NUM = 'PORT_NUM_' ;
35+
36+ const VISIBLE_PORT_TYPE_CAN = 'can' ;
37+ const VISIBLE_PORT_TYPE_SMART_IO = 'smartio' ;
38+ const VISIBLE_PORT_TYPE_SMART_MOTOR = 'MotionCore port' ;
39+ const VISIBLE_PORT_TYPE_I2C = 'i2c' ;
40+ const VISIBLE_PORT_TYPE_USB = 'usb' ;
41+ const VISIBLE_PORT_TYPE_MOTOR = 'motor' ;
42+ const VISIBLE_PORT_TYPE_SERVO = 'servo' ;
43+ const VISIBLE_PORT_TYPE_USB_HUB_IN = 'usb in' ;
44+ const VISIBLE_PORT_TYPE_USB_HUB_OUT = 'usb out' ;
45+
46+ type MrcPortType = {
3447 portType : string ,
3548 portNumber : number ,
3649} ;
3750
3851type PortBlock = Blockly . Block & PortMixin ;
3952interface PortMixin extends PortMixinType {
40- portType_ : string ,
41- ports_ : MrcPortType [ ] ,
53+ mrcPortType : string ,
54+ mrcPorts : MrcPortType [ ] ,
4255}
4356type PortMixinType = typeof PORT ;
4457
@@ -62,8 +75,8 @@ const PORT = {
6275 */
6376 saveExtraState : function ( this : PortBlock ) : PortExtraState {
6477 const state : PortExtraState = {
65- portType : this . portType_ ,
66- ports : this . ports_
78+ portType : this . mrcPortType ,
79+ ports : this . mrcPorts
6780 } ;
6881
6982 return state ;
@@ -73,34 +86,15 @@ const PORT = {
7386 * Load the ports from the block's extra state.
7487 */
7588 loadExtraState : function ( this : PortBlock , state : PortExtraState ) : void {
76- this . portType_ = state . portType || '' ;
77- this . ports_ = state . ports || [ ] ;
78- this . updateShape_ ( ) ;
79- } ,
80-
81- /**
82- * Update the block's shape based on the current ports.
83- */
84- updateShape_ : function ( this : PortBlock ) : void {
85- // Remove all existing inputs
86- for ( let i = this . inputList . length - 1 ; i >= 0 ; i -- ) {
87- const input = this . inputList [ i ] ;
88- if ( input && ( input . name . startsWith ( 'PORT_' ) ) ) {
89- this . removeInput ( input . name , true ) ;
90- }
91- }
92-
93- // Initialize ports if not set
94- if ( ! this . ports_ ) {
95- this . ports_ = [ { portType : '' , portNumber : 0 } ] ;
96- }
97-
98- // Add inputs for each port
99- for ( let i = 0 ; i < this . ports_ . length ; i ++ ) {
100- const port = this . ports_ [ i ] ;
101- this . appendDummyInput ( 'PORT_' + i )
102- . appendField ( createFieldNonEditableText ( port . portType ) , 'TYPE_' + i )
103- . appendField ( createFieldDropdownForPortType ( port . portType , port . portNumber ) , 'PORT_NUM_' + i )
89+ this . mrcPortType = state . portType || '' ;
90+ this . mrcPorts = state . ports || [ ] ;
91+
92+ // Add an input for each port.
93+ for ( let i = 0 ; i < this . mrcPorts . length ; i ++ ) {
94+ const port = this . mrcPorts [ i ] ;
95+ this . appendDummyInput ( )
96+ . appendField ( createFieldNonEditableText ( port . portType ) , FIELD_PREFIX_TYPE + i )
97+ . appendField ( createFieldDropdownForPortType ( port . portType , port . portNumber ) , FIELD_PREFIX_PORT_NUM + i )
10498 . setAlign ( Blockly . inputs . Align . RIGHT ) ;
10599 }
106100 } ,
@@ -117,25 +111,19 @@ export const pythonFromBlock = function (
117111
118112 const ports : string [ ] = [ ] ;
119113
120- for ( let i = 0 ; i < block . inputList . length ; i ++ ) {
121- const input = block . inputList [ i ] ;
122- if ( input . name . startsWith ( 'PORT_' ) ) {
123- const portNumField = input . fieldRow . find ( field => field . name === 'PORT_NUM_' + i ) ;
124- if ( portNumField ) {
125- ports . push ( portNumField . getValue ( ) as string ) ;
126- }
127- }
114+ for ( let i = 0 ; i < block . mrcPorts . length ; i ++ ) {
115+ ports . push ( block . getFieldValue ( FIELD_PREFIX_PORT_NUM + i ) ) ;
128116 }
129117 let code = 'port.' ;
130118
131119 if ( ports . length === 1 ) {
132- code += `SimplePort(port_type = port.PortType.${ block . portType_ } , location = ${ ports [ 0 ] } )` ;
120+ code += `SimplePort(port_type = port.PortType.${ block . mrcPortType } , location = ${ ports [ 0 ] } )` ;
133121
134122 } else if ( ports . length === 2 ) {
135123 let port1Type = 'UNKNOWN' ;
136124 let port2Type = 'UNKNOWN' ;
137125
138- switch ( block . portType_ ) {
126+ switch ( block . mrcPortType ) {
139127 case 'USB_HUB' :
140128 port1Type = 'USB_PORT' ;
141129 port2Type = 'USB_PORT' ;
@@ -149,29 +137,29 @@ export const pythonFromBlock = function (
149137 port2Type = 'EXPANSION_HUB_SERVO_PORT' ;
150138 break ;
151139 }
152- code += `CompoundPort(port_type = port.PortType.${ block . portType_ } , ` ;
153- code += `\\ \n ${ generator . INDENT } port1 = port.SimplePort(port_type = port.PortType.${ port1Type } , location = ${ ports [ 0 ] } ), ` ;
154- code += `\\ \n ${ generator . INDENT } port2 = port.SimplePort(port_type = port.PortType.${ port2Type } , location = ${ ports [ 1 ] } ))` ;
140+ code += `CompoundPort(port_type = port.PortType.${ block . mrcPortType } ,\n ` ;
141+ code += `${ generator . INDENT } port1 = port.SimplePort(port_type = port.PortType.${ port1Type } , location = ${ ports [ 0 ] } ),\n ` ;
142+ code += `${ generator . INDENT } port2 = port.SimplePort(port_type = port.PortType.${ port2Type } , location = ${ ports [ 1 ] } ))` ;
155143 }
156144
157145 return [ code , Order . ATOMIC ] ;
158146}
159147
160148function createFieldDropdownForPortType ( portType : string , defaultVal : number ) : Blockly . Field {
161149 switch ( portType ) {
162- case 'can' :
150+ case VISIBLE_PORT_TYPE_CAN :
163151 return createFieldNumberDropdown ( 0 , 4 , defaultVal ) ;
164- case 'smartio' :
152+ case VISIBLE_PORT_TYPE_SMART_IO :
165153 return createFieldNumberDropdown ( 0 , 5 , defaultVal ) ;
166- case 'MotionCore port' :
154+ case VISIBLE_PORT_TYPE_SMART_MOTOR :
167155 return createFieldNumberDropdown ( 1 , 6 , defaultVal ) ;
168- case 'i2c' :
156+ case VISIBLE_PORT_TYPE_I2C :
169157 return createFieldNumberDropdown ( 0 , 1 , defaultVal ) ;
170- case 'usb in' :
158+ case VISIBLE_PORT_TYPE_USB_HUB_IN :
171159 return createFieldNumberDropdown ( 0 , 3 , defaultVal ) ;
172- case 'motor' :
160+ case VISIBLE_PORT_TYPE_MOTOR :
173161 return createFieldNumberDropdown ( 1 , 6 , defaultVal ) ;
174- case 'servo' :
162+ case VISIBLE_PORT_TYPE_SERVO :
175163 return createFieldNumberDropdown ( 1 , 6 , defaultVal ) ;
176164 default :
177165 return createFieldNumberDropdown ( 0 , 99 , defaultVal ) ;
@@ -183,40 +171,40 @@ export function createPort(portType: string) {
183171 const ports : MrcPortType [ ] = [ ] ;
184172 switch ( portType ) {
185173 case 'CAN_PORT' :
186- ports . push ( { portType : 'can' , portNumber : 1 } ) ;
174+ ports . push ( { portType : VISIBLE_PORT_TYPE_CAN , portNumber : 1 } ) ;
187175 break ;
188176 case 'SMART_IO_PORT' :
189- ports . push ( { portType : 'smartio' , portNumber : 1 } ) ;
177+ ports . push ( { portType : VISIBLE_PORT_TYPE_SMART_IO , portNumber : 1 } ) ;
190178 break ;
191179 case 'SMART_MOTOR_PORT' :
192- ports . push ( { portType : 'MotionCore port' , portNumber : 1 } ) ;
180+ ports . push ( { portType : VISIBLE_PORT_TYPE_SMART_MOTOR , portNumber : 1 } ) ;
193181 break ;
194182 case 'SERVO_PORT' :
195- ports . push ( { portType : 'servo' , portNumber : 1 } ) ;
183+ ports . push ( { portType : VISIBLE_PORT_TYPE_SERVO , portNumber : 1 } ) ;
196184 break ;
197185 case 'I2C_PORT' :
198- ports . push ( { portType : 'i2c' , portNumber : 1 } ) ;
186+ ports . push ( { portType : VISIBLE_PORT_TYPE_I2C , portNumber : 1 } ) ;
199187 break ;
200188 case 'USB_PORT' :
201- ports . push ( { portType : 'usb' , portNumber : 1 } ) ;
189+ ports . push ( { portType : VISIBLE_PORT_TYPE_USB , portNumber : 1 } ) ;
202190 break ;
203191 case 'EXPANSION_HUB_MOTOR_PORT' :
204- ports . push ( { portType : 'motor' , portNumber : 1 } ) ;
192+ ports . push ( { portType : VISIBLE_PORT_TYPE_MOTOR , portNumber : 1 } ) ;
205193 break ;
206194 case 'EXPANSION_HUB_SERVO_PORT' :
207- ports . push ( { portType : 'servo' , portNumber : 1 } ) ;
195+ ports . push ( { portType : VISIBLE_PORT_TYPE_SERVO , portNumber : 1 } ) ;
208196 break ;
209197 case 'USB_HUB' :
210- ports . push ( { portType : 'usb in' , portNumber : 1 } ) ;
211- ports . push ( { portType : 'usb out' , portNumber : 1 } ) ;
198+ ports . push ( { portType : VISIBLE_PORT_TYPE_USB_HUB_IN , portNumber : 1 } ) ;
199+ ports . push ( { portType : VISIBLE_PORT_TYPE_USB_HUB_OUT , portNumber : 1 } ) ;
212200 break ;
213201 case 'EXPANSION_HUB_MOTOR' :
214- ports . push ( { portType : 'usb in' , portNumber : 1 } ) ;
215- ports . push ( { portType : 'motor' , portNumber : 1 } ) ;
202+ ports . push ( { portType : VISIBLE_PORT_TYPE_USB_HUB_IN , portNumber : 1 } ) ;
203+ ports . push ( { portType : VISIBLE_PORT_TYPE_MOTOR , portNumber : 1 } ) ;
216204 break ;
217205 case 'EXPANSION_HUB_SERVO' :
218- ports . push ( { portType : 'usb in' , portNumber : 1 } ) ;
219- ports . push ( { portType : 'servo' , portNumber : 1 } ) ;
206+ ports . push ( { portType : VISIBLE_PORT_TYPE_USB_HUB_IN , portNumber : 1 } ) ;
207+ ports . push ( { portType : VISIBLE_PORT_TYPE_SERVO , portNumber : 1 } ) ;
220208 break ;
221209 default :
222210 ports . push ( { portType : 'unknown' + portType , portNumber : 1 } ) ;
0 commit comments