@@ -36,16 +36,14 @@ export type MrcPortType = {
3636
3737type PortBlock = Blockly . Block & PortMixin ;
3838interface PortMixin extends PortMixinType {
39+ portType_ : string ,
3940 ports_ : MrcPortType [ ] ,
4041}
4142type PortMixinType = typeof PORT ;
4243
4344type PortExtraState = {
44- /**
45- * The ports.
46- * For instance methods, args[0].name is the self label and args[0].type is
47- * the self type.
48- */
45+ //TODO(Alan) - Figure out how to not have this duplicated for a simple port
46+ portType : string ,
4947 ports : MrcPortType [ ] ,
5048}
5149
@@ -63,6 +61,7 @@ const PORT = {
6361 */
6462 saveExtraState : function ( this : PortBlock ) : PortExtraState {
6563 const state : PortExtraState = {
64+ portType : this . portType_ ,
6665 ports : this . ports_
6766 } ;
6867
@@ -73,6 +72,7 @@ const PORT = {
7372 * Load the ports from the block's extra state.
7473 */
7574 loadExtraState : function ( this : PortBlock , state : PortExtraState ) : void {
75+ this . portType_ = state . portType || '' ;
7676 this . ports_ = state . ports || [ ] ;
7777 this . updateShape_ ( ) ;
7878 } ,
@@ -123,12 +123,38 @@ export const pythonFromBlock = function (
123123 }
124124 }
125125 }
126+ let code = `Port(port_type = PortType.${ block . portType_ } , ` ;
126127
127- const code = ports . length === 1 ? ports [ 0 ] : `[${ ports . join ( ', ' ) } ]` ;
128+ if ( ports . length === 1 ) {
129+ code += `location = ${ ports [ 0 ] } )` ;
130+ }
131+ else if ( ports . length === 2 ) {
132+ let port1Type = 'UNKNOWN' ;
133+ let port2Type = 'UNKNOWN' ;
134+
135+ switch ( block . portType_ ) {
136+ case 'USB_HUB' :
137+ port1Type = 'USB_PORT' ;
138+ port2Type = 'USB_PORT' ;
139+ break ;
140+ case 'EXPANSION_HUB_MOTOR' :
141+ port1Type = 'USB_PORT' ;
142+ port2Type = 'MOTOR_PORT' ;
143+ break ;
144+ case 'EXPANSION_HUB_SERVO' :
145+ port1Type = 'USB_PORT' ;
146+ port2Type = 'SERVO_PORT' ;
147+ break ;
148+ }
149+
150+ code += `\\ \n${ _generator . INDENT } port1 = Port(port_type = PortType.${ port1Type } , location = ${ ports [ 0 ] } ), ` ;
151+ code += `\\ \n${ _generator . INDENT } port2 = Port(port_type = PortType.${ port2Type } , location = ${ ports [ 1 ] } ))` ;
152+ }
153+
128154 return [ code , Order . ATOMIC ] ;
129155}
130156
131- export function createPortShadow ( portType : string ) {
157+ export function createPort ( portType : string ) {
132158 //TODO: Based off of the port type, create the right number and type of ports
133159 const ports : MrcPortType [ ] = [ ] ;
134160 switch ( portType ) {
@@ -172,13 +198,14 @@ export function createPortShadow(portType : string) {
172198 ports . push ( { portType : 'servo' , portNumber : 1 } ) ;
173199 break ;
174200 default :
175- ports . push ( { portType : 'unknown' , portNumber : 1 } ) ;
201+ ports . push ( { portType : 'unknown:' + portType , portNumber : 1 } ) ;
176202 break ;
177203 }
178204 return {
179- shadow : {
205+ block : {
180206 type : 'mrc_port' ,
181207 extraState : {
208+ portType : portType ,
182209 ports : ports . map ( port => ( {
183210 portType : port . portType ,
184211 portNumber : port . portNumber
0 commit comments