Urt.java
Don Scales
Posts: 6
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
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
and use the Uart object.
The code is ok. The >>> denotes an unsigned shift, whereas >> denotes a signed shift.
regards 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
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