-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChromaduinoController.ino
104 lines (89 loc) · 2.08 KB
/
ChromaduinoController.ino
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
#include <Wire.h>
// Example of using I2C to drive a Chromaduino/Funduino LED matrix Peripheral
// Just cycles colours
// Mark Wilson Dec '16
#define I2C_ADDR 0x70 // the Chromaduino/Funduino I2C address
#define RST_PIN A0 // this pin is connected to DTR on the Chromaduino/Funduino
byte balanceRGB[3] = {22, 63, 63};
byte colourRGB[3] = {255, 0, 0};
void StartBuffer()
{
// start writing
Wire.beginTransmission(I2C_ADDR);
Wire.write((byte)0x00);
Wire.endTransmission();
delay(1);
}
void WriteRGB(byte* pRGB)
{
// write a triple
Wire.beginTransmission(I2C_ADDR);
for (int idx = 0; idx < 3; idx++, pRGB++)
Wire.write(*pRGB);
Wire.endTransmission();
delay(1);
}
void ShowBuffer()
{
// flip the buffers
Wire.beginTransmission(I2C_ADDR);
Wire.write((byte)0x01);
Wire.endTransmission();
delay(1);
}
bool SetBalance()
{
// true if there are 3 bytes in the Peripheral's buffer
Wire.requestFrom(I2C_ADDR, 1);
byte count = 0;
if (Wire.available())
count = Wire.read();
Wire.beginTransmission(I2C_ADDR);
Wire.write((byte)0x02); // set the 3 bytes to be balance
Wire.endTransmission();
delay(1);
return count == 3; // the Peripheral got 3 bytes
}
void DisplayBuffer()
{
// fill the buffer and display it
StartBuffer();
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++)
WriteRGB(colourRGB);
ShowBuffer();
}
bool Configure()
{
// write the balance, true if got expected response from matrix
StartBuffer();
WriteRGB(balanceRGB);
return SetBalance();
}
void swap(byte& a, byte& b)
{
byte t = a;
a = b;
b = t;
}
void setup()
{
Wire.begin();
// reset the board
pinMode(RST_PIN, OUTPUT);
digitalWrite(RST_PIN, LOW);
delay(1);
digitalWrite(RST_PIN, HIGH);
// keep trying to set the Peripheral's balance until it's awake
do
{
delay(100);
} while (!Configure());
}
void loop()
{
DisplayBuffer();
delay(1000);
swap(colourRGB[0], colourRGB[1]);
swap(colourRGB[1], colourRGB[2]);
}