Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ardublockly
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
ardublockly
Commits
41debd97
Commit
41debd97
authored
Jul 20, 2015
by
carlosperate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enforce setup code ordering and move all codebase to use it.
parent
ab0cbe56
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
53 additions
and
35 deletions
+53
-35
blockly/generators/arduino.js
blockly/generators/arduino.js
+15
-8
blockly/generators/arduino/procedures.js
blockly/generators/arduino/procedures.js
+3
-3
blockly/generators/arduino/servo.js
blockly/generators/arduino/servo.js
+24
-15
blockly/generators/arduino/spi.js
blockly/generators/arduino/spi.js
+3
-3
blockly/generators/arduino/stepper.js
blockly/generators/arduino/stepper.js
+5
-5
blockly/generators/arduino/text.js
blockly/generators/arduino/text.js
+3
-1
No files found.
blockly/generators/arduino.js
View file @
41debd97
...
...
@@ -145,7 +145,7 @@ Blockly.Arduino.finish = function(code) {
}
// Convert the includes, functions, and setup dictionaries into lists
var
includes
=
[],
functions
=
[],
setups
=
[];
var
includes
=
[],
functions
=
[],
setups
=
[]
,
userSetupCode
=
''
;
for
(
var
name
in
Blockly
.
Arduino
.
includes_
)
{
includes
.
push
(
Blockly
.
Arduino
.
includes_
[
name
]);
}
...
...
@@ -155,13 +155,19 @@ Blockly.Arduino.finish = function(code) {
for
(
var
name
in
Blockly
.
Arduino
.
userFunctions_
)
{
functions
.
push
(
Blockly
.
Arduino
.
userFunctions_
[
name
]);
}
// userSetupCode is added at the end of the setup function
if
(
Blockly
.
Arduino
.
setups_
[
'
userSetupCode
'
]
!==
undefined
)
{
userSetupCode
=
'
\n
'
+
Blockly
.
Arduino
.
setups_
[
'
userSetupCode
'
];
delete
Blockly
.
Arduino
.
setups_
[
'
userSetupCode
'
];
}
for
(
var
name
in
Blockly
.
Arduino
.
setups_
)
{
setups
.
push
(
Blockly
.
Arduino
.
setups_
[
name
]);
}
var
allDefs
=
includes
.
join
(
'
\n
'
)
+
'
\n
'
+
imports
.
join
(
'
\n
'
)
+
'
\n
'
+
definitions
.
join
(
'
\n
'
)
+
'
\n\n
'
+
functions
.
join
(
'
\n\n
'
)
+
'
\n\n
void setup() {
\n
'
+
setups
.
join
(
'
\n
'
)
+
'
\n
}
'
;
'
\n\n
void setup() {
\n
'
+
setups
.
join
(
'
\n
'
)
+
'
\n
'
+
userSetupCode
+
'
}
'
;
return
allDefs
.
replace
(
/
\n\n
+/g
,
'
\n\n
'
).
replace
(
/
\n
*$/
,
'
\n\n\n
'
)
+
code
;
};
...
...
@@ -199,9 +205,8 @@ Blockly.Arduino.addSetup = function(setupTag, code, overwrite) {
* function name) to only keep a single copy even if multiple blocks might
* request this function to be created.
* A function (and its code) will only be added on first request.
* @param {!string}
function
Name Identifier for the function.
* @param {!string}
prefered
Name Identifier for the function.
* @param {!string} code Code to be included in the setup() function.
* @param {boolean=} overwrite Description.
* @return {!string} A unique function name based on input name.
*/
Blockly
.
Arduino
.
addFunction
=
function
(
preferedName
,
code
)
{
...
...
@@ -217,15 +222,17 @@ Blockly.Arduino.addFunction = function(preferedName, code) {
/**
* Description.
* @param {!Blockly.Block}
* @param {!Blockly.Block} block Description.
* @param {!string} pin Description.
* @param {!string} pinType Description.
* @param {!string} warningTag Description.
*/
Blockly
.
Arduino
.
reservePin
=
function
(
block
,
pin
,
pinType
,
warningTag
)
{
if
(
Blockly
.
Arduino
.
pins_
[
pin
]
!==
undefined
)
{
if
(
Blockly
.
Arduino
.
pins_
[
pin
]
!=
pinType
)
{
block
.
setWarningText
(
'
Pin
'
+
pin
+
'
is needed for
'
+
warningTag
+
'
as pin
'
+
pinType
+
'
. Already used as
'
+
Blockly
.
Arduino
.
pins_
[
pin
]
+
'
instead.
'
,
warningTag
);
'
. Already used as
'
+
Blockly
.
Arduino
.
pins_
[
pin
]
+
'
.
'
,
warningTag
);
}
else
{
block
.
setWarningText
(
null
,
warningTag
);
}
...
...
blockly/generators/arduino/procedures.js
View file @
41debd97
...
...
@@ -148,9 +148,9 @@ Blockly.Arduino['arduino_functions'] = function(block) {
var
setupBranch
=
Blockly
.
Arduino
.
statementToCode
(
block
,
'
SETUP_FUNC
'
);
// Remove first spacers as they will be added again in "addSetup()"
setupBranch
=
setupBranch
.
substring
(
2
);
var
setup
c
ode
=
Blockly
.
Arduino
.
scrub_
(
block
,
setupBranch
);
Blockly
.
Arduino
.
addSetup
(
'
userSetupCode
'
,
setup
c
ode
,
true
);
//
setupBranch = setupBranch.substring(2);
var
setup
C
ode
=
Blockly
.
Arduino
.
scrub_
(
block
,
setupBranch
);
Blockly
.
Arduino
.
addSetup
(
'
userSetupCode
'
,
setup
C
ode
,
true
);
var
loopBranch
=
statementToCodeNoTab
(
block
,
'
LOOP_FUNC
'
);
var
loopcode
=
Blockly
.
Arduino
.
scrub_
(
block
,
loopBranch
);
...
...
blockly/generators/arduino/servo.js
View file @
41debd97
...
...
@@ -18,29 +18,34 @@ goog.require('Blockly.Arduino');
/**
* Code generator to set an angle (Y) value to a servo PWM pin (X).
* Arduino code: #include <Servo.h>
* Servo myServo
_
X;
* setup { myServo
_
X.attach(X); }
* loop { myServo
_X.write(Y); }
* Servo myServoX;
* setup { myServoX.attach(X); }
* loop { myServo
X.write(Y); }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly
.
Arduino
[
'
servo_write
'
]
=
function
(
block
)
{
var
pinKey
=
block
.
getFieldValue
(
'
SERVO_PIN
'
);
var
pinType
=
Blockly
.
Arduino
.
Boards
.
pinTypes
.
SERVO
;
var
servoAngle
=
Blockly
.
Arduino
.
valueToCode
(
block
,
'
SERVO_ANGLE
'
,
Blockly
.
Arduino
.
ORDER_ATOMIC
)
||
'
90
'
;
var
servoAngle
=
Blockly
.
Arduino
.
valueToCode
(
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
.
definitions_
[
'
define_servo
'
]
=
'
#include <Servo.h>
\n
'
;
Blockly
.
Arduino
.
definitions_
[
'
global_servo_
'
+
pinKey
]
=
'
Servo
'
+
servoName
+
'
;
'
;
Blockly
.
Arduino
.
setups_
[
'
setup_servo_
'
+
pinKey
]
=
servoName
+
'
.attach(
'
+
pinKey
+
'
);
'
;
Blockly
.
Arduino
.
definitions_
[
'
global_servo_
'
+
pinKey
]
=
'
Servo
'
+
servoName
+
'
;
'
;
var
setupCode
=
servoName
+
'
.attach(
'
+
pinKey
+
'
);
'
Blockly
.
Arduino
.
addSetup
(
'
servo_
'
+
pinKey
,
setupCode
,
true
);
// If the IO has been configured already set a block warning for the user
if
(
pinKey
in
Blockly
.
Arduino
.
pins_
)
{
if
(
Blockly
.
Arduino
.
pins_
[
pinKey
]
!=
pinType
)
{
block
.
setWarningText
(
'
Pin already used as
'
+
Blockly
.
Arduino
.
pins_
[
pinKey
]);
block
.
setWarningText
(
'
Pin already used as
'
+
Blockly
.
Arduino
.
pins_
[
pinKey
]);
}
else
{
block
.
setWarningText
(
null
);
}
...
...
@@ -56,9 +61,9 @@ Blockly.Arduino['servo_write'] = function(block) {
/**
* Code generator to read an angle value from a servo PWM pin (X).
* Arduino code: #include <Servo.h>
* Servo myServo
_
X;
* setup { myServo
_
X.attach(X); }
* loop { myServo
_
X.read(); }
* Servo myServoX;
* setup { myServoX.attach(X); }
* loop { myServoX.read(); }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
...
...
@@ -66,18 +71,22 @@ Blockly.Arduino['servo_read'] = function(block) {
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
.
definitions_
[
'
define_servo
'
]
=
'
#include <Servo.h>
\n
'
;
Blockly
.
Arduino
.
definitions_
[
'
global_servo_
'
+
pinKey
]
=
'
Servo
'
+
servoName
+
'
;
'
;
Blockly
.
Arduino
.
setups_
[
'
setup_servo_
'
+
pinKey
]
=
servoName
+
'
.attach(
'
+
pinKey
+
'
);
'
;
Blockly
.
Arduino
.
definitions_
[
'
global_servo_
'
+
pinKey
]
=
'
Servo
'
+
servoName
+
'
;
'
;
var
setupCode
=
servoName
+
'
.attach(
'
+
pinKey
+
'
);
'
;
Blockly
.
Arduino
.
addSetup
(
'
servo_
'
+
pinKey
,
setupCode
,
true
);
// If the IO has been configured already set a block warning for the user
if
(
pinKey
in
Blockly
.
Arduino
.
pins_
)
{
if
(
Blockly
.
Arduino
.
pins_
[
pinKey
]
!=
pinType
)
{
block
.
setWarningText
(
'
Pin already used as
'
+
Blockly
.
Arduino
.
pins_
[
pinKey
]);
block
.
setWarningText
(
'
Pin already used as
'
+
Blockly
.
Arduino
.
pins_
[
pinKey
]);
}
else
{
block
.
setWarningText
(
null
);
}
...
...
blockly/generators/arduino/spi.js
View file @
41debd97
...
...
@@ -31,9 +31,9 @@ Blockly.Arduino['spi_setup'] = function(block) {
var
spiMode
=
block
.
getFieldValue
(
'
SPI_MODE
'
);
Blockly
.
Arduino
.
addInclude
(
'
spi
'
,
'
#include <SPI.h>
'
);
Blockly
.
Arduino
.
addSetup
(
'
s
etup_s
pi_order
'
,
Blockly
.
Arduino
.
addSetup
(
'
spi_order
'
,
spiId
+
'
.setBitOrder(
'
+
spiShift
+
'
);
'
,
true
);
Blockly
.
Arduino
.
addSetup
(
'
s
etup_s
pi_mode
'
,
Blockly
.
Arduino
.
addSetup
(
'
spi_mode
'
,
spiId
+
'
.setDataMode(
'
+
spiMode
+
'
);
'
,
true
);
Blockly
.
Arduino
.
addSetup
(
'
spi_div
'
,
spiId
+
'
.setClockDivider(
'
+
spiClockDivide
+
'
);
'
,
true
);
...
...
@@ -114,7 +114,7 @@ Blockly.Arduino['spi_transfer_return'] = function(block) {
'
digitalWrite(
'
+
spiSs
+
'
, HIGH);
'
,
'
spiReturn =
'
+
spiId
+
'
.transfer(
'
+
spiData
+
'
);
'
,
'
digitalWrite(
'
+
spiSs
+
'
, LOW);
'
,
'
return spiReturn;
'
'
return spiReturn;
'
,
'
}
'
];
var
functionName
=
Blockly
.
Arduino
.
addFunction
(
'
spiReturnSlave
'
+
spiSs
,
func
.
join
(
'
\n
'
));
...
...
blockly/generators/arduino/stepper.js
View file @
41debd97
...
...
@@ -33,14 +33,14 @@ Blockly.Arduino['stepper_config'] = function(block) {
var
stepperSpeed
=
Blockly
.
Arduino
.
valueToCode
(
block
,
'
STEPPER_SPEED
'
,
Blockly
.
Arduino
.
ORDER_ATOMIC
)
||
'
90
'
;
var
globalCode
=
'
Stepper
'
+
stepperName
+
'
(
'
+
stepperSteps
+
'
,
'
+
pin1
+
'
,
'
+
pin2
+
'
);
'
;
Blockly
.
Arduino
.
definitions_
[
'
define_stepper
'
]
=
'
#include <Stepper.h>
\n
'
;
var
setupCode
=
stepperName
+
'
.setSpeed(
'
+
stepperSpeed
+
'
);
'
;
Blockly
.
Arduino
.
addSetup
(
'
stepper_
'
+
stepperName
,
setupCode
,
true
);
// Maintain the setup regardless of pin conflict, warning should be enough
Blockly
.
Arduino
.
definitions_
[
'
define_stepper
'
]
=
'
#include <Stepper.h>
\n
'
;
var
globalCode
=
'
Stepper
'
+
stepperName
+
'
(
'
+
stepperSteps
+
'
,
'
+
pin1
+
'
,
'
+
pin2
+
'
);
'
;
Blockly
.
Arduino
.
definitions_
[
'
global_stepper_
'
+
stepperName
]
=
globalCode
;
Blockly
.
Arduino
.
setups_
[
'
setup_stepper_
'
+
stepperName
]
=
setupCode
;
// If the IO has been configured already set a block warning for the user
var
warningText
=
''
;
...
...
blockly/generators/arduino/text.js
View file @
41debd97
...
...
@@ -418,7 +418,9 @@ Blockly.Arduino['text_changeCase'] = function(block) {
Blockly
.
Arduino
[
'
text_prompt
'
]
=
function
(
block
)
{
// Prompt function.
Blockly
.
Arduino
.
setups_
[
'
serial_begin
'
]
=
'
Serial.begin(9600);
'
;
var
serialId
=
Blockly
.
Arduino
.
Boards
.
selected
.
serial
[
0
][
1
];
var
setupCode
=
serialId
+
'
.begin(9600);
'
;
Blockly
.
Arduino
.
addSetup
(
'
serial_
'
+
serialId
,
setupCode
,
false
);
var
msg
=
Blockly
.
Arduino
.
quote_
(
block
.
getFieldValue
(
'
TEXT
'
));
var
code
=
'
Serial.print(
'
+
msg
+
'
);
\n
'
;
var
toNumber
=
block
.
getFieldValue
(
'
TYPE
'
)
==
'
NUMBER
'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment