Tag Archives: programming

End of the Nspire Lua Contest

Some time ago, we announced a contest : Nspire programming in Lua . This was a large scale contest since the total sum of the prizes was of about € 750, including three awesome TI-Nspire CAS CX !

The contest is now over !

With not less than 14 Lua programs submitted, we think this is a great success for a language so “new” on the TI-Nspire calculator!

We would like to thank all participants, regardless of their results!

The judges (Levak, Critor and myself) are going to analyze everything you have done in the coming days, and the results should be there soon !

In the meantime, here is the list (just name + Screenshot) of entries received.

We recall that these programs, unless otherwise stated on the download page are subject to the following license : CC BY-SA 2.0 . Not respecting the terms of this license will result in legal consequences. The authors of the respective programs can change their licence at their will by contacting us by e-mail, of course.

Games : 7


Bobby Carrot – – – – Loïc P.

Image
Labyrinthe – – – – David L.


Reversi (Othello) – – – – Deep Thought.

Image
MasterMind – – – – Nick V.


TI-Cran – – – – Julien R.


Nspired Phoenix Lua – – – – Florent D.


Tactical Wars CX – – – – Rehn C.

Mathematics: 4


TabVar 3 – – – – JayTe.


LuaCS – – – – Jonathan L.


LogoMagic – – – – Jim B.


ABA Logique – – – – Loulou54.

Physics/Chemistry: 3


Planétarium – – – – Bastien V.

Image
FormulaOne – – – – Naji A.

Image
Formules de Chimie – – – – Paul J.

 

Well, while waiting for the results to come, fell free to comment on these programs !

Once again, congratulations to all !

Using a predefined variable in a TI-Nspire document

Back to part 3

This part will show you how well the Lua language is integrated in the TI-Nspire framework.
For this, we’re going to study the var API and expected events.

Retour à la partie 3

Cette partie vous montrera que le Lua est bel et bien en parfaite harmonie avec la TI-Nspire et son framework. Pour cela nous vous proposons d’étudier l’API var et les évènements qui en découlent.

Tout d’abord , créez un nouveau classeur TI-Nspire et définissez y une variable n, par exemple un curseur permettant d’en faire varier la valeur entre 0 et 10 ou directement dans la page de l’application calculs.

C’est cette variable que nous allons récupérer dans le script Lua !

Pour cela, créez un nouveau fichier Lua avec votre éditeur de texte préféré et nous allons commencer à le remplir.

L’API var permet de faire pleins de choses intéressantes et nous allons les étudier.

Tout d’abord, notre script doit savoir si la variable n change. Nous trouvons donc cette ligne :

 

-- On indique ici qu'il faut surveiller le contenu de la variable n
var.monitor("n")

Au passage, nous pouvons écrire une fonction récursive qui renverra la factorielle d’un nombre, rien que pour l’exemple :

function factorielle(n)
	if math.floor(n) < n or n < 0 then
		return ("Erreur")
	elseif n == 0 then
		return(1)
	else
		return(n * factorielle(n - 1))
	end
end

Maintenant, nous allons nous intéresser au couplage des évènements. Il nous faut deux évènements. D’abord celui pour dessiner à l’écran, c’est à dire on.paint(), mais également un évènement déclenché lorsque notre variable change. Cet évènement est on.varChange() et est lancé chaque fois qu’une variable appartenant à la liste des variables surveillées (avec var.monitor()) est modifiée.

function on.varChange(varlist)
	-- On provoque une reactualisation de la fenetre
	platform.window:invalidate()
end

Concrètement, dès qu’une variable surveillée change, on demande à ce que l’écran soit actualisé. Donc maintenant, il faut faire cette partie d’affichage en créant la fonction on.paint(). En effet, appeler platform.window:invalidate() marque l’écran comme “invalide”, donc bon à être actualisé. Au prochain tour de boucle de processus, comme l’écran doit être actualisé, le framework de la TI-Nspire lancera la fonction on.paint()

function on.paint(gc)
	local wh, ww, n, x
	-- nombre de pixels (hauteur et largeur)
	wh = platform.window:height()
	ww = platform.window:width()
 
	-- affichage d'un trait de separation
	gc:setPen("medium", "smooth")
	gc:drawLine(0, 60, ww, 60)
 
	-- affichage de la taille de la fenetre en bas de l'ecran
	-- il s'agit juste d'un exemple !
	gc:setColorRGB(200, 200, 200)
	x = gc:drawString(wh, 10, wh - 10, "bottom")
	gc:drawString(ww, x + 10, wh - 10, "bottom")
 
	-- affichage de n!
	n = var.recall("n")
	if math.floor(n) < n or n < 0 then
		gc:setColorRGB(255, 0, 0)
		gc:setFont("sansserif" , "b", 10)
		gc:drawString("n n'est pas un ",10,10,"top")
		gc:drawString("entier naturel ",10,25,"top")
	else
		gc:setColorRGB(0, 0, 255)
		gc:setFont("sansserif" , "b", 20)
		x = gc:drawString(n.."! = ", 10, 10, "top")
		x = gc:drawString(factorielle(n), x + 5, 10, "top")
	end
end

Voilà ! C’est tout !

Lancez ensuite TI-Nspire Scripting tools et cliquez sur le premier bouton (ou choisissez tools, Lua Script to Clipboard) puis sélectionnez le fichier contenant le script que nous venons de créer. Allez dans le logiciel TI-Nspire, et utilisez Ctrl V pour copier le code Lua.

Faites ensuite varier la valeur de n, et la miracle !

La valeur dans la fenêtre Lua est actualisée !

Tip : Si vous avez modifié le script, il vous suffit dans TI-Nspire Scripting Tools de

  • cliquer sur le second bouton (ou choisissez tools, Reload Script).
  • Allez ensuite dans le logiciel TI-Nspire, cliquer sur la fenêtre correspondant au code Lua.
  • Ctrl K pour sélectionner cette fenêtre
  • Ctrl V pour copier le nouveau script Lua

Idées d’améliorations :

Vous pouvez tester une première “amélioration” de ce script en ajoutant les lignes suivantes, permettant de “piloter” n avec les touches “vers le haut” ou “vers le bas” :

function on.arrowUp()
	var.store("n", var.recall("n") + 1)
end
 
function on.arrowDown()
	var.store("n", var.recall("n") - 1)
end

>> Partie 5

Concepts and Basics

This part will explain you how the Lua actually works inside the OS and help you to figure out what you’re doing when you write a script for the TI-Nspire. It is recommended to have some basics on Lua programming or some knowledge of event-driven languages, but keep in mind that it is not required.

The Lua is an interpreted script language which means that it isn’t as fast as ASM/C programs, but is still better than the TI-BASIC. One good thing is that this language is in a complete harmony with the OS with basic events and a powerful graphic context.

Lua is normally a sequential script language. For example, when we use the print() command to display a value, we can easily guess when the command will be run in the script. Here’s an example :

a = 1
print(a)
a = a + 1
print(a)

Output :

1
2

Nothing special. However, on the TI-Nspire, Lua has a completely different approach. We meet this approach with high-level programming or with object-oriented languages (like C++, C#, …). In those languages, we don’t have the ability to control the flow/execution of any function. Yes, it can be quite strange to hear that, but it’s the way it is. Are we going to learn a language that doesn’t do what we tell it to do ? Well, in a way, yes.

But don’t worry ! We’re here to learn how to cross this quite unstable bridge ! A wonderful world is on the other side.

First of all, you have to “change team”. As in before, you were the boss, this means you used to tell the machine to compute 1 + 1 and it would proudly output “2”. Now, you are a worker. A task authorization is given to you, thus, you explain to the machine how to do this work. Actually, those “authorizations” are called events. When the event is called, you can do what ever you want. Here is a pseudo-code explaining what to do when the “Cook” event is called :

function Cook()
	organize_the_bench()
	cut_ingredients()
	assemble()
	heat_up()
	serve()
end

You can easily understand that you won’t cook when you get the job. You’ll wait for your boss’ order ! Well, it is exactly the same thing between the TI-Nspire and you. But this time, the TI-Nspire framework is the boss. Everything is event-based.

In a nutshell, our functions have to be called by the TI-Nspire. But how to be sure that they will be executed ? It is the moment to look at the Events list. When an event is fired, the TI-Nspire gives zero, one or multiple parameters (“arguments”) that we can use in our function. This lets us know, for example, which key has been pressed, because when the TI-Nspire executes the charIn(ch) event, it gives also a string as an argument corresponding to the key. However, when the enterKey() event is fired, no argument is given.

Before, in a non-event-based language (like BASIC, C, PHP …) we used to program like this :

-- Init Constants here
...
k = 0
while k == 0 do
	k = getKey()
end
if k == 72 then
	-- do something
end

Now, it looks like that :

-- Init Constants here
...
function on.charIn(ch)
	if ch == "7" then
		-- do something
	end
end

We hope you understood that very important part…

 

Let’s go to Part 2 now !