额解耦1
This commit is contained in:
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -16,6 +16,8 @@
|
||||
"stdio.h": "c",
|
||||
"ostream": "c",
|
||||
"system_error": "c",
|
||||
"variant": "c"
|
||||
"variant": "c",
|
||||
"string.h": "c",
|
||||
"cstdlib": "c"
|
||||
}
|
||||
}
|
||||
1
Makefile
1
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)
|
||||
|
||||
85
src/app.c
Normal file
85
src/app.c
Normal file
@ -0,0 +1,85 @@
|
||||
#include "app.h"
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
23
src/app.h
Normal file
23
src/app.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef APP_H
|
||||
#define APP_H
|
||||
|
||||
#include <sys/time.h>
|
||||
#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
|
||||
83
src/main.c
83
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;
|
||||
}
|
||||
@ -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);
|
||||
|
||||
@ -8,6 +8,7 @@ typedef struct {
|
||||
double time;
|
||||
} Status;
|
||||
|
||||
void init_status(Status* status);
|
||||
Color shader(Status* status);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user