[NXC] Check if pixel is on/off?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

[NXC] Check if pixel is on/off?

Post by nxtboyiii »

Hi,
I was wondering if there was a way to check if a certain pixel on the NXT screen is black or white using NXC. Or is there a way to use muntoo's SCR_FILE_LIB to do it?

Thanks, and have a nice day,
nxtboy III
Thanks, and have a nice day,
nxtboy III

programnxt.com
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: [NXC] Check if pixel is on/off?

Post by nxtboyiii »

So does anybody know how to check if a pixel is on or off?
Thanks, and have a nice day,
nxtboy III

programnxt.com
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: [NXC] Check if pixel is on/off?

Post by h-g-t »

The only place I have seen that is on the pblua site - http://hempeldesigngroup.com/lego/pblua ... DisplayAPI

"pixel = nxt.DisplayGetPixel( x, y )

Returns 1 if the pixel given by the display coordinate pair (x,y) is turned on, and 0 if the pixel is turned off."

Presumably you would have to dis-assemble the pblua firmware to see how that works.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
afanofosc_99
Posts: 15
Joined: 26 Sep 2010, 18:18

Re: [NXC] Check if pixel is on/off?

Post by afanofosc_99 »

You read a byte of screen memory that contains the pixel that you want to check (no way currently to read just a single pixel) and then you check to see if that pixel's bit is set in the byte you read. This seems to work okay:

Code: Select all

bool isPixelSet(byte X, byte Y)
{
  byte masks[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
  byte Data[1];
  byte Line = (63-Y & 0xF8) / 8;
  byte offset = Y % 8; // how many bits up from 0?
  GetDisplayNormal(X, Line, 1, Data);
  return ((Data[0] & masks[offset]) != 0);
}

task main()
{
  while (true)
  {
    byte x = Random(100);
    byte y = Random(56) + 8; // skip the bottom line
    PointOut(x, y);
    if (isPixelSet(x, y))
      TextOut(0, LCD_LINE8, "set");
    else
      TextOut(0, LCD_LINE8, "not set");
    Wait(SEC_1);
    ClearScreen();
  }
}
John Hansen
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: [NXC] Check if pixel is on/off?

Post by nxtboyiii »

Thanks!
Is this the most efficient (quickest) way to do it?
Thanks, and have a nice day,
nxtboy III

programnxt.com
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] Check if pixel is on/off?

Post by afanofosc »

Almost always there is a faster way but that may not always make it a better way.

The original function executed in about .648 ms with the enhanced NBC/NXC firmware. The standard firmware would have been a bit slower since it doesn't have the IOMapReadByID system call function and this code has to resort to the IOMapRead system call function which uses the module name and has to look up the module ID from the name.

Here's a version that is almost 200 ms faster over 1000 iterations so each execution takes about .458 ms:

Code: Select all

byte __ips_masks[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

byte isPixelSet(byte X, byte Y)
{
  byte Data[];
  byte Line = (63-Y & 0xF8) / 8;
  byte offset = Y % 8;
  GetDisplayNormal(X, Line, 1, Data);
  return Data[0] & __ips_masks[offset];
}
It might be faster to & with (0x01 << offset):

Code: Select all

byte isPixelSet(byte X, byte Y)
{
  byte Data[];
  byte Line = (63-Y & 0xF8) / 8;
  byte offset = Y % 8;
  GetDisplayNormal(X, Line, 1, Data);
  return Data[0] & (0x01 << offset);
}
This runs in about .447 ms rather than .458 ms but it also depends on the enhanced NBC/NXC firmware's native shift operations while the version above does not. But no global array is needed. You could probably get this a little faster by coding purely in NBC but it doesn't really seem worth it.

All my tests were run with optimization level 3.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: [NXC] Check if pixel is on/off?

Post by nxtboyiii »

Thank you so much!

Have a nice day,
nxtboy III
Thanks, and have a nice day,
nxtboy III

programnxt.com
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: [NXC] Check if pixel is on/off?

Post by nxtboyiii »

Would it run faster if this was converted to NBC? If so, could you?

Code: Select all

byte __ips_masks[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

byte isPixelSet(byte X, byte Y)
{
  byte Data[];
  byte Line = (63-Y & 0xF8) / 8;
  byte offset = Y % 8;
  GetDisplayNormal(X, Line, 1, Data);
  return Data[0] & __ips_masks[offset];
}
Thanks, and have a nice day,
nxtboy III

programnxt.com
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] Check if pixel is on/off?

Post by afanofosc »

Already answered that question above.
You could probably get this a little faster by coding purely in NBC but it doesn't really seem worth it.
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC] Check if pixel is on/off?

Post by muntoo »

nxtboyiii wrote:Would it run faster if this was converted to NBC?
I don't think it would be... seems like a pointless optimization.

EDIT: Ninja'd!
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests