common: lcd: extend lcd api by function lcd_get_position_cursor()
authorLukasz Majewski <l.majewski@samsung.com>
Mon, 16 Feb 2015 16:40:25 +0000 (17:40 +0100)
committerJaehoon Chung <jh80.chung@samsung.com>
Thu, 24 Oct 2024 00:52:53 +0000 (09:52 +0900)
Changes for v2015.04-rc1:
- Moving lcd console related code to lcd_console.c

Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
common/lcd_console.c [new file with mode: 0644]
include/lcd_console.h [new file with mode: 0644]

diff --git a/common/lcd_console.c b/common/lcd_console.c
new file mode 100644 (file)
index 0000000..97e50c0
--- /dev/null
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2001-2015
+ * DENX Software Engineering -- wd@denx.de
+ * Compulab Ltd - http://compulab.co.il/
+ * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
+ */
+
+#include <common.h>
+#include <command.h>
+#include <lcd.h>
+#include <log.h>
+#include <serial.h>
+#include <video_font.h>                /* Get font data, width and height */
+#if defined(CONFIG_LCD_LOGO)
+#include <bmp_logo.h>
+#endif
+
+static struct console_t cons;
+
+void lcd_set_position_cursor(unsigned col, unsigned row)
+{
+       lcd_position_cursor(col, row);
+}
+
+void lcd_get_position_cursor(unsigned *col, unsigned *row)
+{
+       *col = cons.curr_col;
+       *row = cons.curr_row;
+}
+
+void lcd_set_col(short col)
+{
+       cons.curr_col = col;
+}
+
+void lcd_set_row(short row)
+{
+       cons.curr_row = row;
+}
+
+void lcd_position_cursor(unsigned col, unsigned row)
+{
+       cons.curr_col = min_t(short, col, cons.cols - 1);
+       cons.curr_row = min_t(short, row, cons.rows - 1);
+}
+
+int lcd_get_screen_rows(void)
+{
+       return cons.rows;
+}
+
+int lcd_get_screen_columns(void)
+{
+       return cons.cols;
+}
+
+static void lcd_putc_xy0(struct console_t *pcons, ushort x, ushort y, char c)
+{
+       int fg_color = lcd_getfgcolor();
+       int bg_color = lcd_getbgcolor();
+       int i, row;
+       fbptr_t *dst = (fbptr_t *)pcons->fbbase +
+                                 y * pcons->lcdsizex +
+                                 x;
+
+       for (row = 0; row < VIDEO_FONT_HEIGHT; row++) {
+               uchar bits = video_fontdata[c * VIDEO_FONT_HEIGHT + row];
+               for (i = 0; i < VIDEO_FONT_WIDTH; ++i) {
+                       *dst++ = (bits & 0x80) ? fg_color : bg_color;
+                       bits <<= 1;
+               }
+               dst += (pcons->lcdsizex - VIDEO_FONT_WIDTH);
+       }
+}
+
+static inline void console_setrow0(struct console_t *pcons, u32 row, int clr)
+{
+       int i;
+       fbptr_t *dst = (fbptr_t *)pcons->fbbase +
+                                 row * VIDEO_FONT_HEIGHT *
+                                 pcons->lcdsizex;
+
+       for (i = 0; i < (VIDEO_FONT_HEIGHT * pcons->lcdsizex); i++)
+               *dst++ = clr;
+}
+
+static inline void console_moverow0(struct console_t *pcons,
+                                   u32 rowdst, u32 rowsrc)
+{
+       int i;
+       fbptr_t *dst = (fbptr_t *)pcons->fbbase +
+                                 rowdst * VIDEO_FONT_HEIGHT *
+                                 pcons->lcdsizex;
+
+       fbptr_t *src = (fbptr_t *)pcons->fbbase +
+                                 rowsrc * VIDEO_FONT_HEIGHT *
+                                 pcons->lcdsizex;
+
+       for (i = 0; i < (VIDEO_FONT_HEIGHT * pcons->lcdsizex); i++)
+               *dst++ = *src++;
+}
+
+static inline void console_back(void)
+{
+       if (--cons.curr_col < 0) {
+               cons.curr_col = cons.cols - 1;
+               if (--cons.curr_row < 0)
+                       cons.curr_row = 0;
+       }
+
+       cons.fp_putc_xy(&cons,
+                       cons.curr_col * VIDEO_FONT_WIDTH,
+                       cons.curr_row * VIDEO_FONT_HEIGHT, ' ');
+}
+
+static inline void console_newline(void)
+{
+       const int rows = CONFIG_CONSOLE_SCROLL_LINES;
+       int bg_color = lcd_getbgcolor();
+       int i;
+
+       cons.curr_col = 0;
+
+       /* Check if we need to scroll the terminal */
+       if (++cons.curr_row >= cons.rows) {
+               for (i = 0; i < cons.rows-rows; i++)
+                       cons.fp_console_moverow(&cons, i, i+rows);
+               for (i = 0; i < rows; i++)
+                       cons.fp_console_setrow(&cons, cons.rows-i-1, bg_color);
+               cons.curr_row -= rows;
+       }
+       lcd_sync();
+}
+
+void console_calc_rowcol(struct console_t *pcons, u32 sizex, u32 sizey)
+{
+       pcons->cols = sizex / VIDEO_FONT_WIDTH;
+#if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO)
+       pcons->rows = (pcons->lcdsizey - BMP_LOGO_HEIGHT);
+       pcons->rows /= VIDEO_FONT_HEIGHT;
+#else
+       pcons->rows = sizey / VIDEO_FONT_HEIGHT;
+#endif
+}
+
+void __weak lcd_init_console_rot(struct console_t *pcons)
+{
+       return;
+}
+
+void lcd_init_console(void *address, int vl_cols, int vl_rows, int vl_rot)
+{
+       memset(&cons, 0, sizeof(cons));
+       cons.fbbase = address;
+
+       cons.lcdsizex = vl_cols;
+       cons.lcdsizey = vl_rows;
+       cons.lcdrot = vl_rot;
+
+       cons.fp_putc_xy = &lcd_putc_xy0;
+       cons.fp_console_moverow = &console_moverow0;
+       cons.fp_console_setrow = &console_setrow0;
+       console_calc_rowcol(&cons, cons.lcdsizex, cons.lcdsizey);
+
+       lcd_init_console_rot(&cons);
+
+       debug("lcd_console: have %d/%d col/rws on scr %dx%d (%d deg rotated)\n",
+             cons.cols, cons.rows, cons.lcdsizex, cons.lcdsizey, vl_rot);
+}
+
+void lcd_putc(const char c)
+{
+       if (!lcd_is_enabled) {
+               serial_putc(c);
+
+               return;
+       }
+
+       switch (c) {
+       case '\r':
+               cons.curr_col = 0;
+               return;
+       case '\n':
+               console_newline();
+
+               return;
+       case '\t':      /* Tab (8 chars alignment) */
+               cons.curr_col +=  8;
+               cons.curr_col &= ~7;
+
+               if (cons.curr_col >= cons.cols)
+                       console_newline();
+
+               return;
+       case '\b':
+               console_back();
+
+               return;
+       default:
+               cons.fp_putc_xy(&cons,
+                               cons.curr_col * VIDEO_FONT_WIDTH,
+                               cons.curr_row * VIDEO_FONT_HEIGHT, c);
+               if (++cons.curr_col >= cons.cols)
+                       console_newline();
+       }
+}
+
+void lcd_puts(const char *s)
+{
+       if (!lcd_is_enabled) {
+               serial_puts(s);
+
+               return;
+       }
+
+       while (*s)
+               lcd_putc(*s++);
+
+       lcd_sync();
+}
+
+void lcd_printf(const char *fmt, ...)
+{
+       va_list args;
+       char buf[CONFIG_SYS_PBSIZE];
+
+       va_start(args, fmt);
+       vsprintf(buf, fmt, args);
+       va_end(args);
+
+       lcd_puts(buf);
+}
+
+static int do_lcd_setcursor(struct cmd_tbl *cmdtp, int flag, int argc,
+                           char *const argv[])
+{
+       unsigned int col, row;
+
+       if (argc != 3)
+               return CMD_RET_USAGE;
+
+       col = dectoul(argv[1], NULL);
+       row = dectoul(argv[2], NULL);
+       lcd_position_cursor(col, row);
+
+       return 0;
+}
+
+static int do_lcd_puts(struct cmd_tbl *cmdtp, int flag, int argc,
+                      char *const argv[])
+{
+       if (argc != 2)
+               return CMD_RET_USAGE;
+
+       lcd_puts(argv[1]);
+
+       return 0;
+}
+
+U_BOOT_CMD(
+       setcurs, 3,     1,      do_lcd_setcursor,
+       "set cursor position within screen",
+       "    <col> <row> in character"
+);
+
+U_BOOT_CMD(
+       lcdputs, 2,     1,      do_lcd_puts,
+       "print string on lcd-framebuffer",
+       "    <string>"
+);
diff --git a/include/lcd_console.h b/include/lcd_console.h
new file mode 100644 (file)
index 0000000..e376718
--- /dev/null
@@ -0,0 +1,118 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2014, Compulab Ltd - http://compulab.co.il/
+ */
+
+/* By default we scroll by a single line */
+
+struct console_t {
+       short curr_col, curr_row;
+       short cols, rows;
+       void *fbbase;
+       u32 lcdsizex, lcdsizey, lcdrot;
+       void (*fp_putc_xy)(struct console_t *pcons, ushort x, ushort y, char c);
+       void (*fp_console_moverow)(struct console_t *pcons,
+                                  u32 rowdst, u32 rowsrc);
+       void (*fp_console_setrow)(struct console_t *pcons, u32 row, int clr);
+};
+
+/**
+ * console_calc_rowcol() - calculate available rows / columns wihtin a given
+ * screen-size based on used VIDEO_FONT.
+ *
+ * @pcons: Pointer to struct console_t
+ * @sizex: size X of the screen in pixel
+ * @sizey: size Y of the screen in pixel
+ */
+void console_calc_rowcol(struct console_t *pcons, u32 sizex, u32 sizey);
+/**
+ * lcd_init_console() - Initialize lcd console parameters
+ *
+ * Setup the address of console base, and the number of rows and columns the
+ * console has.
+ *
+ * @address: Console base address
+ * @vl_rows: Number of rows in the console
+ * @vl_cols: Number of columns in the console
+ * @vl_rot: Rotation of display in degree (0 - 90 - 180 - 270) counterlockwise
+ */
+void lcd_init_console(void *address, int vl_cols, int vl_rows, int vl_rot);
+/**
+ * lcd_set_col() - Set the number of the current lcd console column
+ *
+ * Set the number of the console column where the cursor is.
+ *
+ * @col: Column number
+ */
+void lcd_set_col(short col);
+
+/**
+ * lcd_set_row() - Set the number of the current lcd console row
+ *
+ * Set the number of the console row where the cursor is.
+ *
+ * @row: Row number
+ */
+void lcd_set_row(short row);
+
+/**
+ * lcd_position_cursor() - Position the cursor on the screen
+ *
+ * Position the cursor at the given coordinates on the screen.
+ *
+ * @col: Column number
+ * @row: Row number
+ */
+void lcd_position_cursor(unsigned col, unsigned row);
+
+/**
+ * lcd_get_screen_rows() - Get the total number of screen rows
+ *
+ * @return: Number of screen rows
+ */
+int lcd_get_screen_rows(void);
+
+/**
+ * lcd_get_screen_columns() - Get the total number of screen columns
+ *
+ * @return: Number of screen columns
+ */
+int lcd_get_screen_columns(void);
+
+/**
+ * lcd_putc() - Print to screen a single character at the location of the cursor
+ *
+ * @c: The character to print
+ */
+void lcd_putc(const char c);
+
+/**
+ * lcd_puts() - Print to screen a string at the location of the cursor
+ *
+ * @s: The string to print
+ */
+void lcd_puts(const char *s);
+
+/**
+ * lcd_printf() - Print to screen a formatted string at location of the cursor
+ *
+ * @fmt: The formatted string to print
+ * @...: The arguments for the formatted string
+ */
+void lcd_printf(const char *fmt, ...);
+
+/**
+ * Set the position of the text cursor
+ *
+ * @param col   Column to place cursor (0 = left side)
+ * @param row   Row to place cursor (0 = top line)
+ */
+void lcd_set_position_cursor(unsigned col, unsigned row);
+
+/**
+ * Get the position of the text cursor
+ *
+ * @param *col  Pointer to store cursor placement column (0 = left side)
+ * @param *row  Pointer to store cursor placement row (0 = top line)
+ */
+void lcd_get_position_cursor(unsigned *col, unsigned *row);