Page 1 of 2

[NXC] Check if pixel is on/off?

Posted: 27 Jan 2012, 01:08
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

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

Posted: 10 Feb 2012, 23:13
by nxtboyiii
So does anybody know how to check if a pixel is on or off?

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

Posted: 11 Feb 2012, 00:43
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.

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

Posted: 11 Feb 2012, 00:59
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

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

Posted: 11 Feb 2012, 01:24
by nxtboyiii
Thanks!
Is this the most efficient (quickest) way to do it?

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

Posted: 11 Feb 2012, 17:33
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

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

Posted: 11 Feb 2012, 20:42
by nxtboyiii
Thank you so much!

Have a nice day,
nxtboy III

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

Posted: 07 Mar 2012, 03:21
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];
}

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

Posted: 07 Mar 2012, 03:28
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

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

Posted: 07 Mar 2012, 03:30
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!