/* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
/* NXT console
version 0.1
Description:
NXT console provides an output-only reprecentation of text-data
in a similar fashion to cmd.exe on Windows.
It supports the control-characters \n, \t, \a and \b, however \b
only works on the text currently being added.
It is possible to scroll up and down using the arrow buttons.
Changelog:
0.1: Initial release
Possible future improvements:
- Better \b support
- Limit the amount of buffered lines, to keep memory in check
Credits:
Coding - Sebastian Wahl (aka Spiller/spillerrec)
*/
//Settings
const int max_characters = 100 / 6; //Max amount of characters on a line
const int tab_width = 4; //The spacing of tab allignment
//Variables used for displaying lines
mutex line_edit; //Acquire this before using the variables
string lines[] = { "" }; //Contains all lines to display
int current_line = 0; //The index of the last line
int display_line = 0; //The index of the line displayed at the top
mutex redraw; //Release this in order to redraw
bool exit = false;
void move_view( int amount ){
Acquire( line_edit );
display_line += amount;
//Make sure you can't scroll past the first line
if( display_line < 0 )
display_line = 0;
Release( line_edit );
//request update of view
Release( redraw );
}
task move(){
while( !exit ){
//Deside how much to move
int move_amount = 0;
if( ButtonPressed( BTNLEFT ) )
move_amount--;
if( ButtonPressed( BTNRIGHT ) )
move_amount++;
//Move if changed
if( move_amount != 0 ){
move_view( move_amount );
Wait( 200 );
}
Yield();
}
}
task draw(){
while( !exit ){
Acquire( redraw ); //Wait for redraw
Acquire( line_edit );
//Calculate last line to draw
int last = ArrayLen( lines ) - 1;
if( last > display_line + 7 )
last = display_line + 7;
//Draw the lines to the screen
ClearScreen();
for(int i=display_line; i<=last; i++ )
TextOut( 0, 56-(i-display_line)*8, lines[i] );
Release( line_edit );
}
}
void add_text( string text ){
Acquire( line_edit );
//Remove \b's from string
int p;
while( (p = Pos( FormatNum( "%c", 8 ), text )) != -1 ){
if( p == 0 )
text = SubStr( text, 1, NA ); //Just ignore initial \b's for now
else{
//Remove both the \b and the character before it
//Actually, we could just replace them with non-printable characters!
string text_left = LeftStr( text, p-1 );
string text_right = SubStr( text, p+1, NA );
text = strcat( text_left, text_right );
}
}
//Create a buffer of the current line
string copy_simple = lines[ current_line ];
int f = StrLen( copy_simple );
//Go through every character in text
int s_len = strlen( text );
for( int i=0; i0; j--, f++ )
copy_simple = StrCat( copy_simple, " " );
break;
case 7: PlayTone( TONE_C5, 500 ); break; //ALERT!!!
}
continue;
}
}
else if( c < 128 ){
//Add normal characters to the current line
copy_simple = StrCat( copy_simple, FormatNum( "%c", c ) );
f++;
//Continue without adding new line if we haven't reached max
if( f < max_characters )
continue;
}
else
continue; //Don't print non-ASCII characters
//If we came this far, the line has been filled
//Finallize this line, create a new one and reset buffer
lines[ current_line++ ] = copy_simple;
copy_simple = "";
ArrayBuild( lines, lines, copy_simple );
f = 0;
}
//Save the buffer back to the line
lines[ current_line ] = copy_simple;
//Reset view, even though it is mostly annoying
display_line = current_line - 7;
if( display_line < 0 )
display_line = 0;
Release( line_edit );
//request update of view
Release( redraw );
}
task write_text(){
//Output a bunch to the console to show how it works
add_text( "This is an console!\n" );
Wait( 5000 );
add_text( "Features:\n" );
Wait( 5000 );
add_text( "1:\tScrolling\n" );
Wait( 5000 );
add_text( FormatNum( "2:\t\\t \\a%c \\b \\n\n", 7 ) );
Wait( 5000 );
add_text( "3:\tCon" );
Wait( 1000 );
add_text( "ti" );
Wait( 1000 );
add_text( "nue " );
Wait( 1000 );
add_text( "li" );
Wait( 1000 );
add_text( "nes!\n" );
Wait( 5000 );
add_text( FormatNum( "4:\tVery long lines wrapd%cs and continues onto the next line\n", 8 ) );
Wait( 10000 );
add_text( "\nThat's is all for now" );
Wait( 50000 );
exit = true; //Stop the other tasks
}
task main(){
Precedes( draw, write_text, move );
}