Skip to content

Commit 6b11d55

Browse files
committed
wip
1 parent 64667d7 commit 6b11d55

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

server_python_scripts/blocks_base_classes/opmode.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ def __init__(self, robot: RobotBase):
77
def start(self) -> None:
88
self.robot.start()
99
def loop(self) -> None:
10-
# Call steps method if it exists in the derived class
11-
if hasattr(self, 'steps') and callable(self.steps):
12-
self.steps()
1310
self.robot.update()
1411
def stop(self) -> None:
1512
self.robot.stop()

src/blocks/mrc_class_method_def.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ export const pythonFromBlock = function (
457457
xfix2 = xfix1;
458458
}
459459
if (block.mrcPythonMethodName === '__init__') {
460-
const classSpecific = generator.getClassSpecificForInit();
461-
branch = generator.INDENT + 'super().__init__(' + classSpecific + ')\n' +
460+
const superInitParameters = generator.getSuperInitParameters();
461+
branch = generator.INDENT + 'super().__init__(' + superInitParameters + ')\n' +
462462
generator.generateInitStatements() + branch;
463463
}
464464
else if (generator.inBaseClassMethod(blocklyName)) {

src/blocks/mrc_steps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2727
import { createStepFieldFlydown } from '../fields/field_flydown';
2828
import { renameSteps as updateJumpToStepBlocks } from './mrc_jump_to_step';
2929
import * as stepContainer from './mrc_step_container'
30+
import { STEPS_METHOD_NAME } from './utils/python';
3031
import { createBooleanShadowValue } from './utils/value';
3132
import * as toolboxItems from '../toolbox/items';
3233

@@ -219,7 +220,7 @@ export const pythonFromBlock = function (
219220
block: StepsBlock,
220221
generator: ExtendedPythonGenerator,
221222
) {
222-
let code = 'def steps(self):\n';
223+
let code = 'def ' + STEPS_METHOD_NAME + '(self):\n';
223224
code += generator.INDENT + 'if not hasattr(self, "_initialized_steps"):\n';
224225
code += generator.INDENT.repeat(2) + 'self._current_step = "' + block.mrcStepNames[0] + '"\n';
225226
code += generator.INDENT.repeat(2) + 'self._initialized_steps = True\n\n';
@@ -243,7 +244,7 @@ export const pythonFromBlock = function (
243244
}
244245
});
245246

246-
generator.addClassMethodDefinition('steps', code);
247+
generator.addClassMethodDefinition(STEPS_METHOD_NAME, code);
247248

248249
return ''
249250
}

src/blocks/utils/python.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ import * as SetPythonVariable from "../mrc_set_python_variable";
3333

3434
export const MODULE_NAME_BLOCKS_BASE_CLASSES = 'blocks_base_classes';
3535
export const CLASS_NAME_ROBOT_BASE = MODULE_NAME_BLOCKS_BASE_CLASSES + '.RobotBase';
36-
export const CLASS_NAME_OPMODE = MODULE_NAME_BLOCKS_BASE_CLASSES + '.OpMode';
3736
export const CLASS_NAME_MECHANISM = MODULE_NAME_BLOCKS_BASE_CLASSES + '.Mechanism';
3837

38+
// TODO(lizlooney): what about PeriodicOpMode and LinearOpMode?
39+
export const CLASS_NAME_OPMODE = MODULE_NAME_BLOCKS_BASE_CLASSES + '.OpMode';
40+
// TODO(lizlooney): Make sure to update the value of PERIODIC_METHOD_NAME when we update wpilib.
41+
export const PERIODIC_METHOD_NAME = 'loop';
42+
export const STEPS_METHOD_NAME = 'steps';
3943

4044
export const robotPyData = generatedRobotPyData as PythonData;
4145
const externalSamplesData = generatedExternalSamplesData as PythonData

src/editor/extended_python_generator.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import * as eventHandler from '../blocks/mrc_event_handler';
2727
import {
2828
MODULE_NAME_BLOCKS_BASE_CLASSES,
2929
CLASS_NAME_OPMODE,
30+
PERIODIC_METHOD_NAME,
31+
STEPS_METHOD_NAME,
3032
getClassData,
3133
} from '../blocks/utils/python';
3234
import * as storageModule from '../storage/module';
@@ -248,6 +250,22 @@ export class ExtendedPythonGenerator extends PythonGenerator {
248250

249251
code = decorators + 'class ' + className + '(' + baseClassName + '):\n';
250252

253+
if (this.getModuleType() === storageModule.ModuleType.OPMODE) {
254+
// If the user has a steps method, we need to generate code to call it from the periodic method.
255+
if (STEPS_METHOD_NAME in this.classMethods) {
256+
let periodicCode: string;
257+
if (PERIODIC_METHOD_NAME in this.classMethods) {
258+
periodicCode = this.classMethods[PERIODIC_METHOD_NAME];
259+
} else {
260+
periodicCode = `def {PERIODIC_METHOD_NAME}():\n`;
261+
periodicCode += this.INDENT + `super().{PERIODIC_METHOD_NAME}()\n`;
262+
}
263+
periodicCode += this.INDENT + `if hasattr(self, '${STEPS_METHOD_NAME}') and callable(self.${STEPS_METHOD_NAME}):\n`;
264+
periodicCode += this.INDENT.repeat(2) + `self.${STEPS_METHOD_NAME}()\n`;
265+
this.classMethods[PERIODIC_METHOD_NAME] = periodicCode;
266+
}
267+
}
268+
251269
const classMethods = [];
252270

253271
// Generate the __init__ method first.
@@ -295,7 +313,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
295313
this.opModeDetails = opModeDetails;
296314
}
297315

298-
getClassSpecificForInit(): string {
316+
getSuperInitParameters(): string {
299317
if (this.context?.getBaseClassName() == CLASS_NAME_OPMODE) {
300318
return 'robot'
301319
}

src/modules/opmode_start.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"type": "mrc_class_method_def",
3838
"x": 10,
3939
"y": 190,
40-
"deletable": false,
4140
"editable": false,
4241
"extraState": {
4342
"canChangeSignature": false,

0 commit comments

Comments
 (0)