Commit bb486337 authored by carlosperate's avatar carlosperate

Moved all codebase to use the reservePin function for pin allocation.

The pin type definitions has also been moved to the Arduino main generator, as it is not directly related to the board profiles (its original location).
parent 41debd97
...@@ -58,7 +58,20 @@ Blockly.Arduino.ORDER_CONDITIONAL = 13; // expr ? expr : expr ...@@ -58,7 +58,20 @@ Blockly.Arduino.ORDER_CONDITIONAL = 13; // expr ? expr : expr
Blockly.Arduino.ORDER_ASSIGNMENT = 14; // = *= /= ~/= %= += -= <<= >>= &= ^= |= Blockly.Arduino.ORDER_ASSIGNMENT = 14; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
Blockly.Arduino.ORDER_NONE = 99; // (...) Blockly.Arduino.ORDER_NONE = 99; // (...)
/**
* A list of types tasks that the pins can be assigned. Used to track usage and
* warn if the same pin has been assigned to more than one task.
*/
Blockly.Arduino.PinTypes = {
INPUT: 'INPUT',
OUTPUT: 'OUTPUT',
PWM: 'PWM',
SERVO: 'SERVO',
STEPPER: 'STEPPER',
SERIAL: 'SERIAL',
I2C: 'I2C/TWI',
SPI: 'SPI'
};
/** /**
* Arduino generator short name for * Arduino generator short name for
...@@ -174,12 +187,12 @@ Blockly.Arduino.finish = function(code) { ...@@ -174,12 +187,12 @@ Blockly.Arduino.finish = function(code) {
/** /**
* Adds a string of "include" code to be added to the sketch. * Adds a string of "include" code to be added to the sketch.
* Once a include is added it will not get overwritten with new code. * Once a include is added it will not get overwritten with new code.
* @param {!string} includetag Identifier for this include code. * @param {!string} includeTag Identifier for this include code.
* @param {!string} code Code to be included in the setup() function. * @param {!string} code Code to be included at the very top of the sketch.
*/ */
Blockly.Arduino.addInclude = function(includetag, code) { Blockly.Arduino.addInclude = function(includeTag, code) {
if (Blockly.Arduino.includes_[includetag] === undefined) { if (Blockly.Arduino.includes_[includeTag] === undefined) {
Blockly.Arduino.includes_[includetag] = code; Blockly.Arduino.includes_[includeTag] = code;
} }
}; };
......
...@@ -43,23 +43,6 @@ Blockly.Arduino.Boards.generateAnalogIo = function(pinStart, pinEnd) { ...@@ -43,23 +43,6 @@ Blockly.Arduino.Boards.generateAnalogIo = function(pinStart, pinEnd) {
return analogIo; return analogIo;
}; };
/**
* A list of types tasks that the pins can be assigned. This is used to keep
* track of which pins have been assigned and be able to warn the user if the
* same pin has been assigned more than one different task
*/
Blockly.Arduino.Boards.pinTypes = {
INPUT: 'INPUT',
OUTPUT: 'OUTPUT',
PWM: 'PWM',
SERVO: 'SERVO',
STEPPER: 'STEPPER',
SERIAL: 'SERIAL',
I2C: 'I2C/TWI',
SPI: 'SPI'
};
/** Arduino Uno board profile. */ /** Arduino Uno board profile. */
Blockly.Arduino.Boards.uno = { Blockly.Arduino.Boards.uno = {
name: 'Arduino Uno', name: 'Arduino Uno',
......
...@@ -26,7 +26,7 @@ Blockly.Arduino['io_digitalwrite'] = function(block) { ...@@ -26,7 +26,7 @@ Blockly.Arduino['io_digitalwrite'] = function(block) {
block, 'STATE', Blockly.Arduino.ORDER_ATOMIC) || '0'; block, 'STATE', Blockly.Arduino.ORDER_ATOMIC) || '0';
Blockly.Arduino.reservePin( Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.OUTPUT, 'Digital Write'); block, pin, Blockly.Arduino.PinTypes.OUTPUT, 'Digital Write');
var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);'; var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false); Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
...@@ -45,7 +45,7 @@ Blockly.Arduino['io_digitalwrite'] = function(block) { ...@@ -45,7 +45,7 @@ Blockly.Arduino['io_digitalwrite'] = function(block) {
Blockly.Arduino['io_digitalread'] = function(block) { Blockly.Arduino['io_digitalread'] = function(block) {
var pin = block.getFieldValue('PIN'); var pin = block.getFieldValue('PIN');
Blockly.Arduino.reservePin( Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.INPUT, 'Digital Read'); block, pin, Blockly.Arduino.PinTypes.INPUT, 'Digital Read');
var pinSetupCode = 'pinMode(' + pin + ', INPUT);'; var pinSetupCode = 'pinMode(' + pin + ', INPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false); Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
...@@ -67,7 +67,7 @@ Blockly.Arduino['io_builtin_led'] = function(block) { ...@@ -67,7 +67,7 @@ Blockly.Arduino['io_builtin_led'] = function(block) {
block, 'STATE', Blockly.Arduino.ORDER_ATOMIC) || '0'; block, 'STATE', Blockly.Arduino.ORDER_ATOMIC) || '0';
Blockly.Arduino.reservePin( Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.OUTPUT, 'Set LED'); block, pin, Blockly.Arduino.PinTypes.OUTPUT, 'Set LED');
var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);'; var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false); Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
...@@ -89,7 +89,7 @@ Blockly.Arduino['io_analogwrite'] = function(block) { ...@@ -89,7 +89,7 @@ Blockly.Arduino['io_analogwrite'] = function(block) {
block, 'NUM', Blockly.Arduino.ORDER_ATOMIC) || '0'; block, 'NUM', Blockly.Arduino.ORDER_ATOMIC) || '0';
Blockly.Arduino.reservePin( Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.OUTPUT, 'Analogue Write'); block, pin, Blockly.Arduino.PinTypes.OUTPUT, 'Analogue Write');
var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);'; var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false); Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
...@@ -108,7 +108,7 @@ Blockly.Arduino['io_analogwrite'] = function(block) { ...@@ -108,7 +108,7 @@ Blockly.Arduino['io_analogwrite'] = function(block) {
Blockly.Arduino['io_analogread'] = function(block) { Blockly.Arduino['io_analogread'] = function(block) {
var pin = block.getFieldValue('PIN'); var pin = block.getFieldValue('PIN');
Blockly.Arduino.reservePin( Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.INPUT, 'Analogue Read'); block, pin, Blockly.Arduino.PinTypes.INPUT, 'Analogue Read');
var pinSetupCode = 'pinMode(' + pin + ', INPUT);'; var pinSetupCode = 'pinMode(' + pin + ', INPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false); Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
......
...@@ -26,35 +26,20 @@ goog.require('Blockly.Arduino'); ...@@ -26,35 +26,20 @@ goog.require('Blockly.Arduino');
*/ */
Blockly.Arduino['servo_write'] = function(block) { Blockly.Arduino['servo_write'] = function(block) {
var pinKey = block.getFieldValue('SERVO_PIN'); var pinKey = block.getFieldValue('SERVO_PIN');
var pinType = Blockly.Arduino.Boards.pinTypes.SERVO;
var servoAngle = Blockly.Arduino.valueToCode( var servoAngle = Blockly.Arduino.valueToCode(
block, 'SERVO_ANGLE', Blockly.Arduino.ORDER_ATOMIC) || '90'; block, 'SERVO_ANGLE', Blockly.Arduino.ORDER_ATOMIC) || '90';
var servoName = 'myServo' + pinKey; var servoName = 'myServo' + pinKey;
var code = servoName + '.write(' + servoAngle + ');\n';
// Maintain the setup regardless of pin conflict, warning should be enough Blockly.Arduino.reservePin(
block, pinKey, Blockly.Arduino.PinTypes.SERVO, 'Servo Write');
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n'; Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['global_servo_' + pinKey] = Blockly.Arduino.definitions_['global_servo_' + pinKey] =
'Servo ' + servoName + ';'; 'Servo ' + servoName + ';';
var setupCode = servoName + '.attach(' + pinKey + ');' var setupCode = servoName + '.attach(' + pinKey + ');';
Blockly.Arduino.addSetup('servo_' + pinKey, setupCode, true); Blockly.Arduino.addSetup('servo_' + pinKey, setupCode, true);
// If the IO has been configured already set a block warning for the user var code = servoName + '.write(' + servoAngle + ');\n';
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText(
'Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pinKey] = pinType;
block.setWarningText(null);
}
return code; return code;
}; };
...@@ -69,12 +54,10 @@ Blockly.Arduino['servo_write'] = function(block) { ...@@ -69,12 +54,10 @@ Blockly.Arduino['servo_write'] = function(block) {
*/ */
Blockly.Arduino['servo_read'] = function(block) { Blockly.Arduino['servo_read'] = function(block) {
var pinKey = block.getFieldValue('SERVO_PIN'); var pinKey = block.getFieldValue('SERVO_PIN');
var pinType = Blockly.Arduino.Boards.pinTypes.SERVO;
var servoName = 'myServo' + pinKey; var servoName = 'myServo' + pinKey;
var code = servoName + '.read()';
// Maintain the setup regardless of pin conflict, warning should be enough Blockly.Arduino.reservePin(
block, pinKey, Blockly.Arduino.PinTypes.SERVO, 'Servo Read');
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n'; Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['global_servo_' + pinKey] = Blockly.Arduino.definitions_['global_servo_' + pinKey] =
'Servo ' + servoName + ';'; 'Servo ' + servoName + ';';
...@@ -82,19 +65,6 @@ Blockly.Arduino['servo_read'] = function(block) { ...@@ -82,19 +65,6 @@ Blockly.Arduino['servo_read'] = function(block) {
var setupCode = servoName + '.attach(' + pinKey + ');'; var setupCode = servoName + '.attach(' + pinKey + ');';
Blockly.Arduino.addSetup('servo_' + pinKey, setupCode, true); Blockly.Arduino.addSetup('servo_' + pinKey, setupCode, true);
// If the IO has been configured already set a block warning for the user var code = servoName + '.read()';
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText(
'Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pinKey] = pinType;
block.setWarningText(null);
}
return [code, Blockly.Arduino.ORDER_ATOMIC]; return [code, Blockly.Arduino.ORDER_ATOMIC];
}; };
...@@ -68,14 +68,14 @@ Blockly.Arduino['spi_transfer'] = function(block) { ...@@ -68,14 +68,14 @@ Blockly.Arduino['spi_transfer'] = function(block) {
for (var i = 0; i < Blockly.Arduino.Boards.selected.spiPins.length; i++) { for (var i = 0; i < Blockly.Arduino.Boards.selected.spiPins.length; i++) {
Blockly.Arduino.reservePin(block, Blockly.Arduino.reservePin(block,
Blockly.Arduino.Boards.selected.spiPins[i][1], Blockly.Arduino.Boards.selected.spiPins[i][1],
Blockly.Arduino.Boards.pinTypes.SPI, Blockly.Arduino.PinTypes.SPI,
'SPI ' + Blockly.Arduino.Boards.selected.spiPins[i][0]); 'SPI ' + Blockly.Arduino.Boards.selected.spiPins[i][0]);
} }
// Configure the Slave Select as a normal output if a pin is used // Configure the Slave Select as a normal output if a pin is used
if (spiSs !== 'none') { if (spiSs !== 'none') {
Blockly.Arduino.reservePin( Blockly.Arduino.reservePin(
block, spiSs, Blockly.Arduino.Boards.pinTypes.OUTPUT, 'SPI Slave pin'); block, spiSs, Blockly.Arduino.PinTypes.OUTPUT, 'SPI Slave pin');
var setupCode = 'pinMode(' + spiSs + ', OUTPUT);'; var setupCode = 'pinMode(' + spiSs + ', OUTPUT);';
Blockly.Arduino.addSetup('io_' + spiSs, setupCode, false); Blockly.Arduino.addSetup('io_' + spiSs, setupCode, false);
} // else means the SS pin is always set for the device } // else means the SS pin is always set for the device
......
...@@ -26,44 +26,24 @@ goog.require('Blockly.Arduino'); ...@@ -26,44 +26,24 @@ goog.require('Blockly.Arduino');
Blockly.Arduino['stepper_config'] = function(block) { Blockly.Arduino['stepper_config'] = function(block) {
var pin1 = block.getFieldValue('STEPPER_PIN1'); var pin1 = block.getFieldValue('STEPPER_PIN1');
var pin2 = block.getFieldValue('STEPPER_PIN2'); var pin2 = block.getFieldValue('STEPPER_PIN2');
var pinType = Blockly.Arduino.Boards.pinTypes.STEPPER; var pinType = Blockly.Arduino.PinTypes.STEPPER;
var stepperName = block.getStepperSetupInstance(); var stepperName = block.getStepperSetupInstance();
var stepperSteps = Blockly.Arduino.valueToCode(block, 'STEPPER_STEPS', var stepperSteps = Blockly.Arduino.valueToCode(block, 'STEPPER_STEPS',
Blockly.Arduino.ORDER_ATOMIC) || '360'; Blockly.Arduino.ORDER_ATOMIC) || '360';
var stepperSpeed = Blockly.Arduino.valueToCode(block, 'STEPPER_SPEED', var stepperSpeed = Blockly.Arduino.valueToCode(block, 'STEPPER_SPEED',
Blockly.Arduino.ORDER_ATOMIC) || '90'; Blockly.Arduino.ORDER_ATOMIC) || '90';
Blockly.Arduino.definitions_['define_stepper'] = '#include <Stepper.h>\n'; Blockly.Arduino.reservePin(block, pin1, pinType, 'Stepper');
Blockly.Arduino.reservePin(block, pin2, pinType, 'Stepper');
var setupCode = stepperName + '.setSpeed(' + stepperSpeed + ');'; Blockly.Arduino.definitions_['define_stepper'] = '#include <Stepper.h>\n';
Blockly.Arduino.addSetup('stepper_' + stepperName, setupCode, true);
var globalCode = 'Stepper ' + stepperName + '(' + stepperSteps + ', ' + var globalCode = 'Stepper ' + stepperName + '(' + stepperSteps + ', ' +
pin1 + ', ' + pin2 + ');'; pin1 + ', ' + pin2 + ');';
Blockly.Arduino.definitions_['global_stepper_' + stepperName] = globalCode; Blockly.Arduino.definitions_['global_stepper_' + stepperName] = globalCode;
// If the IO has been configured already set a block warning for the user var setupCode = stepperName + '.setSpeed(' + stepperSpeed + ');';
var warningText = ''; Blockly.Arduino.addSetup('stepper_' + stepperName, setupCode, true);
if (pin1 in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pin1] != pinType) {
warningText = 'Pin ' + pin1 + ' already used as ' +
Blockly.Arduino.pins_[pin1] + '. ';
}
}
if (pin2 in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pin2] != pinType) {
warningText = warningText + 'Pin ' + pin2 + ' already used as ' +
Blockly.Arduino.pins_[pin2] + '. ';
}
}
if (warningText === '') {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pin1] = pinType;
Blockly.Arduino.pins_[pin2] = pinType;
block.setWarningText(null);
} else {
block.setWarningText(warningText);
}
return ''; return '';
}; };
......
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