-
Notifications
You must be signed in to change notification settings - Fork 2
/
ws281x.cpp
38 lines (30 loc) · 943 Bytes
/
ws281x.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include "ws281x.hpp"
#define SYSERR(expr) do { if((long)(expr) == -1L) { perror(#expr); throw #expr; } } while(false)
namespace ws281x
{
void TSPIDriver::SendData(const void* buffer, const size_t n_bytes)
{
struct spi_ioc_transfer xfer_cmd;
memset(&xfer_cmd, 0, sizeof(xfer_cmd));
xfer_cmd.tx_buf = (size_t)buffer;
xfer_cmd.len = n_bytes;
xfer_cmd.delay_usecs = 0;
xfer_cmd.speed_hz = this->hz_speed;
xfer_cmd.bits_per_word = 8;
SYSERR(ioctl(this->fd, SPI_IOC_MESSAGE(1), &xfer_cmd));
}
TSPIDriver::TSPIDriver(const char* const spidev, const long long hz_speed) : hz_speed(hz_speed), fd(-1)
{
SYSERR(this->fd = open(spidev, O_RDWR | O_CLOEXEC | O_NOCTTY));
}
TSPIDriver::~TSPIDriver()
{
if(close(this->fd) == -1)
perror("TSPIDriver::~TSPIDriver: failed to close handle to spidev");
}
}