Skip to content

Commit

Permalink
fixes ElectroBlocks#129 motor issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavpr2404 committed May 30, 2024
1 parent 000ef3a commit d0b93ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
17 changes: 16 additions & 1 deletion src/blocks/color/generators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Blockly from "blockly";
import { variables } from "blockly/blocks";

function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
Expand All @@ -13,6 +14,7 @@ function hexToRgb(hex) {

Blockly["Arduino"]["color_picker_custom"] = function (block) {
const rgb = hexToRgb(block.getFieldValue("COLOR"));
addColorStructToLibarires();

return [
"{ " + rgb.r + ", " + rgb.g + ", " + rgb.b + "}",
Expand All @@ -21,9 +23,11 @@ Blockly["Arduino"]["color_picker_custom"] = function (block) {
};

Blockly["Arduino"]["colour_random"] = function (block) {
addColorStructToLibarires();
return [
"{ random(0, 255), random(0, 255), random(0, 255)}",
Blockly["Arduino"].ORDER_ATOMIC,

];
};

Expand All @@ -43,9 +47,20 @@ Blockly["Arduino"]["colour_rgb"] = function (block) {
"BLUE",
Blockly["Arduino"].ORDER_ATOMIC
);

addColorStructToLibarires();
return [
"{ " + red + ", " + green + ", " + blue + "}",
Blockly["Arduino"].ORDER_ATOMIC,
];
};

function addColorStructToLibarires() {
Blockly["Arduino"].libraries_['Color_Struct']= `
struct RGB {
int red;
int green;
int blue;
};
`;
}

10 changes: 8 additions & 2 deletions src/blocks/loops/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Blockly["Arduino"]["controls_repeat_ext"] = function (block: Block) {
"TIMES",
Blockly["Arduino"].ORDER_ASSIGNMENT
) || "0";

loopLine();
let branch = Blockly["Arduino"].statementToCode(block, "DO");
branch = Blockly["Arduino"].addLoopTrap(branch, block.id);
let code = "";
Expand All @@ -35,7 +35,7 @@ Blockly["Arduino"]["controls_for"] = function (block: Block) {
const loopIndexVariable = Blockly.getMainWorkspace().getVariableById(
block.getFieldValue("VAR")
).name;

const branch = Blockly["Arduino"].statementToCode(block, "DO");

const startNumber =
Expand Down Expand Up @@ -81,6 +81,7 @@ Blockly["Arduino"]["controls_for"] = function (block: Block) {

Blockly["Arduino"]["controls_whileUntil"] = function (block: Block) {
// Do while/until loop.

const until = block.getFieldValue("MODE") === "UNTIL";
let argument0 =
Blockly["Arduino"].valueToCode(
Expand All @@ -106,3 +107,8 @@ Blockly["Arduino"]["controls_flow_statements"] = function (block: Block) {
}
throw Error("Unknown flow statement.");
};

function loopLine(){
Blockly["Arduino"].libraries_['LoopL']= `int simple_loop_variable = 0;
`;
}
9 changes: 6 additions & 3 deletions src/blocks/motors/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ Blockly.defineBlocksWithJsonArray([
type: "field_dropdown",
name: "DIRECTION",
options: [
["Forward", "FORWARD"],
["Backward", "BACKWARD"],
["Clockwise", "CLOCKWISE"],
["Anti-Clockwise", "ANTI-CLOCKWISE"],
],
},



{
type: "input_dummy",
align: "RIGHT",
Expand All @@ -50,4 +53,4 @@ Blockly.defineBlocksWithJsonArray([
tooltip: "",
helpUrl: "",
},
]);
]);
7 changes: 4 additions & 3 deletions src/blocks/motors/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ Blockly['Arduino']['move_motor'] = function(block: Block) {
'SPEED',
Blockly['Arduino'].ORDER_ATOMIC
);

let dir = 'backward';
if(direction=='CLOCKWISE') dir= 'forward';
Blockly['Arduino'].libraries_['include_motor_library'] =
'#include <AFMotor.h>;\n';
Blockly['Arduino'].libraries_['include_motor_init_' + motorNumber] =
'AF_DCMotor motor_' + motorNumber + '(' + motorNumber + ');\n';

let code = 'motor_' + motorNumber + '.run("' + direction + '");\n';
let code = 'motor_' + motorNumber + '.run("' + dir + '");\n';
code += 'motor_' + motorNumber + '.setSpeed(' + speed + ');\n';

return code;
};
};

0 comments on commit d0b93ee

Please sign in to comment.