diff --git a/.vscode/settings.json b/.vscode/settings.json index a58cb9b..1f5ed49 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -16,6 +16,8 @@ "stdio.h": "c", "ostream": "c", "system_error": "c", - "variant": "c" + "variant": "c", + "string.h": "c", + "cstdlib": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 24d6015..37814ad 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ SRCS = $(wildcard $(SRC_DIR)/*.c) OBJS = $(SRCS:$(SRC_DIR)/%.c=$(DIST_DIR)/%.o) TARGET = $(DIST_DIR)/main -# 确保 dist 目录存在 MKDIR_P = mkdir -p all: $(TARGET) diff --git a/src/app.c b/src/app.c new file mode 100644 index 0000000..e8edab3 --- /dev/null +++ b/src/app.c @@ -0,0 +1,85 @@ +#include "app.h" + +#include +#include +#include + +#define RES_BYTES 40 +#define COLOR_BYTES 24 + +volatile sig_atomic_t stop = 0; + +double get_time_elapsed(const struct timeval* start_time) { + struct timeval current_time; + gettimeofday(¤t_time, NULL); + + return (current_time.tv_sec - start_time->tv_sec) + + (current_time.tv_usec - start_time->tv_usec) / 1000000.0; +} + +void handle_sigint(int) { + stop = 1; +} + +void init_app(AppContext* ctx) { + ctx->stop_flag = 0; + + signal(SIGINT, handle_sigint); + gettimeofday(&ctx->start_time, NULL); + ctx->light_fd = init_device("/dev/spidev1.0"); + if (ctx->light_fd < 0) { + exit(EXIT_FAILURE); + } + + ctx->num_leds = 60; + ctx->data_size = COLOR_BYTES * ctx->num_leds + RES_BYTES; + ctx->buffer = malloc(ctx->data_size); + ctx->colors = malloc(sizeof(Color) * ctx->num_leds); + ctx->status = malloc(sizeof(Status)); + + if (!ctx->buffer || !ctx->colors || !ctx->status) { + exit(EXIT_FAILURE); + } + + init_status(ctx->status); +} + +void render(AppContext* ctx) { + ctx->status->time = get_time_elapsed(&ctx->start_time); + + for (int i = 0; i < ctx->num_leds; i++) { + ctx->status->index = i; + ctx->colors[i] = shader(ctx->status); + } +} + +void submit(AppContext* ctx) { + construct_buffer(ctx->buffer, ctx->colors, ctx->num_leds, COLOR_BYTES, + RES_BYTES); + send_buffer(ctx->light_fd, ctx->buffer, ctx->data_size); +} + +void event_loop(AppContext* ctx) { + render(ctx); + submit(ctx); + + if (stop != 0) { + ctx->stop_flag = 1; + } +} + +void exit_app(AppContext* ctx) { + for (int i = 0; i < ctx->num_leds; i++) { + if (i == 0) { + ctx->colors[i] = hex_to_color(0xFFFFFF); + } else { + ctx->colors[i] = hex_to_color(0x000000); + } + } + + submit(ctx); + + free(ctx->buffer); + free(ctx->colors); + close(ctx->light_fd); +} diff --git a/src/app.h b/src/app.h new file mode 100644 index 0000000..1bf453e --- /dev/null +++ b/src/app.h @@ -0,0 +1,23 @@ +#ifndef APP_H +#define APP_H + +#include +#include "shader.h" +#include "spi_utils.h" + +typedef struct AppContext { + int stop_flag; + int light_fd; + uint8_t* buffer; + Color* colors; + Status* status; + struct timeval start_time; + int num_leds; + int data_size; +} AppContext; + +void init_app(AppContext* ctx); +void event_loop(AppContext* ctx); +void exit_app(AppContext* ctx); + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index d54fa9b..c7ae289 100644 --- a/src/main.c +++ b/src/main.c @@ -8,87 +8,16 @@ #include "color_utils.h" #include "shader.h" #include "spi_utils.h" - -#define RES_BYTES 40 -#define COLOR_BYTES 24 - -volatile sig_atomic_t stop = 0; - -const int num_leds = 60; -const int data_size = COLOR_BYTES * num_leds + RES_BYTES; - -int light_fd; -int server_fd; - -struct timeval start_time; -uint8_t* buffer; -Color* colors; -Status* status; - -void handle_sigint(int) { - stop = 1; -} - -double get_time_elapsed() { - struct timeval current_time; - gettimeofday(¤t_time, NULL); - - return (current_time.tv_sec - start_time.tv_sec) + - (current_time.tv_usec - start_time.tv_usec) / 1000000.0; -} - -void submit() { - construct_buffer(buffer, colors, num_leds, COLOR_BYTES, RES_BYTES); - send_buffer(light_fd, buffer, data_size); -} - -void render_exit() { - for (int i = 0; i < num_leds; i++) { - if (i == 0) { - colors[i] = hex_to_color(0xFFFFFF); - } else { - colors[i] = hex_to_color(0x000000); - } - } -} - -void render() { - status->time = get_time_elapsed(start_time); - - for (int i = 0; i < num_leds; i++) { - status->index = i; - colors[i] = shader(status); - } -} - -void init() { - signal(SIGINT, handle_sigint); - gettimeofday(&start_time, NULL); - const char* device = "/dev/spidev1.0"; - light_fd = init_device(device); - - buffer = malloc(data_size); - colors = malloc(sizeof(Color) * num_leds); - status = malloc(sizeof(Status)); -} - -void clean_up() { - render_exit(); - submit(); - - free(buffer); - free(colors); - close(light_fd); -} +#include "app.h" int main() { - init(); + AppContext ctx; + init_app(&ctx); - while (!stop) { - render(); - submit(); + while (!ctx.stop_flag) { + event_loop(&ctx); } - clean_up(); + exit_app(&ctx); return 0; } \ No newline at end of file diff --git a/src/shader.c b/src/shader.c index ebe1c5b..55d3813 100644 --- a/src/shader.c +++ b/src/shader.c @@ -1,5 +1,10 @@ #include "shader.h" +void init_status(Status* status) { + status->index = 0; + status->time = 0; +} + Color shader(Status* status) { if (status->index == 0) { return hex_to_color(0xFFFFFF); diff --git a/src/shader.h b/src/shader.h index 318c82b..96999c7 100644 --- a/src/shader.h +++ b/src/shader.h @@ -8,6 +8,7 @@ typedef struct { double time; } Status; +void init_status(Status* status); Color shader(Status* status); #endif \ No newline at end of file