C and assembly development introduction and Lua Programming: Difference between pages

From Hackspire
(Difference between pages)
Jump to navigation Jump to search
 
No edit summary
 
Line 1: Line 1:
This tutorial describes how to set up an environment and use the Ndless SDK to write native Ndless-compatible programs for the TI-Nspire.
The TI-Nspire allows, since OS v3.0, users to run Lua scripts.


==Setting up a development environment==
==Prerequisites==
===On Linux distros===
====Option 1: Build and install the SDK yourself====
* '''Make sure your system has the following dependencies''': git, GCC (with c++ support), binutils, GMP (libgmp-dev), MPFR (libmpfr-dev), MPC (libmpc-dev), boost-program-options, wget. Install them with your system's package manager if not.


====Option 2: Get the Ndless SDK Docker image====
Lua is only supported starting from OS v3.0.1. You can create Lua applications using the built-in script editor in the TI-Nspire computer software or one of the following third-party tools:
'''Warning: This option is very outdated and it is strongly recommended to use Option 1 and build the SDK yourself!'''


[https://www.docker.com/ Docker] is a platform to build and share applications through lightweight containers. The Ndless SDK Docker image packs the Ndless SDK and all its dependencies. This is the easiest and quickest method to get a fully functional development environment.
'''For any OS''':
*[https://github.com/ndless-nspire/Luna Luna] (cross-platform) [[https://www.omnimaga.org/news/'luna'-is-here-and-converts-your-lua-files-into-3-0-2-compatible-tns-files/ forum thread]]
'''For OS < 3.0.1''': these tools won't work with the OS 3.0.2 since the format used is now blocked
*[http://www.ticalc.org/archives/files/fileinfo/437/43704.html LUAtoTNS.sh] (bash script, works on Linux, Mac OS and Unix, or Windows + Cygwin or Windows + MSYS).
*[http://www.mirari.fr/KbOD maketns.py] (Python script, cross-platform)
*[https://www.omnimaga.org/files/User-Contributed-Calculator-Games-amp-Development-Tools/TI-Nspire-amp-TI-Nspire-CAS/TI-Nspire-Programming-Tools/lua2ti.zip lua2ti] (Windows + .NET Framework v4.0)
*[https://www.omnimaga.org/files/User-Contributed-Calculator-Games-amp-Development-Tools/TI-Nspire-amp-TI-Nspire-CAS/TI-Nspire-Programming-Tools/lua2ti_linux.zip lua2ti_linux] (Linux (No longer requires Mono, it is now a native Linux binary!))
You may also want to install Lua [https://www.lua.org/download.html on your computer]: <tt>luac -p</tt> can be used to check the syntax of your script before running it on a TI-Nspire or an emulator.


To install the Ndless SDK Docker image [https://registry.hub.docker.com/u/ndless/ndless-sdk/ follow the instructions on hub.docker.com]. You can skip the "Building and installing" part.
==API==


===On Mac OS X===
The documentation is now maintained in the "[http://wiki.inspired-lua.org Inspired Lua Wiki]".
* TODO
Please go there in order to have a full detailed documentation based on TI's information.
* '''Make sure your system has the following dependencies''': git, GCC (with c++ support), binutils, GMP, MPFR, MPC, boost-program-options, wget. Install them with your system's package manager if not, for example brew.


===On Windows===
==XML==
* Install Cygwin (32bit, x86) and the following dependencies: php (5.6+), libboost-devel, libboost_program_options*, binutils, gcc-core, gcc-g++, git, mpfr, mpfr-devel, gmp, libgmp-devel, libmpc3, libmpc-devel, openssl-dev, make, texinfo, zlib-devel, wget
* Download or git clone the ndless repo from GitHub.
* Extract the zip and go to the ndless-master/ndless-sdk/toolchain folder
* Fix the few symlinks, for instance genzehn/zehn.h in the genzehn folder, which has to be deleted then copied there from ndless-sdk/include (and if you intend to rebuild Ndless, utils.c from the resources folder into the different installers folders)


==Building and installing the toolchain and the SDK==
===Basic structure===
*'''Get the latest code from [https://github.com/ndless-nspire/Ndless GitHub]'''
git clone git://github.com/ndless-nspire/Ndless.git


* '''Run the SDK's ''build_toolchain.sh'' '''script that will download and build a complete ARM toolchain compatible with Ndless, and install it <(edit the <tt>PREFIX</tt> variable at the beginning of the script to change the install location). You don't need to be root for this.
<syntaxhighlight lang="xml">
cd ndless-sdk/toolchain/
<wdgt
./build_toolchain.sh
    xmlns:sc="urn:TI.ScriptApp" type="TI.ScriptApp" ver="1.0">


Running the script again will continue from the last successful step (not redownloading everything for instance). At the end of a successful build you should see <tt>Done!</tt>. Alternatively you can verify the build using <tt>echo $?</tt>. <tt>0</tt> indicates success.
    <sc:mFlags>1024</sc:mFlags>
* Now '''add the following folders to your PATH environment variable'''. On linux, <tt>~/.bash_profile</tt> should be a good place for this, just add something like this to it:
    <sc:value>8</sc:value>
export PATH="[path_to_ndless]/ndless-sdk/toolchain/install/bin:[path_to_ndless]/ndless-sdk/bin:${PATH}"
    <sc:cry>0</sc:cry>
* '''Build Ndless and the SDK''', in the top level of the repository, run:
    <sc:legal>none</sc:legal>
make
    <sc:schk>false</sc:schk>


===Verifying the installation===
    <sc:md>
*Open a console, and run:
        <sc:mde name="_VER" prop="134217728">1:1</sc:mde>
$ nspire-gcc
        <sc:mde name="TITLE" prop="2147549184">Hello</sc:mde>
If everything has been set up correctly you should see something similar to:
        <sc:mde name="TARAL" prop="134217728">2:5</sc:mde>
arm-none-eabi-gcc: fatal error: no input files
    </sc:md>
compilation terminated.


==2-minute tutorial==
    <sc:script version="33882629" id="1">
As a convention for the next chapters, lines starting with <tt>$</tt> are commands you should type in a console. Other lines are the command's output.
-- Lua script, XML encoded
===Your first build===
    </sc:script>
Ndless comes with sample programs in the ''samples/'' directory of the Ndless SDK. We will try to build the C Hello World.
Change the current directory of the console:
$ cd "<my_ndless_sdk_copy>/ndless-sdk/samples/helloworld-sdl"
Ndless programs are built with ''GNU Make'', which is run with the command <tt>make</tt>. So let's ''make'' the program:
$ make
nspire-gcc -Wall -W -marm -Os -c hello-sdl.c
mkdir -p .
nspire-ld hello-sdl.o -o ./helloworld-sdl.elf
genzehn --input ./helloworld-sdl.elf --output ./helloworld-sdl.tns --name "helloworld-sdl"
make-prg ./helloworld-sdl.tns ./helloworld-sdl.prg.tns
<tt>nspire-gcc</tt> is Ndless's wrapper for the GNU C Compiler ''GCC'', which compiles C and assembly source files to object files (here ''hello.o'').


<tt>nspire-ld</tt> is the wrapper for ''GCC'', which redirects gcc with the option "-fuse-ld=gold" to use another wrapper "arm-none-eabi-ld.gold" as linker. "arm-none-eabi-ld.gold" adds some necessary libraries to the final program.
    <sc:state>
        return {} -- Lua save state
    </sc:state>
</wdgt></syntaxhighlight>


<tt>genzehn</tt> converts the executable created by "nspire-ld" to a format, which ndless supports.
===Script tags===


<tt>make-prg</tt> adds a simple loader on top so the executable works on older versions of ndless.
====sc:script====


===Your first program===
{| class="wikitable"
If you want to create a program from scratch:
|-
*Create a new directory for the program
! scope="col"| attribute
*Type in a console:
! scope="col"| description
cd "<your directory path>"
|-
nspire-tools new <name>
| version
:where <name> is your program name. This will create a Makefile to build ''<program>.tns''
| taral and reqal versions, one byte per digit (for older OSes?)
*Create a new .c file and edit your program
|-
*Run <tt>make</tt> to build it
| id
| 1 = normal script, 2 & 3 = Question and Answer
|}
 
===Script metadata configuration===
 
{| class="wikitable"
|-
! scope="col"| Name
! scope="col"| Prop
! scope="col"| Description
! scope="col"| Example
|-
| TARAL
| 134217728
| TARget ApiLevel
| <code><nowiki><sc:mde name="TARAL" prop="134217728">2:2</sc:mde></nowiki></code>
|-
| REQAL
| 134217728
| REQuired ApiLevel
| <code><nowiki><sc:mde name="REQAL" prop="134217728">2:0</sc:mde></nowiki></code>
|-
| _VER
| 134217728
| TNS Version
| <code><nowiki><sc:mde name="_VER" prop="134217728">1:1</sc:mde></nowiki></code>
|-
| TITLE
| 2147549184
| ScriptApp title
| <code><nowiki><sc:mde name="TITLE" prop="2147549184">Hello</sc:mde></nowiki></code>
|-
| -
| 270532608
| Lua meta script
| <code><nowiki><sc:mde name="TEST" prop="270532608">--lua code</sc:mde></nowiki></code>
|-
| BLERQ
| 134217728
| BLE ReQuire - require ble libs
| <code><nowiki><sc:mde name="BLERQ" prop="134217728">1</sc:mde></nowiki></code>
|-
| _FFunc
| -
| FailMe function (error catching)
| Not known
|-
| _TRACE
| -
| Trace/log?
| Not known
|}

Latest revision as of 14:48, 21 June 2019

The TI-Nspire allows, since OS v3.0, users to run Lua scripts.

Prerequisites

Lua is only supported starting from OS v3.0.1. You can create Lua applications using the built-in script editor in the TI-Nspire computer software or one of the following third-party tools:

For any OS:

For OS < 3.0.1: these tools won't work with the OS 3.0.2 since the format used is now blocked

  • LUAtoTNS.sh (bash script, works on Linux, Mac OS and Unix, or Windows + Cygwin or Windows + MSYS).
  • maketns.py (Python script, cross-platform)
  • lua2ti (Windows + .NET Framework v4.0)
  • lua2ti_linux (Linux (No longer requires Mono, it is now a native Linux binary!))

You may also want to install Lua on your computer: luac -p can be used to check the syntax of your script before running it on a TI-Nspire or an emulator.

API

The documentation is now maintained in the "Inspired Lua Wiki". Please go there in order to have a full detailed documentation based on TI's information.

XML

Basic structure

<wdgt
    xmlns:sc="urn:TI.ScriptApp" type="TI.ScriptApp" ver="1.0">

    <sc:mFlags>1024</sc:mFlags>
    <sc:value>8</sc:value>
    <sc:cry>0</sc:cry>
    <sc:legal>none</sc:legal>
    <sc:schk>false</sc:schk>

    <sc:md>
        <sc:mde name="_VER" prop="134217728">1:1</sc:mde>
        <sc:mde name="TITLE" prop="2147549184">Hello</sc:mde>
        <sc:mde name="TARAL" prop="134217728">2:5</sc:mde>
    </sc:md>

    <sc:script version="33882629" id="1">
	-- Lua script, XML encoded
    </sc:script>

    <sc:state>
        return {} -- Lua save state
    </sc:state>
</wdgt>

Script tags

sc:script

attribute description
version taral and reqal versions, one byte per digit (for older OSes?)
id 1 = normal script, 2 & 3 = Question and Answer

Script metadata configuration

Name Prop Description Example
TARAL 134217728 TARget ApiLevel <sc:mde name="TARAL" prop="134217728">2:2</sc:mde>
REQAL 134217728 REQuired ApiLevel <sc:mde name="REQAL" prop="134217728">2:0</sc:mde>
_VER 134217728 TNS Version <sc:mde name="_VER" prop="134217728">1:1</sc:mde>
TITLE 2147549184 ScriptApp title <sc:mde name="TITLE" prop="2147549184">Hello</sc:mde>
- 270532608 Lua meta script <sc:mde name="TEST" prop="270532608">--lua code</sc:mde>
BLERQ 134217728 BLE ReQuire - require ble libs <sc:mde name="BLERQ" prop="134217728">1</sc:mde>
_FFunc - FailMe function (error catching) Not known
_TRACE - Trace/log? Not known