Skip to content

Commit 86e148f

Browse files
committed
Making sure ports get passed from robot through
1 parent 8c35186 commit 86e148f

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

src/blocks/mrc_component.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import * as toolboxItems from '../toolbox/items';
3131
import * as storageModule from '../storage/module';
3232
import * as storageModuleContent from '../storage/module_content';
3333
import * as storageNames from '../storage/names';
34-
import { createPortShadow } from './mrc_port';
34+
import { createPort } from './mrc_port';
3535
import { createNumberShadowValue } from './utils/value';
3636
import { ClassData, FunctionData } from './utils/python_json_types';
3737
import { renameMethodCallers } from './mrc_call_python_function'
@@ -167,13 +167,15 @@ const COMPONENT = {
167167
};
168168
},
169169
getArgName: function (this: ComponentBlock, i: number): string {
170-
return this.getFieldValue(FIELD_NAME) + '__' + this.mrcArgs[i].name;
170+
return this.getFieldValue(FIELD_NAME) + '__' + 'port';
171171
},
172+
173+
172174
getComponentPorts: function (this: ComponentBlock, ports: {[argName: string]: string}): void {
173175
// Collect the ports for this component block.
174176
for (let i = 0; i < this.mrcArgs.length; i++) {
175177
const argName = this.getArgName(i);
176-
ports[argName] = this.mrcArgs[i].type;
178+
ports[argName] = this.mrcArgs[i].name;
177179
}
178180
},
179181
/**
@@ -197,20 +199,16 @@ export const pythonFromBlock = function (
197199
if (block.mrcImportModule) {
198200
generator.addImport(block.mrcImportModule);
199201
}
200-
let code = 'self.' + block.getFieldValue(FIELD_NAME) + ' = ' + block.getFieldValue(FIELD_TYPE);
201-
if (block.mrcStaticFunctionName) {
202-
code += '.' + block.mrcStaticFunctionName;
203-
}
204-
code += '(';
202+
let code = 'self.' + block.getFieldValue(FIELD_NAME) + ' = ' + block.getFieldValue(FIELD_TYPE) + "(";
205203

206204
for (let i = 0; i < block.mrcArgs.length; i++) {
207205
if (i != 0) {
208206
code += ', ';
209207
}
210208
if (generator.getModuleType() === storageModule.ModuleType.ROBOT) {
211-
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, 'ARG' + i, Order.NONE);
209+
code += generator.valueToCode(block, 'ARG' + i, Order.NONE);
212210
} else {
213-
code += block.mrcArgs[i].name + ' = ' + block.getArgName(i);
211+
code += block.getArgName(i);
214212
}
215213
}
216214
code += ')\n' + 'self.hardware.append(self.' + block.getFieldValue(FIELD_NAME) + ')\n';
@@ -257,12 +255,11 @@ function createComponentBlock(
257255

258256
if (constructorData.expectedPortType) {
259257
extraState.params!.push({
260-
//TODO(Alan): Make this a localized version of the port type
261258
'name': constructorData.expectedPortType,
262259
'type': 'Port',
263260
});
264261
if ( moduleType == storageModule.ModuleType.ROBOT ) {
265-
inputs['ARG0'] = createPortShadow(constructorData.expectedPortType);
262+
inputs['ARG0'] = createPort(constructorData.expectedPortType);
266263
}
267264
}
268265
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, Object.keys(inputs).length ? inputs : null);

src/blocks/mrc_mechanism.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as storageNames from '../storage/names';
3434
import * as value from './utils/value';
3535
import { renameMethodCallers } from './mrc_call_python_function'
3636
import { renameMechanismName as renameMechanismNameInEventHandlers } from './mrc_event_handler'
37+
import { createPort } from './mrc_port';
3738

3839
export const BLOCK_NAME = 'mrc_mechanism';
3940
export const OUTPUT_NAME = 'mrc_mechansim';
@@ -296,8 +297,7 @@ export function createMechanismBlock(
296297
name: port,
297298
type: parameterType,
298299
});
299-
const defaultValue = (parameterType === 'int') ? '0' : '';
300-
inputs['ARG' + i] = value.valueForFunctionArgInput(parameterType, defaultValue);
300+
inputs['ARG' + i] = createPort(parameterType);
301301
i++;
302302
}
303303
});

src/blocks/mrc_port.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,14 @@ export type MrcPortType = {
3636

3737
type PortBlock = Blockly.Block & PortMixin;
3838
interface PortMixin extends PortMixinType {
39+
portType_ : string,
3940
ports_ : MrcPortType[],
4041
}
4142
type PortMixinType = typeof PORT;
4243

4344
type 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

Comments
 (0)