Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: klebue on May 24, 2010, 12:28:30 pm

Title: crash after calling function twice
Post by: klebue on May 24, 2010, 12:28:30 pm
Hi there!
I'm trying to get into c programming for nspire (actually I'm trying to get into c programming at all ;) )

I can have a very simple function, say to draw a dot of 2*2 pixels.
The function works just fine and draws my dot, but when the function is called for a second time the nspire crashes.
Here is the code I use:
Code: [Select]
int main(void) {
clearScreen();

void drawdot(int posx, int posy, int colour) {
setPixel(posx, posy, colour);
setPixel(posx+1, posy, colour);
setPixel(posx, posy+1, colour);
setPixel(posx+1, posy+1, colour);
}

drawdot(10,10,0); //works fine
drawdot(10,15,0); //crashes when I add this line

while (!isKeyPressed(KEY_NSPIRE_ESC)) {}
return 0;
}

There is also another problem: When I press ESC the calculator will stop responding instead of redrawing the normal screen.
What am I missing to make him do so? I can't figure out what makes it work in other programs.

Thanks in advance!
Title: Re: crash after calling function twice
Post by: bwang on May 24, 2010, 05:40:44 pm
Don't define your functions inside main().
In fact, place main() in its own file and all your functions in other header files. I've had trouble with functions defined in the same file as main() crashing.
Title: Re: crash after calling function twice
Post by: klebue on May 26, 2010, 04:53:13 pm
Thanks for the advice, the function thing is working now.
However, I still couldn't figure out how to quit a program and make the calculator continue working the old-fashioned way.

/edit:
problem solved, the last lines should be
Code: [Select]
while (!isKeyPressed(KEY_NSPIRE_ESC)) {
rand();
}
return 0;
The nspire doesn't like doing nothing ;)
Title: Re: crash after calling function twice
Post by: bwang on May 27, 2010, 12:56:03 am
while (!isKeyPressed(KEY_NSPIRE_ESC)); also works.