-
Notifications
You must be signed in to change notification settings - Fork 1
/
gradient.pde
104 lines (85 loc) · 2.37 KB
/
gradient.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
// Simple Gradient
// (C) 2010 Jacob Good
// License: MIT (included in LICENSE.txt)
//
// Notes: This demonstrates a gradient based on distance from the center.
// Each IXM cell responds to a click and sets itself as center,
// broadcasting it's event and passing around distance calculations,
// out over the graph. Each cell then turns on a set of LEDs to
// indicate it's *shortest* distance from the center.
u32 shortestDistance;
void setRed() {
ledOn(BODY_RGB_RED_PIN);
ledOff(BODY_RGB_BLUE_PIN);
ledOff(BODY_RGB_GREEN_PIN);
}
void setBlue() {
ledOff(BODY_RGB_RED_PIN);
ledOn(BODY_RGB_BLUE_PIN);
ledOff(BODY_RGB_GREEN_PIN);
}
void setGreen() {
ledOff(BODY_RGB_RED_PIN);
ledOff(BODY_RGB_BLUE_PIN);
ledOn(BODY_RGB_GREEN_PIN);
}
void setWhite() {
ledOn(BODY_RGB_RED_PIN);
ledOn(BODY_RGB_BLUE_PIN);
ledOn(BODY_RGB_GREEN_PIN);
}
void setBlack() {
ledOff(BODY_RGB_RED_PIN);
ledOff(BODY_RGB_BLUE_PIN);
ledOff(BODY_RGB_GREEN_PIN);
}
void branchGradient( u32 sourceFace, u32 distance ) {
if (sourceFace != NORTH) facePrintf(NORTH,"g%d\n",distance);
if (sourceFace != SOUTH) facePrintf(SOUTH,"g%d\n",distance);
if (sourceFace != EAST) facePrintf(EAST,"g%d\n",distance);
if (sourceFace != WEST) facePrintf(WEST,"g%d\n",distance);
}
void branchReset( u32 sourceFace ) {
if (sourceFace != NORTH) facePrintf(NORTH,"r\n");
if (sourceFace != SOUTH) facePrintf(SOUTH,"r\n");
if (sourceFace != EAST) facePrintf(EAST,"r\n");
if (sourceFace != WEST) facePrintf(WEST,"r\n");
}
void handleGradient( u8 * packet ) {
u32 distance;
packetScanf(packet,"g%d\n",&distance);
if (distance >= shortestDistance) return;
shortestDistance = distance;
if (distance == 1) setBlue();
else if (distance == 2) setGreen();
else if (distance == 3) setWhite();
branchGradient(packetSource(packet), distance + 1);
}
void resetSelf() {
setBlack();
shortestDistance = U32_MAX;
}
void handleReset( u8 * packet) {
resetSelf();
branchReset(packetSource(packet));
}
void stealGradient() {
println("r");
delay(500);
resetSelf();
setRed();
branchGradient(NOT_A_FACE,1);
}
void setup() {
resetSelf();
Body.reflex('g', handleGradient);
Body.reflex('r', handleReset);
}
void loop() {
if (buttonDown()) {
stealGradient();
}
}
#define SFB_SKETCH_CREATOR_ID B36_4(j,a,k,e)
#define SFB_SKETCH_PROGRAM_ID B36_4(g,r,a,d)
#define SFB_SKETCH_COPYRIGHT_NOTICE "MIT 2010 Jacob Good"