add uart framework

This commit is contained in:
Zihao Yu 2024-01-03 23:30:03 +08:00
parent 28ab4be2d6
commit 7d5900e5ea
8 changed files with 89 additions and 22 deletions

View File

@ -6,7 +6,8 @@
// component type // component type
enum { enum {
BUTTON_TYPE = 1, SWITCH_TYPE, NAIVE_LED_TYPE, RGB_LED_TYPE, SEGS7_TYPE, VGA_TYPE, KEYBOARD_TYPE BUTTON_TYPE = 1, SWITCH_TYPE, NAIVE_LED_TYPE, RGB_LED_TYPE, SEGS7_TYPE,
VGA_TYPE, KEYBOARD_TYPE, UART_TYPE
}; };
// logic type // logic type

View File

@ -13,10 +13,6 @@ typedef struct PinNode {
} PinNode; } PinNode;
extern PinNode pin_array[]; extern PinNode pin_array[];
static inline bool is_input_pin(int pin) {
return (pin < NR_INPUT_PINS);
}
static inline uint8_t pin_peek(int pin) { static inline uint8_t pin_peek(int pin) {
PinNode *p = &pin_array[pin]; PinNode *p = &pin_array[pin];
if (p->vector_len == 1) { if (p->vector_len == 1) {

19
include/uart.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef _NVBOARD_UART_H
#define _NVBOARD_UART_H
#include <component.h>
#include <string>
class UART : public Component{
private:
int region_w, region_h;
std::string str;
public:
UART(SDL_Renderer *rend, int cnt, int init_val, int ct);
~UART();
virtual void update_gui();
virtual void update_state();
};
#endif

View File

@ -96,7 +96,7 @@ void RGB_LED::update_state() {
#endif #endif
void init_components(SDL_Renderer *renderer) { void init_components(SDL_Renderer *renderer) {
#define COMPONENT_LIST(f) f(led) f(switch) f(button) f(segs7) f(keyboard) f(vga) #define COMPONENT_LIST(f) f(led) f(switch) f(button) f(segs7) f(keyboard) f(vga) f(uart)
#define INIT_FN(c) { void concat(init_, c)(SDL_Renderer *); concat(init_, c)(renderer); } #define INIT_FN(c) { void concat(init_, c)(SDL_Renderer *); concat(init_, c)(renderer); }
COMPONENT_LIST(INIT_FN); COMPONENT_LIST(INIT_FN);
} }

View File

@ -7,18 +7,19 @@ void init_font() {
int ret = TTF_Init(); int ret = TTF_Init();
assert(ret != -1); assert(ret != -1);
std::string nvboard_home = getenv("NVBOARD_HOME"); std::string nvboard_home = getenv("NVBOARD_HOME");
font = TTF_OpenFont((nvboard_home + "/resources/font/" + "FreeMono.ttf").c_str(), 28); font = TTF_OpenFont((nvboard_home + "/resources/font/" + "FreeMono.ttf").c_str(), 16);
assert(font != NULL); assert(font != NULL);
} }
void test_font(SDL_Renderer *renderer) { SDL_Texture* render_str(SDL_Renderer *renderer, std::string str, int wrap_len_in_pixel, int *w, int *h) {
SDL_Color c = {.r = 0xff, .g = 0xff, .b = 0xff }; SDL_Color c = {.r = 0x00, .g = 0x00, .b = 0x00 };
SDL_Surface *s = TTF_RenderText_Solid_Wrapped(font, "abc", c, 0); SDL_Surface *s = TTF_RenderText_Solid_Wrapped(font, str.c_str(), c, wrap_len_in_pixel);
assert(s != NULL);
SDL_Texture *t = SDL_CreateTextureFromSurface(renderer, s); SDL_Texture *t = SDL_CreateTextureFromSurface(renderer, s);
SDL_Rect r = { 0, 0, s->w, s->h }; assert(t != NULL);
SDL_RenderCopy(renderer, t, NULL, &r); *w = s->w; *h = s->h;
SDL_FreeSurface(s); SDL_FreeSurface(s);
SDL_DestroyTexture(t); return t;
} }
void close_font() { void close_font() {

View File

@ -77,16 +77,15 @@ void nvboard_init(int vga_clk_cycle) {
#endif #endif
0 0
); );
SDL_SetRenderDrawColor(main_renderer, 0xff, 0xff, 0xff, 0);
void init_font();
init_font();
init_render(main_renderer); init_render(main_renderer);
init_components(main_renderer); init_components(main_renderer);
init_gui(main_renderer); init_gui(main_renderer);
void init_font();
void test_font(SDL_Renderer *renderer);
init_font();
test_font(main_renderer);
for (int i = 0; i < NR_PINS; i ++) { for (int i = 0; i < NR_PINS; i ++) {
if (pin_array[i].ptr == NULL) pin_array[i].ptr = &pin_array[i].data; if (pin_array[i].ptr == NULL) pin_array[i].ptr = &pin_array[i].data;
} }

50
src/uart.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <nvboard.h>
#include <uart.h>
UART* uart = NULL;
SDL_Texture* render_str(SDL_Renderer *renderer, std::string str, int wrap_len_in_pixel, int *w, int *h);
UART::UART(SDL_Renderer *rend, int cnt, int init_val, int ct):
Component(rend, cnt, init_val, ct),
region_w(WINDOW_WIDTH / 2), region_h(WINDOW_HEIGHT / 2), str(" ") {
SDL_Texture *temp_texture = SDL_CreateTexture(rend, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, region_w, region_h);
set_texture(temp_texture, 0);
}
UART::~UART() {
SDL_DestroyTexture(get_texture(0));
}
void UART::update_gui() {
SDL_Renderer *r = get_renderer();
SDL_Rect rect = *get_rect(0);
SDL_RenderFillRect(r, &rect);
int w = 0, h = 0;
SDL_Texture *t = render_str(r, str, region_w, &w, &h);
rect.w = w; rect.h = h;
SDL_RenderCopy(r, t, NULL, &rect);
SDL_DestroyTexture(t);
set_redraw();
}
void UART::update_state() {
static int i = 0;
i ++;
if (i < 10) return;
i = 0;
char last = str[str.length() - 1];
str += last + 1;
update_gui();
}
void init_uart(SDL_Renderer *renderer) {
uart = new UART(renderer, 1, 0, UART_TYPE);
SDL_Rect *rect_ptr = new SDL_Rect;
*rect_ptr = (SDL_Rect){WINDOW_WIDTH / 2, 0, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2};
uart->set_rect(rect_ptr, 0);
uart->add_pin(UART_TX);
uart->add_pin(UART_RX);
add_component(uart);
}

View File

@ -30,7 +30,8 @@
SEG6A, SEG6B, SEG6C, SEG6D, SEG6E, SEG6F, SEG6G, DEC6P, \ SEG6A, SEG6B, SEG6C, SEG6D, SEG6E, SEG6F, SEG6G, DEC6P, \
SEG7A, SEG7B, SEG7C, SEG7D, SEG7E, SEG7F, SEG7G, DEC7P SEG7A, SEG7B, SEG7C, SEG7D, SEG7E, SEG7F, SEG7G, DEC7P
//#define UART_OUTPUT #define UART_OUTPUT UART_TX
#define UART_INPUT UART_RX
#define VGA_OUTPUT VGA_VSYNC, VGA_HSYNC, VGA_BLANK_N, \ #define VGA_OUTPUT VGA_VSYNC, VGA_HSYNC, VGA_BLANK_N, \
VGA_R0, VGA_R1, VGA_R2, VGA_R3, VGA_R4, VGA_R5, VGA_R6, VGA_R7, \ VGA_R0, VGA_R1, VGA_R2, VGA_R3, VGA_R4, VGA_R5, VGA_R6, VGA_R7, \
@ -43,14 +44,14 @@ enum {
BTN_INPUT, BTN_INPUT,
SW_INPUT, SW_INPUT,
KEYBOARD_INPUT, KEYBOARD_INPUT,
NR_INPUT_PINS, UART_INPUT,
NAIVE_LEDS_OUTPUT, NAIVE_LEDS_OUTPUT,
RGB_LEDS_OUTPUT, RGB_LEDS_OUTPUT,
SEG7_ENBS_OUTPUT,
SEG7_SEGS_OUTPUT, SEG7_SEGS_OUTPUT,
VGA_OUTPUT, VGA_OUTPUT,
UART_OUTPUT,
NR_PINS, NR_PINS,
NR_OUTPUT_PINS = NR_PINS - NR_INPUT_PINS
}; };
#endif #endif