diff --git a/clover_blocks/programs/examples/led-compass.xml b/clover_blocks/programs/examples/led-compass.xml
index a6c48cc3..594bfffc 100644
--- a/clover_blocks/programs/examples/led-compass.xml
+++ b/clover_blocks/programs/examples/led-compass.xml
@@ -1,106 +1,91 @@
-
- led_count
-
-
- led_count
-
-
- 58
+
+ WHILE
+
+
+ TRUE
-
-
- WHILE
-
-
- TRUE
-
+
+
+ FILL
+
+
+ #000000
+
-
-
- FILL
-
-
- #000000
+
+
+
+
+ 0
-
-
-
-
-
- 0
+
+ MULTIPLY
+
+
+ 1
-
- MULTIPLY
+
+ DIVIDE
-
+
1
-
- DIVIDE
+
+ ADD
-
+
1
-
- ADD
-
-
- 1
-
-
- MAP
-
-
- 0
-
-
-
-
-
-
- 180
+
+ MAP
+
+
+ 0
-
- 360
+
+ 180
-
- 1
+
+ 360
-
- led_count
-
-
-
- #3366ff
+
+
+ 1
+
+
+
+
+
+
+
+ #3366ff
+
+
+
+
+
+
+ 0.1
-
-
-
-
- 0.1
-
-
-
-
-
+
-
+
-
+
\ No newline at end of file
diff --git a/clover_blocks/www/blocks.js b/clover_blocks/www/blocks.js
index 71307c07..8f7d68c9 100644
--- a/clover_blocks/www/blocks.js
+++ b/clover_blocks/www/blocks.js
@@ -353,6 +353,17 @@ Blockly.Blocks['set_effect'] = {
}
};
+Blockly.Blocks['led_count'] = {
+ init: function () {
+ this.appendDummyInput()
+ .appendField("LED count");
+ this.setOutput(true, "Number");
+ this.setColour(COLOR_LED);
+ this.setTooltip("Returns the number of LEDs (configured in led.launch).");
+ this.setHelpUrl(DOCS_URL + '#' + this.type);
+ }
+};
+
Blockly.Blocks['take_off'] = {
init: function () {
this.appendValueInput("ALT")
diff --git a/clover_blocks/www/index.html b/clover_blocks/www/index.html
index 1fd1fdff..770325d8 100644
--- a/clover_blocks/www/index.html
+++ b/clover_blocks/www/index.html
@@ -106,6 +106,7 @@
0
+
diff --git a/clover_blocks/www/python.js b/clover_blocks/www/python.js
index 8bc7cf08..a598ad68 100644
--- a/clover_blocks/www/python.js
+++ b/clover_blocks/www/python.js
@@ -15,7 +15,7 @@ Blockly.Python.addReservedWords('_b,_print');
Blockly.Python.addReservedWords('rospy,srv,Trigger,get_telemetry,navigate,set_velocity,land');
Blockly.Python.addReservedWords('navigate_wait,land_wait,wait_arrival,wait_yaw,get_distance');
Blockly.Python.addReservedWords('pigpio,pi,Range');
-Blockly.Python.addReservedWords('SetLEDEffect,set_effect');
+Blockly.Python.addReservedWords('SetLEDEffect,set_effect,led_count,get_led_count');
Blockly.Python.addReservedWords('SetLEDs,LEDState,set_leds');
const IMPORT_SRV = `from clover import srv
@@ -87,6 +87,9 @@ function generateROSDefinitions() {
Blockly.Python.definitions_['import_set_led'] = 'from led_msgs.srv import SetLEDs\nfrom led_msgs.msg import LEDState';
code += `set_leds = rospy.ServiceProxy('led/set_leds', SetLEDs, persistent=True)\n`;
}
+ if (rosDefinitions.ledStateArray) {
+ Blockly.Python.definitions_['import_led_state_array'] = 'from led_msgs.msg import LEDStateArray';
+ }
if (rosDefinitions.navigateWait) {
Blockly.Python.definitions_['import_math'] = 'import math';
code += NAVIGATE_WAIT();
@@ -395,6 +398,21 @@ Blockly.Python.set_led = function(block) {
}
}
+const GET_LED_COUNT = `led_count = None
+
+def get_led_count():
+ global led_count
+ if led_count is None:
+ led_count = len(rospy.wait_for_message('led/state', LEDStateArray, timeout=10).leds)
+ return led_count\n`;
+
+Blockly.Python.led_count = function(block) {
+ rosDefinitions.ledStateArray = true;
+ initNode();
+ Blockly.Python.definitions_['get_led_count'] = GET_LED_COUNT;
+ return [`get_led_count()`, Blockly.Python.ORDER_FUNCTION_CALL]
+}
+
function pigpio() {
Blockly.Python.definitions_['import_pigpio'] = 'import pigpio';
Blockly.Python.definitions_['init_pigpio'] = 'pi = pigpio.pi()';