/* PhrasesCompiler.java * * (c) 2005 fnh */ import java.io.*; /** * read a text file with one-line phrases and create the upper/lower * ROM (read-only memory) initialization files suitable for the * TextLCD demonstration. * We assume a display with 2 lines of 40 characters each: the phrases * or jokes should be less than 80 characters (or rather less than 70 * characters due to the 2-char indent and the line-breaking algorithm). *
* Specify the input filename as the first argument to this class; * the two output files have the hardcoded names 'phrases-upper.rom' * and 'phrases-lower.rom' *
*/
class PhrasesCompiler {
FileInputStream infile = null;
PrintStream upper = null;
PrintStream lower = null;
int addr = 0;
public PhrasesCompiler() {
// empty
}
public void parse( String filename ) throws Exception
{
infile = new FileInputStream( filename );
upper = new PrintStream( new FileOutputStream( "phrases-upper.rom" ));
lower = new PrintStream( new FileOutputStream( "phrases-lower.rom" ));
displayInit();
BufferedReader br = new BufferedReader( new InputStreamReader( infile ));
String line = null;
while( (line=br.readLine()) != null ) {
msg( "-#- '" + line + "'" );
if (line.length() == 0) continue; // empty line
if (line.charAt(0) == '#') continue; // comment line
displayCursorHome();
tokenizeLine( line );
}
restOfRom();
upper.close();
lower.close();
infile.close();
}
/**
* try to find a suitable line break for a display with two rows of 80
* columns (characters) each.
*/
public void tokenizeLine( String line ) {
int n = line.length();
int x = n-1;
for( int i=Math.min(x,39); i >= 0; i-- ) {
char c = line.charAt(i);
if ((c == ' ') || (c == ',') || (c == ':')) {
x = i; break;
}
}
String line1 = line.substring(0,x);
String line2 = " " + line.substring(x,n);
// some debug output
// msg( " '" + line1 + "'" );
// msg( " '" + line2 + "'" );
// msg( "" );
for( int i=0,m=line1.length(); i < m; i++ ) {
displayOneChar( line1.charAt(i) );
}
displayNewLine();
for( int i=0,m=line2.length(); i < Math.min(m,40); i++ ) {
displayOneChar( line2.charAt(i) );
}
displayPause();
}
public void restOfRom() {
int rom_size = 8192; // 8K hardcoded
for( int a = addr; a < rom_size; a++ ) {
_write( 0, 0x0f ); // cursor-on: a NOP here
}
}
public void displayInit() {
_write( 0, 0x38 ); // reset
_write( 0, 0x3c ); // reset: 8-bit two-lines
_write( 0, 0x0f ); // display-on cursonr on
_write( 0, 0x01 ); // clear display
_write( 0, 0x80 ); // data addr 0
}
public void displayCursorHome() {
_write( 0, 0x01 ); // clear display
_write( 0, 0x03 ); // cursor home // cursor home
_write( 0, 0x80 ); // data addr 0
}
public void displayNewLine() {
_write( 0, 0xc0 ); // data addr 64 (first char, second line)
}
public void displayOneChar( char c ) {
_write( 2, c );
}
public void displayPause() {
for( int i=0; i < 20; i++ ) {
_write( 0, 0x0f ); // display-on curson-on ...
}
}
public void _write( int uval, int lval) {
upper.println( _hex4( addr ) + ":" + _hex2( uval ));
lower.println( _hex4( addr ) + ":" + _hex2( lval ));
addr ++;
}
public String _hex2( int val ) {
if ((val > 255) || (val < 0)) return "XX";
if (val > 15) return Integer.toHexString( val );
else return "0" + Integer.toHexString( val );
}
public String _hex4( int addr ) {
if ((addr > 65535) || (addr < 0)) return "XXXX";
if (addr > 4095) return Integer.toHexString( addr );
if (addr > 255) return "0" + Integer.toHexString( addr );
if (addr > 15) return "00" + Integer.toHexString( addr );
else return "000" + Integer.toHexString( addr );
}
public static void usage() {
System.out.println(
"Usage: java PhrasesCompiler