Shop OBEX P1 Docs P2 Docs Learn Events
Urt.java — Parallax Forums

Urt.java

Don ScalesDon Scales Posts: 6
edited 2010-03-04 17:43 in General Discussion
I have been looking through the code for Uart.java that was supplied with the Javelin Stamp IDE
The file in question is in lib/stamp/core
I am not sure about the following code
public int receiveByte() {
int b;

if ( vpBank == NOT_INSTALLED ) {
return -1 ;
}

while ( true ) {
int headPointer = CPU.readRegister( vpBank|nmUartHead );
int tailPointer = CPU.readRegister( vpBank|nmUartTail );

if ( headPointer != tailPointer ) {
int index = tailPointer>>1;

if ( (tailPointer&1) == 0 )
b = (byte)(buffer[noparse][[/noparse]index] >>> 8);
else
b = (byte)(buffer[noparse][[/noparse]index] & 0x00ff);

tailPointer++;
CPU.writeRegister( vpBank|nmUartTail, tailPointer );
break;
}
}
// Inform the receiver so it can do the handshaking.
rxRead(vpBank);

return b;
}
I suspect the line b = (byte)(buffer[noparse][[/noparse]index] >>> 8);
should be b = (byte)(buffer[noparse][[/noparse]index] >> 8);

There is no class with this code so I am not sure how and when it gets compiled.
It compiles clean using the IDE but it will not return the correct result.
Regards
Don

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-03-04 12:09
    The Uart.java class compiles when you import the class in your program
    and use the Uart object.

    The code is ok. The >>> denotes an unsigned shift, whereas >> denotes a signed shift.

    regards peter
  • Don ScalesDon Scales Posts: 6
    edited 2010-03-04 16:11
    Thanks Peter,
    I have been writing Java for many years and never come across the
    unsigned shift. Looks like I can improve some of my code !!
    Regards
    Don
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-03-04 17:43
    I am not sure if the >>> is a genuine java operator,
    but it is a helpful one for javelin programming as there are no unsigned ints.

    The workaround for·>>> N using >> is

    while (N > 0) {
    · value = (value·>> 1) & 0x7FFF;
    · N--;
    }

    regards peter
Sign In or Register to comment.