-
Notifications
You must be signed in to change notification settings - Fork 115
/
DisplayUSB2812.c
174 lines (154 loc) · 3.74 KB
/
DisplayUSB2812.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//Copyright 2015 <>< Charles Lohr under the ColorChord License.
// only compile file if we are one a unix system
#if defined( WINDOWS ) || defined( USE_WINDOWS ) || defined( WIN32 ) || defined( WIN64 ) || \
defined( _WIN32 ) || defined( _WIN64 )
#warning Windows does not support the DisplayUSB2812 module
#else
#include "outdrivers.h"
#include "notefinder.h"
#include <stdio.h>
#include <string.h>
#include "parameters.h"
#include <stdlib.h>
#include <libusb.h>
#include "color.h"
#include <math.h>
#include <unistd.h>
struct LEDOutDriver
{
struct libusb_device_handle *devh;
int did_init;
int zigzag;
int total_leds;
int array;
float outamp;
uint8_t * last_leds;
volatile int readyFlag;
int xn;
int yn;
int rot90;
};
static void * LEDOutThread( void * v )
{
struct LEDOutDriver * led = (struct LEDOutDriver*)v;
while(1)
{
int total_bytes = (led->total_leds*3)+1;
total_bytes = ((total_bytes-1)&0xffc0) + 0x40; //Round up.
if( led->readyFlag )
{
int r = libusb_control_transfer( led->devh,
0x40, //reqtype
0xA5, //request
0x0100, //wValue
0x0000, //wIndex
led->last_leds,
total_bytes,
1000 );
if( r < 0 )
{
led->did_init = 0;
printf( "Fault sending LEDs.\n" );
}
led->readyFlag = 0;
}
usleep(100);
}
return 0;
}
static void LEDUpdate(void * id, struct NoteFinder*nf)
{
int i;
struct LEDOutDriver * led = (struct LEDOutDriver*)id;
if( !led->did_init )
{
led->did_init = 1;
if( libusb_init(NULL) < 0 )
{
fprintf( stderr, "Error: Could not initialize libUSB\n" );
// exit( -99 );
}
led->devh = libusb_open_device_with_vid_pid( NULL, 0xabcd, 0xf003 );
if( !led->devh )
{
fprintf( stderr, "Error: Cannot find device.\n" );
// exit( -98 );
}
}
while( led->readyFlag ) usleep(100);
//Advance the LEDs to this position when outputting the values.
for( i = 0; i < led->total_leds; i++ )
{
int source = i;
if( !led->array )
{
int sx, sy;
if( led->rot90 )
{
sy = i % led->yn;
sx = i / led->yn;
}
else
{
sx = i % led->xn;
sy = i / led->xn;
}
if( led->zigzag )
{
if( led->rot90 )
{
if( sx & 1 )
{
sy = led->yn - sy - 1;
}
}
else
{
if( sy & 1 )
{
sx = led->xn - sx - 1;
}
}
}
if( led->rot90 )
{
source = sx + sy * led->xn;
}
else
{
source = sx + sy * led->yn;
}
}
led->last_leds[i*3+0] = OutLEDs[source*3+1] * led->outamp;
led->last_leds[i*3+1] = OutLEDs[source*3+0] * led->outamp;
led->last_leds[i*3+2] = OutLEDs[source*3+2] * led->outamp;
}
led->readyFlag = 1;
}
static void LEDParams(void * id )
{
struct LEDOutDriver * led = (struct LEDOutDriver*)id;
led->total_leds = GetParameterI( "leds", 300 );
led->last_leds = malloc( led->total_leds * 3 + 1 );
led->outamp = .1; RegisterValue( "ledoutamp", PAFLOAT, &led->outamp, sizeof( led->outamp ) );
led->zigzag = 0; RegisterValue( "zigzag", PAINT, &led->zigzag, sizeof( led->zigzag ) );
led->xn = 16; RegisterValue( "lightx", PAINT, &led->xn, sizeof( led->xn ) );
led->yn = 9; RegisterValue( "lighty", PAINT, &led->yn, sizeof( led->yn ) );
led->rot90 = 0; RegisterValue( "rot90", PAINT, &led->rot90, sizeof( led->rot90 ) );
led->array = 0; RegisterValue( "ledarray", PAINT, &led->array, sizeof( led->array ) );
led->did_init = 0;
}
static struct DriverInstances * DisplayUSB2812()
{
struct DriverInstances * ret = malloc( sizeof( struct DriverInstances ) );
memset( ret, 0, sizeof( struct DriverInstances ) );
struct LEDOutDriver * led = ret->id = malloc( sizeof( struct LEDOutDriver ) );
ret->Func = LEDUpdate;
ret->Params = LEDParams;
OGCreateThread( LEDOutThread, led );
led->readyFlag = 0;
LEDParams( led );
return ret;
}
REGISTER_OUT_DRIVER(DisplayUSB2812);
#endif