Fixed FPS example, font functions moved to extras/fonts, added

possibility of using non-builtin fonts
This commit is contained in:
UncleRus 2016-12-11 19:03:16 +05:00
parent dd546223bd
commit 75352783bb
10 changed files with 203 additions and 9888 deletions

View file

@ -53,10 +53,15 @@ static const ssd1306_t dev = {
/* Local frame buffer */
static uint8_t buffer[DISPLAY_WIDTH * DISPLAY_HEIGHT / 8];
TimerHandle_t timerHandle = 0; // Timer handler
TimerHandle_t fps_timer_handle = NULL; // Timer handler
TimerHandle_t font_timer_handle = NULL;
uint8_t frame_done = 0; // number of frame send.
uint8_t fps = 0; // image per second.
const font_info_t *font = NULL; // current font
font_face_t font_face = 0;
#define SECOND (1000 / portTICK_PERIOD_MS)
static void ssd1306_task(void *pvParameters)
@ -72,23 +77,15 @@ static void ssd1306_task(void *pvParameters)
uint8_t x1 = LOAD_ICON_X + LOAD_ICON_SIZE;
uint8_t y1 = LOAD_ICON_Y + LOAD_ICON_SIZE;
uint16_t count = 0;
uint8_t font = 0;
//ssd1306_select_font(FONT_FACE_TERMINUS_BOLD_8X14_KOI8_R);
while (1) {
if (!(count % 200))
while (true)
{
if (font++ >= FONT_FACE_MAX) font = 0;
if (!ssd1306_select_font(font)) break;
}
ssd1306_draw_string(&dev, buffer, font, 0, 0, "Hello, esp-open-rtos!", OLED_COLOR_WHITE, OLED_COLOR_BLACK);
sprintf(text, "FPS: %u ", fps);
ssd1306_draw_string(&dev, buffer, font_builtin_fonts[FONT_FACE_TERMINUS_6X12_KOI8_R],
0, 40, text, OLED_COLOR_WHITE, OLED_COLOR_BLACK);
ssd1306_draw_string(&dev, buffer, 10, 10,"Hello, esp-open-rtos !", OLED_COLOR_WHITE, OLED_COLOR_BLACK);
sprintf(text,"FPS: %u",fps);
ssd1306_draw_string(&dev, buffer, 10, 40, text, OLED_COLOR_WHITE, OLED_COLOR_BLACK);
//generate loading icon
// generate loading icon
ssd1306_draw_line(&dev, buffer, x0, y0, x1, y1, OLED_COLOR_BLACK);
if (x0 < (LOAD_ICON_X + LOAD_ICON_SIZE)) {
x0++;
@ -108,7 +105,7 @@ static void ssd1306_task(void *pvParameters)
ssd1306_draw_rectangle(&dev, buffer, LOAD_ICON_X, LOAD_ICON_Y,
LOAD_ICON_SIZE + 1, LOAD_ICON_SIZE + 1, OLED_COLOR_WHITE);
//generate circle counting
// generate circle counting
for (uint8_t i = 0; i < 10; i++) {
if ((count >> i) & 0x01)
ssd1306_draw_circle(&dev, buffer, CIRCLE_COUNT_ICON_X, CIRCLE_COUNT_ICON_Y, i, OLED_COLOR_BLACK);
@ -135,12 +132,23 @@ error_loop:
}
}
void SoftTimer(TimerHandle_t xTimer)
void fps_timer(TimerHandle_t h)
{
fps = frame_done; // Save number of frame already send to screen
frame_done = 0;
}
void font_timer(TimerHandle_t h)
{
do {
if (++font_face >= font_builtin_fonts_count)
font_face = 0;
font = font_builtin_fonts[font_face];
} while (!font);
printf("Selected builtin font %d\n", font_face);
}
void user_init(void)
{
//uncomment to test with CPU overclocked
@ -162,9 +170,14 @@ void user_init(void)
ssd1306_set_whole_display_lighting(&dev, true);
vTaskDelay(SECOND);
font = font_builtin_fonts[font_face];
// Create user interface task
xTaskCreate(ssd1306_task, "ssd1306_task", 256, NULL, 2, NULL);
timerHandle = xTimerCreate("Timer", SECOND, pdTRUE, NULL, SoftTimer);
xTimerStart(timerHandle, 0);
fps_timer_handle = xTimerCreate("fps_timer", SECOND, pdTRUE, NULL, fps_timer);
xTimerStart(fps_timer_handle, 0);
font_timer_handle = xTimerCreate("font_timer", 5 * SECOND, pdTRUE, NULL, font_timer);
xTimerStart(font_timer_handle, 0);
}

97
extras/fonts/OFL.txt Normal file
View file

@ -0,0 +1,97 @@
Copyright (c) <dates>, <Copyright Holder> (<URL|email>),
with Reserved Font Name <Reserved Font Name>.
Copyright (c) <dates>, <additional Copyright Holder> (<URL|email>),
with Reserved Font Name <additional Reserved Font Name>.
Copyright (c) <dates>, <additional Copyright Holder> (<URL|email>).
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

View file

@ -26,14 +26,6 @@
#define FONTS_TERMINUS_BOLD_8X14_KOI8_R 1
#endif
#ifndef FONTS_TERMINUS_8X16_KOI8_R
#define FONTS_TERMINUS_8X16_KOI8_R 1
#endif
#ifndef FONTS_TERMINUS_BOLD_8X16_KOI8_R
#define FONTS_TERMINUS_BOLD_8X16_KOI8_R 1
#endif
#ifndef FONTS_TERMINUS_14X28_KOI8_R
#define FONTS_TERMINUS_14X28_KOI8_R 1
#endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -3,5 +3,15 @@
#########################################
FONTS_GLCD_5X7 ?= 1
FONTS_ROBOTO_8PT ?= 0
FONTS_ROBOTO_10PT ?= 0
# Terminus, KOI8-R
FONTS_TERMINUS_6X12_KOI8_R ?= 1
FONTS_TERMINUS_8X14_KOI8_R ?= 1
FONTS_TERMINUS_BOLD_8X14_KOI8_R ?= 1
FONTS_TERMINUS_14X28_KOI8_R ?= 1
FONTS_TERMINUS_BOLD_14X28_KOI8_R ?= 1
FONTS_TERMINUS_16X32_KOI8_R ?= 1
FONTS_TERMINUS_BOLD_16X32_KOI8_R ?= 1

View file

@ -1,7 +1,9 @@
/*
* fonts.c
/**
* LCD/OLED fonts library
*
* Created on: 8 dec. 2016
* FIXME: License?
*
* @date: 8 dec. 2016
* Author: zaltora
*/
#include "fonts.h"
@ -44,7 +46,9 @@
/////////////////////////////////////////////
const font_info_t *fonts[] =
// FIXME: this declaration is noisy
const font_info_t *font_builtin_fonts[] =
{
#if FONTS_GLCD_5X7
[FONT_FACE_GLCD5x7] = &_fonts_glcd_5x7_info,
@ -103,4 +107,23 @@ const font_info_t *fonts[] =
#endif
};
const size_t fonts_count = (sizeof(fonts) / sizeof(font_info_t *));
const size_t font_builtin_fonts_count = (sizeof(font_builtin_fonts) / sizeof(font_info_t *));
/////////////////////////////////////////////
uint16_t font_measure_string(const font_info_t *fnt, const char *s)
{
if (!s || !fnt) return 0;
uint16_t res = 0;
while (s)
{
font_char_desc_t *d = font_get_char_desc(fnt, *s);
if (d)
res += d->width + fnt->c;
s++;
}
return res > 0 ? res - fnt->c : 0;
}

View file

@ -25,16 +25,12 @@ typedef enum
FONT_FACE_TERMINUS_6X12_KOI8_R,
FONT_FACE_TERMINUS_8X14_KOI8_R,
FONT_FACE_TERMINUS_BOLD_8X14_KOI8_R,
FONT_FACE_TERMINUS_8X16_KOI8_R,
FONT_FACE_TERMINUS_BOLD_8X16_KOI8_R,
FONT_FACE_TERMINUS_14X28_KOI8_R,
FONT_FACE_TERMINUS_BOLD_14X28_KOI8_R,
FONT_FACE_TERMINUS_16X32_KOI8_R,
FONT_FACE_TERMINUS_BOLD_16X32_KOI8_R,
} font_face_t;
#define FONT_FACE_MAX FONT_FACE_TERMINUS_BOLD_16X32_KOI8_R
/**
* Character descriptor
*/
@ -60,8 +56,29 @@ typedef struct _font_info
/**
* Built-in fonts
*/
extern const font_info_t *fonts[];
extern const size_t fonts_count;
extern const font_info_t *font_builtin_fonts[];
extern const size_t font_builtin_fonts_count;
/**
* Find character decriptor in font
* @param fnt Poniter to font information struct
* @param c Character
* @return Character descriptor or NULL if no character found
*/
inline const font_char_desc_t *font_get_char_desc(const font_info_t *fnt, char c)
{
return c < fnt->char_start || c > fnt->char_end
? NULL
: fnt->char_descriptors + c - fnt->char_start;
}
/**
* Calculate width of string in pixels
* @param fnt Poniter to font information struct
* @param s String
* @return String width
*/
uint16_t font_measure_string(const font_info_t *fnt, const char *s);
#ifdef __cplusplus
}

View file

@ -71,8 +71,6 @@
#define abs(x) ((x)<0 ? -(x) : (x))
#define swap(x, y) do { typeof(x) temp##x##y = x; x = y; y = temp##x##y; } while (0)
static const font_info_t *font; // save font selection
/* Issue a command to SSD1306 device
* I2C proto format:
* |S|Slave Address|W|ACK|0x00|Command|Ack|P|
@ -877,18 +875,7 @@ int ssd1306_fill_triangle(const ssd1306_t *dev, uint8_t *fb, int16_t x0, int16_t
return 0 ;
}
int ssd1306_select_font(font_face_t font_face)
{
if (font_face < fonts_count)
font = fonts[font_face];
else
return -EINVAL;
if (!font)
return -EINVAL;
return 0;
}
uint8_t ssd1306_draw_char(const ssd1306_t *dev, uint8_t *fb, uint8_t x, uint8_t y, char c, ssd1306_color_t foreground, ssd1306_color_t background)
uint8_t ssd1306_draw_char(const ssd1306_t *dev, uint8_t *fb, const font_info_t *font, uint8_t x, uint8_t y, char c, ssd1306_color_t foreground, ssd1306_color_t background)
{
uint8_t i, j;
const uint8_t *bitmap;
@ -897,18 +884,18 @@ uint8_t ssd1306_draw_char(const ssd1306_t *dev, uint8_t *fb, uint8_t x, uint8_t
if (font == NULL)
return 0;
// we always have space in the font set
if ((c < font->char_start) || (c > font->char_end))
c = ' ';
c = c - font->char_start; // c now become index to tables
bitmap = font->bitmap + font->char_descriptors[(unsigned char)c].offset;
const font_char_desc_t *d = font_get_char_desc(font, c);
if (d == NULL)
return 0;
bitmap = font->bitmap + d->offset;
for (j = 0; j < font->height; ++j)
{
for (i = 0; i < font->char_descriptors[(unsigned char)c].width; ++i)
for (i = 0; i < d->width; ++i)
{
if (i % 8 == 0)
{
line = bitmap[(font->char_descriptors[(unsigned char)c].width + 7) / 8 * j + i / 8]; // line data
line = bitmap[(d->width + 7) / 8 * j + i / 8]; // line data
}
if (line & 0x80)
{
@ -933,21 +920,19 @@ uint8_t ssd1306_draw_char(const ssd1306_t *dev, uint8_t *fb, uint8_t x, uint8_t
line = line << 1;
}
}
return (font->char_descriptors[(unsigned char)c].width);
return d->width;
}
uint8_t ssd1306_draw_string(const ssd1306_t *dev, uint8_t *fb, uint8_t x, uint8_t y, char *str, ssd1306_color_t foreground, ssd1306_color_t background)
uint8_t ssd1306_draw_string(const ssd1306_t *dev, uint8_t *fb, const font_info_t *font, uint8_t x, uint8_t y, char *str, ssd1306_color_t foreground, ssd1306_color_t background)
{
uint8_t t = x;
if (font == NULL)
return 0;
if (str == NULL)
if (font == NULL || str == NULL)
return 0;
while (*str)
{
x += ssd1306_draw_char(dev, fb, x, y, *str, foreground, background);
x += ssd1306_draw_char(dev, fb, font, x, y, *str, foreground, background);
++str;
if (*str)
x += font->c;
@ -955,41 +940,3 @@ uint8_t ssd1306_draw_string(const ssd1306_t *dev, uint8_t *fb, uint8_t x, uint8_
}
return (x - t);
}
uint8_t ssd1306_get_font_height()
{
if (font == NULL)
return 0;
return (font->height);
}
uint8_t ssd1306_get_font_c()
{
if (font == NULL)
return 0;
return (font->c);
}
uint8_t ssd1306_measure_string(char *str)
{
uint8_t w = 0;
char c;
if (font == NULL)
return 0;
while (*str)
{
c = *str;
// we always have space in the font set
if ((c < font->char_start) || (c > font->char_end))
c = ' ';
c = c - font->char_start; // c now become index to tables
w += font->char_descriptors[(unsigned char)c].width;
++str;
if (*str)
w += font->c;
}
return w;
}

View file

@ -414,25 +414,6 @@ int ssd1306_draw_triangle(const ssd1306_t *dev, uint8_t *fb, int16_t x0, int16_t
*/
int ssd1306_fill_triangle(const ssd1306_t *dev, uint8_t *fb, int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, ssd1306_color_t color);
/**
* Select the font
* @param font_face id value of the font
* @return Non-zero if error occured
*/
int ssd1306_select_font(font_face_t font_face);
/**
* Get the height of current selected font
* @return Height of the font (in pixels) or 0 if none font selected
*/
uint8_t ssd1306_get_font_height();
/**
* Get the "C" value (space between adjacent characters) of current selected font
* @return "C" value
*/
uint8_t ssd1306_get_font_c();
/**
* Draw one character using currently selected font
* @param dev Pointer to device descriptor
@ -444,7 +425,7 @@ uint8_t ssd1306_get_font_c();
* @param background Background color
* @return Width of the character
*/
uint8_t ssd1306_draw_char(const ssd1306_t *dev, uint8_t *fb, uint8_t x, uint8_t y, char c, ssd1306_color_t foreground, ssd1306_color_t background);
uint8_t ssd1306_draw_char(const ssd1306_t *dev, uint8_t *fb, const font_info_t *fnt, uint8_t x, uint8_t y, char c, ssd1306_color_t foreground, ssd1306_color_t background);
/**
* Draw one character using currently selected font
@ -457,14 +438,7 @@ uint8_t ssd1306_draw_char(const ssd1306_t *dev, uint8_t *fb, uint8_t x, uint8_t
* @param background Background color
* @return Width of the string (out-of-display pixels also included)
*/
uint8_t ssd1306_draw_string(const ssd1306_t *dev, uint8_t *fb, uint8_t x, uint8_t y, char *str, ssd1306_color_t foreground, ssd1306_color_t background);
/**
* Measure width of string with current selected font
* @param str String to measure
* @return Width of the string
*/
uint8_t ssd1306_measure_string(char *str);
uint8_t ssd1306_draw_string(const ssd1306_t *dev, uint8_t *fb, const font_info_t *fnt, uint8_t x, uint8_t y, char *str, ssd1306_color_t foreground, ssd1306_color_t background);
#ifdef __cplusplus
extern "C"