-
Notifications
You must be signed in to change notification settings - Fork 12
/
RainbowduinoDevice.pde
143 lines (111 loc) · 3.29 KB
/
RainbowduinoDevice.pde
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
import processing.serial.*;
import com.rngtng.rainbowduino.*;
class RainbowduinoDevice implements Device, StandaloneDevice {
public Rainbowduino rainbowduino;
public boolean running;
int bright = 4;
RainbowduinoDevice(PApplet app) {
this(app, null, 0);
}
RainbowduinoDevice(PApplet app, String port_name) {
this(app, port_name, 0);
}
RainbowduinoDevice(PApplet app, int baud_rate) {
this(app, null, baud_rate);
}
RainbowduinoDevice(PApplet app, String port_name, int baud_rate) {
rainbowduino = new Rainbowduino(app);
rainbowduino.initPort(port_name, baud_rate, true);
rainbowduino.brightnessSet(this.bright);
rainbowduino.reset();
running = false;
}
void setColorScheme() {
PixelColorScheme.R = new int[]{
0,255 };
PixelColorScheme.G = new int[]{
0,255 };
PixelColorScheme.B = new int[]{
0,255 };
}
boolean draw_as_circle() {
return true;
}
boolean enabled() {
return rainbowduino.connected();
}
/* +++++++++++++++++++++++++++ */
public void write_frame(Frame frame) {
write_frame(0, frame);
}
public void write_frame(int num, Frame frame) {
if(frame == null || running || !enabled() ) return;
rainbowduino.bufferSetAt(num, get_frame_rows(frame));
}
public void write_matrix(Matrix matrix) {
print("Start Writing Matrix - ");
for(int f = 0; f < matrix.num_frames(); f++) {
write_frame(f, matrix.frame(f));
}
rainbowduino.bufferSave();
println("Done");
}
public Matrix read_matrix() {
Matrix matrix = new Matrix(8,8);
print("Start Reading Matrix - ");
int frames = rainbowduino.bufferLoad(); //return num length
println( "Frames:" + frames);
for( int frame_nr = 0; frame_nr < frames; frame_nr++ ) {
//println("Frame Nr: " + frame_nr);
Frame frame = matrix.current_frame();
int frame_byte[] = rainbowduino.bufferGetAt(frame_nr);
for(int y = 0; y < 8; y++ ) {
for(int x = 0; x < 8; x++ ) {
frame.set_pixel(x,y, new PixelColor((frame_byte[3*y+0] >> x) & 1, (frame_byte[3*y+1] >> x) & 1, (frame_byte[3*y+2] >> x) & 1 ) );
}
}
matrix.add_frame();
}
matrix.delete_frame();
println("Done");
return matrix;
}
public void toggle() {
if(running) {
running = false;
rainbowduino.reset();
return;
}
running = true;
rainbowduino.bufferLoad();
rainbowduino.start();
}
public void speedUp() {
rainbowduino.speedUp();
}
public void speedDown() {
rainbowduino.speedDown();
}
public void brightnessUp() {
this.bright++;
rainbowduino.brightnessSet(this.bright);
}
public void brightnessDown() {
this.bright--;
if(this.bright < 1) this.bright = 1;
rainbowduino.brightnessSet(this.bright);
}
////////////////////////////////////////////////////
private int[] get_frame_rows(Frame frame) {
int[] res = new int[3*frame.rows];
for( int y = 0; y < frame.rows; y++ ) {
for( int x = 0; x < frame.cols; x++ ) {
PixelColor p = frame.get_pixel(x,y);
res[3*y + 0] |= (p.r & 1) << x;
res[3*y + 1] |= (p.g & 1) << x;
res[3*y + 2] |= (p.b & 1) << x;
}
}
return res;
}
}