再切分
This commit is contained in:
8
.vscode/settings.json
vendored
8
.vscode/settings.json
vendored
@ -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"
|
||||
}
|
||||
}
|
||||
100
src/main.c
100
src/main.c
@ -3,66 +3,92 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include "spi_utils.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -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
|
||||
@ -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 <fcntl.h>
|
||||
#include <linux/spi/spidev.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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 = {
|
||||
|
||||
@ -1,23 +1,10 @@
|
||||
#ifndef SPI_UTILS_H
|
||||
#define SPI_UTILS_H
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <linux/spi/spidev.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user