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
4a1bdb5a
Commit
4a1bdb5a
authored
Jul 20, 2015
by
carlosperate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add addDeclaration generator function and update codebase to use it.
parent
bb486337
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
27 deletions
+29
-27
blockly/generators/arduino.js
blockly/generators/arduino.js
+21
-19
blockly/generators/arduino/servo.js
blockly/generators/arduino/servo.js
+6
-6
blockly/generators/arduino/stepper.js
blockly/generators/arduino/stepper.js
+2
-2
No files found.
blockly/generators/arduino.js
View file @
4a1bdb5a
...
...
@@ -144,24 +144,15 @@ Blockly.Arduino.finish = function(code) {
code
=
code
.
replace
(
/
\n\s
+$/
,
'
\n
'
);
code
=
'
void loop() {
\n
'
+
code
+
'
\n
}
'
;
// Convert the definitions dictionary into a list
// TODO: remove regex expression once all includes are moved to their own dict
var
imports
=
[];
var
definitions
=
[];
for
(
var
name
in
Blockly
.
Arduino
.
definitions_
)
{
var
def
=
Blockly
.
Arduino
.
definitions_
[
name
];
if
(
def
.
match
(
/^#include/
))
{
imports
.
push
(
def
);
}
else
{
definitions
.
push
(
def
);
}
}
// Convert the includes, functions, and setup dictionaries into lists
var
includes
=
[],
functions
=
[],
setups
=
[],
userSetupCode
=
''
;
// Convert the includes, definitions, functions, and setup dicts into lists
var
includes
=
[],
definitions
=
[],
functions
=
[],
setups
=
[];
var
userSetupCode
=
''
;
for
(
var
name
in
Blockly
.
Arduino
.
includes_
)
{
includes
.
push
(
Blockly
.
Arduino
.
includes_
[
name
]);
}
for
(
var
name
in
Blockly
.
Arduino
.
definitions_
)
{
definitions
.
push
(
Blockly
.
Arduino
.
definitions_
[
name
]);
}
for
(
var
name
in
Blockly
.
Arduino
.
codeFunctions_
)
{
functions
.
push
(
Blockly
.
Arduino
.
codeFunctions_
[
name
]);
}
...
...
@@ -177,10 +168,9 @@ Blockly.Arduino.finish = function(code) {
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
'
+
userSetupCode
+
'
}
'
;
var
allDefs
=
includes
.
join
(
'
\n
'
)
+
'
\n\n
'
+
definitions
.
join
(
'
\n
'
)
+
'
\n\n
'
+
functions
.
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
;
};
...
...
@@ -196,6 +186,18 @@ Blockly.Arduino.addInclude = function(includeTag, code) {
}
};
/**
* Adds a string of code to be declared globally to the sketch.
* Once it is added it will not get overwritten with new code.
* @param {!string} declarationTag Identifier for this declaration code.
* @param {!string} code Code to be added below the includes.
*/
Blockly
.
Arduino
.
addDeclaration
=
function
(
declarationTag
,
code
)
{
if
(
Blockly
.
Arduino
.
definitions_
[
declarationTag
]
===
undefined
)
{
Blockly
.
Arduino
.
definitions_
[
declarationTag
]
=
code
;
}
};
/**
* Adds a string of code into the Arduino setup() function. It takes an
* identifier to not repeat the same kind of initialisation code from several
...
...
blockly/generators/arduino/servo.js
View file @
4a1bdb5a
...
...
@@ -32,9 +32,9 @@ Blockly.Arduino['servo_write'] = function(block) {
Blockly
.
Arduino
.
reservePin
(
block
,
pinKey
,
Blockly
.
Arduino
.
PinTypes
.
SERVO
,
'
Servo Write
'
);
Blockly
.
Arduino
.
definitions_
[
'
define_servo
'
]
=
'
#include <Servo.h>
\n
'
;
Blockly
.
Arduino
.
definitions_
[
'
global_servo_
'
+
pinKey
]
=
'
Servo
'
+
servoName
+
'
;
'
;
Blockly
.
Arduino
.
addInclude
(
'
servo
'
,
'
#include <Servo.h>
'
);
Blockly
.
Arduino
.
addDeclaration
(
'
servo_
'
+
pinKey
,
'
Servo
'
+
servoName
+
'
;
'
)
;
var
setupCode
=
servoName
+
'
.attach(
'
+
pinKey
+
'
);
'
;
Blockly
.
Arduino
.
addSetup
(
'
servo_
'
+
pinKey
,
setupCode
,
true
);
...
...
@@ -58,9 +58,9 @@ Blockly.Arduino['servo_read'] = function(block) {
Blockly
.
Arduino
.
reservePin
(
block
,
pinKey
,
Blockly
.
Arduino
.
PinTypes
.
SERVO
,
'
Servo Read
'
);
Blockly
.
Arduino
.
definitions_
[
'
define_servo
'
]
=
'
#include <Servo.h>
\n
'
;
Blockly
.
Arduino
.
definitions_
[
'
global_servo_
'
+
pinKey
]
=
'
Servo
'
+
servoName
+
'
;
'
;
Blockly
.
Arduino
.
addInclude
(
'
servo
'
,
'
#include <Servo.h>
'
);
Blockly
.
Arduino
.
addDeclaration
(
'
servo_
'
+
pinKey
,
'
Servo
'
+
servoName
+
'
;
'
)
;
var
setupCode
=
servoName
+
'
.attach(
'
+
pinKey
+
'
);
'
;
Blockly
.
Arduino
.
addSetup
(
'
servo_
'
+
pinKey
,
setupCode
,
true
);
...
...
blockly/generators/arduino/stepper.js
View file @
4a1bdb5a
...
...
@@ -36,11 +36,11 @@ Blockly.Arduino['stepper_config'] = function(block) {
Blockly
.
Arduino
.
reservePin
(
block
,
pin1
,
pinType
,
'
Stepper
'
);
Blockly
.
Arduino
.
reservePin
(
block
,
pin2
,
pinType
,
'
Stepper
'
);
Blockly
.
Arduino
.
definitions_
[
'
define_stepper
'
]
=
'
#include <Stepper.h>
\n
'
;
Blockly
.
Arduino
.
addInclude
(
'
stepper
'
,
'
#include <Stepper.h>
'
)
;
var
globalCode
=
'
Stepper
'
+
stepperName
+
'
(
'
+
stepperSteps
+
'
,
'
+
pin1
+
'
,
'
+
pin2
+
'
);
'
;
Blockly
.
Arduino
.
definitions_
[
'
global_stepper_
'
+
stepperName
]
=
globalCode
;
Blockly
.
Arduino
.
addDeclaration
(
'
stepper_
'
+
stepperName
,
globalCode
)
;
var
setupCode
=
stepperName
+
'
.setSpeed(
'
+
stepperSpeed
+
'
);
'
;
Blockly
.
Arduino
.
addSetup
(
'
stepper_
'
+
stepperName
,
setupCode
,
true
);
...
...
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