/*
* Copyright (c) 2006 Ondrej Palkovsky
* Copyright (c) 2008 Martin Decky
* Copyright (c) 2008 Pavel Rimsky
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup serial Serial console
* @brief Serial console services (putc, puts, clear screen, cursor goto,...).*
* @{
*/
/** @file
*/
#include <stdio.h>
#include "serial_console.h"
#define MAX_CONTROL 20
static uint32_t width;
static uint32_t height;
static putc_function_t putc_function;
void serial_puts(char *str)
{
while (*str)
putc_function(*(str++));
}
void serial_goto(const unsigned int row, const unsigned int col)
{
if ((row > height) || (col > width))
return;
char control[20];
snprintf(control
, 20, "\033[%u;%uf", row
+ 1, col
+ 1);
serial_puts(control);
}
void serial_clrscr(void)
{
serial_puts("\033[2J");
}
void serial_scroll(int i)
{
if (i > 0) {
serial_goto(height - 1, 0);
while (i--)
serial_puts("\033D");
} else if (i < 0) {
serial_goto(0, 0);
while (i++)
serial_puts("\033M");
}
}
void serial_set_style(const unsigned int mode)
{
char control[MAX_CONTROL];
snprintf(control
, MAX_CONTROL
, "\033[%um", mode
);
serial_puts(control);
}
void serial_cursor_disable(void)
{
serial_puts("\033[?25l");
}
void serial_cursor_enable(void)
{
serial_puts("\033[?25h");
}
void serial_console_init(putc_function_t putc_fn, uint32_t w, uint32_t h)
{
width = w;
height = h;
putc_function = putc_fn;
}
/**
* @}
*/