Added graphic primitives
This commit is contained in:
parent
6673290c3f
commit
0da083fb1a
4
Makefile
4
Makefile
|
@ -5,8 +5,8 @@ ISPPORT ?= /dev/kaboard
|
|||
VERSION = 0.1
|
||||
|
||||
HEADERS = include/spi.h include/st7735.h include/st7735initcmds.h
|
||||
HEADERS += images/logo_bw.h images/logo.h
|
||||
SRC = main.c spi.c st7735.c
|
||||
HEADERS += images/logo_bw.h images/logo.h include/st7735_gfx.h
|
||||
SRC = main.c spi.c st7735.c st7735_gfx.c
|
||||
TARGET = st7735_test
|
||||
OBJDIR = bin
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ void st7735_init(void);
|
|||
|
||||
void st7735_set_orientation(enum ST7735_ORIENTATION orientation);
|
||||
|
||||
void st7735_draw_pixel(uint8_t x, uint8_t y, uint16_t color);
|
||||
void st7735_draw_pixel(int16_t x, int16_t y, uint16_t color);
|
||||
void st7735_fill_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color);
|
||||
|
||||
static inline void st7735_draw_fast_vline(uint8_t x, uint8_t y, uint8_t h, uint16_t color) {
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef _ST7735_GFX_H_
|
||||
#define _ST7735_GFX_H_
|
||||
|
||||
#include<stdint.h>
|
||||
|
||||
void st7735_draw_line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color);
|
||||
void st7735_draw_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color);
|
||||
void st7735_draw_circle(uint8_t x0, uint8_t y0, uint8_t r, uint16_t color);
|
||||
|
||||
#endif
|
26
main.c
26
main.c
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "spi.h"
|
||||
#include "st7735.h"
|
||||
#include "st7735_gfx.h"
|
||||
|
||||
#include "logo.h"
|
||||
#include "logo_bw.h"
|
||||
|
@ -18,5 +19,30 @@ int main(void) {
|
|||
|
||||
st7735_draw_bitmap(10, 10, &logo);
|
||||
|
||||
|
||||
for(uint8_t x = 8; x <= 160; x += 8) {
|
||||
st7735_draw_line(0, 0, x, 128, ST7735_COLOR_GREEN);
|
||||
}
|
||||
|
||||
for(uint8_t y = 8; y <= 128; y += 8) {
|
||||
st7735_draw_line(0, 0, 160, y, ST7735_COLOR_GREEN);
|
||||
}
|
||||
|
||||
for(uint8_t r = 8; r <= 80; r += 8) {
|
||||
st7735_draw_circle(80, 64, r, ST7735_COLOR_RED);
|
||||
}
|
||||
|
||||
for(uint8_t r = 8; r <= 160; r += 8) {
|
||||
st7735_draw_circle(0, 0, r, ST7735_COLOR_BLUE);
|
||||
}
|
||||
|
||||
uint8_t h = 16;
|
||||
uint8_t w = 20;
|
||||
for(uint8_t i = 0; i < 8; i++) {
|
||||
st7735_draw_rect(80 - w / 2, 64 - h / 2, w, h, ST7735_COLOR_YELLOW);
|
||||
h += 16;
|
||||
w += 20;
|
||||
}
|
||||
|
||||
while(1);
|
||||
}
|
||||
|
|
2
st7735.c
2
st7735.c
|
@ -243,7 +243,7 @@ void st7735_set_addr_win(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
|
|||
st7735_write_cmd(ST7735_RAMWR); // write to RAM
|
||||
}
|
||||
|
||||
void st7735_draw_pixel(uint8_t x, uint8_t y, uint16_t color) {
|
||||
void st7735_draw_pixel(int16_t x, int16_t y, uint16_t color) {
|
||||
if(x < 0 || x >= st7735_width || y < 0 || y >= st7735_height){
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
#include "st7735_gfx.h"
|
||||
|
||||
#include<stdlib.h>
|
||||
|
||||
#include "st7735.h"
|
||||
|
||||
#define _swap_uint8_t(a, b) { uint8_t t = a; a = b; b = t; }
|
||||
|
||||
void st7735_draw_line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color) {
|
||||
uint8_t steep_dir = abs(y1 - y0) > abs(x1 - x0);
|
||||
if (steep_dir) {
|
||||
_swap_uint8_t(x0, y0);
|
||||
_swap_uint8_t(x1, y1);
|
||||
}
|
||||
|
||||
if (x0 > x1) {
|
||||
_swap_uint8_t(x0, x1);
|
||||
_swap_uint8_t(y0, y1);
|
||||
}
|
||||
|
||||
int16_t dx, dy;
|
||||
dx = x1 - x0;
|
||||
dy = abs(y1 - y0);
|
||||
|
||||
int16_t err = dx / 2;
|
||||
int16_t y_step;
|
||||
|
||||
if (y0 < y1) {
|
||||
y_step = 1;
|
||||
} else {
|
||||
y_step = -1;
|
||||
}
|
||||
|
||||
uint8_t seg = x0;
|
||||
uint8_t cur_x;
|
||||
for(cur_x = x0; cur_x <= x1; cur_x++) {
|
||||
err -= dy;
|
||||
if (err < 0) {
|
||||
if (steep_dir) {
|
||||
st7735_draw_fast_vline(y0, seg, cur_x - seg + 1, color);
|
||||
} else {
|
||||
st7735_draw_fast_hline(seg, y0, cur_x - seg +1, color);
|
||||
}
|
||||
y0 += y_step;
|
||||
err += dx;
|
||||
seg = cur_x + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// x0 incremented
|
||||
if (steep_dir) {
|
||||
st7735_draw_fast_vline(y0, seg, cur_x - seg, color);
|
||||
} else {
|
||||
st7735_draw_fast_hline(seg, y0, cur_x - seg, color);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void st7735_draw_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color) {
|
||||
if(w < 1 || h < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
st7735_draw_fast_hline(x, y, w, color);
|
||||
st7735_draw_fast_hline(x, y + h - 1, w, color);
|
||||
st7735_draw_fast_vline(x, y, h, color);
|
||||
st7735_draw_fast_vline(x + w - 1, y, h, color);
|
||||
}
|
||||
|
||||
|
||||
void st7735_draw_circle(uint8_t x0, uint8_t y0, uint8_t r, uint16_t color) {
|
||||
int16_t f = 1 - r;
|
||||
int16_t ddF_x = 1;
|
||||
int16_t ddF_y = -2 * r;
|
||||
int16_t x = 0;
|
||||
int16_t y = r;
|
||||
|
||||
st7735_draw_pixel(x0, y0 + r, color);
|
||||
st7735_draw_pixel(x0, y0 - r, color);
|
||||
st7735_draw_pixel(x0 + r, y0, color);
|
||||
st7735_draw_pixel(x0 - r, y0, color);
|
||||
|
||||
while (x<y) {
|
||||
if (f >= 0) {
|
||||
y--;
|
||||
ddF_y += 2;
|
||||
f += ddF_y;
|
||||
}
|
||||
|
||||
x++;
|
||||
ddF_x += 2;
|
||||
f += ddF_x;
|
||||
|
||||
st7735_draw_pixel(x0 + x, y0 + y, color);
|
||||
st7735_draw_pixel(x0 - x, y0 + y, color);
|
||||
st7735_draw_pixel(x0 + x, y0 - y, color);
|
||||
st7735_draw_pixel(x0 - x, y0 - y, color);
|
||||
st7735_draw_pixel(x0 + y, y0 + x, color);
|
||||
st7735_draw_pixel(x0 - y, y0 + x, color);
|
||||
st7735_draw_pixel(x0 + y, y0 - x, color);
|
||||
st7735_draw_pixel(x0 - y, y0 - x, color);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue