https://bitbucket.org/hudson/magic-lantern
Revision 488e483d16f2f28f7f80cc1ffcffe63fe656bd4d authored by a1ex on 03 June 2011, 22:01:47 UTC, committed by a1ex on 03 June 2011, 22:01:47 UTC
1 parent 8fc97f6
Raw File
Tip revision: 488e483d16f2f28f7f80cc1ffcffe63fe656bd4d authored by a1ex on 03 June 2011, 22:01:47 UTC
Swap MENU <-> ERASE (experimental)
Tip revision: 488e483
console.c
// script console

#include "bmp.h"
#include "dryos.h"
#include "menu.h"
#include "gui.h"
#include "property.h"

void console_printf(const char* fmt, ...); // how to replace the normal printf?
#define printf console_printf

#define CONSOLE_W 80
#define CONSOLE_H 25

// buffer is circular and filled with spaces
#define BUFSIZE (CONSOLE_H * CONSOLE_W)
char* console_buffer = 0;

char* console_puts_buffer = 0; // "normal" copy of the circular buffer

int console_buffer_index = 0; 

int console_visible = 0;

FILE* console_log_file = 0;
void console_show() 
{ 
	console_visible = 1;
	set_global_draw(0);
	FIO_RemoveFile("B:/console.log");
	console_log_file = FIO_CreateFile("B:/console.log");
	bmp_printf(FONT_LARGE, 0, 0, "CONSOLE ON ");
}
void console_hide() 
{ 
	console_visible = 0;
	msleep(500);
	set_global_draw(1);
	clrscr();
	FIO_CloseFile(console_log_file);
	console_log_file = 0;
	bmp_printf(FONT_LARGE, 0, 0, "CONSOLE OFF");
}

static void
console_toggle( void * priv )
{
	if (console_visible) console_hide();
	else console_show();
}

static void
console_test( void * priv )
{
	console_visible = 1;
	printf("Hello World!\n");
	printf("The quick brown fox jumps over the lazy dog. Computer programs expand so as to fill the core available. El trabajo y la economia son la mejor loteria. \n");
}

static void
console_print( void * priv, int x, int y, int selected )
{
	bmp_printf(
		selected ? MENU_FONT_SEL : MENU_FONT,
		x, y,
		"Debug Console: %s",
		*(unsigned*) priv ? "ON " : "OFF"
	);
}

static struct menu_entry script_menu[] = {
	/*{
		.priv		= "Console test",
		.display	= menu_print,
		.select		= console_test,
	},*/
	{
		.priv = &console_visible,
		.display	= console_print,
		.select		= console_toggle,
	},
};

void console_clear()
{
	if (!console_buffer) return;
	int i;
	for (i = 0; i < BUFSIZE; i++)
		console_buffer[i] = ' ';
}
void console_init()
{
	console_buffer = AllocateMemory(BUFSIZE+32);
	console_puts_buffer = AllocateMemory(BUFSIZE+32);

	console_clear();
	
	menu_add( "Debug", script_menu, COUNT(script_menu) );
}

void console_puts(const char* str) // don't DebugMsg from here!
{
	#define NEW_CHAR(c) console_buffer[mod(console_buffer_index++, BUFSIZE)] = (c)
	
	if (console_log_file)
		FIO_WriteFile( console_log_file, UNCACHEABLE(str), strlen(str) );
	
	if (!console_buffer) return 0;
	char* c = str;
	while (*c)
	{
		if (*c == '\n')
			while (mod(console_buffer_index, CONSOLE_W) != 0) 
				NEW_CHAR(' ');
		else if (*c == '\t')
		{
			NEW_CHAR(' ');
			while (mod(mod(console_buffer_index, CONSOLE_W), 4) != 0) 
				NEW_CHAR(' ');
		}
		else
			NEW_CHAR(*c);
		c++;
	}
}

void console_printf(const char* fmt, ...) // don't DebugMsg from here!
{
	char buf[256];
	va_list			ap;
	va_start( ap, fmt );
	int len = vsnprintf( buf, 256, fmt, ap );
	va_end( ap );
	console_puts(buf);
}

void console_draw()
{
	if (!console_buffer) return 0;
	if (!console_puts_buffer) return 0;
	unsigned x0 = 720/2 - font_small.width * CONSOLE_W/2;
	unsigned y0 = 480/2 - font_small.height * CONSOLE_H/2;
	unsigned w = font_small.width * CONSOLE_W;
	unsigned h = font_small.height * CONSOLE_H;
	int i;
	for (i = 0; i < BUFSIZE; i++)
	{
		console_puts_buffer[i] = console_buffer[mod(console_buffer_index + i, BUFSIZE)];
	}
	console_puts_buffer[BUFSIZE] = 0;
	bmp_puts_w(FONT(FONT_SMALL,COLOR_WHITE,COLOR_BG_DARK), &x0, &y0, CONSOLE_W, console_puts_buffer);
}


static void
console_task( void )
{
	console_init();
	while(1)
	{
		if (console_visible && !gui_menu_shown() && gui_state == GUISTATE_IDLE)
		{
			set_global_draw(0);
			console_draw();
		}
		msleep(200);
	}
}

TASK_CREATE( "console_task", console_task, 0, 0x1f, 0x1000 );


//~ INIT_FUNC(__FILE__, console_init);


back to top