separate image manipulation code

This commit is contained in:
Thomas Klaehn 2020-02-21 14:23:57 +01:00
parent 7d313f9f74
commit fc8d9186be
3 changed files with 95 additions and 72 deletions

11
include/framebuffer.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef __INCLUDE_FRAMEBUFFER_H__
#define __INCLUDE_FRAMEBUFFER_H__
#include <stdint.h>
void fb_draw_pixel(uint16_t *image, uint16_t x, uint16_t y, uint16_t color);
void fb_draw_line(uint16_t *image, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
void fb_load_image(uint16_t *dst, uint16_t *src, uint32_t length);
void fb_set_image(uint16_t *dst, uint16_t value, uint32_t length);
#endif

77
src/framebuffer.c Normal file
View File

@ -0,0 +1,77 @@
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "framebuffer.h"
void fb_draw_pixel(uint16_t *image, uint16_t x, uint16_t y, uint16_t color)
{
assert(image != NULL);
image[x + 240 * y] = color;
}
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
#endif
void fb_draw_line(uint16_t *image, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
{
assert(image != NULL);
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if(steep) {
_swap_int16_t(x0, y0);
_swap_int16_t(x1, y1);
}
if(x0 > x1) {
_swap_int16_t(x0, x1);
_swap_int16_t(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if(y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for(; x0 <= x1; x0++) {
if(steep) {
fb_draw_pixel(image, y0, x0, color);
} else {
fb_draw_pixel(image, x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
void fb_load_image(uint16_t *dst, uint16_t *src, uint32_t length)
{
assert(NULL != dst);
assert(NULL != src);
for(uint32_t i = 0; i < length; i++) {
dst[i] = src[i];
}
}
void fb_set_image(uint16_t *dst, uint16_t value, uint32_t length)
{
assert(NULL != dst);
for (uint32_t i = 0; i < length; i++) {
dst[i] = value;
}
}

View File

@ -9,75 +9,10 @@
#include "board.h"
#include "driver.h"
#include "clock_face.h"
#include "framebuffer.h"
uint16_t image[240 * 240];
static void bmp_draw_pixel(uint16_t *image, uint16_t x, uint16_t y, uint16_t color)
{
assert(image != NULL);
image[x + 240 * y] = color;
}
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
#endif
static void img_draw_line(uint16_t *image, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
{
assert(image != NULL);
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if(steep) {
_swap_int16_t(x0, y0);
_swap_int16_t(x1, y1);
}
if(x0 > x1) {
_swap_int16_t(x0, x1);
_swap_int16_t(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if(y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for(; x0 <= x1; x0++) {
if(steep) {
bmp_draw_pixel(image, y0, x0, color);
} else {
bmp_draw_pixel(image, x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
static void img_load(uint16_t *dst, uint16_t *src, uint32_t length)
{
for(uint32_t i = 0; i < length; i++) {
dst[i] = src[i];
}
}
static void img_set(uint16_t *dst, uint16_t value, uint32_t length)
{
for (uint32_t i = 0; i < length; i++) {
image[i] = value;
}
}
int main(void)
{
drv_open(&lcd_drv);
@ -95,12 +30,12 @@ int main(void)
struct tm tm;
while(1) {
img_load(image, clock_face, 240 * 240);
fb_load_image(image, clock_face, 240 * 240);
// just to shown image orientation
img_draw_line(image, 10, 10, 12, 12, 0x00f8);
img_draw_line(image, 10, 11, 12, 12, 0x00f8);
img_draw_line(image, 10, 12, 12, 12, 0x00f8);
fb_draw_line(image, 10, 10, 12, 12, 0x00f8);
fb_draw_line(image, 10, 11, 12, 12, 0x00f8);
fb_draw_line(image, 10, 12, 12, 12, 0x00f8);
T = time(NULL);
tm = *localtime(&T);
@ -112,7 +47,7 @@ int main(void)
dest_x = root_x + min_len * cos(min_angle * M_PI / 180);
dest_y = root_y + min_len * sin(min_angle * M_PI / 180);
img_draw_line(image, (uint16_t)root_x, (uint16_t)root_y, (uint16_t)dest_x, (uint16_t)dest_y, 0xffff);
fb_draw_line(image, (uint16_t)root_x, (uint16_t)root_y, (uint16_t)dest_x, (uint16_t)dest_y, 0xffff);
std_angle = 0.5 * (60 * tm.tm_hour + tm.tm_min);
if(std_angle >= 360.0) {
@ -121,7 +56,7 @@ int main(void)
dest_x = root_x + std_len * cos(std_angle * M_PI / 180);
dest_y = root_y + std_len * sin(std_angle * M_PI / 180);
img_draw_line(image, (uint16_t)root_x, (uint16_t)root_y, (uint16_t)dest_x, (uint16_t)dest_y, 0xffff);
fb_draw_line(image, (uint16_t)root_x, (uint16_t)root_y, (uint16_t)dest_x, (uint16_t)dest_y, 0xffff);
drv_write(&lcd_drv, (const char *)image, 240 * 240);
sleep(1);