diff --git a/.vscode/settings.json b/.vscode/settings.json index b81c16f..a58cb9b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,12 @@ "cstdint": "c", "color_utils.h": "c", "data_sban_period2.h": "c", - "shader.h": "c" + "shader.h": "c", + "signal.h": "c", + "ioctl.h": "c", + "stdio.h": "c", + "ostream": "c", + "system_error": "c", + "variant": "c" } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index a9cba37..d54fa9b 100644 --- a/src/main.c +++ b/src/main.c @@ -3,66 +3,92 @@ #include #include #include -#include "spi_utils.h" +#include + #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; -void handle_sigint(int sig) { +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; } -int main() { - signal(SIGINT, handle_sigint); +double get_time_elapsed() { + struct timeval current_time; + gettimeofday(¤t_time, NULL); - struct timeval start_time; - gettimeofday(&start_time, NULL); + return (current_time.tv_sec - start_time.tv_sec) + + (current_time.tv_usec - start_time.tv_usec) / 1000000.0; +} - const char* device = "/dev/spidev1.0"; - const uint8_t spi_mode = SPI_MODE_0; - const uint8_t spi_bits = 8; - const uint32_t spi_speed = 6400000; +void submit() { + construct_buffer(buffer, colors, num_leds, COLOR_BYTES, RES_BYTES); + send_buffer(light_fd, buffer, data_size); +} - int fd = init_device(device, spi_mode, spi_bits, spi_speed); - if (fd < 0) - return -1; - - const int num_leds = 60; - const int data_size = COLOR_BYTES * num_leds + RES_BYTES; - - uint8_t* buffer = malloc(data_size); - Color* colors = malloc(sizeof(Color) * num_leds); - - while (!stop) { - struct timeval current_time; - gettimeofday(¤t_time, NULL); - - double time_elapsed = - (current_time.tv_sec - start_time.tv_sec) + - (current_time.tv_usec - start_time.tv_usec) / 1000000.0; - - for (int i = 0; i < num_leds; i++) { - colors[i] = shader(i, time_elapsed); +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); } - - construct_buffer(buffer, colors, num_leds, COLOR_BYTES, RES_BYTES); - send_buffer(fd, buffer, data_size, spi_speed, spi_bits); } +} + +void render() { + status->time = get_time_elapsed(start_time); for (int i = 0; i < num_leds; i++) { - colors[i] = hex_to_color(0x000000); + status->index = i; + colors[i] = shader(status); } +} - construct_buffer(buffer, colors, num_leds, COLOR_BYTES, RES_BYTES); - send_buffer(fd, buffer, data_size, spi_speed, spi_bits); +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(fd); + close(light_fd); +} +int main() { + init(); + + while (!stop) { + render(); + submit(); + } + + clean_up(); return 0; } \ No newline at end of file diff --git a/src/shader.c b/src/shader.c index 32d4354..ebe1c5b 100644 --- a/src/shader.c +++ b/src/shader.c @@ -1,9 +1,9 @@ #include "shader.h" -Color shader(int index, double time) { - if (index == 0) { +Color shader(Status* status) { + if (status->index == 0) { return hex_to_color(0xFFFFFF); } - return hsv_to_color(time * 120 + index * 6, 1, 1); + return hsv_to_color(status->time * 120 + status->index * 6, 1, 1); } \ No newline at end of file diff --git a/src/shader.h b/src/shader.h index 35ab603..318c82b 100644 --- a/src/shader.h +++ b/src/shader.h @@ -3,6 +3,11 @@ #include "color_utils.h" -Color shader(int index, double time); +typedef struct { + int index; + double time; +} Status; + +Color shader(Status* status); #endif \ No newline at end of file diff --git a/src/spi_utils.c b/src/spi_utils.c index d0539c6..2476911 100644 --- a/src/spi_utils.c +++ b/src/spi_utils.c @@ -1,9 +1,16 @@ #include "spi_utils.h" -int init_device(const char* device, - uint8_t spi_mode, - uint8_t spi_bits, - uint32_t spi_speed) { +#include +#include +#include +#include +#include + +const uint8_t spi_mode = SPI_MODE_0; +const uint8_t spi_bits = 8; +const uint32_t spi_speed = 6400000; + +int init_device(const char* device) { int fd; if ((fd = open(device, O_RDWR)) < 0) { @@ -19,11 +26,7 @@ int init_device(const char* device, return fd; } -int send_buffer(int fd, - uint8_t* buffer, - const int data_size, - uint32_t spi_speed, - uint8_t spi_bits) { +int send_buffer(int fd, uint8_t* buffer, const int data_size) { int ret; struct spi_ioc_transfer tr = { diff --git a/src/spi_utils.h b/src/spi_utils.h index e6ecc2a..b162a40 100644 --- a/src/spi_utils.h +++ b/src/spi_utils.h @@ -1,23 +1,10 @@ #ifndef SPI_UTILS_H #define SPI_UTILS_H -#include -#include -#include -#include -#include - typedef unsigned char uint8_t; typedef unsigned int uint32_t; -int init_device(const char* device, - uint8_t spi_mode, - uint8_t spi_bits, - uint32_t spi_speed); -int send_buffer(int fd, - uint8_t* buffer, - const int data_size, - uint32_t spi_speed, - uint8_t spi_bits); +int init_device(const char* device); +int send_buffer(int fd, uint8_t* buffer, const int data_size); #endif \ No newline at end of file