Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocky module for amperka's motor shield. #150

Merged
merged 5 commits into from Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions blockly/blockly.html
Expand Up @@ -4,9 +4,11 @@
<script type="text/javascript" src="blockly_compressed.js"></script>
<script type="text/javascript" src="blocks_compressed.js"></script>
<script type="text/javascript" src="en.js"></script>
<!-- <script type="text/javascript" src="ru.js"></script> -->
<script type="text/javascript" src="javascript_compressed.js"></script>
<script type="text/javascript" src="blockly_espruino.js"></script>
<script type="text/javascript" src="blockly_robot.js"></script>
<script type="text/javascript" src="blockly_amperka_motorshield.js"></script>
<script type="text/javascript" src="blockly_ble.js"></script>
<script type="text/javascript" src="field_textarea.js"></script>
<style>
Expand Down Expand Up @@ -310,6 +312,13 @@
<block type="robot_led"></block>
<block type="robot_ldr"></block>
</category>
<!-- Amperka's MotorShield -->
<category name="MotorShield">
<block type="amperka_motorshield_motor"></block>
<block type="amperka_motorshield_motor_dir"></block>
</category>
<!-- /Amperka's MotorShield -->

<category name="Bluetooth">
<block type="ble_connected"></block>
<block type="ble_disconnected"></block>
Expand Down
80 changes: 80 additions & 0 deletions blockly/blockly_amperka_motorshield.js
@@ -0,0 +1,80 @@
/**
Copyright 2014 Gordon Williams (gw@pur3.co.uk)
2016 Vasiliev Mikhail (mickvav@gmail.com)

This Source Code is subject to the terms of the Mozilla Public
License, v2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

------------------------------------------------------------------
Blockly blocks for Amperka Motor Shield
------------------------------------------------------------------
**/

var AMPERKA_MOTORSHIELD_COL = 200;


function amperka_motorshieldStatement(blk, comment) {
blk.setPreviousStatement(true);
blk.setNextStatement(true);
blk.setColour(AMPERKA_MOTORSHIELD_COL);
blk.setInputsInline(true);
blk.setTooltip(comment);
}
function amperka_motorshieldInput(blk, comment) {
blk.setOutput(true, 'Number');
blk.setColour(AMPERKA_MOTORSHIELD_COL);
blk.setInputsInline(true);
blk.setTooltip(comment);
}

// ----------------------------------------------------------
Blockly.Blocks.amperka_motorshield_motor = {
category: 'MotorShield',
init: function() {
var dropdown = new Blockly.FieldDropdown([
[Blockly.Msg.AMPERKA_MOTORSHIELD_RIGHT , 'B0'],
[Blockly.Msg.AMPERKA_MOTORSHIELD_LEFT, 'B1']
]);
this.appendValueInput('VAL')
.setCheck(['Number'])
.appendField(Blockly.Msg.AMPERKA_MOTORSHIELD_SET)
.appendField(dropdown, 'PIN')
.appendField(Blockly.Msg.AMPERKA_MOTORSHIELD_SPEED)
.appendField(new Blockly.FieldImage("media/speed.png",16,16, ".:"));
amperka_motorshieldStatement(this, Blockly.Msg.AMPERKA_MOTORSHIELD_SPEED_TOOLTIP);
}
};
Blockly.JavaScript.amperka_motorshield_motor = function() {
var pin = this.getTitleValue('PIN');
var mul = (pin=="B1") ? "-1" : "+1";

var val = Blockly.JavaScript.valueToCode(this, 'VAL', Blockly.JavaScript.ORDER_ASSIGNMENT) || '0';
return "var x = "+val+";\nanalogWrite("+pin+", x);\n";
};
// ----------------------------------------------------------
Blockly.Blocks.amperka_motorshield_motor_dir = {
category: 'MotorShield',
init: function() {
var dropdown = new Blockly.FieldDropdown([
[Blockly.Msg.AMPERKA_MOTORSHIELD_RIGHT, 'C2'],
[Blockly.Msg.AMPERKA_MOTORSHIELD_LEFT, 'C3']
]);
this.appendValueInput('VAL')
.setCheck(['Boolean'])
.appendField(Blockly.Msg.AMPERKA_MOTORSHIELD_SET)
.appendField(dropdown, 'PIN')
.appendField('Motor Direction')
.appendField(new Blockly.FieldImage("media/direction.png",16,16, "<->"));
amperka_motorshieldStatement(this,Blockly.Msg.AMPERKA_MOTORSHIELD_DIRECTION_TOOLTIP );
}
};
Blockly.JavaScript.amperka_motorshield_motor_dir = function() {
var pin = this.getTitleValue('PIN');
var m1 = (pin=="C3") ? "1" : "0";
var m2 = (pin=="C3") ? "0" : "1";
var val = Blockly.JavaScript.valueToCode(this, 'VAL', Blockly.JavaScript.ORDER_ASSIGNMENT) || 'false';
return "var x = "+val+";\nif (x) digitalWrite("+pin+","+m1+"); else digitalWrite("+pin+", "+m2+");\n";
};


88 changes: 45 additions & 43 deletions blockly/blockly_espruino.js
Expand Up @@ -58,51 +58,52 @@ Blockly.Blocks.espruino_delay = {
init: function() {
this.appendValueInput('SECONDS')
.setCheck('Number')
.appendField('wait');
.appendField(Blockly.Msg.ESPRUINO_WAIT);
this.appendDummyInput()
.appendField("seconds");
.appendField(Blockly.Msg.ESPRUINO_SECONDS);

this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Delay for a certain amount of time');
this.setTooltip(Blockly.Msg.ESPRUINO_WAIT_TOOLTIP);
}
};
Blockly.Blocks.espruino_timeout = {
category: 'Espruino',
init: function() {
this.appendValueInput('SECONDS')
.setCheck('Number')
.appendField('after');
.appendField(Blockly.Msg.ESPRUINO_AFTER);
this.appendDummyInput()
.appendField("seconds");
.appendField(Blockly.Msg.ESPRUINO_SECONDS);
this.appendStatementInput('DO')
.appendField('do');
.appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);


this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Waits for a certain period before running code');
this.setTooltip(Blockly.Msg.ESPRUINO_AFTER_TOOLTIP);
}
};
Blockly.Blocks.espruino_interval = {
category: 'Espruino',
init: function() {
this.appendValueInput('SECONDS')
.setCheck('Number')
.appendField('every');
.appendField(Blockly.Msg.ESPRUINO_EVERY);
this.appendDummyInput()
.appendField("seconds");
.appendField(Blockly.Msg.ESPRUINO_SECONDS);
this.appendStatementInput('DO')
.appendField('do');
.appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);

this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Runs code repeatedly, every so many seconds');
this.setTooltip(Blockly.Msg.ESPRUINO_EVERY_TOOLTIP );
}
};

Expand All @@ -116,16 +117,16 @@ Blockly.Blocks.espruino_pin = {
var listGen = function() {
originalPin = this.value_;
var list = PINS.slice(start, start+incrementStep);
if (start>0) list.unshift(['Back...', 'Back']);
if (start+incrementStep<PINS.length) list.push(['More...', 'More']);
if (start>0) list.unshift([Blockly.Msg.ESPRUINO_BACK+"...", Blockly.Msg.ESPRUINO_BACK]);
if (start+incrementStep<PINS.length) list.push([Blockly.Msg.ESPRUINO_MORE + '...', Blockly.Msg.ESPRUINO_MORE]);
return list;
};

var pinSelector = new Blockly.FieldDropdown(listGen, function(selection){
var ret = undefined;

if (selection == "More" || selection == "Back") {
if (selection == "More")
if (selection == Blockly.Msg.ESPRUINO_MORE || selection == Blockly.Msg.ESPRUINO_BACK) {
if (selection == Blockly.Msg.ESPRUINO_MORE)
start += incrementStep;
else
start -= incrementStep;
Expand All @@ -140,7 +141,7 @@ Blockly.Blocks.espruino_pin = {
this.setColour(ESPRUINO_COL);
this.setOutput(true, 'Pin');
this.appendDummyInput().appendField(pinSelector, 'PIN');
this.setTooltip('The Name of a Pin');
this.setTooltip(Blockly.Msg.ESPRUINO_PIN_NAME);
},
};

Expand All @@ -150,17 +151,17 @@ Blockly.Blocks.espruino_watch = {
init: function() {
this.appendValueInput('PIN')
.setCheck('Pin')
.appendField('watch');
.appendField(Blockly.Msg.ESPRUINO_WATCH);
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown(this.EDGES), 'EDGE').appendField('edge');;
this.appendStatementInput('DO')
.appendField('do');
.appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);

this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Runs code when an input changes');
this.setTooltip(Blockly.Msg.ESPRUINO_WATCH_TOOLTIP);
},
EDGES: [
["both", 'both'],
Expand All @@ -172,11 +173,11 @@ EDGES: [
Blockly.Blocks.espruino_getTime = {
category: 'Espruino',
init: function() {
this.appendDummyInput().appendField('Time');
this.appendDummyInput().appendField(Blockly.Msg.ESPRUINO_TIME);
this.setOutput(true, 'Number');
this.setColour(230/*Number*/);
this.setInputsInline(true);
this.setTooltip('Read the current time in seconds');
this.setTooltip(Blockly.Msg.ESPRUINO_TIME_TOOLTIP);
}
};

Expand All @@ -186,48 +187,48 @@ Blockly.Blocks.espruino_digitalWrite = {
init: function() {
this.appendValueInput('PIN')
.setCheck('Pin')
.appendField('digitalWrite Pin');
.appendField(Blockly.Msg.ESPRUINO_DIGITALWRITE);
this.appendValueInput('VAL')
.setCheck(['Number','Boolean'])
.appendField('Value');
.appendField(Blockly.Msg.ESPRUINO_VALUE);

this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Writes a Digital Value to a Pin');
this.setTooltip(Blockly.Msg.ESPRUINO_DIGITALWRITE_TOOLTIP);
}
};
Blockly.Blocks.espruino_digitalPulse = {
category: 'Espruino',
init: function() {
this.appendValueInput('PIN')
.setCheck('Pin')
.appendField('digitalPulse Pin');
.appendField(Blockly.Msg.ESPRUINO_DIGITALPULSE);
this.appendValueInput('VAL')
.setCheck(['Boolean']);
this.appendValueInput('TIME')
.setCheck(['Number'])
.appendField('Milliseconds');
.appendField(Blockly.Msg.ESPRUINO_MILLISECONDS);

this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Pulses a pin for the given number of milliseconds');
this.setTooltip(Blockly.Msg.ESPRUINO_DIGITALPULSE_TOOLTIP);
}
};
Blockly.Blocks.espruino_digitalRead = {
category: 'Espruino',
init: function() {
this.appendValueInput('PIN')
.setCheck('Pin')
.appendField('digitalRead Pin');
.appendField(Blockly.Msg.ESPRUINO_DIGITALREAD);

this.setOutput(true, 'Boolean');
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Read a Digital Value from a Pin');
this.setTooltip(Blockly.Msg.ESPRUINO_DIGITALREAD_TOOLTIP);
}
};

Expand All @@ -236,29 +237,29 @@ Blockly.Blocks.espruino_analogWrite = {
init: function() {
this.appendValueInput('PIN')
.setCheck('Pin')
.appendField('analogWrite Pin');
.appendField(Blockly.Msg.ESPRUINO_ANALOGWRITE);
this.appendValueInput('VAL')
.setCheck(['Number','Boolean'])
.appendField('Value');
.appendField(Blockly.Msg.ESPRUINO_VALUE);

this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Writes an Analog Value to a Pin');
this.setTooltip(Blockly.Msg.ESPRUINO_ANALOGWRITE_TOOLTIP);
}
};
Blockly.Blocks.espruino_analogRead = {
category: 'Espruino',
init: function() {
this.appendValueInput('PIN')
.setCheck('Pin')
.appendField('analogRead Pin');
.appendField(Blockly.Msg.ESPRUINO_ANALOGREAD);

this.setOutput(true, 'Number');
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Read an Analog Value from a Pin');
this.setTooltip(Blockly.Msg.ESPRUINO_ANALOGREAD_TOOLTIP);
}
};

Expand All @@ -271,7 +272,7 @@ Blockly.Blocks.espruino_code = {
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Executes the given JavaScript code');
this.setTooltip(Blockly.Msg.ESPRUINO_JS_TOOLTIP);
}
};
// -----------------------------------------------------------------------------------
Expand All @@ -280,45 +281,46 @@ Blockly.Blocks.hw_servoMove = {
init: function() {
this.appendValueInput('PIN')
.setCheck('Pin')
.appendField('Move Servo on Pin');
.appendField(Blockly.Msg.ESPRUINO_MOVE_SERVO);
this.appendValueInput('VAL')
.setCheck(['Number','Boolean'])
.appendField('to');
.appendField(Blockly.Msg.ESPRUINO_TO);

this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Start moving the servo motor - position between -1 and 1');
this.setTooltip(Blockly.Msg.ESPRUINO_MOVE_SERVO_TOOLTIP);
}
};
Blockly.Blocks.hw_servoStop = {
category: 'Espruino',
init: function() {
this.appendValueInput('PIN')
.setCheck('Pin')
.appendField('Stop Servo on Pin');
.appendField(Blockly.Msg.ESPRUINO_STOP_SERVO);

this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Stop moving the servo motor');
this.setTooltip(Blockly.Msg.ESPRUINO_STOP_SERVO_TOOLTIP);

}
};
Blockly.Blocks.hw_ultrasonic = {
category: 'Espruino',
init: function() {
this.appendValueInput('TRIG')
.setCheck('Pin')
.appendField('Get distance, trigger');
.appendField(Blockly.Msg.ESPRUINO_ULTRASONIC_GET_TRIG);
this.appendValueInput('ECHO')
.setCheck('Pin')
.appendField(', echo');
.appendField(Blockly.Msg.ESPRUINO_ULTRASONIC_ECHO);
this.setOutput(true, 'Number');
this.setColour(ESPRUINO_COL);
this.setInputsInline(true);
this.setTooltip('Return distance in centimetres from the ultrasonic sensor');
this.setTooltip(Blockly.Msg.ESPRUINO_ULTRASONIC_TOOLTIP);
}
};

Expand Down