-
-
Save anonymous/efdc5af748162f268428b6b60485f58a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
arduino I2C to Colorduino demo | |
based on | |
-arduino firmware by michael vogt <michu@neophob.com> | |
-blinkm firmware by thingM | |
-"daft punk" firmware by Scott C / ThreeFN | |
libraries to patch: | |
Wire: | |
utility/twi.h: #define TWI_FREQ 400000L (was 100000L) | |
#define TWI_BUFFER_LENGTH 70 (was 32) | |
wire.h: #define BUFFER_LENGTH 70 (was 32) | |
This DEMO is free software; you can redistribute it and/or | |
modify it under the terms of the GNU Lesser General Public | |
License as published by the Free Software Foundation; either | |
version 2.1 of the License, or (at your option) any later version. | |
This DEMO is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
Lesser General Public License for more details. | |
You should have received a copy of the GNU Lesser General Public | |
License along with this library; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
#include <Wire.h> | |
#define I2C_START_OF_DATA 0x10 //data markers | |
#define I2C_END_OF_DATA 0x20 //data markers | |
#define I2C_MASTER_ADDRESS 1 | |
#define SERIAL_DEVICE_ID 0xD2 | |
#define SERIAL_START_BYTE 0xFE | |
#define SERIAL_END_BYTE 0x01 | |
#define SERIAL_ACK_COMMAND 0xA0 | |
#define SERIAL_PING_COMMAND 0xB0 | |
#define SERIAL_TEXT_COMMAND 0xB1 | |
#define SERIAL_RGB_COMMAND 0xB2 | |
#define LED_SCREENSIZEX 8 //num of LEDs accross | |
#define LED_SCREENSIZEY 8 //num of LEDs down | |
byte display_byte[3][64]; //display array - 64 bytes x 3 colours | |
int x; | |
int y; | |
int r; | |
int g; | |
int b; | |
int a; | |
byte major = 0; | |
byte minor = 20; | |
byte buff[256]; | |
int index = 0; | |
void setup() | |
{ | |
pinMode(SDA, INPUT); // remove internal pullup (also check twi.h) | |
pinMode(SCL, INPUT); // remove internal pullup (also check twi.h) | |
Serial.begin(115200, SERIAL_8N1); | |
Serial.setTimeout(1); | |
Serial.println("Setting up Master"); | |
Serial.print("Master Address: "); | |
Serial.println(I2C_MASTER_ADDRESS); | |
Serial.print("Wire Buffer Length: "); | |
Serial.println(BUFFER_LENGTH); | |
Wire.setClock ( 400000L ) ; | |
Wire.begin(I2C_MASTER_ADDRESS); // join i2c bus (address optional for master) | |
// plasma_setup(); //plasma setup | |
} | |
void loop() | |
{ | |
while (Serial.available() > 0) { | |
byte recv = Serial.read(); | |
if (recv == SERIAL_START_BYTE) { | |
index = 0; | |
} | |
buff[index] = recv; | |
index++; | |
if (recv == SERIAL_END_BYTE) { | |
handlePacket(buff); | |
} | |
} | |
delay(20); | |
} | |
void handlePacket(byte packet[]) { | |
byte command = packet[3]; | |
if ( command == SERIAL_RGB_COMMAND ) { | |
a = packet[4]; // address | |
for (x = 0; x < LED_SCREENSIZEX; x++) { | |
for (y = 0; y < LED_SCREENSIZEY; y++) { | |
r = packet[5 + (3 * x) + (y * 24)]; // red | |
g = packet[5 + (3 * x) + 1 + (y * 24)]; // green | |
b = packet[5 + (3 * x) + 2 + (y * 24)]; // blue | |
display(x, y, r, g, b); | |
} | |
} | |
update_display(a); | |
ackCommand(command); | |
} else { | |
textCommand("Unknown Command: " + String(command)); | |
} | |
} | |
void textCommand(String text) { | |
byte payload[ 4 ] = | |
{ | |
SERIAL_START_BYTE, // (FF) start byte | |
major, // (00-FE) major firmware version | |
minor, // (00-FE) minor firmware version | |
SERIAL_TEXT_COMMAND, // (00-FE) command id | |
}; | |
for ( int i = 0; i < sizeof(payload) / sizeof(payload[ 0 ]); i++ ) | |
Serial.write( payload[ i ] ); | |
byte textBytes[text.length() + 1]; | |
text.getBytes(textBytes, text.length() + 1); | |
Serial.write(textBytes, text.length() + 1 ); | |
Serial.write(SERIAL_END_BYTE); | |
} | |
void pingCommand() | |
{ | |
byte payload[ 6 ] = | |
{ | |
SERIAL_START_BYTE, | |
major, | |
minor, | |
SERIAL_PING_COMMAND, | |
SERIAL_DEVICE_ID, | |
SERIAL_END_BYTE | |
}; | |
for ( int i = 0; i < sizeof(payload) / sizeof(payload[ 0 ]); i++ ) | |
Serial.write( payload[ i ] ); | |
} | |
void ackCommand(byte command) | |
{ | |
byte payload[ 6 ] = | |
{ | |
SERIAL_START_BYTE, | |
major, | |
minor, | |
SERIAL_ACK_COMMAND, | |
command, | |
SERIAL_END_BYTE | |
}; | |
for ( int i = 0; i < sizeof(payload) / sizeof(payload[ 0 ]); i++ ) | |
Serial.write( payload[ i ] ); | |
} | |
//update display buffer using x,y,r,g,b format | |
void display(byte x, byte y, byte r, byte g, byte b) { | |
byte p = (y * 8) + x; //convert from x,y to pixel number in array | |
display_byte[0][p] = r; | |
display_byte[1][p] = g; | |
display_byte[2][p] = b; | |
} | |
//send display buffer to display | |
void update_display(byte addr) { | |
send_i2c(addr, 0, display_byte[0]); // red | |
send_i2c(addr, 1, display_byte[1]); // green | |
send_i2c(addr, 2, display_byte[2]); // blue | |
} | |
//send data via I2C to a client | |
static byte send_i2c(byte addr, byte col, byte disp_data[]) { | |
Wire.beginTransmission(addr); | |
Wire.write(I2C_START_OF_DATA); | |
Wire.write(col); | |
Wire.write(disp_data, 64); | |
Wire.write(I2C_END_OF_DATA); | |
return Wire.endTransmission(); | |
delay(1); | |
} | |
void serialEvent() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment