12/3/2021 Blockly Lua Integrate With C__
This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores. By buying the book, you also help to support the Lua project.
One interesting feature of Blockly is that it can run in a web browser. Visual cues in Blockly can be linked together to make writing code easier. The central value of Blockly is the ability to generate code in many different languages, such as JavaScript, Lua, Dart, Python, and PHP. A typical application that uses Blockly is Scratch 14. Example of embedding Lua in C. GitHub Gist: instantly share code, notes, and snippets.
The corresponding Lua, Dart and XML codes also can be obtained by simply moving to the particular tab. Using Blockly in a web application. This can be easily done by including the core Blockly script and the core blocks set. Then we can set the toolbox as follows. We can include our blocks in several categories.
Lua is an embedded language.That means that Lua is not a stand-alone package,but a library that can be linked with other applicationsso as to incorporate Lua facilities into these applications.
You may be wondering:If Lua is not a stand-alone program,how come we have been using Lua stand alone through the whole book?The solution to this puzzle is the Lua interpreter(the executable
lua ).This interpreter is a tiny application(with less than five hundred lines of code)that uses the Lua library to implement the stand-alone interpreter.This program handles the interface with the user,taking her files and strings to feed them to the Lua library,which does the bulk of the work (such as actually running Lua code).
This ability to be used as a library to extend an applicationis what makes Lua an extension language.At the same time, a program that uses Lua can register newfunctions in the Lua environment;such functions are implemented in C (or another language)and can add facilities that cannot bewritten directly in Lua.This is what makes Lua an extensible language.
These two views of Lua(as an extension language and as an extensible language)correspond to two kinds of interaction between C and Lua.In the first kind, C has the control and Lua is the library.The C code in this kind of interactionis what we call application code.In the second kind, Lua has the control and C is the library.Here, the C code is called library code.Both application code and library code usethe same API to communicate with Lua,the so called C API.
The C API is the set of functions that allow C codeto interact with Lua.It comprises functions to read and write Lua global variables,to call Lua functions,to run pieces of Lua code,to register C functions so that they can later be called by Lua code,and so on.(Throughout this text, the term 'function' actually means'function or macro'.The API implements several facilities as macros.)
The C API follows the C modus operandi,which is quite different from Lua.When programming in C,we must care about type checking (and type errors),error recovery, memory-allocation errors,and several other sources of complexity.Most functions in the API do not check the correctness of their arguments;it is your responsibility to make sure that the argumentsare valid before calling a function.If you make mistakes,you can get a 'segmentation fault' error or something similar,instead of a well-behaved error message.Moreover, the API emphasizes flexibility and simplicity,sometimes at the cost of ease of use.Common tasks may involve several API calls.This may be boring,but it gives you full control over all details,such as error handling, buffer sizes, and the like.
As its title says,the goal of this chapter is to give an overview of what isinvolved when you use Lua from C.Do not bother understanding all the details of what is going on now.Later we will fill in the details.Nevertheless,do not forget that you can find more details about specific functionsin the Lua reference manual.Moreover, you can find several examples of the use of the API in theLua distribution itself.The Lua stand-alone interpreter (
lua.c )provides examples of application code,while the standard libraries (lmathlib.c , lstrlib.c , etc.)provide examples of library code.
From now on, we are wearing a C programmers' hat.When we talk about 'you', we mean you when programming in C,or you impersonated by the C code you write.
A major component in the communication between Lua and C is anomnipresent virtual stack.Almost all API calls operate on values on this stack.All data exchange from Lua to C and from C to Lua occursthrough this stack.Moreover, you can use the stack to keep intermediateresults too.The stack helps to solve two impedance mismatches between Lua and C:The first is caused by Lua being garbage collected,whereas C requires explicit deallocation;the second results from the shock between dynamic typing in Luaversus the static typing of C.We will discuss the stack in more detail in Section 24.2.
Blockly Editor
For anyone getting started with LEGO MINDSTORMS, the EV3 Software provides a great introduction to programming, but sooner or later you and your students might be ready for something different. For example, one option is to use a text-based programming language, such as RobotC, that better reflects the dominant style of programming in the computer industry.
Whether you’re looking for a way to extend your students or to provide a link with another programming language, here is a list of some of the most popular third-party alternatives.
MakeCode (EV3)
Microsoft MakeCode is an online programming platform that can control the EV3 and other devices, such as Cue and the BBC micro:bit. Being able to use MakeCode on various devices might make it an attractive option. MakeCode uses blocks (like Scratch) or JavaScript (text) programming.
Swift Playgrounds (EV3)
Swift Playgrounds is an iPad-only environment designed to help users learn to program using the Swift programming language. In Swift Playground, users can program robots, gather information through sensors, and cause actions via the motos. There is also an EV3 Animal Rescue Book that teachers might find they can use with their students.
EV3Python (EV3)
EV3Python helps users familiar with Python programming to use this language to control an EV3 robot. This is done with the assistance of the Microsoft Visual Studio Code.
RobotC (EV3, NXT, RCX)
RobotC is a C-based programming language with a fully integrated software debugger that supports a range of different hardware platforms. Extensive documentation and online support is available. For more information, visit: http://www.robotc.net/.
Scratch (EV3, NXT)
Scratch is a very popular visually programming environment. Several extensions have been created to support the NXT and EV3. For more information about Scratch, see: https://scratch.mit.edu/ Scratch 3.0 is in development and should be available in 2019 with continued support for EV3. Scratch can also be used with LEGO WeDo (see Scratch-ing the LEGO WeDo) and WeDo 2.0. For more about using Scratch with WeDo, see: https://scratch.mit.edu/wedo.
EV3dev (EV3)
EV3dev isn’t actually a programming language, but rather a Debian Linux-based operating system that can run almost all languages that any other linux distribution can run, including C++, Node.js, and Python. For more information, see: http://www.ev3dev.org/.
leJOS (EV3, NXT, RCX)
LeJOS (pronounced like the Spanish word “lejos” for “far”) is a tiny Java Virtual Machine that supports Java. For more information, see: http://www.lejos.org/ev3.php.
OpenRoberta (EV3, NXT)
Open Roberta is a free, drag and drop, cloud-based platform for programming LEGO EV3 and NXT robots. For more information, see: Open Roberta: A Review.
EV3 Basic (EV3)
EV3 Basic is a textual programming language. For more information, see: Introducing EV3 Basic.
Enchanting (NXT)
Enchanting is a programming environment for the NXT that is built on Scratch. For more information, see: http://enchanting.robotclub.ab.ca/.
CoderZ (EV3)
CoderZ is a 3D simulated robot environment using virtual robots that are similar to the EV3 robot. Programming can be done with either Blockly or Java programming languages. The benefit is that students can create code at home, extending their learning at school. CoderZ is a commercial product and offers a 14-day free trial.
LabVIEW (EV3, NXT)
LabVIEW for LEGO MINDSTORMS (LVLM) and LabVIEW for Education (LV4E) are visual programming environments. The EV3 Software was built in LabVIEW, so LVLM provides a great next step for students who are familiar with that programming language and ready for something more powerful and versatile. Browse all LabVIEW posts on this site.
ROBOLAB (NXT, RCX)
ROBOLAB is a visual programming language built in LabVIEW that was developed by the Tufts CEEO. ROBOLAB was the dominant programming language for educational LEGO robotics prior to the release of the NXT Software. It is dated and to use it, older computers and operating systems are necessary. Browse all ROBOLAB posts on this site.
There are other languages out there. Try this Wikipedia page for a longer list.
The following two tabs change content below.
Wayne Burnett, Ed.D., is a Canadian father of two boys (both of whom used the Mindstorms NXT) who teaches at an international school in Malaysia. In addition to teaching robotics, ICT, and information literacy, Wayne has recently completed a doctoral dissertation on using the LEGO WeDo to teach science.
Blockly CodeRelated PostsComments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |