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
ad52c839
Commit
ad52c839
authored
Feb 26, 2015
by
carlosperate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Stepper update: better warnings, fix carlosperate/ardublockly#10 and code cleanup.
parent
e74d6e11
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
67 deletions
+85
-67
blocks/arduino/stepper.js
blocks/arduino/stepper.js
+81
-63
generators/arduino/stepper.js
generators/arduino/stepper.js
+4
-4
No files found.
blocks/arduino/stepper.js
View file @
ad52c839
...
...
@@ -8,9 +8,9 @@
* Additional functions apart from the normal generators have
* been added to be able to generate the 'set' drop down menu
* with all current instaces of the Stepper class:
* Blockly.Blocks.Arduino.stepper.
all
Instances
* Blockly.Blocks.Arduino.stepper.
stepper
Instances
* Blockly.Blocks.Arduino.stepper.FieldStepperInstance
* Blockly.Blocks.Arduino.stepper.
FieldStepperInstance.dropdownCreate
* Blockly.Blocks.Arduino.stepper.
stepperDropdownList
*
* TODO: Still need to had some kind of handler to refresh the "set" drop down
* menu values if an instance in a 'configure' block is renamed.
...
...
@@ -29,81 +29,63 @@ Blockly.Blocks.Arduino.stepper.HUE = 75;
/**
* Strings for easy reference
*/
Blockly
.
Blocks
.
Arduino
.
stepper
.
no
_i
nstance
=
'
No_Instances
'
;
Blockly
.
Blocks
.
Arduino
.
stepper
.
no
_n
ame
=
'
Empty_input_name
'
;
Blockly
.
Blocks
.
Arduino
.
stepper
.
no
I
nstance
=
'
No_Instances
'
;
Blockly
.
Blocks
.
Arduino
.
stepper
.
no
N
ame
=
'
Empty_input_name
'
;
/**
* Finds all user-created instances of the Stepper block config.
* Based on Blockly.Variables.allVariables
* @return {!Array.<string>} Array of instance names.
*/
Blockly
.
Blocks
.
Arduino
.
stepper
.
allInstances
=
function
()
{
Blockly
.
Blocks
.
Arduino
.
stepper
.
stepperInstances
=
function
()
{
var
stepperList
=
[];
var
blocks
=
Blockly
.
mainWorkspace
.
getAllBlocks
();
var
variableHash
=
Object
.
create
(
null
);
// Iterate through every block and add each Stetter config name to the hash.
for
(
var
x
=
0
;
x
<
blocks
.
length
;
x
++
)
{
var
func
=
blocks
[
x
].
getStepperInstance
;
if
(
func
)
{
var
blockVariables
=
func
.
call
(
blocks
[
x
]);
for
(
var
y
=
0
;
y
<
blockVariables
.
length
;
y
++
)
{
var
varName
=
blockVariables
[
y
];
// Variable name may be null if the block is only half-built.
if
(
varName
)
{
variableHash
[
varName
.
toLowerCase
()]
=
varName
;
var
getStepperInstance
=
blocks
[
x
].
getStepperInstance
;
if
(
getStepperInstance
)
{
var
stepperInstance
=
getStepperInstance
.
call
(
blocks
[
x
]);
if
(
stepperInstance
)
{
stepperList
.
push
(
stepperInstance
);
}
}
}
}
// Flatten the hash into a list.
var
variableList
=
[];
for
(
var
name
in
variableHash
)
{
variableList
.
push
(
variableHash
[
name
]);
}
return
variableList
;
};
/**
* Class for a variable's dropdown field.
* @param {Function} opt_changeHandler A function that is executed when a new
* option is selected. Its sole argument is the new option value. Its
* return value is ignored.
* @extends {Blockly.FieldDropdown}
* @constructor
*/
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
=
function
()
{
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
.
superClass_
.
constructor
.
call
(
this
,
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
.
dropdownCreate
);
return
stepperList
;
};
goog
.
inherits
(
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
,
Blockly
.
FieldDropdown
);
/**
* Return a sorted list of instances names for set dropdown menu.
* @return {!Array.<string>} Array of variable names.
* @this {!Blockly.Blocks.Arduino.stepper.FieldStepperInstance}
* @return {!Array.<string>} Array of stepper instances names.
*/
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
.
dropdownCreate
=
function
()
{
var
variableList
=
Blockly
.
Blocks
.
Arduino
.
stepper
.
all
Instances
();
Blockly
.
Blocks
.
Arduino
.
stepper
.
stepperDropdownList
=
function
()
{
var
stepperList
=
Blockly
.
Blocks
.
Arduino
.
stepper
.
stepper
Instances
();
var
options
=
[];
if
(
variable
List
.
length
>
0
)
{
variable
List
.
sort
(
goog
.
string
.
caseInsensitiveCompare
);
if
(
stepper
List
.
length
>
0
)
{
stepper
List
.
sort
(
goog
.
string
.
caseInsensitiveCompare
);
// Variables are not language-specific, use the name as both the
// user-facing text and the internal representation.
for
(
var
x
=
0
;
x
<
variable
List
.
length
;
x
++
)
{
options
[
x
]
=
[
variableList
[
x
],
variable
List
[
x
]];
for
(
var
x
=
0
;
x
<
stepper
List
.
length
;
x
++
)
{
options
[
x
]
=
[
stepperList
[
x
],
stepper
List
[
x
]];
}
}
else
{
// There are no config blocks in the work area
options
[
0
]
=
[
Blockly
.
Blocks
.
Arduino
.
stepper
.
no
_i
nstance
,
Blockly
.
Blocks
.
Arduino
.
stepper
.
no_i
nstance
];
options
[
0
]
=
[
Blockly
.
Blocks
.
Arduino
.
stepper
.
no
I
nstance
,
Blockly
.
Blocks
.
Arduino
.
stepper
.
noI
nstance
];
}
return
options
;
};
/**
* Class for a variable's dropdown field.
* @extends {Blockly.FieldDropdown}
* @constructor
*/
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
=
function
()
{
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
.
superClass_
.
constructor
.
call
(
this
,
Blockly
.
Blocks
.
Arduino
.
stepper
.
stepperDropdownList
);
};
goog
.
inherits
(
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
,
Blockly
.
FieldDropdown
);
Blockly
.
Blocks
[
'
stepper_config
'
]
=
{
/**
* Block for for the stepper generator configuration including creating
...
...
@@ -140,14 +122,14 @@ Blockly.Blocks['stepper_config'] = {
* @this Blockly.Block
*/
getStepperInstance
:
function
()
{
var
InstanceName
;
var
InstanceName
=
null
;
var
instanceNameBlock
=
this
.
getInputTargetBlock
(
'
STEPPER_NAME
'
);
if
(
!
instanceNameBlock
)
{
InstanceName
=
Blockly
.
Blocks
.
Arduino
.
stepper
.
no
_n
ame
;
InstanceName
=
Blockly
.
Blocks
.
Arduino
.
stepper
.
no
N
ame
;
}
else
{
InstanceName
=
instanceNameBlock
.
getFieldValue
(
'
TEXT
'
);
}
return
[
InstanceName
]
;
return
InstanceName
;
}
};
...
...
@@ -181,16 +163,52 @@ Blockly.Blocks['stepper_step'] = {
// Block has been deleted.
return
;
}
var
instances
=
Blockly
.
Blocks
.
Arduino
.
stepper
.
FieldStepperInstance
.
dropdownCreate
();
if
(
instances
[
0
][
0
]
===
Blockly
.
Blocks
.
Arduino
.
stepper
.
no_instance
)
{
this
.
setWarningText
(
'
A STEPPER configuration block must be added to
'
+
var
currentDropdown
=
this
.
getFieldValue
(
'
STEPPER_NAME
'
);
var
instances
=
Blockly
.
Blocks
.
Arduino
.
stepper
.
stepperDropdownList
();
// Check for configuration block presence
if
(
instances
[
0
][
0
]
===
Blockly
.
Blocks
.
Arduino
.
stepper
.
noInstance
)
{
// Ensure dropdown menu says there is no config block
if
(
currentDropdown
!==
Blockly
.
Blocks
.
Arduino
.
stepper
.
noInstance
)
{
this
.
setFieldValue
(
Blockly
.
Blocks
.
Arduino
.
stepper
.
noInstance
,
'
STEPPER_NAME
'
);
}
this
.
setWarningText
(
'
A STEPPER configuration block must be added to
'
+
'
use this block!
'
);
}
else
if
(
instances
[
0
][
0
]
===
Blockly
.
Blocks
.
Arduino
.
stepper
.
no_name
)
{
this
.
setWarningText
(
'
A Name input must be added to the Stepper
'
+
'
configuration block!
'
);
}
else
{
this
.
setWarningText
(
null
);
// Configuration blocks present, check if any selected and contains name
var
existingConfigSelected
=
false
;
for
(
var
x
=
0
;
x
<
instances
.
length
;
x
++
)
{
// Check if any of the config blocks does not have a name
if
(
instances
[
x
][
0
]
===
Blockly
.
Blocks
.
Arduino
.
stepper
.
noName
)
{
// If selected config has no name either, set warning and exit func
if
(
currentDropdown
===
Blockly
.
Blocks
.
Arduino
.
stepper
.
noName
)
{
this
.
setWarningText
(
'
A Name input must be added to the Stepper
'
+
'
configuration block!
'
);
return
;
}
}
else
if
(
instances
[
x
][
0
]
===
currentDropdown
)
{
existingConfigSelected
=
true
;
}
}
// At this point select config has a name, check if it exist
if
(
existingConfigSelected
)
{
// All good, just remove any warnings and exit the function
this
.
setWarningText
(
null
);
}
else
{
if
((
currentDropdown
===
Blockly
.
Blocks
.
Arduino
.
stepper
.
noName
)
||
(
currentDropdown
===
Blockly
.
Blocks
.
Arduino
.
stepper
.
noInstance
))
{
// Just pick the first config block
this
.
setFieldValue
(
instances
[
0
][
0
],
'
STEPPER_NAME
'
);
this
.
setWarningText
(
null
);
}
else
{
// Al this point just set a waning to select a valid stepper config
this
.
setWarningText
(
'
Selected stepper does not exist anymore,
'
+
'
please select a new one.
'
);
}
}
}
}
};
generators/arduino/stepper.js
View file @
ad52c839
...
...
@@ -46,13 +46,13 @@ Blockly.Arduino['stepper_config'] = function(block) {
var
warningText
=
''
;
if
(
pin1
in
Blockly
.
Arduino
.
pins_
)
{
if
(
Blockly
.
Arduino
.
pins_
[
pin1
]
!=
pinType
)
{
warningText
=
'
Pin
'
+
pin1
+
'
alredy used as
'
+
warningText
=
'
Pin
'
+
pin1
+
'
alre
a
dy used as
'
+
Blockly
.
Arduino
.
pins_
[
pin1
]
+
'
.
'
;
}
}
if
(
pin2
in
Blockly
.
Arduino
.
pins_
)
{
if
(
Blockly
.
Arduino
.
pins_
[
pin2
]
!=
pinType
)
{
warningText
=
warningText
+
'
Pin
'
+
pin2
+
'
alredy used as
'
+
warningText
=
warningText
+
'
Pin
'
+
pin2
+
'
alre
a
dy used as
'
+
Blockly
.
Arduino
.
pins_
[
pin2
]
+
'
.
'
;
}
}
...
...
@@ -81,7 +81,7 @@ Blockly.Arduino['stepper_step'] = function(block) {
var
stepperSteps
=
Blockly
.
Arduino
.
valueToCode
(
block
,
'
STEPPER_STEPS
'
,
Blockly
.
Arduino
.
ORDER_ATOMIC
)
||
'
0
'
;
var
code
=
''
;
code
=
stepperInstance
+
'
.steps(
'
+
stepperSteps
+
'
)
'
;
code
=
stepperInstance
+
'
.steps(
'
+
stepperSteps
+
'
)
;
\n
'
;
return
[
code
,
Blockly
.
Arduino
.
ORDER_ATOMIC
]
;
return
code
;
};
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