Skip to content
Draft
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
2 changes: 1 addition & 1 deletion frontend/blocks/mrc_call_python_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ export function pythonFromBlock(
}
case FunctionKind.EVENT: {
const eventName = block.getFieldValue(FIELD_EVENT_NAME);
code = 'self.fireEvent(\'' + eventName + '\'';
code = 'self.fire_event(\'' + eventName + '\'';
needOpenParen = false;
delimiterBeforeArgs = ', ';
break;
Expand Down
31 changes: 29 additions & 2 deletions frontend/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,24 @@ const CLASS_METHOD_DEF = {
});
return parameterNames;
},
upgrade_0_1_x_to_0_2_0: function(this: ClassMethodDefBlock) {
// Change opmodeStart to opmode_start
// Change opmodePeriodic to opmode_periodic
// Change opmodeEnd to opmode_end
if (!this.mrcCanChangeSignature &&
!this.mrcCanBeCalledWithinClass &&
!this.mrcCanBeCalledOutsideClass &&
this.mrcReturnType === 'None' &&
this.mrcParameters.length === 0) {
if (this.getFieldValue('NAME') === 'opmodeStart') {
this.setFieldValue('opmode_start', FIELD_METHOD_NAME);
} else if (this.getFieldValue('NAME') === 'opmodePeriodic') {
this.setFieldValue('opmode_periodic', FIELD_METHOD_NAME);
} else if (this.getFieldValue('NAME') === 'opmodeEnd') {
this.setFieldValue('opmode_end', FIELD_METHOD_NAME);
}
}
},
};

export const setup = function () {
Expand Down Expand Up @@ -485,7 +503,7 @@ export function createCustomMethodBlock(): toolboxItems.Block {
params: [],
};
const fields: {[key: string]: any} = {};
fields[FIELD_METHOD_NAME] = 'myMethod';
fields[FIELD_METHOD_NAME] = 'my_method';
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, null);
}

Expand All @@ -498,7 +516,7 @@ export function createCustomMethodBlockWithReturn(): toolboxItems.Block {
params: [],
};
const fields: {[key: string]: any} = {};
fields[FIELD_METHOD_NAME] = 'myMethodWithReturn';
fields[FIELD_METHOD_NAME] = 'my_method_with_return';
const inputs: {[key: string]: any} = {};
inputs[INPUT_RETURN] = {
type: 'input_value',
Expand Down Expand Up @@ -596,3 +614,12 @@ export function registerToolboxButton(workspace: Blockly.WorkspaceSvg, messageAp
});
}

/**
* Upgrades the ClassMethodDefBlocks in the given workspace from version 0.1.x to 0.2.0.
* This function should only be called when upgrading old projects.
*/
export function upgrade_0_1_x_to_0_2_0(workspace: Blockly.Workspace): void {
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
(block as ClassMethodDefBlock).upgrade_0_1_x_to_0_2_0();
});
}
10 changes: 5 additions & 5 deletions frontend/blocks/mrc_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
import { makeLegalName } from './utils/validator';
import * as toolboxItems from '../toolbox/items';
import * as storageModule from '../storage/module';
import * as storageNames from '../storage/names';
import * as storageModuleContent from '../storage/module_content';
import { NONCOPYABLE_BLOCK } from './noncopyable_block';
import {
Expand Down Expand Up @@ -219,7 +220,7 @@ const COMPONENT = {
},
getMechanismInitArgName: function (this: ComponentBlock): string {
// Return the name of the arg used to represent this component's arguments in a mechanism's
// constructor or a mechanism's defineHardware method.
// constructor or a mechanism's define_hardware method.
return getMechanismInitArgName(this.getFieldValue(FIELD_NAME));
},
getComponentArgs: function (this: ComponentBlock, args: storageModuleContent.MethodArg[]): void {
Expand Down Expand Up @@ -354,7 +355,7 @@ export const pythonFromBlock = function (
}
} else {
// In a mechanism, a component block does not have input sockets.
// Each argument of the mechanism's constructor (and the mechanism's defineHardware method) is
// Each argument of the mechanism's constructor (and the mechanism's define_hardware method) is
// a tuple containing the constructor parameters of a component.
// Use the * operator to unpack the elements of the tuple and pass each
// element as a separate positional argument to the component constructor.
Expand All @@ -371,7 +372,7 @@ export function getAllPossibleComponents(
const contents: toolboxItems.ContentsType[] = [];
// Iterate through all the component classes and add definition blocks.
componentClasses.forEach(classData => {
const componentName = 'my' + simpleClassName(classData.className);
const componentName = 'my_' + storageNames.pascalCaseToSnakeCase(simpleClassName(classData.className));
classData.constructors.forEach(constructorData => {
if (constructorData.isComponent) {
contents.push(createComponentBlock(componentName, classData, constructorData, moduleType, showSimpleClassNames));
Expand All @@ -383,7 +384,7 @@ export function getAllPossibleComponents(
}

export function getMechanismInitArgName(componentName: string): string {
return componentName + 'Args';
return componentName + '_args';
}

function createComponentBlock(
Expand Down Expand Up @@ -424,4 +425,3 @@ function createComponentBlock(
});
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, Object.keys(inputs).length ? inputs : null);
}

2 changes: 1 addition & 1 deletion frontend/blocks/mrc_event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ function generateRegisterEventHandler(
}
if (fullSender) {
generator.addRegisterEventHandlerStatement(
fullSender + '.registerEventHandler(\'' + eventName + '\', self.' + funcName + ')\n');
fullSender + '.register_event_handler(\'' + eventName + '\', self.' + funcName + ')\n');
}
}

Expand Down
6 changes: 3 additions & 3 deletions frontend/blocks/mrc_mechanism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,11 @@ export function createMechanismBlock(
mechanism: storageModule.Mechanism,
components: storageModuleContent.Component[],
showSimpleClassNames: boolean): toolboxItems.Block {
const importModule = storageNames.pascalCaseToSnakeCase(mechanism.className);
const mechanismName = 'my' + mechanism.className;
const snakeCaseName = storageNames.pascalCaseToSnakeCase(mechanism.className);
const mechanismName = 'my_' + snakeCaseName;
const extraState: MechanismExtraState = {
mechanismModuleId: mechanism.moduleId,
importModule: importModule,
importModule: snakeCaseName,
parameters: [],
};
const fields: {[key: string]: any} = {};
Expand Down
10 changes: 5 additions & 5 deletions frontend/blocks/mrc_mechanism_component_holder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export const setup = function () {
}

function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator) {
let code = 'def defineHardware(self):\n';
let code = 'def define_hardware(self):\n';

const mechanisms = generator.statementToCode(block, INPUT_MECHANISMS);
const components = generator.statementToCode(block, INPUT_COMPONENTS);
Expand All @@ -344,11 +344,11 @@ function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator:
code += generator.INDENT + 'pass\n';
}

generator.addClassMethodDefinition('defineHardware', code);
generator.addClassMethodDefinition('define_hardware', code);
}

function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator) {
let code = 'def defineHardware(self';
let code = 'def define_hardware(self';
const mechanismInitArgNames: string[] = generator.getMechanismInitArgNames();
if (mechanismInitArgNames.length) {
code += ', ' + mechanismInitArgNames.join(', ');
Expand All @@ -361,7 +361,7 @@ function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, genera
const allComponents = components + privateComponents;
if (allComponents) {
code += allComponents;
generator.addClassMethodDefinition('defineHardware', code);
generator.addClassMethodDefinition('define_hardware', code);
}
}

Expand Down Expand Up @@ -419,7 +419,7 @@ export function hasAnyComponents(workspace: Blockly.Workspace): boolean {
}

/**
* Collects the args for the mechanism's constructor and defineHardware method.
* Collects the args for the mechanism's constructor and define_hardware method.
*/
export function getMechanismInitArgNames(workspace: Blockly.Workspace, mechanismInitArgNames: string[]): void {
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
Expand Down
16 changes: 8 additions & 8 deletions frontend/blocks/utils/generated/robotpy_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -24139,7 +24139,7 @@
"componentArgs": [
{
"defaultValue": "",
"name": "smartIoPort",
"name": "smart_io_port",
"type": "SYSTEMCORE_SMART_IO_PORT"
}
],
Expand Down Expand Up @@ -24761,7 +24761,7 @@
"componentArgs": [
{
"defaultValue": "",
"name": "smartIoPort",
"name": "smart_io_port",
"type": "SYSTEMCORE_SMART_IO_PORT"
},
{
Expand Down Expand Up @@ -25026,7 +25026,7 @@
"componentArgs": [
{
"defaultValue": "",
"name": "smartIoPort",
"name": "smart_io_port",
"type": "SYSTEMCORE_SMART_IO_PORT"
},
{
Expand Down Expand Up @@ -26638,7 +26638,7 @@
"componentArgs": [
{
"defaultValue": "",
"name": "smartIoPort",
"name": "smart_io_port",
"type": "SYSTEMCORE_SMART_IO_PORT"
}
],
Expand Down Expand Up @@ -28112,7 +28112,7 @@
"componentArgs": [
{
"defaultValue": "",
"name": "smartIoPort",
"name": "smart_io_port",
"type": "SYSTEMCORE_SMART_IO_PORT"
},
{
Expand Down Expand Up @@ -28846,7 +28846,7 @@
"componentArgs": [
{
"defaultValue": "",
"name": "expansionHubMotor",
"name": "expansion_hub_motor",
"type": "SYSTEMCORE_USB_PORT__EXPANSION_HUB_MOTOR_PORT"
}
],
Expand Down Expand Up @@ -29260,7 +29260,7 @@
"componentArgs": [
{
"defaultValue": "",
"name": "expansionHubServo",
"name": "expansion_hub_servo",
"type": "SYSTEMCORE_USB_PORT__EXPANSION_HUB_SERVO_PORT"
}
],
Expand Down Expand Up @@ -45959,7 +45959,7 @@
"componentArgs": [
{
"defaultValue": "",
"name": "smartIoPort",
"name": "smart_io_port",
"type": "SYSTEMCORE_SMART_IO_PORT"
}
],
Expand Down
22 changes: 11 additions & 11 deletions frontend/blocks/utils/generated/runtime_python.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
{
"defaultValue": "",
"name": "eventName",
"name": "event_name",
"type": "str"
},
{
Expand All @@ -34,7 +34,7 @@
}
],
"declaringClassName": "blocks_base_classes.Mechanism",
"functionName": "fireEvent",
"functionName": "fire_event",
"returnType": "None",
"tooltip": ""
},
Expand All @@ -47,7 +47,7 @@
}
],
"declaringClassName": "blocks_base_classes.Mechanism",
"functionName": "opmodeEnd",
"functionName": "opmode_end",
"returnType": "None",
"tooltip": ""
},
Expand All @@ -60,7 +60,7 @@
}
],
"declaringClassName": "blocks_base_classes.Mechanism",
"functionName": "opmodePeriodic",
"functionName": "opmode_periodic",
"returnType": "None",
"tooltip": ""
},
Expand All @@ -73,7 +73,7 @@
}
],
"declaringClassName": "blocks_base_classes.Mechanism",
"functionName": "opmodeStart",
"functionName": "opmode_start",
"returnType": "None",
"tooltip": ""
},
Expand All @@ -86,17 +86,17 @@
},
{
"defaultValue": "",
"name": "eventName",
"name": "event_name",
"type": "str"
},
{
"defaultValue": "",
"name": "eventHandler",
"name": "event_handler",
"type": "typing.Callable"
}
],
"declaringClassName": "blocks_base_classes.Mechanism",
"functionName": "registerEventHandler",
"functionName": "register_event_handler",
"returnType": "None",
"tooltip": ""
},
Expand All @@ -109,17 +109,17 @@
},
{
"defaultValue": "",
"name": "eventName",
"name": "event_name",
"type": "str"
},
{
"defaultValue": "",
"name": "eventHandler",
"name": "event_handler",
"type": "typing.Callable"
}
],
"declaringClassName": "blocks_base_classes.Mechanism",
"functionName": "unregisterEventHandler",
"functionName": "unregister_event_handler",
"returnType": "None",
"tooltip": ""
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/blocks/utils/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ export const ROBOT_METHOD_NAMES_OVERRIDEABLE: string[] = [

export const CLASS_NAME_MECHANISM = MODULE_NAME_BLOCKS_BASE_CLASSES + '.Mechanism';
export const MECHANISM_METHOD_NAMES_OVERRIDEABLE: string[] = [
'opmodeEnd',
'opmodePeriodic',
'opmodeStart',
'opmode_end',
'opmode_periodic',
'opmode_start',
];

export const CLASS_NAME_OPMODE = 'wpilib.PeriodicOpMode';
Expand Down Expand Up @@ -350,4 +350,4 @@ export function simpleClassName(className: string): string {
return (lastDot !== -1)
? className.substring(lastDot + 1)
: className;
}
}
Loading
Loading