Skip to content

Commit

Permalink
fix: flip direction (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverben authored Dec 7, 2024
1 parent 7b7867d commit 482aa76
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dist",
"media"
],
"version": "3.3.0",
"version": "3.3.1",
"description": "Leaphy custom Blockly blocks and arduino code generator",
"name": "@leaphy-robotics/leaphy-blocks"
}
2 changes: 1 addition & 1 deletion src/blocks/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ export default function registerExtensions(blockly: typeof Blockly) {
undefined,
["controls_if_elseif", "controls_if_else"],
);
Blockly.Extensions.registerMutator(
blockly.Extensions.registerMutator(
"l_format_date_mutator",
DATE_FORMAT_MUTATOR,
undefined,
Expand Down
16 changes: 12 additions & 4 deletions src/blocks/leaphy_original.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ const motorLeftRightDropdown = [
["%{BKY_LEAPHY_MOTOR_LEFT_DROPDOWN}", "9"],
["%{BKY_LEAPHY_MOTOR_RIGHT_DROPDOWN}", "10"],
];

export enum MotorDirection {
FORWARD = "1",
BACKWARD = "2",
LEFT = "3",
RIGHT = "4",
}

const motorForwardBackwardDropdown = [
["%{BKY_LEAPHY_MOTOR_FORWARD}", "1"],
["%{BKY_LEAPHY_MOTOR_BACKWARD}", "2"],
["%{BKY_LEAPHY_MOTOR_LEFT}", "3"],
["%{BKY_LEAPHY_MOTOR_RIGHT}", "4"],
["%{BKY_LEAPHY_MOTOR_FORWARD}", MotorDirection.FORWARD],
["%{BKY_LEAPHY_MOTOR_BACKWARD}", MotorDirection.BACKWARD],
["%{BKY_LEAPHY_MOTOR_LEFT}", MotorDirection.LEFT],
["%{BKY_LEAPHY_MOTOR_RIGHT}", MotorDirection.RIGHT],
];

const blocks: BlockDefinition = [
Expand Down
18 changes: 15 additions & 3 deletions src/generators/arduino/leaphy_original.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Arduino } from "../arduino";
import { MotorDirection } from "../../blocks/leaphy_original";

function getCodeGenerators(arduino: Arduino) {
arduino.forBlock["leaphy_original_set_led"] = function (block) {
Expand Down Expand Up @@ -71,23 +72,34 @@ function getCodeGenerators(arduino: Arduino) {
};

arduino.forBlock["leaphy_original_move_motors"] = function (block) {
const dropdown_Type = block.getFieldValue("MOTOR_DIRECTION");
let direction = block.getFieldValue(
"MOTOR_DIRECTION",
) as MotorDirection;
const speed =
arduino.valueToCode(block, "MOTOR_SPEED", arduino.ORDER_ATOMIC) ||
"100";
arduino.addInclude(
"include_leaphy_original",
'#include "Leaphyoriginal1.h"',
);

// Set different motor pins for nano robots
if (arduino.robotType.includes("nano"))
if (arduino.robotType.includes("nano")) {
const directionMap: Record<MotorDirection, number> = {
[MotorDirection.FORWARD]: 2,
[MotorDirection.BACKWARD]: 1,
[MotorDirection.LEFT]: 4,
[MotorDirection.RIGHT]: 3,
};
direction = directionMap[direction];
arduino.addSetup(
"set_motor_pins",
"setMotorPins(3, 2, 11, 4);",
true,
);
}

return `moveMotors(${dropdown_Type}, ${speed});\n`;
return `moveMotors(${direction}, ${speed});\n`;
};

arduino.forBlock["digital_read"] = function (block) {
Expand Down

0 comments on commit 482aa76

Please sign in to comment.