[NXC] Check if pixel is on/off?
[NXC] Check if pixel is on/off?
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
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
nxtboy III
programnxt.com
Re: [NXC] Check if pixel is on/off?
So does anybody know how to check if a pixel is on or off?
Thanks, and have a nice day,
nxtboy III
programnxt.com
nxtboy III
programnxt.com
Re: [NXC] Check if pixel is on/off?
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.
"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.
-
- Posts: 15
- Joined: 26 Sep 2010, 18:18
Re: [NXC] Check if pixel is on/off?
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:
John Hansen
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();
}
}
Re: [NXC] Check if pixel is on/off?
Thanks!
Is this the most efficient (quickest) way to do it?
Is this the most efficient (quickest) way to do it?
Thanks, and have a nice day,
nxtboy III
programnxt.com
nxtboy III
programnxt.com
Re: [NXC] Check if pixel is on/off?
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:
It might be faster to & with (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
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];
}
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);
}
All my tests were run with optimization level 3.
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: [NXC] Check if pixel is on/off?
Thank you so much!
Have a nice day,
nxtboy III
Have a nice day,
nxtboy III
Thanks, and have a nice day,
nxtboy III
programnxt.com
nxtboy III
programnxt.com
Re: [NXC] Check if pixel is on/off?
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
nxtboy III
programnxt.com
Re: [NXC] Check if pixel is on/off?
Already answered that question above.
John HansenYou could probably get this a little faster by coding purely in NBC but it doesn't really seem worth it.
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: [NXC] Check if pixel is on/off?
I don't think it would be... seems like a pointless optimization.nxtboyiii wrote:Would it run faster if this was converted to NBC?
EDIT: Ninja'd!
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Who is online
Users browsing this forum: No registered users and 3 guests