Block-Based Programming – How to Create Blocks in Ardublock

This is the final post of a series of three publications about block-based programming with a special focus on Ardublock.

The first post has presented a comparison between text-based and block-based programming and an overview of some block-based environments.

The second post has demonstrated how to add the Ardublock plugin to Arduino IDE, and how to edit the plugin file in Eclipse in order to have the possibility of creating more blocks.

In this final post, you will learn how to create new blocks. The creation of new blocks can be useful, in order to make students, that don’t know how to code, program some specific robot or tool.

This tutorial explains how to create new blocks, how to test the implementations, and how to generate an Ardublock plugin with the changes done. At the final of the tutorial is presented all the implemented code.


Once the introduction is done, it is time to develop the blocks! Follow the steps below and learn how to create them.

Parts Required

  • Arduino IDE previously installed;
  • Eclipse previously installed;
  • Ardublock files prepared to edit.

Creating blocks

To make changes on the Ardublock plugin in order to create blocks, you will need to modify two existing files and produce one Class file per block. These three things you need to change in the plugin are individually explained in the following subsections.

1. Creating a class file

If you are thinking about editing the Ardublock plugin, you probably want to create more than one block. For file organization, the first step you are going to do is create a new package where each block will be saved in an individual file.

To do this select the path src/main/java, check Figure 1.

Figure 1 – Path where a new package will be created.

Click with the right button over the path and then New>Package, see Figure 2. Now give a name to your package and then click on the Finish button. In Figure 3 the name given to the package was geekering.

Figure 2 – Creation of a new package.
Figure 3 – Giving a name to the package.

Now that you have a package where all your blocks will be located, let’s create a block. Right-click over the package that you created and then New>Class, see Figure 4.

Figure 4 – Creating a new Class.

Give a name to the Class file and then click on the Finish button. Figure 5 shows the creation of a Class with the name Geek.

Figure 5 – Naming a new Class.

Double click in the previously created class. The file should be opened and present a visual like Figure 6.

Figure 6 – Visual aspect of a clean Class file.

This file will contain the code that will be transported from the Ardublock to the Arduino IDE. To create a block in Ardublock you can use the code of Figure 7. As you can see, comparing Figures 7 and Figure 8, the strings used inside addHeaderFile will be translated as libraries. The strings inside addDefinitionComand will be to, for example, define global variables. The strings inside addSetupCommand will be added to the setup of Arduino code. Finally, the text after return will be added to the loop of the Arduino code.

Figure 7 – Code of the created class that when uploaded generate the Arduino IDE code of Figure 8.
Figure 8 – Code generated by the block of Figure 7.

2. Editing files

In the Project Explorer tab, open the path src/main/resources/com/ardublock/block. There you will find three files that you need to edit in order to implement the block that you created in the previous subsection in the Ardublock. The files are ardublock.xml, ardublock.properties, and block-mapping.properties (Figure 9). Open them.

Figure 9 – Needed files to create a new block.
2.1 block-mapping.properties

In this file create a variable that contains the path to the block you created. Something similar to Figure 10.

Figure 10 – Code to add at block-mapping.properties file. This is adapted to the previously created Class “Geek”.
2.2 ardublock.xml

In this file, scroll down almost to the end of the code and near to the BlockGenus structures create a new one. In the example of Figure 11 was created a block of purple color with no block connectors.

Figure 11 – Code to add at ardublock.xml file to implement the class previously created in Ardublock plugin.

You may want to group all your blocks in the same place. With this in mind, now a drawer will be created to group all the blocks. Scroll down until you find <BlockDrawerSets>. Figure 12 implements an example code of a drawer with three blocks inside (geek, basicota, ds1822). The drawer has purple color and the string of the variable bd.geekering (defined in the next section 2.3).

Figure 12 – Example code to create a drawer. Since in this tutorial only one class with the name geek was created, only this one will be added to the drawer. If you add blocks that are not created errors will occur.
2.3 ardublock.properties

At the end of the ardublock.properties file add the following code lines of Figure 13, which attributes to the drawer and to the block titles and descriptions.

Figure 13 – Code to add at ardublock.properties file.

Test the implementations

To test the code, click with the right button on the main folder “ardublock” in the Project Explorer tab, and then click Run as > Java Application, see Figure 14.

Figure 14 – Run the implemented code.

Generating an executable

To compile the executable, click with the right button in the main folder and then Export. Then select Java>Runnable JAR file, see Figure 15. Define the export destination, check Figure 16.

Figure 15 – Export the file as a Runnable JAR file.
Figure 16 – Export JAR window configuration.

After generating the .jar file you just need to add it to the tools of the Arduino IDE, like was done at the end of the previous tutorial.

Conclusion

The final result of this tutorial can be seen in Figure 17, where a drawer with the name “Geekering drawer” and a block with the name “Geek block” were created.

Figure 17 – The obtained result of this tutorial.

Code

For an easier implementation of this tutorial, I leave below all the code used. In order to develop your skills make sure that before you copy and paste the code you truly understand it.

New block class file
package com.ardublock.translator.block.geekering;

import com.ardublock.translator.Translator; import com.ardublock.translator.block.TranslatorBlock; 
import com.ardublock.translator.block.exception.SocketNullException; 
import com.ardublock.translator.block.exception.SubroutineNotDeclaredException; 

public class Geek extends TranslatorBlock{ 
	 
	 	public Geek(Long blockId, Translator translator, String codePrefix, String codeSuffix, String label){ 
	 	 	super(blockId, translator, codePrefix, codeSuffix, label); 
	 	} 

	 	public String toCode() throws SocketNullException, SubroutineNotDeclaredException{ 
	 	 	translator.addHeaderFile("Biblioteca.h");  	 	 
	 	 	translator.addDefinitionCommand("//texto");  	 	 	
	 	 	translator.addSetupCommand("//setup");  	 	 
	 	 	return "//loop"; 
	 	} 

}
block-mapping.properties file
(...)
#Geekering
geekering=com.ardublock.translator.block.geekering.Geek
ardublock.xml file

Block structure.

(...)
	<!-- Geekering blocks -->
	<BlockGenus name="geek" kind="command" color="255 0 255" initlabel="bg.geek_command">
		<description>
			<text>Geekering</text>
		</description>
		<BlockConnectors>
		</BlockConnectors>
	</BlockGenus>
(...)

Drawer structure.

(...)
	<!-- Geekering drawer -->
	<BlockDrawer button-color="255 0 255" name="bd.geekering">
		<BlockGenusMember>geek</BlockGenusMember>
	</BlockDrawer>
(...)
ardublock.properties file
(...)
bd.geekering=Geekering drawer
bg.geek=Geek block
bg.geek_command=Geek
bg.geek.description=Put here the description of the block

Leave a Reply

Your email address will not be published. Required fields are marked *