Classes InputStream
and OutputStream
in package mcujavasource.mcu.io
are similar to standard classes from java.io
Now streams are used for UART, they cannot be closed, so streams for MCU haven't close() method (it may change later). AVR issue: don't use getInputStream() if input stream's read() or available() or skip() is not used in the program (bad interrupt will appear).
There are no readers and writers in MCU. Charset support is not implemented (only default charset is used). It doesn't fit into microcontroller. Instead of buffered readers and writers there are BufferedInputStream
and BufferedOutputStream
Arrays in Java must allways be initialized. In MCU Java source arrays must be declared and initialized in one line:
int[] numberArray = new int[20]; //numberArray has 20 elements
int array2[] = new int[10]; //array2 has 10 elements
import mcujavasource.mcu.*;
import mcujavasource.mcu.io.*;
/** Tutorial 4: UART repeater.
* Receives text from a terminal and when Enter is pressed ('\n' char) or
* maximum text size is reached, transmits back "You entered ",
* then the text received.
* Uart speed = 4800 bps at 1 MHz MCU clock frequency, 38400 bps at 8 MHz.
* Uart settings: 8 bits character, no parity, one stop bit.
* Maximum text size is 32 chars.
*/
public class Tutorial4 extends Microcontroller
{
private static final String HELLO = "Hello from Java!\r\n";
private static final String BEROFE_TEXT = "You entered '";
private static final String AFTER_TEXT = "'.\r\n";
private UartInputStream uartInput;
private UartOutputStream uartOutput;
private byte[] buffer = new byte[32];
/** Buffer limit */
private int limit = 0;
public void init()
{ //register initialization on startup
Uart uart = getHardware().getDefaultUart();
uart.init(25, true, 8, Uart.Parity.NONE, false);
uartInput = uart.getInputStream();
uartOutput = uart.getOutputStream();
}
public void start()
{ //main program
getHardware().setInterruptsEnabled(true);
uartOutput.write(HELLO);
while(true)
{ limit = 0;
byte b;
do
{ b = (byte) uartInput.read();
buffer[limit++] = b;
}
while(b != '\n' && limit < 32);
uartOutput.write(BEROFE_TEXT);
int position;
for(position = 0; position < limit; position++)
{ uartOutput.write(buffer[position]);
}
uartOutput.write(AFTER_TEXT);
}
}
}