Files
my-ws2812b-led-deprecated/main.c
Passthem 7e455c591a init
2025-04-11 15:40:18 +08:00

68 lines
1.6 KiB
C

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "spi_utils.h"
#include "color_utils.h"
#include "shader.h"
#define RES_BYTES 40
#define COLOR_BYTES 24
volatile sig_atomic_t stop = 0;
void handle_sigint(int sig) {
stop = 1;
}
int main() {
signal(SIGINT, handle_sigint);
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;
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);
struct timeval start_time;
gettimeofday(&start_time, NULL);
while (!stop) {
struct timeval current_time;
gettimeofday(&current_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);
}
construct_buffer(buffer, colors, num_leds, COLOR_BYTES, RES_BYTES);
send_buffer(fd, buffer, data_size, spi_speed, spi_bits);
}
for (int i = 0; i < num_leds; i++) {
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);
free(buffer);
free(colors);
close(fd);
return 0;
}