Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | /*
* Copyright (c) 2018 PHYTEC Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <string.h>
#include <display/cfb.h>
#define LOG_LEVEL CONFIG_CFB_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(cfb);
extern const struct cfb_font __font_entry_start[];
extern const struct cfb_font __font_entry_end[];
static inline uint8_t byte_reverse(uint8_t b)
{
b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
return b;
}
struct char_framebuffer {
/** Pointer to a buffer in RAM */
uint8_t *buf;
/** Size of the framebuffer */
uint32_t size;
/** Pointer to the font entry array */
const struct cfb_font *fonts;
/** Display pixel format */
enum display_pixel_format pixel_format;
/** Display screen info */
enum display_screen_info screen_info;
/** Resolution of a framebuffer in pixels in X direction */
uint16_t x_res;
/** Resolution of a framebuffer in pixels in Y direction */
uint16_t y_res;
/** Number of pixels per tile, typically 8 */
uint8_t ppt;
/** Number of available fonts */
uint8_t numof_fonts;
/** Current font index */
uint8_t font_idx;
/** Font kerning */
int8_t kerning;
/** Invertedj*/
bool inverted;
};
static struct char_framebuffer char_fb;
static inline uint8_t *get_glyph_ptr(const struct cfb_font *fptr, char c)
{
if (fptr->caps & CFB_FONT_MONO_VPACKED) {
return (uint8_t *)fptr->data +
(c - fptr->first_char) *
(fptr->width * fptr->height / 8U);
}
return NULL;
}
/*
* Draw the monochrome character in the monochrome tiled framebuffer,
* a byte is interpreted as 8 pixels ordered vertically among each other.
*/
static uint8_t draw_char_vtmono(const struct char_framebuffer *fb,
char c, uint16_t x, uint16_t y)
{
const struct cfb_font *fptr = &(fb->fonts[fb->font_idx]);
uint8_t *glyph_ptr;
bool need_reverse = (((fb->screen_info & SCREEN_INFO_MONO_MSB_FIRST) != 0)
!= ((fptr->caps & CFB_FONT_MSB_FIRST) != 0));
if (c < fptr->first_char || c > fptr->last_char) {
c = ' ';
}
glyph_ptr = get_glyph_ptr(fptr, c);
if (!glyph_ptr) {
return 0;
}
for (size_t g_x = 0; g_x < fptr->width; g_x++) {
uint32_t y_segment = y / 8U;
for (size_t g_y = 0; g_y < fptr->height / 8U; g_y++) {
uint32_t fb_y = (y_segment + g_y) * fb->x_res;
uint8_t byte;
if ((fb_y + x + g_x) >= fb->size) {
return 0;
}
byte = glyph_ptr[g_x * (fptr->height / 8U) + g_y];
if (need_reverse) {
byte = byte_reverse(byte);
}
fb->buf[fb_y + x + g_x] = byte;
}
}
return fptr->width;
}
int cfb_print(const struct device *dev, char *str, uint16_t x, uint16_t y)
{
const struct char_framebuffer *fb = &char_fb;
const struct cfb_font *fptr;
if (!fb->fonts || !fb->buf) {
return -1;
}
fptr = &(fb->fonts[fb->font_idx]);
if (fptr->height % 8) {
LOG_ERR("Wrong font size");
return -1;
}
if ((fb->screen_info & SCREEN_INFO_MONO_VTILED) && !(y % 8)) {
for (size_t i = 0; i < strlen(str); i++) {
if (x + fptr->width > fb->x_res) {
x = 0U;
y += fptr->height;
}
x += fb->kerning + draw_char_vtmono(fb, str[i], x, y);
}
return 0;
}
LOG_ERR("Unsupported framebuffer configuration");
return -1;
}
static int cfb_invert(const struct char_framebuffer *fb)
{
for (size_t i = 0; i < fb->x_res * fb->y_res / 8U; i++) {
fb->buf[i] = ~fb->buf[i];
}
return 0;
}
int cfb_framebuffer_clear(const struct device *dev, bool clear_display)
{
const struct char_framebuffer *fb = &char_fb;
struct display_buffer_descriptor desc;
if (!fb || !fb->buf) {
return -1;
}
desc.buf_size = fb->size;
desc.width = fb->x_res;
desc.height = fb->y_res;
desc.pitch = fb->x_res;
memset(fb->buf, 0, fb->size);
return 0;
}
int cfb_framebuffer_invert(const struct device *dev)
{
struct char_framebuffer *fb = &char_fb;
if (!fb || !fb->buf) {
return -1;
}
fb->inverted = !fb->inverted;
return 0;
}
int cfb_framebuffer_finalize(const struct device *dev)
{
const struct display_driver_api *api = dev->api;
const struct char_framebuffer *fb = &char_fb;
struct display_buffer_descriptor desc;
if (!fb || !fb->buf) {
return -1;
}
desc.buf_size = fb->size;
desc.width = fb->x_res;
desc.height = fb->y_res;
desc.pitch = fb->x_res;
if (!(fb->pixel_format & PIXEL_FORMAT_MONO10) != !(fb->inverted)) {
cfb_invert(fb);
}
return api->write(dev, 0, 0, &desc, fb->buf);
}
int cfb_get_display_parameter(const struct device *dev,
enum cfb_display_param param)
{
const struct char_framebuffer *fb = &char_fb;
switch (param) {
case CFB_DISPLAY_HEIGH:
return fb->y_res;
case CFB_DISPLAY_WIDTH:
return fb->x_res;
case CFB_DISPLAY_PPT:
return fb->ppt;
case CFB_DISPLAY_ROWS:
if (fb->screen_info & SCREEN_INFO_MONO_VTILED) {
return fb->y_res / fb->ppt;
}
return fb->y_res;
case CFB_DISPLAY_COLS:
if (fb->screen_info & SCREEN_INFO_MONO_VTILED) {
return fb->x_res;
}
return fb->x_res / fb->ppt;
}
return 0;
}
int cfb_framebuffer_set_font(const struct device *dev, uint8_t idx)
{
struct char_framebuffer *fb = &char_fb;
if (idx >= fb->numof_fonts) {
return -1;
}
fb->font_idx = idx;
return 0;
}
int cfb_get_font_size(const struct device *dev, uint8_t idx, uint8_t *width,
uint8_t *height)
{
const struct char_framebuffer *fb = &char_fb;
if (idx >= fb->numof_fonts) {
return -1;
}
if (width) {
*width = __font_entry_start[idx].width;
}
if (height) {
*height = __font_entry_start[idx].height;
}
return 0;
}
int cfb_get_numof_fonts(const struct device *dev)
{
const struct char_framebuffer *fb = &char_fb;
return fb->numof_fonts;
}
int cfb_framebuffer_init(const struct device *dev)
{
const struct display_driver_api *api = dev->api;
struct char_framebuffer *fb = &char_fb;
struct display_capabilities cfg;
api->get_capabilities(dev, &cfg);
fb->numof_fonts = __font_entry_end - __font_entry_start;
LOG_DBG("number of fonts %d", fb->numof_fonts);
if (!fb->numof_fonts) {
return -1;
}
fb->x_res = cfg.x_resolution;
fb->y_res = cfg.y_resolution;
fb->ppt = 8U;
fb->pixel_format = cfg.current_pixel_format;
fb->screen_info = cfg.screen_info;
fb->buf = NULL;
fb->font_idx = 0U;
fb->kerning = 0;
fb->inverted = false;
fb->fonts = __font_entry_start;
fb->font_idx = 0U;
fb->size = fb->x_res * fb->y_res / fb->ppt;
fb->buf = k_malloc(fb->size);
if (!fb->buf) {
return -1;
}
memset(fb->buf, 0, fb->size);
return 0;
}
|