This commit is contained in:
kraus 2018-04-21 18:02:43 +02:00
parent e70a6b47c0
commit 326c7d1ccd
7 changed files with 291 additions and 27 deletions

View File

@ -1096,6 +1096,9 @@ void u8g2_SetFontRefHeightText(u8g2_t *u8g2);
void u8g2_SetFontRefHeightExtendedText(u8g2_t *u8g2);
void u8g2_SetFontRefHeightAll(u8g2_t *u8g2);
/*==========================================*/
/* u8log_u8g2.c */
void u8log_u8g2_cb(u8log_t * u8log);
/*==========================================*/

View File

@ -3,35 +3,41 @@
u8log.c
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2018, olikraus@gmail.com
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "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 COPYRIGHT HOLDER OR
CONTRIBUTORS 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.
*/
#include <stdint.h>
#include <string.h>
#include "u8x8.h"
typedef struct u8log_struct u8log_t;
/* redraw the specified line. */
typedef void (*u8log_cb)(u8log_t * u8log);
struct u8log_struct
{
/* configuration */
void *aux_data; /* pointer to u8x8 or u8g2 */
uint8_t width, height; /* size of the terminal */
u8log_cb cb; /* callback redraw function */
uint8_t *screen_buffer; /* size must be width*heigh bytes */
uint8_t is_redraw_line_for_each_char;
/* internal data */
//uint8_t last_x, last_y; /* position of the last printed char */
uint8_t cursor_x, cursor_y; /* position of the cursor, might be off screen */
uint8_t redraw_line; /* redraw specific line if is_redraw_line is not 0 */
uint8_t is_redraw_line;
uint8_t is_redraw_all;
};
typedef struct u8log_struct u8log_t;
/*
static uint8_t u8log_is_on_screen(u8log_t *u8log, uint8_t x, uint8_t y)
@ -77,7 +83,9 @@ static void u8log_scroll_up(u8log_t *u8log)
*dest++ = ' ';
cnt--;
} while(cnt > 0);
u8log->is_redraw_all = 1;
if ( u8log->is_redraw_line_for_each_char )
u8log->is_redraw_all = 1;
}
/*
@ -152,13 +160,14 @@ void u8log_write_char(u8log_t *u8log, uint8_t c)
}
}
void u8log_Init(u8log_t *u8log, uint8_t width, uint8_t height, uint8_t *buf, u8log_cb cb)
void u8log_Init(u8log_t *u8log, uint8_t width, uint8_t height, uint8_t *buf, u8log_cb cb, void *aux_data)
{
memset(u8log, 0, sizeof(u8log_t));
u8log->width = width;
u8log->height = height;
u8log->screen_buffer = buf;
u8log->cb = cb;
u8log->aux_data = aux_data;
u8log_clear_screen(u8log);
}
@ -167,12 +176,23 @@ void u8log_SetRedrawMode(u8log_t *u8log, uint8_t is_redraw_line_for_each_char)
u8log->is_redraw_line_for_each_char = is_redraw_line_for_each_char;
}
/* offset can be negative or positive, it is 0 by default */
void u8log_SetLineHeightOffset(u8log_t *u8log, int8_t line_height_offset)
{
u8log->line_height_offset = line_height_offset;
}
void u8log_WriteChar(u8log_t *u8log, uint8_t c)
{
u8log_write_char(u8log, c);
if ( u8log->is_redraw_line || u8log->is_redraw_all )
{
u8log->cb(u8log);
if ( u8log->cb != 0 )
{
u8log->cb(u8log);
}
u8log->is_redraw_line = 0;
u8log->is_redraw_all = 0;
}

99
csrc/u8log_u8g2.c Normal file
View File

@ -0,0 +1,99 @@
/*
u8log_u8g2.c
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2018, olikraus@gmail.com
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "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 COPYRIGHT HOLDER OR
CONTRIBUTORS 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.
*/
#include "u8g2.h"
/*
Draw the u8log text at the specified x/y position.
x/y position is the reference position of the first char of the first line.
the line height is
u8g2_GetAscent(u8g2) - u8g2_GetDescent(u8g2) + line_height_offset;
line_height_offset can be set with u8log_SetLineHeightOffset()
Use
u8g2_SetFontRefHeightText(u8g2_t *u8g2);
u8g2_SetFontRefHeightExtendedText(u8g2_t *u8g2);
u8g2_SetFontRefHeightAll(u8g2_t *u8g2);
to change the return values for u8g2_GetAscent and u8g2_GetDescent
*/
void u8g2_DrawU8log(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8log_t *u8log)
{
u8g2_uint_t disp_x, disp_y;
uint8_t buf_x, buf_y;
uint8_t c;
disp_y = y;
if ( u8log->is_redraw_line || u8log->is_redraw_all )
{
u8g2_SetFontDirection(u8g2, 0);
u8g2_FirstPage(u8g2);
do
{
disp_y = u8g2_GetAscent(u8g2);
for( buf_y = 0; buf_y < u8log->height; buf_y++ )
{
disp_x = x;
for( buf_x = 0; buf_x < u8log->width; buf_x++ )
{
c = u8log->screen_buffer[buf_y * u8log->width + buf_x];
disp_x += u8g2_DrawGlyph(u8g2, disp_x, disp_y, c);
}
disp_y += u8g2_GetAscent(u8g2) - u8g2_GetDescent(u8g2);
disp_y += u8log->line_height_offset;
}
}
while( u8g2_NextPage(u8g2) );
}
}
/*
u8lib callback for u8g2
Only font direction 0 is supported: u8g2_SetFontDirection(u8g2, 0)
Use
u8g2_SetFontRefHeightText(u8g2_t *u8g2);
u8g2_SetFontRefHeightExtendedText(u8g2_t *u8g2);
u8g2_SetFontRefHeightAll(u8g2_t *u8g2);
to change the top offset and the line height and
u8log_SetLineHeightOffset(u8log_t *u8log, int8_t line_height_offset)
to change the line height.
*/
void u8log_u8g2_cb(u8log_t * u8log)
{
u8g2_t *u8g2 = (u8g2_t *)(u8log->aux_data);
u8g2_DrawU8log( u8g2, 0, u8g2_GetAscent(u8g2), u8log);
}

37
csrc/u8log_u8x8.c Normal file
View File

@ -0,0 +1,37 @@
/*
u8log_u8x8.c
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2018, olikraus@gmail.com
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "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 COPYRIGHT HOLDER OR
CONTRIBUTORS 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.
*/
#include "u8x8.h"

View File

@ -372,6 +372,35 @@ struct u8x8_struct
#endif
/*==========================================*/
/* u8log extension for u8x8 and u8g2 */
typedef struct u8log_struct u8log_t;
/* redraw the specified line. */
typedef void (*u8log_cb)(u8log_t * u8log);
struct u8log_struct
{
/* configuration */
void *aux_data; /* pointer to u8x8 or u8g2 */
uint8_t width, height; /* size of the terminal */
u8log_cb cb; /* callback redraw function */
uint8_t *screen_buffer; /* size must be width*heigh bytes */
uint8_t is_redraw_line_for_each_char;
int8_t line_height_offset; /* extra offset for the line height (u8g2 only) */
/* internal data */
//uint8_t last_x, last_y; /* position of the last printed char */
uint8_t cursor_x, cursor_y; /* position of the cursor, might be off screen */
uint8_t redraw_line; /* redraw specific line if is_redraw_line is not 0 */
uint8_t is_redraw_line;
uint8_t is_redraw_all;
};
typedef struct u8log_struct u8log_t;
/*==========================================*/
/* helper functions */
@ -924,6 +953,14 @@ uint8_t u8x8_UserInterfaceMessage(u8x8_t *u8x8, const char *title1, const char *
uint8_t u8x8_UserInterfaceInputValue(u8x8_t *u8x8, const char *title, const char *pre, uint8_t *value, uint8_t lo, uint8_t hi, uint8_t digits, const char *post);
/*==========================================*/
/* u8log.c */
void u8log_Init(u8log_t *u8log, uint8_t width, uint8_t height, uint8_t *buf, u8log_cb cb, void *aux_data);
void u8log_SetRedrawMode(u8log_t *u8log, uint8_t is_redraw_line_for_each_char);
void u8log_SetLineHeightOffset(u8log_t *u8log, int8_t line_height_offset);
void u8log_WriteChar(u8log_t *u8log, uint8_t c);
/*==========================================*/
/* start font list */

View File

@ -0,0 +1,12 @@
CFLAGS = -g -Wall -I../../../csrc/. `sdl2-config --cflags` -fsanitize=address -fsanitize=undefined -fsanitize=bounds-strict -fsanitize=object-size
SRC = $(shell ls ../../../csrc/*.c) ../../bitmap/common/u8x8_d_bitmap.c $(shell ls ../common/*.c ) main.c
OBJ = $(SRC:.c=.o)
helloworld: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) `sdl2-config --libs` -o u8g2_sdl
clean:
-rm $(OBJ) u8g2_sdl

56
sys/sdl/u8log_u8g2/main.c Normal file
View File

@ -0,0 +1,56 @@
#include "u8g2.h"
#include <stdio.h>
#include <time.h>
u8g2_t u8g2;
u8log_t u8log;
#define U8LOG_WIDTH 10
#define U8LOG_HEIGHT 4
uint8_t u8log_buf[U8LOG_WIDTH*U8LOG_HEIGHT];
int main(void)
{
int k;
clock_t t;
uint8_t c;
u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
u8x8_ConnectBitmapToU8x8(u8g2_GetU8x8(&u8g2)); /* connect to bitmap */
u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
u8g2_SetFont(&u8g2, u8g2_font_helvB12_tr);
u8log_Init(&u8log, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buf, u8log_u8g2_cb, &u8g2);
u8log_SetRedrawMode(&u8log, /* is_redraw_line_for_each_char = */ 1);
//u8log_SetRedrawMode(&u8log, /* is_redraw_line_for_each_char = */ 0);
u8log_SetLineHeightOffset(&u8log, -3); /* decrese line spacing, u8g2 only */
u8g2_SetFontRefHeightExtendedText(&u8g2); /* increase distance from top and line height */
u8g2_SetFontRefHeightAll(&u8g2); /* increase distance from top and line height */
t = clock()+CLOCKS_PER_SEC/10;
for(;;)
{
if ( t < clock() )
{
c = (t % 10) + '0';
if ( c == '0' )
c = '\n';
u8log_WriteChar(&u8log, c);
t = clock()+CLOCKS_PER_SEC/10;
}
k = u8g_sdl_get_key();
if ( k == 'q' ) break;
if ( k == 't' )
u8x8_SaveBitmapTGA(u8g2_GetU8x8(&u8g2), "screenshot.tga");
}
return 0;
}