Skip to content
Snippets Groups Projects
Select Git revision
  • 1a32b8a72d88530133bc31eb19632f0b353408f8
  • master default protected
  • add-utime-unix_time-expansion
  • add-monotonic-time
  • add-digiclk
  • genofire/rockets-state
  • genofire/ble-follow-py
  • plaetzchen/ios-workaround
  • blinkisync-as-preload
  • genofire/haule-ble-fs-deactive
  • schneider/max30001-pycardium
  • schneider/max30001-epicaridum
  • schneider/max30001
  • schneider/stream-locks
  • schneider/fundamental-test
  • schneider/ble-buffers
  • schneider/maxim-sdk-update
  • ch3/splashscreen
  • koalo/bhi160-works-but-dirty
  • koalo/wip/i2c-for-python
  • renze/safe_mode
  • v1.9
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0
  • release-1
  • bootloader-v1
  • v0.0
34 results

gfx.c

Blame
  • Forked from card10 / firmware
    Source project has a limited visibility.
    gfx.c 7.46 KiB
    #include "gfx.h"
    #include "framebuffer.h"
    #include <math.h>
    #include <stddef.h>
    #include <stdlib.h>
    
    const struct gfx_color_rgb gfx_colors_rgb[COLORS] = {
    	{ 255, 255, 255 }, /* WHITE */
    	{ 0, 0, 0 },       /* BLACK */
    	{ 255, 0, 0 },     /* RED */
    	{ 0, 255, 0 },     /* GREEN */
    	{ 0, 0, 255 },     /* BLUE */
    	{ 255, 255, 0 }    /* YELLOW */
    };
    
    void gfx_setpixel(struct gfx_region *r, int x, int y, Color c)
    {
    	if (x < 0 || y < 0)
    		return;
    	if (x >= r->width || y >= r->height)
    		return;
    
    	fb_setpixel(r->fb, r->x + x, r->y + y, c);
    }
    
    struct gfx_region gfx_screen(struct framebuffer *fb)
    {
    	struct gfx_region r = { .fb     = fb,
    				.x      = 0,
    				.y      = 0,
    				.width  = fb->width,
    				.height = fb->height };
    	return r;
    }
    
    static inline int letter_bit(sFONT *font, char c, int x, int y)
    {
    	if (x < 0 || y < 0)
    		return 0;
    	if (x >= font->Width || y >= font->Height)
    		return 0;
    	if (c < ' ' || c > '~')
    		return 0;
    
    	size_t bytes_per_row      = font->Width / 8 + 1;
    	size_t bytes_per_letter   = bytes_per_row * font->Height;
    	int letter                = c - ' ';
    	const uint8_t *letter_ptr = font->table + bytes_per_letter * letter;
    	int horz_byte             = x / 8;
    	int horz_bit              = 7 - x % 8;
    
    	return (*(letter_ptr + y * bytes_per_row + horz_byte) >> horz_bit) & 1;
    }
    
    void gfx_putchar(
    	sFONT *font,
    	struct gfx_region *r,
    	int x,
    	int y,
    	char ch,
    	Color fg,
    	Color bg
    ) {
    	for (int yo = 0; yo < font->Height; yo++) {
    		for (int xo = 0; xo < font->Width; xo++) {
    			int lb = letter_bit(font, ch, xo, yo);
    
    			if (fg != bg) {
    				Color c = lb ? fg : bg;
    				gfx_setpixel(r, x + xo, y + yo, c);