Home STEM HQ TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 40

           


Scripting Tutorial - Lesson 40: Lua Scripting and the TI Innovator™ Hub:

Using Lua to store and edit TI-BASIC programs for the Innovator™ Hub



Download supporting file for this tutorial

Texas Instruments TI-Nspire Scripting Support Page

       
  

Scripting for the Innovator Hub: Lesson 0: Using Lua to store and edit TI-BASIC programs for the Innovator™ Hub

Top of Page

While Lua allows us to do some great things with the Innovator Hub, the most natural environment remains TI-BASIC. Students are able to quickly and easily create code that controls the Hub and allows them to interact with the real world in a multitude of ways.

One way in which Lua can offer another support mechanism for this platform, then, could be in offering a simple way to store a collection of TI-BASIC programs. The user uses a drop down menu (drawn from the Lua Objects Gallery) to display both a brief description of each program as well as the text of the program itself. In fact, this text is able to be edited, and any changes made are recorded in the actual TI-BASIC program made available in the document.

In this way, such a document offers a repository of useful TI-BASIC programs (which are automatically defined on opening the file), an overview of each, and the means to study, edit and play with these programs. Such a document is an ideal "widget" for versions 4.4+ of TI-Nspire, readily dropped into any TI-Nspire document and carrying with it whatever TI-BASIC programs might be required, along with their descriptions - AND the ability to easily edit and test variations on these programs.

  
 

The attached document uses two very useful Lua tricks to achieve its goals

  1. First, the text box created makes use of the powerful setTextChangeListener function:

    TextBox1:setTextChangeListener(

    function()
    local text = TextBox1:getText()
    text = text:gsub(string.char(10), ":")
    math.eval(text)
    end)

    This device listens for any changes made in the text box and then enacts whatever commands are enabled. In this case, it attempts to evaluate whatever is typed as a TI-Nspire variable using math.eval. When changes result in a valid TI-BASIC program definition, then this automatically defines that program.

    Note that the program only exists in its revised form until the reset button is pressed, or the document is closed. The new program is NOT saved.

  2. The other key ingredient here is the math.eval command itself. It can be used to define and store a valid TI-BASIC program. For example, study this program:

    'Define blink(t,f)=Prgm'

    ..':Send "BEGIN "'
    ..':DelVar iostr.str0:GetStr iostr.str0:Disp iostr.str0'
    ..':For n,1,t'
    ..':Send "SET LIGHT ON "'
    ..':Wait f'
    ..':Send "SET LIGHT OFF "'
    ..':Wait f'
    ..':EndFor'
    ..':EndPrgm',

    You will see that this entire program is written as a Lua string, which can then be placed within a math.eval function. It will be defined and stored as the program variable "blink" and will run as a normal TI-BASIC program when called. Simple - but very powerful.

  

In this document, the various programs are stored as elements of a list, as are the brief descriptions of each. When on.resize is called (or the reset button pressed) then the elements of these lists are evaluated and stored. They will then be available as TI-BASIC programs to be run from the Calculator.

As mentioned above, the textbox offers the facility to not only view the TI-BASIC program within the Lua window, but also to edit it. So students are able to quickly and easily try their own variations or add new features to the stored code. If their changes produce a valid program, then the revised version will then be available to run from the Calculator for testing - until the reset button is pressed, or the document is closed. If the changes introduce an error in the TI-BASIC code, then the revised program will not be stored and will not be available until the error is corrected.


  

Back to Top

  

Home STEM HQ TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 40