From 7d5900e5ea4ffec5e6b3e65d8b4de7eca84000b8 Mon Sep 17 00:00:00 2001 From: Zihao Yu Date: Wed, 3 Jan 2024 23:30:03 +0800 Subject: [PATCH] add uart framework --- include/component.h | 3 ++- include/pins.h | 4 ---- include/uart.h | 19 +++++++++++++++++ src/component.cpp | 2 +- src/font.cpp | 15 +++++++------- src/nvboard.cpp | 9 ++++---- src/uart.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++ usr/include/pins.h | 9 ++++---- 8 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 include/uart.h create mode 100644 src/uart.cpp diff --git a/include/component.h b/include/component.h index 876a35c..6127342 100644 --- a/include/component.h +++ b/include/component.h @@ -6,7 +6,8 @@ // component type 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 diff --git a/include/pins.h b/include/pins.h index 965f252..6075a29 100644 --- a/include/pins.h +++ b/include/pins.h @@ -13,10 +13,6 @@ typedef struct PinNode { } PinNode; 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) { PinNode *p = &pin_array[pin]; if (p->vector_len == 1) { diff --git a/include/uart.h b/include/uart.h new file mode 100644 index 0000000..b5c18c9 --- /dev/null +++ b/include/uart.h @@ -0,0 +1,19 @@ +#ifndef _NVBOARD_UART_H +#define _NVBOARD_UART_H + +#include +#include + +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 diff --git a/src/component.cpp b/src/component.cpp index c8c5551..bd11288 100644 --- a/src/component.cpp +++ b/src/component.cpp @@ -96,7 +96,7 @@ void RGB_LED::update_state() { #endif 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); } COMPONENT_LIST(INIT_FN); } diff --git a/src/font.cpp b/src/font.cpp index 82df44c..acfd5d5 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -7,18 +7,19 @@ void init_font() { int ret = TTF_Init(); assert(ret != -1); 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); } -void test_font(SDL_Renderer *renderer) { - SDL_Color c = {.r = 0xff, .g = 0xff, .b = 0xff }; - SDL_Surface *s = TTF_RenderText_Solid_Wrapped(font, "abc", c, 0); +SDL_Texture* render_str(SDL_Renderer *renderer, std::string str, int wrap_len_in_pixel, int *w, int *h) { + SDL_Color c = {.r = 0x00, .g = 0x00, .b = 0x00 }; + 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_Rect r = { 0, 0, s->w, s->h }; - SDL_RenderCopy(renderer, t, NULL, &r); + assert(t != NULL); + *w = s->w; *h = s->h; SDL_FreeSurface(s); - SDL_DestroyTexture(t); + return t; } void close_font() { diff --git a/src/nvboard.cpp b/src/nvboard.cpp index a2cc9b7..e1d2d4c 100644 --- a/src/nvboard.cpp +++ b/src/nvboard.cpp @@ -77,16 +77,15 @@ void nvboard_init(int vga_clk_cycle) { #endif 0 ); + SDL_SetRenderDrawColor(main_renderer, 0xff, 0xff, 0xff, 0); + + void init_font(); + init_font(); init_render(main_renderer); init_components(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 ++) { if (pin_array[i].ptr == NULL) pin_array[i].ptr = &pin_array[i].data; } diff --git a/src/uart.cpp b/src/uart.cpp new file mode 100644 index 0000000..4b07343 --- /dev/null +++ b/src/uart.cpp @@ -0,0 +1,50 @@ +#include +#include + +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); +} diff --git a/usr/include/pins.h b/usr/include/pins.h index 8f577d7..60142aa 100644 --- a/usr/include/pins.h +++ b/usr/include/pins.h @@ -30,7 +30,8 @@ SEG6A, SEG6B, SEG6C, SEG6D, SEG6E, SEG6F, SEG6G, DEC6P, \ 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, \ VGA_R0, VGA_R1, VGA_R2, VGA_R3, VGA_R4, VGA_R5, VGA_R6, VGA_R7, \ @@ -43,14 +44,14 @@ enum { BTN_INPUT, SW_INPUT, KEYBOARD_INPUT, - NR_INPUT_PINS, + UART_INPUT, + NAIVE_LEDS_OUTPUT, RGB_LEDS_OUTPUT, - SEG7_ENBS_OUTPUT, SEG7_SEGS_OUTPUT, VGA_OUTPUT, + UART_OUTPUT, NR_PINS, - NR_OUTPUT_PINS = NR_PINS - NR_INPUT_PINS }; #endif