SlideShare a Scribd company logo
1
Creating mBlock Extensions
by Wang Yu, Product Manager at Makeblock
2
Creating mBlock Extensions
How Extensions Work
Extensions allow custom blocks for mBlock. You can use
extensions to support third-party Arduino sensors or
other robotic products such as Lego or LittleBits.
Anybody can write extensions for mBlock. This makes
mBlock an awesome platform for every types of
hardware-related programming.
Every block in mBlock has two modes: Scratch Mode
and Arduino Mode. It’s important to know their
difference before writing extensions.
Scratch Mode and Arduino Mode
> Scratch Mode
In Scratch Mode, the
robot or Arduino board
must be connect to
the computer in order
to run the program. You
can use Scratch blocks to
create graphics or make
games.
- Arduino Mode
In Arduino Mode, the
program is uploaded
into the robot and the
robot is run on its own.
However, you cannot use
graphics from Scratch
since the computer is no
longer there.
use Edit/Arduino Mode menu item to
toggle between Scratch and Arduino
modes
this “Say” block can only be used in
the Scratch mode; while the “(Repeat)
Forever” block can be accessed in
both modes.
Creating mBlock Extensions 3
Using the Extension Manager, you can browse
extensions, find one you want, and get the blocks
included in the extension with a single click.
The following instructions shows how to add an
extension to mBlock.
Using an Extension
Use the menu item “Extensions”, “Manage
Extensions” to open up the Extension
Manager.
You may search for extensions with the
search box. Click “Download” at the right of
the table item to download the extension.
Internet connection is required.
The extension you downloaded will show up
in the “Robots” group of mBlock.
1
2
3
Extensions you can
download from the Internet
Extensions installed in this computer. You may
upgrade or remove those extensions
Read more information from
the author’s webpage
Click here to
download
Install an
extension from
a zip file in your
computer
These blocks come from
the “Demo” extension I
just downloaded
Creating mBlock Extensions 4
Writing an extension is not as hard as it seems. Mostly it
is done by editing some text files. Arduino knowledge
is required in implementing the Arduino Mode, while
Javascript is used in the Scratch Mode. You may skip one
of the modes, and the block will simply not work in that
mode.
Here’s a list of things extension writers need to do:
• Write basic information, such as the name and author
• Define how the blocks look like
• Tell mBlock how to generate the Arduino code
• Write the functions run in the Scratch mode
• Include additional Arduino header / C files.
Writing an Extension
Every extension is a .zip file compressed from a folder.
Here is the folder of the “Demo” extension when
unzipped:
The File Structure of an Extension
.s2e file: basic description, blocks
definition of the extension, and the
Arduino code generated by the
extension in the Arduino Mode. “s2e”
stands for “Scratch 2 extension” files
“src” folder is for additional files
included in the Arduino mode. You
may skip this if you don’t use additional
libraries
Javascript code that run on the
Scratch Mode stays in the “js”
folder
Source code of existing extensions are always the best resource
in writing extensions. In mBlock, you can always view the source
code of existing extensions through the Extension Manager.
“View Source” will bring up a window of that extension’s folder.
You may try to change the extension code and it will take effect in
the next time mBlock is launched (this is one way of debugging).
However, any changes made here will be discarded when
upgrading mBlock or running the “clear cache” command.
Tips
Click here to view the source
code of any existing extension.
Creating mBlock Extensions 5
Download the “Demo” extension as a starting point at:
http://www.mblock.cc/site-images/Demo.zip
The next step is editing the s2e file. You need a text
editor. the Notepad program is fine, but I recommend
editors dedicated for code editing, such as Github
Atom, Visual Studio Code, Brackets, or Sublime text.
At the beginning of the .s2e file lies some basic
information. You need to tell the user what does the
extension do and who wrote it. Here is the starting lines
of demo.s2e:
Filling out Basic Information
1
name your extension
the purpose of the extension.
It shows up in the extension
manager
version number, in forms
of x.y.z. It is used in
updating the extension
your name, and email
address
Basic information
Block definition
Menu definition
How menu items are
transformed into numerical
values in the Arduino Mode
Translations
A web page to read more
about the extension Location of the Javascript
file for Scratch mode
The order of the extension when
show up with multiple extensions
“firmware” and
“extensionPort” are not
used, just leave it be
Tips
the .s2e file is the main file of the extension. Aside from the basic
information, it defines blocks, tells mBlock how the dropdown
menus look like, and how to translate the extension to another
language (if you want to).
If you are familiar with Javascript, you may notice it is written in a
plain JSON (JavaScript Object Notation) object.
Creating mBlock Extensions 6
Defining Blocks
2
the “blockSpecs” section tells mBlock how blocks look
like, and gives clues on how they behave in Scratch and
Arduino mode.
Every block is described
in a Javascript Array form
/ Block Types are indicated by a single letter:
/ The text that appears on the blocks
words begin with “%” are parameters - “slots” that users can fill up by typing or with
other blocks. This block (“digitalWrite( %n , %d.digital )”) looks like:
Other parameter types include:
“h” stands for “header blocks”; they are rarely used in
custom extensions.
“w” stands for “write blocks”; they send commands to
the hardware and do not expect a response.
%n gives a round slot for numbers, and will
give a number in the Arduino Mode.
%d.name gives a round dropdown box, and will give a
number in the Arduino Mode. The dropdown’s content is
defined in the “menus” section and identified by name.
%s gives a rectangular hole for
strings
%m.name gives a rectangular
dropdown box for strings
%c gives a color picker
“r” is for “reading blocks”; in Scratch Mode, it waits
the function to return a value; “R” is for asynchronous
reading - the value is not returned by the function but
told later (“callback”) through a function in Javascript.
“r” and “R” looks the same and have no difference in
the Arduino mode.
“b” is for “binary blocks”; they return a binary yes-or-
no value. Similarly, “B” is for asynchronous reading of
binary values
Creating mBlock Extensions 7
Making Menus
Finishing Up the Block Definition
3
4
the “digitalWrite” block has a convenient menu for
selecting HIGH or LOW output. It is defined in the
following way:
“%d.digital” means menu information is stored in the
“digital” list of the menus section
give a Javascript function name
to use in the Scratch Mode
this means the menu has two options:
“HIGH” and “LOW”
give default values to the
block you just defined
in Arduino mode, “HIGH” generates a
value of 1 and “LOW” generates 0. This
does not affect the Scratch Mode.
Creating mBlock Extensions 8
Arduino Code Generation
5
When defining each block, .s2e file also decides how
Arduino code is generated, following rules below:
insert include
statements
define variables
insert once in the
setup() function
insert once in the
loop() function
if you included other source
files, be sure to copy them to
the “src” folder
can appear many times
when the block is used
{0} means the first
parameter, “9” in this case
Creating mBlock Extensions 9
Coding Scratch Mode
6
The Javascript file is pretty long. I recommend take the
demo.js and only make necessary changes. If your extension
does not support Scratch Mode, skip this part.
define your own variables here
setup code for the extension
file name
must match
send bytes via the serial ports
using an array of bytes
this is called every time the computer
receives bytes from serial ports
write code for your
blocks here
demo.s2e
demo.js
(at the end of) demo.js
demo.s2e
don’t change
trace(string) function is really useful for
debugging because it produces logs in the
Arduino Mode panel.
change this to the name of your extension.
function name
defined here
Creating mBlock Extensions 10
After you are satisfied with the extension, zip it into a
.zip file. In MacOS, right click the folder and choose
“Compress xxx...”; in Windows, right click the folder
and choose “Send To”, “Compressed (zipped) folder”.
Then you may import the extension through the “Add
Extension” button in the Extension Manager. But It’ll be
easier for the users if the extension is uploaded to the
Online Extension Center.
Publishing Your Extension
Go to the extension center website:
http://www.mblock.cc/extensions/
And click “Sign-in with Github”. If you don’t have Github
account, you need to register one.
After Signing in, drag your .zip file to the large box (or
click the large box to choose with a file browser)
Sign in with your Github account
Congratulations! You’ve uploaded an extension and
contributed to the world mBlock community!
Upload your extension
1
2
Click here to
sign-in
Drag your .zip
file here
If you want to update your extension, simply upload a .zip file with
a higher version number. And you’re all set.
Makeblock, the company maintaining mBlock, reserves every right
to remove an extension, with or without explanation. But we do
welcome extensions of every kind from every product and hope
we only moderate spam (like one with the name “test123”) and
those with improper content. Simply use your common sense.
Tips
Tips

More Related Content

Similar to mblock_extension_guide.pdf

verilog basics.ppt
verilog basics.pptverilog basics.ppt
verilog basics.ppt
8885684828
 
Description of VivaVisualCode
Description of VivaVisualCodeDescription of VivaVisualCode
Description of VivaVisualCode
PVS-Studio
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
AnassElHousni
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
Danielle780357
 
Livecode widget course
Livecode widget courseLivecode widget course
Livecode widget course
crazyaxe
 
Getting started with code composer studio v3.3 for tms320 f2812
Getting started with code composer studio v3.3 for tms320 f2812Getting started with code composer studio v3.3 for tms320 f2812
Getting started with code composer studio v3.3 for tms320 f2812
Pantech ProLabs India Pvt Ltd
 
ID E's features
ID E's featuresID E's features
ID E's features
wajahat Gul
 
Readme
ReadmeReadme
Readme
rec2006
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java Programming
Katy Allen
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation Slides
Fab L
 
en_GettingStartedInMicroStation.pdf
en_GettingStartedInMicroStation.pdfen_GettingStartedInMicroStation.pdf
en_GettingStartedInMicroStation.pdf
ArielAlejandroLander
 
Understanding ScratchX Extensions with JavaScript
Understanding ScratchX Extensions with JavaScriptUnderstanding ScratchX Extensions with JavaScript
Understanding ScratchX Extensions with JavaScript
Darren Adkinson
 
Easyeda tutorial
Easyeda tutorialEasyeda tutorial
Easyeda tutorial
SANTIAGO PABLO ALBERTO
 
Whats New in MSBuild 3.5 and Team Build 2008
Whats New in MSBuild 3.5 and Team Build 2008Whats New in MSBuild 3.5 and Team Build 2008
Whats New in MSBuild 3.5 and Team Build 2008wbarthol
 
Containers Lab
Containers Lab Containers Lab
Containers Lab
Dev_Events
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
MayurWagh46
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitc
benDesigning
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
NEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
meharikiros2
 

Similar to mblock_extension_guide.pdf (20)

verilog basics.ppt
verilog basics.pptverilog basics.ppt
verilog basics.ppt
 
Description of VivaVisualCode
Description of VivaVisualCodeDescription of VivaVisualCode
Description of VivaVisualCode
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
Livecode widget course
Livecode widget courseLivecode widget course
Livecode widget course
 
Getting started with code composer studio v3.3 for tms320 f2812
Getting started with code composer studio v3.3 for tms320 f2812Getting started with code composer studio v3.3 for tms320 f2812
Getting started with code composer studio v3.3 for tms320 f2812
 
ID E's features
ID E's featuresID E's features
ID E's features
 
Readme
ReadmeReadme
Readme
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java Programming
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation Slides
 
en_GettingStartedInMicroStation.pdf
en_GettingStartedInMicroStation.pdfen_GettingStartedInMicroStation.pdf
en_GettingStartedInMicroStation.pdf
 
Cpb2010
Cpb2010Cpb2010
Cpb2010
 
Understanding ScratchX Extensions with JavaScript
Understanding ScratchX Extensions with JavaScriptUnderstanding ScratchX Extensions with JavaScript
Understanding ScratchX Extensions with JavaScript
 
Easyeda tutorial
Easyeda tutorialEasyeda tutorial
Easyeda tutorial
 
Whats New in MSBuild 3.5 and Team Build 2008
Whats New in MSBuild 3.5 and Team Build 2008Whats New in MSBuild 3.5 and Team Build 2008
Whats New in MSBuild 3.5 and Team Build 2008
 
Containers Lab
Containers Lab Containers Lab
Containers Lab
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitc
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 

Recently uploaded

How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

mblock_extension_guide.pdf

  • 1. 1 Creating mBlock Extensions by Wang Yu, Product Manager at Makeblock
  • 2. 2 Creating mBlock Extensions How Extensions Work Extensions allow custom blocks for mBlock. You can use extensions to support third-party Arduino sensors or other robotic products such as Lego or LittleBits. Anybody can write extensions for mBlock. This makes mBlock an awesome platform for every types of hardware-related programming. Every block in mBlock has two modes: Scratch Mode and Arduino Mode. It’s important to know their difference before writing extensions. Scratch Mode and Arduino Mode > Scratch Mode In Scratch Mode, the robot or Arduino board must be connect to the computer in order to run the program. You can use Scratch blocks to create graphics or make games. - Arduino Mode In Arduino Mode, the program is uploaded into the robot and the robot is run on its own. However, you cannot use graphics from Scratch since the computer is no longer there. use Edit/Arduino Mode menu item to toggle between Scratch and Arduino modes this “Say” block can only be used in the Scratch mode; while the “(Repeat) Forever” block can be accessed in both modes.
  • 3. Creating mBlock Extensions 3 Using the Extension Manager, you can browse extensions, find one you want, and get the blocks included in the extension with a single click. The following instructions shows how to add an extension to mBlock. Using an Extension Use the menu item “Extensions”, “Manage Extensions” to open up the Extension Manager. You may search for extensions with the search box. Click “Download” at the right of the table item to download the extension. Internet connection is required. The extension you downloaded will show up in the “Robots” group of mBlock. 1 2 3 Extensions you can download from the Internet Extensions installed in this computer. You may upgrade or remove those extensions Read more information from the author’s webpage Click here to download Install an extension from a zip file in your computer These blocks come from the “Demo” extension I just downloaded
  • 4. Creating mBlock Extensions 4 Writing an extension is not as hard as it seems. Mostly it is done by editing some text files. Arduino knowledge is required in implementing the Arduino Mode, while Javascript is used in the Scratch Mode. You may skip one of the modes, and the block will simply not work in that mode. Here’s a list of things extension writers need to do: • Write basic information, such as the name and author • Define how the blocks look like • Tell mBlock how to generate the Arduino code • Write the functions run in the Scratch mode • Include additional Arduino header / C files. Writing an Extension Every extension is a .zip file compressed from a folder. Here is the folder of the “Demo” extension when unzipped: The File Structure of an Extension .s2e file: basic description, blocks definition of the extension, and the Arduino code generated by the extension in the Arduino Mode. “s2e” stands for “Scratch 2 extension” files “src” folder is for additional files included in the Arduino mode. You may skip this if you don’t use additional libraries Javascript code that run on the Scratch Mode stays in the “js” folder Source code of existing extensions are always the best resource in writing extensions. In mBlock, you can always view the source code of existing extensions through the Extension Manager. “View Source” will bring up a window of that extension’s folder. You may try to change the extension code and it will take effect in the next time mBlock is launched (this is one way of debugging). However, any changes made here will be discarded when upgrading mBlock or running the “clear cache” command. Tips Click here to view the source code of any existing extension.
  • 5. Creating mBlock Extensions 5 Download the “Demo” extension as a starting point at: http://www.mblock.cc/site-images/Demo.zip The next step is editing the s2e file. You need a text editor. the Notepad program is fine, but I recommend editors dedicated for code editing, such as Github Atom, Visual Studio Code, Brackets, or Sublime text. At the beginning of the .s2e file lies some basic information. You need to tell the user what does the extension do and who wrote it. Here is the starting lines of demo.s2e: Filling out Basic Information 1 name your extension the purpose of the extension. It shows up in the extension manager version number, in forms of x.y.z. It is used in updating the extension your name, and email address Basic information Block definition Menu definition How menu items are transformed into numerical values in the Arduino Mode Translations A web page to read more about the extension Location of the Javascript file for Scratch mode The order of the extension when show up with multiple extensions “firmware” and “extensionPort” are not used, just leave it be Tips the .s2e file is the main file of the extension. Aside from the basic information, it defines blocks, tells mBlock how the dropdown menus look like, and how to translate the extension to another language (if you want to). If you are familiar with Javascript, you may notice it is written in a plain JSON (JavaScript Object Notation) object.
  • 6. Creating mBlock Extensions 6 Defining Blocks 2 the “blockSpecs” section tells mBlock how blocks look like, and gives clues on how they behave in Scratch and Arduino mode. Every block is described in a Javascript Array form / Block Types are indicated by a single letter: / The text that appears on the blocks words begin with “%” are parameters - “slots” that users can fill up by typing or with other blocks. This block (“digitalWrite( %n , %d.digital )”) looks like: Other parameter types include: “h” stands for “header blocks”; they are rarely used in custom extensions. “w” stands for “write blocks”; they send commands to the hardware and do not expect a response. %n gives a round slot for numbers, and will give a number in the Arduino Mode. %d.name gives a round dropdown box, and will give a number in the Arduino Mode. The dropdown’s content is defined in the “menus” section and identified by name. %s gives a rectangular hole for strings %m.name gives a rectangular dropdown box for strings %c gives a color picker “r” is for “reading blocks”; in Scratch Mode, it waits the function to return a value; “R” is for asynchronous reading - the value is not returned by the function but told later (“callback”) through a function in Javascript. “r” and “R” looks the same and have no difference in the Arduino mode. “b” is for “binary blocks”; they return a binary yes-or- no value. Similarly, “B” is for asynchronous reading of binary values
  • 7. Creating mBlock Extensions 7 Making Menus Finishing Up the Block Definition 3 4 the “digitalWrite” block has a convenient menu for selecting HIGH or LOW output. It is defined in the following way: “%d.digital” means menu information is stored in the “digital” list of the menus section give a Javascript function name to use in the Scratch Mode this means the menu has two options: “HIGH” and “LOW” give default values to the block you just defined in Arduino mode, “HIGH” generates a value of 1 and “LOW” generates 0. This does not affect the Scratch Mode.
  • 8. Creating mBlock Extensions 8 Arduino Code Generation 5 When defining each block, .s2e file also decides how Arduino code is generated, following rules below: insert include statements define variables insert once in the setup() function insert once in the loop() function if you included other source files, be sure to copy them to the “src” folder can appear many times when the block is used {0} means the first parameter, “9” in this case
  • 9. Creating mBlock Extensions 9 Coding Scratch Mode 6 The Javascript file is pretty long. I recommend take the demo.js and only make necessary changes. If your extension does not support Scratch Mode, skip this part. define your own variables here setup code for the extension file name must match send bytes via the serial ports using an array of bytes this is called every time the computer receives bytes from serial ports write code for your blocks here demo.s2e demo.js (at the end of) demo.js demo.s2e don’t change trace(string) function is really useful for debugging because it produces logs in the Arduino Mode panel. change this to the name of your extension. function name defined here
  • 10. Creating mBlock Extensions 10 After you are satisfied with the extension, zip it into a .zip file. In MacOS, right click the folder and choose “Compress xxx...”; in Windows, right click the folder and choose “Send To”, “Compressed (zipped) folder”. Then you may import the extension through the “Add Extension” button in the Extension Manager. But It’ll be easier for the users if the extension is uploaded to the Online Extension Center. Publishing Your Extension Go to the extension center website: http://www.mblock.cc/extensions/ And click “Sign-in with Github”. If you don’t have Github account, you need to register one. After Signing in, drag your .zip file to the large box (or click the large box to choose with a file browser) Sign in with your Github account Congratulations! You’ve uploaded an extension and contributed to the world mBlock community! Upload your extension 1 2 Click here to sign-in Drag your .zip file here If you want to update your extension, simply upload a .zip file with a higher version number. And you’re all set. Makeblock, the company maintaining mBlock, reserves every right to remove an extension, with or without explanation. But we do welcome extensions of every kind from every product and hope we only moderate spam (like one with the name “test123”) and those with improper content. Simply use your common sense. Tips Tips