Skip to content

Commit 57b47db

Browse files
committed
Have ports being passed in
1 parent c104f18 commit 57b47db

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

src/blocks/mrc_component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ export const pythonFromBlock = function (
144144
if(i != 0){
145145
extension = '_' + (i + 1).toString();
146146
}
147-
code += block.mrcArgs[i].name + " = " + block.getFieldValue('NAME') + extension;
147+
const newPort = block.getFieldValue('NAME') + extension + '_port';
148+
generator.addHardwarePort(newPort, block.mrcArgs[i].type);
149+
code += block.mrcArgs[i].name + " = " + newPort;
148150
}else{
149151
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, fieldName, Order.NONE);
150152
}

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,49 @@ export const setup = function () {
9090
Blockly.Blocks[BLOCK_NAME] = MECHANISM_COMPONENT_HOLDER;
9191
}
9292

93-
export const pythonFromBlock = function (
94-
block: MechanismComponentHolderBlock,
95-
generator: ExtendedPythonGenerator,
96-
) {
93+
function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator){
9794
let code = 'def define_hardware(self):\n' + generator.INDENT + 'self.hardware = []\n';
9895

9996
let mechanisms = '';
10097
let components = '';
10198

102-
if (block.getInput('MECHANISMS')) {
103-
mechanisms = generator.statementToCode(block, 'MECHANISMS');
104-
}
105-
if (block.getInput('COMPONENTS')) {
106-
components = generator.statementToCode(block, 'COMPONENTS');
107-
}
99+
mechanisms = generator.statementToCode(block, 'MECHANISMS');
100+
components = generator.statementToCode(block, 'COMPONENTS');
101+
108102
const body = mechanisms + components;
109103
if(body != ''){
110104
code += body;
111105
}else{
112106
code += generator.INDENT + 'pass';
113107
}
108+
generator.addClassMethodDefinition('define_hardware', code);
109+
}
110+
111+
function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator){
112+
let components = '';
113+
114+
components = generator.statementToCode(block, 'COMPONENTS');
114115

116+
let code = 'def define_hardware(self' + generator.getListOfPorts(false) + '):\n' +
117+
generator.INDENT + 'self.hardware = []\n';
118+
119+
if(components != ''){
120+
code += components;
121+
}else{
122+
code += generator.INDENT + 'pass';
123+
}
115124
generator.addClassMethodDefinition('define_hardware', code);
116-
return '';
125+
}
126+
127+
export const pythonFromBlock = function (
128+
block: MechanismComponentHolderBlock,
129+
generator: ExtendedPythonGenerator,
130+
) {
131+
if(block.getInput('MECHANISMS')){
132+
pythonFromBlockInRobot(block, generator);
133+
}
134+
else{
135+
pythonFromBlockInMechanism(block, generator);
136+
}
137+
return ''
117138
}

src/blocks/mrc_port.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export const pythonFromBlock = function (
5959
block: PortBlock,
6060
generator: ExtendedPythonGenerator,
6161
) {
62-
let code = "";
63-
//TODO(Alan) - Make this real
62+
//TODO (Alan) : Specify the type here as well
63+
let code = block.getFieldValue('PORT_NUM');
6464

65-
return [code, Order.ATOMIC];
65+
return [code, Order.ATOMIC];
6666
}

src/editor/extended_python_generator.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
3535
private context: GeneratorContext | null = null;
3636

3737
private classMethods: {[key: string]: string} = Object.create(null);
38+
private ports: {[key: string]: string} = Object.create(null);
3839

3940
constructor() {
4041
super('Python');
@@ -65,7 +66,9 @@ export class ExtendedPythonGenerator extends PythonGenerator {
6566
let variableDefinitions = '';
6667

6768
if (this.context?.getHasHardware()) {
68-
variableDefinitions += this.INDENT + "self.define_hardware()\n";
69+
variableDefinitions += this.INDENT + "self.define_hardware(";
70+
variableDefinitions += this.getListOfPorts(true);
71+
variableDefinitions += ')';
6972
}
7073

7174
return variableDefinitions;
@@ -108,6 +111,28 @@ export class ExtendedPythonGenerator extends PythonGenerator {
108111
this.classMethods[methodName] = code;
109112
}
110113

114+
/**
115+
* Add a Hardware Port
116+
*/
117+
addHardwarePort(portName: string, type: string): void{
118+
this.ports[portName] = type;
119+
}
120+
121+
getListOfPorts(startWithFirst: boolean): string{
122+
let returnString = ''
123+
let firstPort = startWithFirst;
124+
for (const port in this.ports) {
125+
if(!firstPort){
126+
returnString += ', ';
127+
}
128+
else{
129+
firstPort = false;
130+
}
131+
returnString += port;
132+
}
133+
return returnString;
134+
}
135+
111136
finish(code: string): string {
112137
if (this.context && this.workspace) {
113138
const className = this.context.getClassName();

src/toolbox/hardware_category.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,4 @@ const category_mechanism =
221221
},
222222
],
223223
}
224-
export const category = category_robot;
224+
export const category = category_mechanism;

src/toolbox/toolbox.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {category as listsCategory} from './lists_category';
2929
import {category as miscCategory} from './misc_category';
3030
import {category as methodsCategory} from './methods_category';
3131
import {category as componentSampleCategory} from './component_samples_category';
32-
import {category as mechanismCategory} from './hardware_category';
32+
import {category as hardwareCategory} from './hardware_category';
3333
import {category as robotCategory} from './robot_category';
3434

3535
export function getToolboxJSON(
@@ -82,7 +82,7 @@ export function getToolboxJSON(
8282
custom: 'VARIABLE',
8383
},
8484
methodsCategory,
85-
//mechanismCategory,
85+
hardwareCategory,
8686
//componentSampleCategory,
8787
]);
8888

0 commit comments

Comments
 (0)