Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: _player1537 on July 31, 2010, 01:17:59 am

Title: Getkey function on the Nspire (C)
Post by: _player1537 on July 31, 2010, 01:17:59 am
Stupid question could be percieved as stupid... what do I use to do "GetKey"?
Title: Re: Getkey function on the Nspire (C)
Post by: northern_snow on July 31, 2010, 01:21:58 am
Err... what do you mean? You want to program on nSpire with OS1.1? Even OS1.1 doesn't support "disp" command!
Title: Re: Getkey function on the Nspire (C)
Post by: _player1537 on July 31, 2010, 01:24:49 am
sorry, I meant in C.  Figured it out btw, isKeyDown().

Edit:  I think this thread might become my Ndless Question/Help thread (moreso C).  If someone wants to move it, that's fine.

Should this code work?  It should darken the screen, wait for you to press ESC, and quit.
Code: [Select]
#include <os.h>
#include "utils.h"

asm(".string \"PRG\"\n");

int main(void)
{
    int A;
    int B;
    for (A=0;A>319;A++){
        for (B=0;B>240;B++){
            setPixel(A,B,0xF);
        }
    }
    while (!isKeyPressed(KEY_NSPIRE_ESC)){}

    return 0;
}
Title: Re: Getkey function on the Nspire (C)
Post by: DJ Omnimaga on July 31, 2010, 02:02:38 am
Split in different topic and moved to C sub-forum.
Title: Re: Getkey function on the Nspire (C)
Post by: bwang on July 31, 2010, 02:23:31 am
You need to change the for loop conditions to A < 320 and B < 240. With that said, here are some tips:
(1) You probably want to examine the first post in the Routines sticky for useful routines. I recommend skeleton.zip if you don't have it already. It has an extended os.h and utils.h with more functions and keycodes. It's also smaller, which makes for cleaner uploads.
(2) C style dictates that your variables should be in lowercase, with constants in uppercase.
(3) while(!isKeyPressed(KEY_NSPIRE_ESC)); works (note no braces!)
(4) For sake of readability (at least in my opinion), it is a good idea to make your hex numbers have leading zeroes (so use 0x0F instead of 0xF).
Congratulations on having written your first Nspire program! Welcome to the wonderful world of Nspire programming :)
Title: Re: Getkey function on the Nspire (C)
Post by: _player1537 on July 31, 2010, 08:36:13 am
ah, thank you!
1) Yep, already have that installed, and that is what I'm using atm for this.
2) Oops, I recall someone telling me that before. Fixed
3) ah, I see
4) Got it, fixed too

I also figured out that the lower the number, the darker the screen.  I thought it was 0x0F that was black x.x  I was looking through your Ncaster code, and I saw that you made your own array for the screen with malloc, is that a good practice I should use, or just use the screen memory itself?
Title: Re: Getkey function on the Nspire (C)
Post by: fb39ca4 on July 31, 2010, 01:55:42 pm
You should use screenbuffers when you have any animation. For still images, or just text, its fine not to.

To allocate one, use:
Code: [Select]
char* scrbuf = (char*) malloc(SCREEN_BYTES_SIZE);and put scrbuf in function arguments.
Title: Re: Getkey function on the Nspire (C)
Post by: bwang on July 31, 2010, 03:53:20 pm
@_player1537: You are right, 0x00 is black, 0x0F is white. Sorry for not pointing that out in my previous post.