Skip to content

Commit

Permalink
feat!: allow 'Arduino.robotType' to be used in generators (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoesbergen authored Nov 6, 2023
1 parent 3220bbb commit b64273d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 132 deletions.
19 changes: 0 additions & 19 deletions blocks/leaphy_click.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,6 @@ const blocks = [
tooltip: "",
helpUrl: "",
},
{
type: "leaphy_click_nano_set_motor",
message0: "%%{BKY_LEAPHY_MOTOR_TYPE} %1 %2 %%{BKY_LEAPHY_MOTOR_SPEED} %3",
args0: [
{
type: "field_dropdown",
name: "MOTOR_TYPE",
options: motorDropdown,
},
{ type: "input_dummy" },
{ type: "input_value", name: "MOTOR_SPEED", check: "Number" },
],
inputsInline: true,
previousStatement: null,
nextStatement: null,
style: "leaphy_blocks",
tooltip: "",
helpUrl: "",
},
{
type: "leaphy_click_rgb_digitalwrite",
message0:
Expand Down
17 changes: 0 additions & 17 deletions blocks/leaphy_flitz.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,6 @@ const blocks = [
tooltip: "",
helpUrl: "",
},
{
type: "leaphy_flitz_nano_led",
message0:
"%%{BKY_LEAPHY_FLITZ_LED} %1 %%{BKY_LEAPHY_FLITZ_LED_R} %2 %%{BKY_LEAPHY_FLITZ_LED_G} %3 %%{BKY_LEAPHY_FLITZ_LED_B} %4",
args0: [
{ type: "input_dummy" },
{ type: "input_value", name: "FLITZ_LED_R", check: "Number" },
{ type: "input_value", name: "FLITZ_LED_G", check: "Number" },
{ type: "input_value", name: "FLITZ_LED_B", check: "Number" },
],
inputsInline: true,
previousStatement: null,
nextStatement: null,
style: "leaphy_blocks",
tooltip: "",
helpUrl: "",
},
];

export { blocks };
38 changes: 0 additions & 38 deletions blocks/leaphy_original.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,6 @@ const blocks = [
tooltip: "",
helpUrl: "",
},
{
type: "leaphy_nano_set_motor",
message0: "%%{BKY_LEAPHY_MOTOR_TYPE} %1 %2 %%{BKY_LEAPHY_MOTOR_SPEED} %3",
args0: [
{
type: "field_dropdown",
name: "MOTOR_TYPE",
options: motorLeftRightDropdown,
},
{ type: "input_dummy" },
{ type: "input_value", name: "MOTOR_SPEED", check: "Number" },
],
inputsInline: true,
previousStatement: null,
nextStatement: null,
style: "leaphy_blocks",
tooltip: "",
helpUrl: "",
},
{
type: "leaphy_original_get_distance",
message0: "%%{BKY_LEAPHY_GET_DISTANCE}",
Expand Down Expand Up @@ -109,25 +90,6 @@ const blocks = [
tooltip: "",
helpUrl: "",
},
{
type: "leaphy_nano_move_motors",
message0: "%%{BKY_LEAPHY_MOTOR_DIRECTION} %1 %2 %3",
args0: [
{
type: "field_dropdown",
name: "MOTOR_DIRECTION",
options: motorForwardBackwardDropdown,
},
{ type: "input_dummy" },
{ type: "input_value", name: "MOTOR_SPEED", check: "Number" },
],
inputsInline: true,
previousStatement: null,
nextStatement: null,
style: "leaphy_blocks",
tooltip: "",
helpUrl: "",
},
{
type: "leaphy_original_buzz",
message0:
Expand Down
8 changes: 8 additions & 0 deletions generators/arduino/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ Arduino.noGeneratorCodeLine = function () {
return "";
};

// Override workspaceToCode with an additional robotType parameter that can be used by code generators
// to change bevaviour (like pins) for different robot types
Arduino._workspaceToCode = Arduino.workspaceToCode;
Arduino.workspaceToCode = function (workspace, robotType) {
Arduino.robotType = robotType;
return Arduino._workspaceToCode(workspace);
};

import * as arduino from "./arduino";
import * as leaphy_click from "./leaphy_click";
import * as leaphy_common from "./leaphy_common";
Expand Down
58 changes: 27 additions & 31 deletions generators/arduino/leaphy_flitz.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,34 @@ function getCodeGenerators(Arduino) {
Arduino.valueToCode(this, "FLITZ_LED_G", Arduino.ORDER_ATOMIC) || "0";
var flitz_blue =
Arduino.valueToCode(this, "FLITZ_LED_B", Arduino.ORDER_ATOMIC) || "0";
var code =
"analogWrite(3, " +
flitz_red +
");\nanalogWrite(5, " +
flitz_green +
");\nanalogWrite(6, " +
flitz_blue +
");\n";
return code;
};

Arduino.forBlock["leaphy_flitz_nano_led"] = function (block) {
var flitz_red =
Arduino.valueToCode(this, "FLITZ_LED_R", Arduino.ORDER_ATOMIC) || "0";
var flitz_green =
Arduino.valueToCode(this, "FLITZ_LED_G", Arduino.ORDER_ATOMIC) || "0";
var flitz_blue =
Arduino.valueToCode(this, "FLITZ_LED_B", Arduino.ORDER_ATOMIC) || "0";
// Ground is connected to pin 8 on the nano, so it needs to be pulled LOW
Arduino.addSetup(
"setup_flitz_nano_rgb",
"pinMode(8, OUTPUT);\n digitalWrite(8, LOW);",
false,
);
var code =
"analogWrite(11, " +
flitz_red +
");\nanalogWrite(10, " +
flitz_green +
");\nanalogWrite(9, " +
flitz_blue +
");\n";
var code;
if (Arduino.robotType.includes("nano")) {
// Ground is connected to pin 8 on the nano, so it needs to be pulled LOW
Arduino.addSetup(
"setup_flitz_nano_rgb",
"pinMode(8, OUTPUT);\n digitalWrite(8, LOW);",
false,
);
code =
"analogWrite(11, " +
flitz_red +
");\nanalogWrite(10, " +
flitz_green +
");\nanalogWrite(9, " +
flitz_blue +
");\n";
} else {
code =
"analogWrite(3, " +
flitz_red +
");\nanalogWrite(5, " +
flitz_green +
");\nanalogWrite(6, " +
flitz_blue +
");\n";
}

return code;
};
}
Expand Down
32 changes: 6 additions & 26 deletions generators/arduino/leaphy_original.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,16 @@ function getCodeGenerators(Arduino) {
"include_leaphy_original",
'#include "Leaphyoriginal1.h"',
);
var code = "setMotor(" + dropdown_Type + ", " + speed + ");\n";
return code;
};
// Set different motor pins for nano robots
if (Arduino.robotType.includes("nano"))
Arduino.addSetup("set_motor_pins", "setMotorPins(3, 2, 11, 4);", true);

Arduino.forBlock["leaphy_nano_set_motor"] = function (block) {
var dropdown_Type = block.getFieldValue("MOTOR_TYPE");
var speed =
Arduino.valueToCode(this, "MOTOR_SPEED", Arduino.ORDER_ATOMIC) || "100";
Arduino.addInclude(
"include_leaphy_original",
'#include "Leaphyoriginal1.h"',
);
Arduino.addSetup("set_motor_pins", "setMotorPins(3, 2, 11, 4);", true);
var code = "setMotor(" + dropdown_Type + ", " + speed + ");\n";
return code;
};

Arduino.forBlock["leaphy_click_set_motor"] =
Arduino.forBlock["leaphy_original_set_motor"];
Arduino.forBlock["leaphy_click_nano_set_motor"] =
Arduino.forBlock["leaphy_nano_set_motor"];

Arduino.forBlock["leaphy_original_get_distance"] = function (block) {
Arduino.addInclude(
Expand All @@ -60,19 +49,10 @@ function getCodeGenerators(Arduino) {
"include_leaphy_original",
'#include "Leaphyoriginal1.h"',
);
var code = "moveMotors(" + dropdown_Type + ", " + speed + ");\n";
return code;
};
// Set different motor pins for nano robots
if (Arduino.robotType.includes("nano"))
Arduino.addSetup("set_motor_pins", "setMotorPins(3, 2, 11, 4);", true);

Arduino.forBlock["leaphy_nano_move_motors"] = function (block) {
var dropdown_Type = block.getFieldValue("MOTOR_DIRECTION");
var speed =
Arduino.valueToCode(this, "MOTOR_SPEED", Arduino.ORDER_ATOMIC) || "100";
Arduino.addInclude(
"include_leaphy_original",
'#include "Leaphyoriginal1.h"',
);
Arduino.addSetup("set_motor_pins", "setMotorPins(3, 2, 11, 4);", true);
var code = "moveMotors(" + dropdown_Type + ", " + speed + ");\n";
return code;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"msg",
"theme"
],
"version": "1.4.1",
"version": "1.5.0",
"description": "Leaphy custom Blockly blocks and arduino code generator",
"name": "@leaphy-robotics/leaphy-blocks"
}

0 comments on commit b64273d

Please sign in to comment.