forked from kukulski/BeagleBoneUDPLED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tclled.c
157 lines (126 loc) · 3.66 KB
/
tclled.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* tclled.c
*
* Copyright 2012 Christopher De Vries
* This program is distributed under the Artistic License 2.0, a copy of which
* is included in the file LICENSE.txt
*/
#include "tclled.h"
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <errno.h>
#include <math.h>
void write_frame(tcl_color *p, uint8_t flag, uint8_t red, uint8_t green, uint8_t blue);
uint8_t make_flag(uint8_t red, uint8_t greem, uint8_t blue);
ssize_t write_all(int filedes, const void *buf, size_t size);
static uint8_t gamma_table_red[256];
static uint8_t gamma_table_green[256];
static uint8_t gamma_table_blue[256];
int tcl_init(tcl_buffer *buf, int leds) {
buf->leds = leds;
buf->size = (leds+3)*sizeof(tcl_color);
buf->buffer = (tcl_color*)malloc(buf->size);
if(buf->buffer==NULL) {
return -1;
}
buf->pixels = buf->buffer+1;
write_frame(buf->buffer,0x00,0x00,0x00,0x00);
write_frame(buf->pixels+leds,0x00,0x00,0x00,0x00);
write_frame(buf->pixels+leds+1,0x00,0x00,0x00,0x00);
return 0;
}
int spi_init(int filedes) {
int ret;
const uint8_t mode = SPI_MODE_0;
const uint8_t bits = 8;
const uint32_t speed = 15000000;
ret = ioctl(filedes,SPI_IOC_WR_MODE, &mode);
if(ret==-1) {
return -1;
}
ret = ioctl(filedes,SPI_IOC_WR_BITS_PER_WORD, &bits);
if(ret==-1) {
return -1;
}
ret = ioctl(filedes,SPI_IOC_WR_MAX_SPEED_HZ,&speed);
if(ret==-1) {
return -1;
}
return 0;
}
void write_bgra(tcl_color *p, uint32_t px) {
write_color(p,(px>>8 & 255),(px>>16 & 255),(px>>24));
}
void write_bgra_gamma(tcl_color *p, uint32_t px) {
write_gamma_color(p,(px>>8 & 255),(px>>16 & 255),(px>>24));
}
void write_color(tcl_color *p, uint8_t red, uint8_t green, uint8_t blue) {
uint8_t flag;
flag = make_flag(red,green,blue);
write_frame(p,flag,red,green,blue);
}
int send_buffer(int filedes, tcl_buffer *buf) {
int ret;
ret = (int)write_all(filedes,buf->buffer,buf->size);
return ret;
}
void tcl_free(tcl_buffer *buf) {
free(buf->buffer);
buf->buffer=NULL;
buf->pixels=NULL;
}
void write_frame(tcl_color *p, uint8_t flag, uint8_t red, uint8_t green, uint8_t blue) {
p->flag=flag;
p->blue=blue;
p->green=green;
p->red=red;
}
uint8_t make_flag(uint8_t red, uint8_t green, uint8_t blue) {
uint8_t flag;
flag = (red&0xc0)>>6;
flag |= (green&0xc0)>>4;
flag |= (blue&0xc0)>>2;
return ~flag;
}
ssize_t write_all(int filedes, const void *vbuf, size_t size) {
const char *buf = (const char*)vbuf;
ssize_t buf_len = (ssize_t)size;
size_t attempt = size;
ssize_t result;
while(size>0) {
result = write(filedes,buf,attempt);
if(result<0) {
if(errno==EINTR) continue;
else if(errno==EMSGSIZE) {
attempt = attempt/2;
result = 0;
}
else {
return result;
}
}
buf+=result;
size-=result;
if(attempt>size) attempt=size;
}
return buf_len;
}
void set_gamma(double gamma_red, double gamma_green, double gamma_blue) {
int i;
for(i=0;i<256;i++) {
gamma_table_red[i] = (uint8_t)(pow(i/255.0,gamma_red)*255.0+0.5);
gamma_table_green[i] = (uint8_t)(pow(i/255.0,gamma_green)*255.0+0.5);
gamma_table_blue[i] = (uint8_t)(pow(i/255.0,gamma_blue)*255.0+0.5);
}
}
void write_gamma_color(tcl_color *p, uint8_t red, uint8_t green, uint8_t blue) {
uint8_t flag;
uint8_t gamma_corrected_red = gamma_table_red[red];
uint8_t gamma_corrected_green = gamma_table_green[green];
uint8_t gamma_corrected_blue = gamma_table_blue[blue];
flag = make_flag(gamma_corrected_red,gamma_corrected_green,gamma_corrected_blue);
write_frame(p,flag,gamma_corrected_red,gamma_corrected_green,gamma_corrected_blue);
}