Commit f842c766 authored by Carlos's avatar Carlos

Added the SPI library to the arduino code generator.

It includes the new blocks and code generators in spi.js
The arduino app has been update the include this blocks in a 'Comms' category
The generators/arduino.js has also been updated to use 80 characters per line
parent 56256f09
......@@ -46,7 +46,7 @@ codepage.start = function(opt_data, opt_ignored, opt_ijData) {
codepage.toolbox = function(opt_data, opt_ignored, opt_ijData) {
return '<xml id="toolbox" style="display: none"><category name="Logic"><block type="controls_if"></block><block type="logic_compare"></block><block type="logic_operation"></block><block type="logic_negate"></block><block type="logic_boolean"></block><block type="logic_null"></block><block type="logic_ternary"></block></category><category name="Loops"><block type="controls_repeat_ext"><value name="TIMES"><block type="math_number"><field name="NUM">10</field></block></value></block><block type="controls_whileUntil"></block><block type="controls_for"><value name="FROM"><block type="math_number"><field name="NUM">1</field></block></value><value name="TO"><block type="math_number"><field name="NUM">10</field></block></value><value name="BY"><block type="math_number"><field name="NUM">1</field></block></value></block><block type="controls_forEach"></block><block type="controls_flow_statements"></block></category><category name="Math"><block type="math_number"></block><block type="math_arithmetic"></block><block type="math_single"></block><block type="math_trig"></block><block type="math_constant"></block><block type="math_number_property"></block><block type="math_change"><value name="DELTA"><block type="math_number"><field name="NUM">1</field></block></value></block><block type="math_round"></block><block type="math_on_list"></block><block type="math_modulo"></block><block type="math_constrain"><value name="LOW"><block type="math_number"><field name="NUM">1</field></block></value><value name="HIGH"><block type="math_number"><field name="NUM">100</field></block></value></block><block type="math_random_int"><value name="FROM"><block type="math_number"><field name="NUM">1</field></block></value><value name="TO"><block type="math_number"><field name="NUM">100</field></block></value></block><block type="math_random_float"></block><block type="base_map"></block></category><category name="Text"><block type="text"></block><block type="text_join"></block><block type="text_append"><value name="TEXT"><block type="text"></block></value></block><block type="text_length"></block><block type="text_isEmpty"></block><block type="text_indexOf"><value name="VALUE"><block type="variables_get"><field name="VAR">text</field></block></value></block><block type="text_charAt"><value name="VALUE"><block type="variables_get"><field name="VAR">text</field></block></value></block><block type="text_getSubstring"><value name="STRING"><block type="variables_get"><field name="VAR">text</field></block></value></block><block type="text_changeCase"></block><block type="text_trim"></block><block type="text_print"></block><block type="text_prompt_ext"><value name="TEXT"><block type="text"></block></value></block></category><category name="Lists"><block type="lists_create_empty"></block><block type="lists_create_with"></block><block type="lists_repeat"><value name="NUM"><block type="math_number"><field name="NUM">5</field></block></value></block><block type="lists_length"></block><block type="lists_isEmpty"></block><block type="lists_indexOf"><value name="VALUE"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_getIndex"><value name="VALUE"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_setIndex"><value name="LIST"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_getSublist"><value name="LIST"><block type="variables_get"><field name="VAR">list</field></block></value></block></category><category name="Variables"><block type="variables_get"></block><block type="variables_set"></block><block type="variables_set_type"></block></category><category name="Functions" custom="PROCEDURE"></category><category name="Input/Output"><block type="io_digitalwrite"></block><block type="io_digitalread"></block><block type="io_builtin_led"></block><block type="io_analogwrite"></block><block type="io_analogread"></block><block type="io_highlow"></block></category><category name="Time"><block type="time_delay"></block><block type="time_delaymicros"></block><block type="time_millis"></block><block type="time_micros"></block></category><category name="Servo"><block type="servo_write"></block><block type="servo_read"></block></category><category name="Stepper"><block type="stepper_config"></block><block type="stepper_step"></block></category></xml>';
return '<xml id="toolbox" style="display: none"><category name="Logic"><block type="controls_if"></block><block type="logic_compare"></block><block type="logic_operation"></block><block type="logic_negate"></block><block type="logic_boolean"></block><block type="logic_null"></block><block type="logic_ternary"></block></category><category name="Loops"><block type="controls_repeat_ext"><value name="TIMES"><block type="math_number"><field name="NUM">10</field></block></value></block><block type="controls_whileUntil"></block><block type="controls_for"><value name="FROM"><block type="math_number"><field name="NUM">1</field></block></value><value name="TO"><block type="math_number"><field name="NUM">10</field></block></value><value name="BY"><block type="math_number"><field name="NUM">1</field></block></value></block><block type="controls_forEach"></block><block type="controls_flow_statements"></block></category><category name="Math"><block type="math_number"></block><block type="math_arithmetic"></block><block type="math_single"></block><block type="math_trig"></block><block type="math_constant"></block><block type="math_number_property"></block><block type="math_change"><value name="DELTA"><block type="math_number"><field name="NUM">1</field></block></value></block><block type="math_round"></block><block type="math_on_list"></block><block type="math_modulo"></block><block type="math_constrain"><value name="LOW"><block type="math_number"><field name="NUM">1</field></block></value><value name="HIGH"><block type="math_number"><field name="NUM">100</field></block></value></block><block type="math_random_int"><value name="FROM"><block type="math_number"><field name="NUM">1</field></block></value><value name="TO"><block type="math_number"><field name="NUM">100</field></block></value></block><block type="math_random_float"></block><block type="base_map"></block></category><category name="Text"><block type="text"></block><block type="text_join"></block><block type="text_append"><value name="TEXT"><block type="text"></block></value></block><block type="text_length"></block><block type="text_isEmpty"></block><block type="text_indexOf"><value name="VALUE"><block type="variables_get"><field name="VAR">text</field></block></value></block><block type="text_charAt"><value name="VALUE"><block type="variables_get"><field name="VAR">text</field></block></value></block><block type="text_getSubstring"><value name="STRING"><block type="variables_get"><field name="VAR">text</field></block></value></block><block type="text_changeCase"></block><block type="text_trim"></block><block type="text_print"></block><block type="text_prompt_ext"><value name="TEXT"><block type="text"></block></value></block></category><category name="Lists"><block type="lists_create_empty"></block><block type="lists_create_with"></block><block type="lists_repeat"><value name="NUM"><block type="math_number"><field name="NUM">5</field></block></value></block><block type="lists_length"></block><block type="lists_isEmpty"></block><block type="lists_indexOf"><value name="VALUE"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_getIndex"><value name="VALUE"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_setIndex"><value name="LIST"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_getSublist"><value name="LIST"><block type="variables_get"><field name="VAR">list</field></block></value></block></category><category name="Variables"><block type="variables_get"></block><block type="variables_set"></block><block type="variables_set_type"></block></category><category name="Functions" custom="PROCEDURE"></category><category name="Input/Output"><block type="io_digitalwrite"></block><block type="io_digitalread"></block><block type="io_builtin_led"></block><block type="io_analogwrite"></block><block type="io_analogread"></block><block type="io_highlow"></block></category><category name="Time"><block type="time_delay"></block><block type="time_delaymicros"></block><block type="time_millis"></block><block type="time_micros"></block></category><category name="Servo"><block type="servo_write"></block><block type="servo_read"></block></category><category name="Stepper"><block type="stepper_config"></block><block type="stepper_step"></block></category><category name="Comms"><block type="spi_config"></block><block type="spi_transfer"></block></category></xml>';
};
......
......@@ -294,6 +294,10 @@
<block type="stepper_config"></block>
<block type="stepper_step"></block>
</category>
<category name="{msg meaning="Apps.catStepper" desc="IBID"}Comms{/msg}">
<block type="spi_config"></block>
<block type="spi_transfer"></block>
</category>
</xml>
{/template}
......
......@@ -69,15 +69,42 @@ Blockly.Arduino.ORDER_NONE = 99; // (...)
*/
var profile = {
arduino: {
name: "Arduino Uno",
description: "Arduino Uno standard-compatible board",
digital : [["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"], ["12", "12"], ["13", "13"], ["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"], ["A5", "A5"]],
analog : [["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"], ["A5", "A5"]],
pwm : [["3", "3"], ["5", "5"], ["6", "6"], ["9", "9"], ["10", "10"], ["11", "11"]],
interrupt: [["Int_1", "1"], ["Int_2", "2"], ["Int_3", "3"], ["Int_4", "4"], ["Int_5", "5"]],
serial : [["300", "300"], ["600", "600"], ["1200", "1200"], ["2400", "2400"], ["4800", "4800"], ["9600", "9600"], ["14400", "14400"], ["19200", "19200"], ["28800", "28800"], ["31250", "31250"], ["38400", "38400"],["57600", "57600"], ["115200", "115200"]],
digital : [["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"],
["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"],
["11", "11"], ["12", "12"], ["13", "13"], ["A0", "A0"],
["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"],
["A5", "A5"]],
analog : [["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"],
["A4", "A4"], ["A5", "A5"]],
pwm : [["3", "3"], ["5", "5"], ["6", "6"], ["9", "9"], ["10", "10"],
["11", "11"]],
interrupt: [["Int_1", "1"], ["Int_2", "2"], ["Int_3", "3"], ["Int_4", "4"],
["Int_5", "5"]],
serial : [["300", "300"], ["600", "600"], ["1200", "1200"],
["2400", "2400"], ["4800", "4800"], ["9600", "9600"],
["14400", "14400"], ["19200", "19200"], ["28800", "28800"],
["31250", "31250"], ["38400", "38400"],["57600", "57600"],
["115200", "115200"]],
builtin_led: [["BUILTIN_1", "13"]],
pin_types: { INPUT: "INPUT", OUTPUT: "OUTPUT", PWM: "PWM", SERVO: "SERVO" , STEPPER: "STEPPER" },
types : [["void", "void"], ["Boolean", "boolean"], ["Character", "char"], ["Unsigned Character", "unsigned char"], ["Byte", "byte"], ["Integer", "int"], ["Unsigned Integer", "unsigned int"], ["Word", "word"], ["Long", "long"], ["Unsigned Long", "unsigned long"], ["Short", "short"], ["Float", "float"], ["Double", "double"], ["String", "String"], ["Char Array", "string"], ["Array", "array"]]
pin_types: { INPUT: "INPUT", OUTPUT: "OUTPUT", PWM: "PWM", SERVO: "SERVO",
STEPPER: "STEPPER", SPI: "SPI" },
types : [["void", "void"], ["Boolean", "boolean"], ["Character", "char"],
["Unsigned Character", "unsigned char"], ["Byte", "byte"],
["Integer", "int"], ["Unsigned Integer", "unsigned int"],
["Word", "word"], ["Long", "long"],
["Unsigned Long", "unsigned long"], ["Short", "short"],
["Float", "float"], ["Double", "double"], ["String", "String"],
["Char Array", "string"], ["Array", "array"]],
spi_clock_divide: [['2 (8MHz)', 'SPI_CLOCK_DIV2'],
['4 (4MHz)', 'SPI_CLOCK_DIV4'],
['8 (2MHz)', 'SPI_CLOCK_DIV8'],
['16 (1MHz)', 'SPI_CLOCK_DIV16'],
['32 (500KHz)', 'SPI_CLOCK_DIV32'],
['64 (250KHz)', 'SPI_CLOCK_DIV64'],
['128 (125KHz)', 'SPI_CLOCK_DIV128']],
spi_pins: [["MOSI", "11"], ["MISO", "12"], ["SCK", "13"]]
},
arduino_mega:{
description: "Arduino Mega-compatible board"
......@@ -112,7 +139,8 @@ Blockly.Arduino.init = function() {
Blockly.Arduino.pins_ = Object.create(null);
if (!Blockly.Arduino.variableDB_) {
Blockly.Arduino.variableDB_ = new Blockly.Names(Blockly.Arduino.RESERVED_WORDS_);
Blockly.Arduino.variableDB_ =
new Blockly.Names(Blockly.Arduino.RESERVED_WORDS_);
} else {
Blockly.Arduino.variableDB_.reset();
}
......@@ -136,7 +164,8 @@ Blockly.Arduino.init = function() {
// If it is the first instance log the variable type
if (unique == true) {
variableWithType[blockVariables[y]] = Blockly.Arduino.evaluateType(
Blockly.Arduino.valueToCode(blocks[x], 'VALUE', Blockly.Arduino.ORDER_ASSIGNMENT));
Blockly.Arduino.valueToCode(blocks[x], 'VALUE',
Blockly.Arduino.ORDER_ASSIGNMENT));
}
}
}
......@@ -147,7 +176,8 @@ Blockly.Arduino.init = function() {
for (var name in variableWithType) {
variableDeclarations.push(variableWithType[name] + ' ' + name + ';');
}
Blockly.Arduino.definitions_['variables'] = variableDeclarations.join('\n') + '\n';
Blockly.Arduino.definitions_['variables'] = variableDeclarations.join('\n') +
'\n';
};
......
/**
* @fileoverview Blocks extension for Arduino SPI library.
* The Arduino SPI functions syntax can be found in the following URL
* http://arduino.cc/en/Reference/SPI
*/
'use strict';
goog.provide('Blockly.Arduino.spi');
goog.require('Blockly.Arduino');
/**
* Block for the spi configuration. Info in the setHelpUrl link.
* @this Blockly.Block
*/
Blockly.Blocks['spi_config'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/SPI');
this.setColour(255);
this.appendDummyInput()
.appendField('SPI Configuration:');
this.appendDummyInput()
.appendField('Data Shift')
.appendField(
new Blockly.FieldDropdown([['MSBFIRST', 'MSBFIRST'], ['LSBFIRST', 'LSBFIRST']]),
'SPI_SHIFT_ORDER');
this.appendDummyInput()
.appendField('Clock Divide')
.appendField(
new Blockly.FieldDropdown(profile.default.spi_clock_divide),
'SPI_CLOCK_DIVIDE');
this.appendDummyInput()
.appendField('SPI Mode (Idle - Edge)')
.appendField(
new Blockly.FieldDropdown(
[['0 (Low - Falling)', 'SPI_MODE0'],
['1 (Low - Rising)', 'SPI_MODE1'],
['2 (High - Falling)', 'SPI_MODE2'],
['3 (High - Rising)', 'SPI_MODE3']]),
'SPI_MODE');
this.setTooltip('');
}
};
/**
* Code generator for the spi configuration block. It does not add any LoC to
* the loop(), but it generates code for the setup() function.
*/
Blockly.Arduino['spi_config'] = function(block) {
var spi_shift = block.getFieldValue('SPI_SHIFT_ORDER');
var spi_clock_divide = block.getFieldValue('SPI_CLOCK_DIVIDE');
var spi_mode = block.getFieldValue('SPI_MODE');
Blockly.Arduino.definitions_['define_spi'] = '#include <SPI.h>';
Blockly.Arduino.setups_['setup_spi_order'] =
'SPI.setBitOrder(' + spi_shift + ');';
Blockly.Arduino.setups_['setup_spi_mode'] =
'SPI.setDataMode(' + spi_mode + ');';
Blockly.Arduino.setups_['setup_spi_div'] =
'SPI.setClockDivider(' + spi_clock_divide + ');';
Blockly.Arduino.setups_['setup_spi_begin'] =
'SPI.begin();';
var code = '';
return code;
};
/**
* Block for for the spi transfer. Info in the setHelpUrl link.
* @this Blockly.Block
*/
Blockly.Blocks['spi_transfer'] = {
init: function() {
// Drop down list to contains all digital pins plus an option for 'none'
var pin_dropdown_extended = [['none', 'none']];
for(var i=0; i<profile.default.digital.length; i++) {
pin_dropdown_extended.push(profile.default.digital[i]);
}
this.setHelpUrl('http://arduino.cc/en/Reference/SPITransfer');
this.setColour(255);
this.appendDummyInput()
.appendField('To SPI Slave pin')
.appendField(
new Blockly.FieldDropdown(pin_dropdown_extended), 'SPI_SS');
this.appendDummyInput('')
.appendField('transfer');
this.appendValueInput('SPI_DATA', '');
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip('Send SPI message to an specified slave device');
}
};
/**
* Code generator for the spi transfer block.
* SPI bus can have several slaves, which are selected using a digital output
* as a SS pin. This digital pin will be configured as a normal output (io.js)
*/
Blockly.Arduino['spi_transfer'] = function(block) {
var spi_ss = block.getFieldValue('SPI_SS');
var spi_data = Blockly.Arduino.valueToCode(
block, 'SPI_DATA', Blockly.Arduino.ORDER_ATOMIC) || '0';
var code = '';
Blockly.Arduino.definitions_['define_spi'] = '#include <SPI.h>';
Blockly.Arduino.setups_['setup_spi_begin'] = 'SPI.begin();';
// Configure the SPI pins as used, or warn if already in use
var warning_text = '';
var pin_type = profile.default.pin_types.SPI;
for(var i=0; i<profile.default.spi_pins.length; i++) {
var pin_number = profile.default.spi_pins[i][1];
if (pin_number in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pin_number] != pin_type) {
warning_text = warning_text +
'Pin alredy used as ' + Blockly.Arduino.pins_[pin_number] + '\n';
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pin_number] = pin_type;
}
}
if (warning_text === '') {
block.setWarningText(null);
} else {
block.setWarningText(warning_text);
}
// Configure the Slave Select as a normal output if a pin is used
if (spi_ss != 'none') {
var pin_type = profile.default.pin_types.OUTPUT;
var set_up_key = 'setup_io_' + spi_ss;
Blockly.Arduino.setups_[set_up_key] = 'pinMode(' + spi_ss + ', OUTPUT);';
// If the IO has been configured already set a warning for the user
if (spi_ss in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[spi_ss] != pin_type) {
warning_text = warning_text + 'Pin alredy used as ' +
Blockly.Arduino.pins_[spi_ss];
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[spi_ss] = pin_type;
}
}
if (warning_text === '') {
block.setWarningText(null);
} else {
block.setWarningText(warning_text);
}
// Add the code, but only use a SS pin if one is selected
if (spi_ss != 'none') {
code = code + 'digitalWrite(' + spi_ss + ', HIGH);\n';
}
code = code + 'SPI.transfer(' + spi_data + ');\n';
if (spi_ss != 'none') {
code = code + 'digitalWrite(' + spi_ss + ', LOW);\n';
}
return code;
};
......@@ -27,7 +27,11 @@ goog.provide('Blockly.Arduino.variables');
goog.require('Blockly.Arduino');
/**
* Code generator for variable getter
* @param {block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['variables_get'] = function(block) {
// Variable getter.
var code = Blockly.Arduino.variableDB_.getName(block.getFieldValue('VAR'),
......@@ -35,9 +39,12 @@ Blockly.Arduino['variables_get'] = function(block) {
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
/**
* Code generator for variable setter
* @param {block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['variables_set'] = function(block) {
// Variable setter.
var argument0 = Blockly.Arduino.valueToCode(block, 'VALUE',
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
var varName = Blockly.Arduino.variableDB_.getName(
......@@ -45,7 +52,10 @@ Blockly.Arduino['variables_set'] = function(block) {
return varName + ' = ' + argument0 + ';\n';
};
/**
* Block for variable casting.
* @this Blockly.Block
*/
Blockly.Blocks['variables_set_type'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/HomePage');
......@@ -61,6 +71,11 @@ Blockly.Blocks['variables_set_type'] = {
}
};
/**
* Code generator for variable casting.
* @param {block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['variables_set_type'] = function(block) {
// Variable set type.
var argument0 = Blockly.Arduino.valueToCode(block, 'VARIABLE_SETTYPE_INPUT',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment