• Stars
    star
    280
  • Rank 141,924 (Top 3 %)
  • Language
    C
  • License
    MIT License
  • Created about 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hardware Agnostic Graphics Library for embedded

Hardware Agnostic Graphics Library

HAGL is a lightweight hardware agnostics graphics library. It supports basic geometric primitives, bitmaps, blitting, fixed width fonts. Library tries to stay lightweight but targets reasonably powerful microchips such as ESP32. There is no dynamic allocation.

This can still be considered work in progress. API should be 90% stable.

Software License

Old school demo effects

Backend

To use HAGL you must provide a backend. The backend must provide atleast a function for putting a pixel. If nothing else is provided all higher level graphical functions will use this function to draw the primitives. While proper documentation is lacking see the example backend implementations for GD, SDL2, ESP-IDF (Ilitek, Sitronix, Galaxycore), ESP-IDF (Solomon), Nuclei RISC-V SDK, Raspberry Pi Pico SDK and Raspberry Pi Pico VGA board.

Usage

High level functions are pretty self explanatory. For example applications see Pico Effects, ESP Effects, SDL2 Effects, ESP GFX, and GD32V Effects.

Lifecycle

Before you start drawing you should call hagl_init(). Some HAL configurations require you to call hagl_flush() to update the contents of the screen. Before exiting your program it is good idea to call hagl_close()to clean things up.

#include <hagl_hal.h>
#include <hagl.h>

hagl_backend_t *display = hagl_init();

/* Main loop. */
while (1) {
    hagl_clear(display);
    hagl_load_image(display, 0, 0, "/sdcard/hello.jpg");
    hagl_flush(display);
};

hagl_close(display);

Colors

HAL defines what kind of pixel format is used. Most common is RGB565 which is represented by two bytes. If you are sure you will be using only RGB565 colors you could use the following shortcut to create a random color.

hagl_color_t color = rand() % 0xffff;

To write portable code which can be run with different pixel formats use the following instead.

uint8_t r = rand() % 255;
uint8_t g = rand() % 255;
uint8_t b = rand() % 255;
hagl_color_t color = hagl_color(display, r, g, b);

Put a pixel

for (uint32_t i = 1; i < 100000; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    hagl_color_t color = rand() % 0xffff;

    hagl_put_pixel(display, x0, y0, color);
}

Random pixels

Get a pixel

int16_t x0 = rand() % display->width;
int16_t y0 = rand() % display->height;

hagl_color_t pixel = hagl_get_pixel(display, x0, y0);

Note that if requesting coordinates outside the clip window color black is returned. This behaviour is unoptimal and might change in the future.

Draw a line

for (uint16_t i = 1; i < 1000; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    int16_t x1 = rand() % display->width;
    int16_t y1 = rand() % display->height;
    hagl_color_t color = rand() % 0xffff;

    hagl_draw_line(display, x0, y0, x1, y1, color);
}

Random lines

Draw a horizontal line

for (uint16_t i = 1; i < 1000; i++) {
    int16_t x0 = rand() % (display->width / 2);
    int16_t y0 = rand() % display->height;
    int16_t x1 = rand() % (display->width / 2);
    int16_t width = rand() % (display->width - x0);
    hagl_color_t color = rand() % 0xffff;

    /* First two are aliases. */
    hagl_draw_hline(display, x0, y0, width, color);
    hagl_draw_hline_xyw(display, x0, y0, width, color);

    hagl_draw_hline_xyx(display, x0, y0, x1, color);
}

Random horizontal lines

Draw a vertical line

for (uint16_t i = 1; i < 1000; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % (display->height / 2);
    int16_t y1 = rand() % (display->height / 2);
    int16_t height = rand() % (display->height - y0);
    hagl_color_t color = rand() % 0xffff;

    /* First two are aliases. */
    hagl_draw_vline(display, x0, y0, height, color);
    hagl_draw_vline_xyh(display, x0, y0, height, color);

    hagl_draw_vline_xyy(display, x0, y0, y1, color);
}

Random vertical lines

Draw a circle

for (uint16_t i = 1; i < 500; i++) {
    int16_t x0 = display->width / 2;
    int16_t y0 = display->height / 2;
    int16_t radius = rand() % display->width;
    hagl_color_t color = rand() % 0xffff;

    hagl_draw_circle(display, x0, y0, radius, color);
}

Random circle

Draw a filled circle

for (uint16_t i = 1; i < 500; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    int16_t radius = rand() % 100;
    hagl_color_t color = rand() % 0xffff;

    hagl_fill_circle(display, x0, y0, radius, color);
}

Random filled circle

Draw an ellipse

for (uint16_t i = 1; i < 500; i++) {
    int16_t x0 = display->width / 2;
    int16_t y0 = display->height / 2;
    int16_t rx = rand() % display->width;
    int16_t ry = rand() % display->height;
    hagl_color_t color = rand() % 0xffff;

    hagl_draw_ellipse(display, x0, y0, rx, ry, color);
}

Random ellipse

Draw a filled ellipse

for (uint16_t i = 1; i < 500; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    int16_t rx = rand() % display->width / 4;
    int16_t ry = rand() % display->height / 4;
    hagl_color_t color = rand() % 0xffff;

    hagl_fill_ellipse(display, x0, y0, rx, ry, color);
}

Random filled ellipse

Draw a triangle

int16_t x0 = rand() % display->width;
int16_t y0 = rand() % display->height;
int16_t x1 = rand() % display->width;
int16_t y1 = rand() % display->height;
int16_t x2 = rand() % display->width;
int16_t y2 = rand() % display->height;
hagl_color_t color = rand() % 0xffff;

hagl_draw_triangle(display, x0, y0, x1, y1, x2, y2, color);

Random triangle

Draw a filled triangle

int16_t x0 = rand() % display->width;
int16_t y0 = rand() % display->height;
int16_t x1 = rand() % display->width;
int16_t y1 = rand() % display->height;
int16_t x2 = rand() % display->width;
int16_t y2 = rand() % display->height;
hagl_color_t color = rand() % 0xffff;

hagl_fill_triangle(display, x0, y0, x1, y1, x2, y2, color);

Random filled triangle

Draw a rectangle

for (uint16_t i = 1; i < 50; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    int16_t x1 = rand() % display->width;
    int16_t y1 = rand() % display->height;
    int16_t w = rand() % display->width / 2;
    int16_t h = rand() % display->height / 2;
    hagl_color_t color = rand() % 0xffff;

    /* First two are aliases. */
    hagl_draw_rectangle(display, x0, y0, x1, y1, color);
    hagl_draw_rectangle_xyxy(display, x0, y0, x1, y1, color);

    hagl_draw_rectangle_xywh(display, x0, y0, w, h, color);
}

Random rectangle

Draw a filled rectangle

for (uint16_t i = 1; i < 10; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    int16_t x1 = rand() % display->width;
    int16_t y1 = rand() % display->height;
    int16_t w = rand() % display->width / 2;
    int16_t h = rand() % display->height / 2;
    hagl_color_t color = rand() % 0xffff;

    /* First two are aliases. */
    hagl_fill_rectangle(display, x0, y0, x1, y1, color);
    hagl_fill_rectangle_xyxy(display, x0, y0, x1, y1, color);

    hagl_fill_rectangle_xywh(display, x0, y0, w, h, color);
}

Random filled rectangle

Draw a rounded rectangle

for (uint16_t i = 1; i < 30; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    int16_t x1 = rand() % display->width;
    int16_t y1 = rand() % display->height;
    int16_t w = rand() % display->width / 2;
    int16_t h = rand() % display->height / 2;
    int16_t r = 10
    hagl_color_t color = rand() % 0xffff;

    /* First two are aliases. */
    hagl_draw_rounded_rectangle(display, x0, y0, x1, y1, r, color);
    hagl_draw_rounded_rectangle_xyxy(display, x0, y0, x1, y1, r, color);

    hagl_draw_rounded_rectangle_xyxy(display, x0, y0, w, h, r, color);
}

Random rounded rectangle

Draw a filled rounded rectangle

for (uint16_t i = 1; i < 30; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    int16_t x1 = rand() % display->width;
    int16_t y1 = rand() % display->height;
    int16_t w = rand() % display->width / 2;
    int16_t h = rand() % display->height / 2;
    int16_t r = 10
    hagl_color_t color = rand() % 0xffff;

    /* First two are aliases. */
    hagl_fill_rounded_rectangle(display, x0, y0, x1, y1, r, color);
    hagl_fill_rounded_rectangle_xyxy(display, x0, y0, x1, y1, r, color);

    hagl_fill_rounded_rectangle_xyxy(display, x0, y0, w, h, r, color);
}

Random filled rounded rectangle

Draw a polygon

You can draw polygons with unlimited number of vertices which are passed as an array. Pass the number of vertices as the first argument.

int16_t x0 = rand() % display->width;
int16_t y0 = rand() % display->height;
int16_t x1 = rand() % display->width;
int16_t y1 = rand() % display->height;
int16_t x2 = rand() % display->width;
int16_t y2 = rand() % display->height;
int16_t x3 = rand() % display->width;
int16_t y3 = rand() % display->height;
int16_t x4 = rand() % display->width;
int16_t y4 = rand() % display->height;
hagl_color_t color = rand() % 0xffff;
int16_t vertices[10] = {x0, y0, x1, y1, x2, y2, x3, y3, x4, y4};

hagl_draw_polygon(display, 5, vertices, color);

Random polygon

Draw a filled polygon

You can draw filled polygons with up to 64 vertices which are passed as an array. First argument is the number of vertices. Polygon does not have to be concave.

int16_t x0 = rand() % display->width;
int16_t y0 = rand() % display->height;
int16_t x1 = rand() % display->width;
int16_t y1 = rand() % display->height;
int16_t x2 = rand() % display->width;
int16_t y2 = rand() % display->height;
int16_t x3 = rand() % display->width;
int16_t y3 = rand() % display->height;
int16_t x4 = rand() % display->width;
int16_t y4 = rand() % display->height;
hagl_color_t color = rand() % 0xffff;
int16_t vertices[10] = {x0, y0, x1, y1, x2, y2, x3, y3, x4, y4};

hagl_fill_polygon(display, 5, vertices, color);

Random filled polygon

Put a single char

The library supports Unicode fonts in fontx format. It only includes three fonts by default. You can find more at tuupola/embedded-fonts and CHiPs44/fontx2-fonts repositories.

for (uint16_t i = 1; i < 10000; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    hagl_color_t color = rand() % 0xffff;
    char code = rand() % 255;

    hagl_put_char(display, code, x0, y0, color, font8x8);
}

Random chars

Put a string

The library supports Unicode fonts in fontx format. It only includes three fonts by default. You can find more at tuupola/embedded-fonts repository.

for (uint16_t i = 1; i < 10000; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    hagl_color_t color = rand() % 0xffff;

    hagl_put_text(display, u"YO! MTV raps.", x0, y0, color, font6x9);
}

Random strings

Blit a bitmap

Blit copies a bitmap to the screen. This example uses a glyph bitmap which is extracted from a font.

hagl_bitmap_t bitmap;
bitmap.buffer = (uint8_t *) malloc(6 * 9 * sizeof(hagl_color_t));

for (uint16_t i = 1; i < 20000; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    hagl_color_t color = rand() % 0xffff;
    uint16_t code = rand() % 0xffff;
    hagl_get_glyph(display, code, color, &bitmap, font6x9);

    /* These two are aliases. */
    hagl_blit(display, x0, y0, &bitmap);
    hagl_blit_xy(display, x0, y0, &bitmap);
}

Random blits

Blit a bitmap scaled up or down

Scale blit copies and scales a bitmap to the surface. This example uses a glyph bitmap which is extracted from a font.

hagl_bitmap_t bitmap;
bitmap.buffer = (uint8_t *) malloc(6 * 9 * sizeof(hagl_color_t));

for (uint16_t i = 1; i < 20000; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    hagl_color_t color = rand() % 0xffff;
    uint16_t code = rand() % 0xffff;
    hagl_get_glyph(display, code, color, &bitmap, font6x9);

    /* These two examples do the same thing. */
    hagl_blit_xywh(display, x0, y0, 24, 36, &bitmap);
    hagl_blit_xyxy(display, x0, y0, x0 + 23, y0 + 35, &bitmap);
}

Random blits

Clip window

You can restrict the area of drawing by setting a clip window.

hagl_set_clip(display, 0, 40, display->width, display->height - 40);

for (uint16_t i = 1; i < 500; i++) {
    int16_t x0 = rand() % display->width;
    int16_t y0 = rand() % display->height;
    int16_t radius = rand() % 100;
    hagl_color_t color = rand() % 0xffff;

    hagl_fill_circle(display, x0, y0, radius, color);
}

Clipped windows

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

lazyload

Vanilla JavaScript plugin for lazyloading images
JavaScript
8,761
star
2

slim-jwt-auth

PSR-7 and PSR-15 JWT Authentication Middleware
PHP
819
star
3

jquery_chained

Chained Selects for jQuery and Zepto
JavaScript
588
star
4

slim-basic-auth

PSR-7 and PSR-15 HTTP Basic Authentication Middleware
PHP
440
star
5

slim-api-skeleton

Slim 3 API skeleton project for Composer
PHP
312
star
6

jquery_viewport

Add viewport selectors to jQuery. For example $("img:below-the-fold").something()
HTML
258
star
7

branca-spec

Authenticated and encrypted API tokens using modern crypto
216
star
8

server-timing-middleware

PSR-7 & PSR-15 middleware to add the Server-Timing header
PHP
197
star
9

base62

Base62 encoder and decoder for arbitrary data
PHP
188
star
10

micropython-mpu9250

MicroPython I2C driver for MPU9250 9-axis motion tracking device
Python
135
star
11

cors-middleware

PSR-7 and PSR-15 CORS middleware
PHP
131
star
12

ksuid

K-Sortable Globally Unique IDs for PHP
PHP
101
star
13

branca-js

Authenticated encrypted API Tokens for JavaScript.
JavaScript
94
star
14

php_google_maps

API to help working with Google Static Maps.
PHP
84
star
15

avr_demo

Atmel demo code. Does not use any Arduino libraries. I want to learn this the hard way (tm).
Makefile
79
star
16

micropython-m5stack

MicroPython Kitchen Sink for M5Stack
Python
75
star
17

slim-image-resize

Image Resize Middleware for Slim Framework
PHP
61
star
18

branca-php

Authenticated and encrypted API tokens using modern crypto
PHP
54
star
19

base58

Base58 encoder and decoder for arbitrary data
PHP
50
star
20

hagl_esp_mipi

ESP32 MIPI DCS abstraction layer for the HAGL graphics library
C
47
star
21

pybranca

Authenticated Encrypted API Tokens for Python.
Python
46
star
22

esp_video

Proof of concept video player for ESP32 boards
C
45
star
23

instrument

PHP application instrumentation toolkit
PHP
44
star
24

hagl_pico_mipi

Raspberry Pi Pico MIPI DCS absraction layer for the HAGL graphics library
C
40
star
25

esp_effects

Old school demo effects for ESP32
C
38
star
26

slim-facebook

Boilerplate Facebook application with Slim, Eloquent and Facebook API.
JavaScript
34
star
27

whereami

Common PHP interface for wifi positioning services
PHP
33
star
28

demo_code

Miscallenous demo code.
JavaScript
32
star
29

axp192

Platform agnostic I2C driver for AXP192 power system management IC
C
30
star
30

esp_examples

The mandatory ESP-IDF (ESP32) examples repository
C
29
star
31

jquery_filestyle

jQuery plugin for styling file input elements.
JavaScript
28
star
32

base85

Base85 encoder and decoder for arbitrary data
PHP
27
star
33

jquery_dictionary

Spotlight searchable offline jQuery API documentation for OS X.
25
star
34

trilateration

Trilateration for PHP
PHP
24
star
35

pico_effects

Old school demo effects for Raspberry Pi Pico
C
21
star
36

witchcraft

Opionated PHP magic methods as traits for PHP 5.4+
PHP
21
star
37

pcf8563

Platform agnostic I2C driver for PCF8563 RTC
C
19
star
38

micropython-mpu6886

MicroPython I2C driver for MPU6886 6-axis motion tracking device
Python
19
star
39

micropython-ili934x

MicroPython SPI Driver for ILI934X Series Based TFT / LCD Displays
Python
17
star
40

wolf_assets

Mephisto style assets management for Wolf CMS. This plugin is currently unmaintained. Let me know if you want to take over.
PHP
17
star
41

esp_software_i2c

Software I2C driver for ESP-IDF (ESP32)
C
17
star
42

http-factory

Lightweight autodiscovering PSR-17 HTTP factories
PHP
17
star
43

flat-ui-theme

Flat UI theme for TextMate 2 and Sublime Text
15
star
44

embedded-fonts

FONTX fonts for embedded projects
C
14
star
45

sdl2_effects

Old school demo effects with HAGL and SDL2
C
14
star
46

base32

Base32 encoder and decoder for arbitrary data
PHP
13
star
47

php-docker-k8s

Examples how to run PHP with Docker and Kubernetes
PHP
13
star
48

slim-todo-backend

Slim 3 + Spot example for todobackend.com
PHP
11
star
49

slim-skeleton

Slim 3 + Datamapper + Monolog + Plates project for Composer
PHP
11
star
50

instrument-middleware

PSR-7 Middleware for instrumenting PHP applications
PHP
11
star
51

callable-handler

Compatibility layer between PSR-7 double pass and PSR-15 middlewares
PHP
10
star
52

branca-middleware

PSR-7 and PSR-15 Branca token authentication middleware
PHP
9
star
53

micropython-lis2hh12

MicroPython I2C driver for LIS2HH12 3-axis accelerometer
Python
9
star
54

wolf_dashboard

Provides simple admin dashboard to Wolf CMS.
PHP
9
star
55

branca-elixir

Secure alternative to JWT. Authenticated encrypted API tokens for Elixir.
Elixir
8
star
56

hagl_sdl2

SDL2 abstraction layer for the HAGL graphics library
C
8
star
57

frog_jquery

Add jQuery to Frog CMS admin interface.
PHP
8
star
58

micropython-examples

The mandatory various experiments with MicroPython repository
Python
8
star
59

jquery_jeditable_wysiwyg

Wysiwyg input for Jeditable.
JavaScript
8
star
60

bm8563

Platform agnostic I2C driver for BM8563 RTC
C
8
star
61

wolf_funky_cache

Funky cache plugin for Wolf CMS
PHP
7
star
62

beeper

Generic paginator for PHP 7.1+
PHP
7
star
63

axp202

Platform agnostic I2C driver for AXP202 PMU
C
7
star
64

jquery_jeditable_markitup

Universal markup editor for Jeditable.
7
star
65

axp173

Platform agnostic I2C driver for AXP173 power system management IC
C
6
star
66

triple-a

Alternative AVR (Arduino) Library
C
6
star
67

wolf_first_child

Redirect to first child page behaviour for Wolf CMS.
PHP
6
star
68

php_record

Simple Active Record implementation in PHP.
PHP
6
star
69

rack-facebook-method-fix

Rack middleware to fix Facebook always POST problem.
Ruby
6
star
70

esp_gfx

HAGL speed tests for ESP32 boards
C
5
star
71

rack-funky-cache

Funky caching for Rack based applications
Ruby
5
star
72

frog_email_template

Provides form mailer backend to Frog CMS. This code is no longer maintained. Please go to: https://github.com/dajare/wolf_email_template
PHP
5
star
73

mephisto_sitemap

Mephisto plugin which implements (Google, MSN, Yahoo ) Sitemap protocol.
Ruby
5
star
74

esp_twatch2020

Kitchen sink project for T-Watch 2020 and ESP-IDF
C
5
star
75

trytes

Trytes encoder and decoder for arbitrary data
PHP
4
star
76

wolf_jquery_ui

Add jQuery UI to Wolf CMS admin interface.
PHP
4
star
77

em-websocket-server

Simple websocket server with EventMachine
Ruby
4
star
78

esp_i2c_helper

ESP I2C master HAL for hardware agnostic I2C drivers
C
4
star
79

screenshot

Full size website screenshots with Sinatra.
JavaScript
4
star
80

micropython-gnssl76l

MicroPython I2C driver for Quectel GNSS L76-L (GPS)
Python
4
star
81

pico_gfx

HAGL speed tests for Rasrpberry Pi Pico boards
C
4
star
82

hagl_esp_solomon

ESP32 Solomon HAL for the HAGL graphics library
C
4
star
83

hagl_gd

GD HAL for the HAGL graphics library
C
3
star
84

fontx_tools

C
3
star
85

slim-fake-mcrypt

Fake PHP mcrypt extension to enable installing Slim 2.3.* with Composer
3
star
86

dbal-psr3-logger

PSR-3 Logger for Doctrine DBAL
PHP
3
star
87

ulid

Universally Unique Lexicographically Sortable Identifier
PHP
3
star
88

corelocation

PHP implementation of Apple location services protocol
PHP
3
star
89

esp_mipi

Low level MIPI DCS compatible display driver for ESP-IDF
C
3
star
90

rack-lazy-load

Companion Rack middleware for Lazy Load jQuery plugin.
Ruby
2
star
91

hagl_gd32v_mipi

GD32V MIPI DCS HAL for the HAGL graphics library
C
2
star
92

appelsiini.net

Sources and build files for my personal blog.
HTML
2
star
93

gd32v_effects

Old school demo effects for GD32V
C
2
star
94

branca-cli

Command line tool for encoding, decoding and inspecting Branca tokens
JavaScript
2
star
95

wolf_jquery_tools

Add jQuery Tools to Wolf CMS admin interface.
PHP
2
star
96

messente

An object-oriented Ruby wrapper for the Messente API.
Ruby
1
star
97

gd32v_gfx

HAGL speed tests for GD32V boards
C
1
star
98

slim-basepath

Demonstrate differences between Slim 3 and Slim 4
PHP
1
star
99

base85.io

CSS
1
star
100

.github

1
star