https://bitbucket.org/hudson/magic-lantern
Tip revision: 2edf3588768b8a4c86d45a6bec3255fe41418bad authored by nanomad on 10 September 2011, 00:10:53 UTC
We can now display a simple Hello World message on the screen
We can now display a simple Hello World message on the screen
Tip revision: 2edf358
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(CARD_DRIVE "console.log");
console_log_file = FIO_CreateFile(CARD_DRIVE "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);