Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server_python_scripts/blocks_base_classes/robot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def __init__(self):
self.hardware = []
# In self.event_handlers, the keys are the event names, the values are a list of handlers.
self.event_handlers = {}
self.define_hardware()

def define_hardware(self):
pass

def register_event_handler(self, event_name: str, event_handler: Callable) -> None:
if event_name in self.event_handlers:
Expand Down
25 changes: 13 additions & 12 deletions src/blocks/mrc_mechanism_component_holder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license
* Copyright 2025 Porpoiseful LLC
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -64,7 +64,7 @@ function setName(block: Blockly.BlockSvg){
otherNames.push(variableBlock.getFieldValue('NAME'));
}
});
const currentName = block.getFieldValue('NAME');
const currentName = block.getFieldValue('NAME');
block.setFieldValue(getLegalName(currentName, otherNames), 'NAME');
}
}
Expand Down Expand Up @@ -205,8 +205,8 @@ function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator:
const body = mechanisms + components;
if (body != '') {
code += body;
generator.addClassMethodDefinition('define_hardware', code);
}
generator.addClassMethodDefinition('define_hardware', code);
}
}

function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator) {
Expand All @@ -218,19 +218,20 @@ function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, genera

if (components != '') {
code += components;
generator.addClassMethodDefinition('define_hardware', code);
generator.addClassMethodDefinition('define_hardware', code);
}
}

export const pythonFromBlock = function (
block: MechanismComponentHolderBlock,
generator: ExtendedPythonGenerator,
) {
if (block.getInput(INPUT_MECHANISMS)) {
pythonFromBlockInRobot(block, generator);
}
else {
pythonFromBlockInMechanism(block, generator);
generator: ExtendedPythonGenerator) {
switch (generator.getModuleType()) {
case commonStorage.MODULE_TYPE_ROBOT:
pythonFromBlockInRobot(block, generator);
break;
case commonStorage.MODULE_TYPE_MECHANISM:
pythonFromBlockInMechanism(block, generator);
break;
}
return ''
}
Expand Down
26 changes: 22 additions & 4 deletions src/editor/extended_python_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
CLASS_NAME_OPMODE,
getClassData,
} from '../blocks/utils/python';
import * as commonStorage from '../storage/common_storage';

export class OpModeDetails {
constructor(private name: string, private group : string, private enabled : boolean, private type : string) {}
Expand Down Expand Up @@ -70,7 +71,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
private workspace: Blockly.Workspace | null = null;
private context: GeneratorContext | null = null;

// Has components or mechanisms (ie, needs to call self.define_hardware in __init__)
// Has components or mechanisms (ie, might need to call self.define_hardware in __init__)
private hasHardware = false;
private ports: {[key: string]: string} = Object.create(null);

Expand Down Expand Up @@ -112,10 +113,20 @@ export class ExtendedPythonGenerator extends PythonGenerator {
let initStatements = '';

if (this.hasHardware) {
initStatements += this.INDENT + "self.define_hardware(";
initStatements += this.getListOfPorts(true);
initStatements += ')\n';
switch (this.getModuleType()) {
case commonStorage.MODULE_TYPE_ROBOT:
// The RobotBase __init__ method already calls self.define_robot(), so
// we don't need to generate that call here.
break;
case commonStorage.MODULE_TYPE_MECHANISM:
// For mechanisms
initStatements += this.INDENT + "self.define_hardware(";
initStatements += this.getListOfPorts(true);
initStatements += ')\n';
break;
}
}

if (this.hasEventHandler) {
initStatements += this.INDENT + "self.register_event_handlers()\n";
}
Expand Down Expand Up @@ -225,7 +236,14 @@ export class ExtendedPythonGenerator extends PythonGenerator {
}
classMethods.push(code);
}
// Generate the __init__ method first.
if ('__init__' in this.classMethods) {
classMethods.push(this.classMethods['__init__'])
}
for (const name in this.classMethods) {
if (name === '__init__') {
continue;
}
classMethods.push(this.classMethods[name])
}
this.eventHandlers = Object.create(null);
Expand Down