I just developed a serial communication protocol, there is also several custom modifications but you can get the idea:
for the seedmaster:
[code]#include <Wire.h>
#define EEPROM_ADDR 0x50 // I2C Buss address of 24LC256 256K EEPROM
unsigned char RainbowCMD[7];
unsigned char State = 0;
unsigned long timeout;
int ledPin=13;
int first=0;
int timeDelaySend=15;
void setup(){
Wire.begin(); // join I2C bus (address optional for master)
Serial.begin(115200);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){
int test;
//blue
/for(test=15;test>0;test=test-4)
{
ShowColor(4,test, 0, 0);delay(500);
ShowColor(4,0, 0, 0);delay(500);
}/
// TESTS FOR EACH FUNCTION BEGIN HERE
/*Serial.println(“Writing Test:”);
for (int i=0; i<32; i++){ // loop for first 20 slots
i2c_eeprom_write_byte(EEPROM_ADDR,i,77-i); // write address + 65 A or 97 a 77-i
Serial.print(". ");
delay(5); // NEED THIS DELAY! (tests suggest it can be as
// small as delay(3) – BBR
}
Serial.println("");
//delay(500);
Serial.println(“Reading Test:”);
for (int i=0; i<32; i++){ // loop for first 20 slots
Serial.print(i2c_eeprom_read_byte(EEPROM_ADDR, i),BYTE);
Serial.print(" “);
}
Serial.println(” “);
Serial.println((int)3/2, DEC);
Serial.println(0%2, DEC);
Serial.println(” ");*/
//SetPixelInImage(0, 0x0F,0, 1,0,3, 3); //show image 1
if (first==0){
int symbol=0;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){
// if (i==3&& j==3) SetPixelInImage(4, 0, 0,0x03, 0,i, j); //SetPixelInImage(Address,blue,red,green,number,rowI,colI)
// else if (i==0&& j==0) SetPixelInImage(4, 0, 0,0x03, 0,i, j);
if (i==2) SetPixelInImage(0, 0,0x03, symbol,i, j); //row 3!
if (j==2) SetPixelInImage(0, 0,0x03, 0,i, j); //colum 3!
if (i!=2 && j!=2) SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage(4, 0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
symbol=1;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){
if (i==1 || i==3 || j==1 || j==3) SetPixelInImage(0, 0x03, 0, symbol,i, j); //row 3!
//if (j==1 || j==3) SetPixelInImage(4, 0x03, 0, 0, 1,i, j); //colum 3!
else SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage(4, 0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
symbol=2;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){//0x00, 0x0D, 0x06 yellow
if (i==0 || i==4 || j==0 || j==4) SetPixelInImage(0x03, 0, 0, symbol,i, j); //row 3!
//if (j==1 || j==3) SetPixelInImage(4, 0x03, 0, 0, 1,i, j); //colum 3!
else SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage(4, 0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
symbol=3;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){//0x00, 0x0D, 0x06 yellow
if (i==2 && j==0) SetPixelInImage(0x03, 0, 0, symbol,i, j); //row 3!
//if (j==1 || j==3) SetPixelInImage(4, 0x03, 0, 0, 1,i, j); //colum 3!
else SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage(0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
symbol=4;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){//0x00, 0x0D, 0x06 yellow
if (i==2 && j==1) SetPixelInImage(0x03, 0 , 0, symbol,i, j); //row 3!
//if (j==1 || j==3) SetPixelInImage(0x03, 0, 0, 1,i, j); //colum 3!
else SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage( 0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
symbol=5;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){//0x00, 0x0D, 0x06 yellow
if (i==2 && j==2) SetPixelInImage(0x03, 0, 0, symbol,i, j); //row 3!
//if (j==1 || j==3) SetPixelInImage(0x03, 0, 0, 1,i, j); //colum 3!
else SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage(0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
symbol=6;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){//0x00, 0x0D, 0x06 yellow
if (i==2 && j==3) SetPixelInImage(0x03, 0x00, 0x00, symbol,i, j); //row 3!
//if (j==1 || j==3) SetPixelInImage(0x03, 0, 0, 1,i, j); //colum 3!
else SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage(0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
symbol=7;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){//0x00, 0x0D, 0x06 yellow
if (i==2 && j==4) SetPixelInImage(0x03, 0x00, 0x00, symbol,i, j); //row 3!
//if (j==1 || j==3) SetPixelInImage(0x03, 0, 0, 1,i, j); //colum 3!
else SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage(0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
symbol=8;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){//0x00, 0x0D, 0x06 yellow
SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
delay(timeDelaySend);
}
}
SetPixelInImage(0x03, 0, 0, symbol,1, 1);delay(timeDelaySend);
SetPixelInImage(0x03, 0, 0, symbol,1, 2);delay(timeDelaySend);
SetPixelInImage(0x03, 0, 0, symbol,1, 3);delay(timeDelaySend);
SetPixelInImage(0x03, 0, 0, symbol,2, 1);delay(timeDelaySend);
SetPixelInImage(0x03, 0, 0, symbol,2, 3);delay(timeDelaySend);
SetPixelInImage(0x03, 0, 0, symbol,3, 1);delay(timeDelaySend);
SetPixelInImage(0x03, 0, 0, symbol,3, 2);delay(timeDelaySend);
SetPixelInImage(0x03, 0, 0, symbol,3, 3);delay(timeDelaySend);
symbol=9;
SetPixelInImage(0, 0,0, symbol,0, 0);
for(int i=0; i<5;i++){
for (int j=0; j<5; j++){
if (i==1 || i==3 || j==1 || j==3) SetPixelInImage(0, 0x03, 0, symbol,i, j); //row 3!
//if (j==1 || j==3) SetPixelInImage(4, 0x03, 0, 0, 1,i, j); //colum 3!
else SetPixelInImage(0, 0,0, symbol,i, j); //colum 3!
//else SetPixelInImage(4, 0, 0,0, 0,i, j);
delay(timeDelaySend);
}
}
first=1;
}
delay(1000);
ShowImage2(9,0, 1);
ShowImage2(9,0, 2);
SetAudio(7, 2);SetAudio(7, 1);
delay(1000);
ShowImage2(8,0, 1);
ShowImage2(8,0, 2);
ClearAudio(0, 2);ClearAudio(0, 1);
delay(1000);
setJustOnePixel(0x0F , 0,0,0, 1, 1 );
setJustOnePixel(0x0F , 0,0,0, 1, 2 );
delay(1000);
/SetAudio(7, 2);SetAudio(7, 1);
delay(1000);/
/digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);/
/*
//test corner (0,0)
setJustOnePixel(0x0F , 0,0,0, 0, 1 );
delay(5000);
ShowColor(15 , 15, 15, 1);
delay(5000);
delay(1);
*/
//test shift
/*ShowImage2(1,0, 2);delay(100);
ShowImage2(1,1, 2);
ShowImage2(1,-4, 1); delay(100);
ShowImage2(1,2, 2);
ShowImage2(1,-3, 1); delay(100);
ShowImage2(1,3, 2);
ShowImage2(1,-2, 1); delay(100);
ShowImage2(1,4, 2);
ShowImage2(1,-1, 1); delay(100);
ShowImage2(1,5, 2);
ShowImage2(1,0, 1); delay(100);
//going back
ShowImage2(1,-1, 1);
ShowImage2(1,4, 2);delay(100);
ShowImage2(1,-2, 1);
ShowImage2(1,3, 2);delay(100);
ShowImage2(1,-3, 1);
ShowImage2(1,2, 2);delay(100);
ShowImage2(1,-4, 1);
ShowImage2(1,1, 2);delay(100);
ShowImage2(1,5, 1);
ShowImage2(1,0, 2);delay(100); */
/for (int i=0; i<6;i ++){
ShowImage2(1,i, 1); //show image2 number,shift, ID_BOARD
ShowImage2(1,i, 2); //show image2 number,shift, ID_BOARD
delay(500);
}
for (int i=5; i>-6; i–){
ShowImage2(1,i, 1); //show image2 number,shift, ID_BOARD
ShowImage2(1,i, 2); //show image2 number,shift, ID_BOARD
delay(500);
}/
//ShowColor(0, 0, 0);delay(500);
//delay(5);
//ShowImage2(0,0, 1); //show image2 number,shift, ID_BOARD
//delay(500);
//ShowColor(0, 0, 0);delay(500);
//ClearAudio(0, 2);ClearAudio(0, 1); //or also now SetAudio(0, 2);SetAudio(0, 1);
//delay(1000);
/digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);/
//delay(5);
//ShowImage2(2,0, 1); //show image 2; (num , shift, ID)
//delay(500);
//ShowColor(0, 0, 0);delay(500);*/
/*
for(int s=3; s<8; s++){
ShowImage2(s,0, 1); //show image 2; (add , num , shift)
delay(25);
}
for(int s=7; s>2; s–){
ShowImage2(s,0, 1); //show image 2; (add , num , shift)
delay(25);
}/
/
int piv=0;
int j=0;
for(int s=0; s<5; s++){
for(int j=0; j<5; j++){
setJustOnePixel(0x0F , 0,0,s, j, 1 );
delay(100);
}
s++;
if(s<5){
for(int j=4; j>=0; j–){
setJustOnePixel(0x0F , 0,0,s, j, 1 );
delay(100);
}
}
}/
/setJustOnePixel(0x0F , 0,0,0, 0, 1 );
delay(1000);/
/
for(test=1;test<10;test++)
{
ShowImage(test, 0);delay(500);
ShowColor(0, 0, 0);delay(500);
}*/
/delay(500);
i2c_eeprom_write_byte(EEPROM_ADDR, 0, 0x4C);
delay(5);
byte val=i2c_eeprom_read_byte(EEPROM_ADDR, 0 );
Serial.println(i2c_eeprom_read_byte(EEPROM_ADDR, 0),BYTE);
delay(500);
if (val==0x4C)digitalWrite(ledPin, HIGH);
else digitalWrite(ledPin,LOW);/
}
//--------------------------------------------------------------------------
//Name:ShowColor
//function: Send a conmand to Rainbowduino for showing a color
//parameter: Address: rainbowduino IIC address
// red,green,blue: the color RGB
//----------------------------------------------------------------------------
void ShowColor(unsigned char red , unsigned char green, unsigned char blue, unsigned char ID_BOARD)
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x03;
RainbowCMD[2]=red;
RainbowCMD[3]=((green<<4)|(blue));
SentCMD();
}
//--------------------------------------------------------------------------
//Name:ShowImage
//function: Send a conmand to Rainbowduino for showing a picture which was pre-set in Rainbowduino Flash
//parameter: Address: rainbowduino IIC address
// number: the pre-set picture position
// shift: the picture shift bit for display
//----------------------------------------------------------------------------
void ShowImage(unsigned char number,unsigned char shift, unsigned char ID_BOARD)
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x01;
RainbowCMD[2]=(shift<<4);
RainbowCMD[4]=number;
SentCMD();
}
//--------------------------------------------------------------------------
//Name:ShowImage2
//function: Send a conmand to Rainbowduino for showing a picture which was pre-set in Rainbowduino EEPROM
//parameter: Address: rainbowduino IIC address
// number: the pre-set picture position
// shift: the picture shift bit for display
//----------------------------------------------------------------------------
void ShowImage2(unsigned char number,signed char shift, unsigned char ID_BOARD)
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x04;
shift+=5;
RainbowCMD[2]=(shift<<4);
RainbowCMD[4]=number;
RainbowCMD[6]=ID_BOARD;
SentCMD();
}
//--------------------------------------------------------------------------
//Name:ShowImage3
//function: Send a conmand to Rainbowduino for showing a picture which was pre-set in Rainbowduino EEPROM
//parameter: Address: rainbowduino IIC address
// number: the pre-set picture position
// shift: the picture shift bit for display
// audioChannels: number of channels ON in binary (ex. 0x03- channels 1 and 2)
//----------------------------------------------------------------------------
void ShowImage3(unsigned char number,signed char shift,unsigned char audioChannels, unsigned char ID_BOARD)
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x09;
shift+=5;
RainbowCMD[2]=(shift<<4);
RainbowCMD[4]=number;
RainbowCMD[5]=audioChannels; //doesnt follow the structure from before but its more efficient
RainbowCMD[6]=ID_BOARD;
SentCMD();
}
//--------------------------------------------------------------------------
//Name:SetPixelInImage
//function: Send a conmand to Rainbowduino for setting a pixel in an image pre-set in Rainbowduino external EEPROM
//parameter: Address: rainbowduino IIC address
// red,green,blue: the color RGB
// number: the stored EEPROM picture position (0- first image, 1- second image, …)
// shift: the picture shift bit for display
// rowI and colI: positions of the pixel
//
// Note: this command doesnt have a ID_BOARD because it is a global command for every board.
//----------------------------------------------------------------------------
void SetPixelInImage(unsigned char red, unsigned char green, unsigned char blue , unsigned char number,unsigned char rowI, unsigned char colI )
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x05;
RainbowCMD[2]=(red);
RainbowCMD[3]=((green<<4)|(blue));
RainbowCMD[4]=number;
RainbowCMD[5]=((rowI<<4)|(colI));
SentCMD();
}
//--------------------------------------------------------------------------
//Name:GetPixelInImage
//function: Send a conmand to Rainbowduino for getting a pixel in an image pre-set in Rainbowduino external EEPROM
//parameter: Address: rainbowduino IIC address
// red,green,blue: the color RGB
// number: the stored EEPROM picture position (0- first image, 1- second image, …)
// shift: the picture shift bit for display
// rowI and colI: positions of the pixel
//
// Note: right now its not implemented due to one-way communication.
//----------------------------------------------------------------------------
void GetPixelInImage(unsigned char red, unsigned char green, unsigned char blue , unsigned char number,unsigned char rowI, unsigned char colI )
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x0B;
/RainbowCMD[2]=(red);
RainbowCMD[3]=((green<<4)|(blue));
RainbowCMD[4]=number;
RainbowCMD[5]=((rowI<<4)|(colI));/
SentCMD();
}
//--------------------------------------------------------------------------
//Name:SetJustOnePixel
//function: Send a conmand to Rainbowduino for setting just one pixel on display
//parameter: Address: rainbowduino IIC address
// red,green,blue: the color RGB
// number: the stored EEPROM picture position
// shift: the picture shift bit for display
// rowI and colI: positions of the pixel
//----------------------------------------------------------------------------
void setJustOnePixel(unsigned char red, unsigned char green, unsigned char blue,unsigned char rowI, unsigned char colI, unsigned char ID_BOARD )
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x06;
RainbowCMD[2]=(red);
RainbowCMD[3]=((green<<4)|(blue));
RainbowCMD[5]=((rowI<<4)|(colI));
RainbowCMD[6]=ID_BOARD;
SentCMD();
}
//--------------------------------------------------------------------------
//Name:SetJustOnePixel
//function: Send a conmand to Rainbowduino for setting just one pixel on display
//parameter: Address: rainbowduino IIC address
// red,green,blue: the color RGB
// number: the stored EEPROM picture position
// shift: the picture shift bit for display
// rowI and colI: positions of the pixel
// AudioChannels: audio channels activated
//----------------------------------------------------------------------------
void setJustOnePixelWithAudio(unsigned char red, unsigned char green, unsigned char blue,unsigned char rowI, unsigned char colI, unsigned char AudioChannels, unsigned char ID_BOARD )
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x0A;
RainbowCMD[2]=(red);
RainbowCMD[3]=((green<<4)|(blue));
RainbowCMD[4]=AudioChannels;
RainbowCMD[5]=((rowI<<4)|(colI));
RainbowCMD[6]=ID_BOARD;
SentCMD();
}
//--------------------------------------------------------------------------
//Name:ShowColor
//function: Send a conmand to Rainbowduino for showing a color
//parameter: Address: rainbowduino IIC address
// red,green,blue: the color RGB
// shift: the picture shift bit for display
// ASCII:the char or Number want to show
//----------------------------------------------------------------------------
void ShowChar(unsigned char ASCII,unsigned char red, unsigned char blue ,unsigned char green,unsigned char shift, unsigned char ID_BOARD)
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x02;
RainbowCMD[2]=((shift<<4)|(red));
RainbowCMD[3]=((green<<4)|(blue));
RainbowCMD[4]=ASCII;
SentCMD();
}
//--------------------------------------------------------------------------
//Name:SetAudio
//function: Send a conmand to Rainbowduino for selecting an audio channel
//parameter: number: number of the channel to activate
// ID_BOARD: identificator of the board
//----------------------------------------------------------------------------
void SetAudio(unsigned char number, unsigned char ID_BOARD)
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x07;
RainbowCMD[4]=number;
RainbowCMD[6]=ID_BOARD;
SentCMD();
}
//--------------------------------------------------------------------------
//Name:ClearAudio
//function: Send a conmand to Rainbowduino for selecting an audio channel
//parameter: number: number of the channel to activate
// ID_BOARD: identificator of the board
//----------------------------------------------------------------------------
void ClearAudio(unsigned char number, unsigned char ID_BOARD)
{
RainbowCMD[0]=‘R’;
RainbowCMD[1]=0x08;
RainbowCMD[4]=number;
RainbowCMD[6]=ID_BOARD;
SentCMD();
}
//--------------------------------------------------------------------------
//Name:SentCMD
//function: Send a 5 byet Rainbow conmand out
//parameter: NC
//----------------------------------------------------------------------------
void SentCMD()
{ unsigned char OK=0;
unsigned char i,temp;
while(!OK)
{
switch (State)
{
case 0:
//Wire.beginTransmission(Add);
for (i=0;i<7;i++) Serial.write(RainbowCMD[i]); //Wire.send(RainbowCMD[i]);
//Wire.endTransmission();
//delay(5); //should be changed in the future
State=1;
break;
case 1:
//Wire.requestFrom(Add,1);
// if (Serial.available()>0)
// temp=Serial.read();
// else {
// temp =0xFF;
// timeout++;
// }
//
// if ((temp==1)||(temp==2)) State=2;
// else if (temp==0) State=0;
//
// if (timeout>5)
// {
// timeout=0;
// State=0;
// }
State=2;
delay(5);
break;
case 2:
OK=1;
State=0;
break;
default:
State=0;
break;
}
}
}
// External EEPROM - Wathc out this one does work for Microchip EEPROMs
//
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // Address High Byte
Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.send(rdata);
Wire.endTransmission();
}
// Address is a page address, 6-bit (63). More and end will wrap around
// But data can be maximum of 28 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddresspage >> 8)); // Address High Byte
Wire.send((int)(eeaddresspage & 0xFF)); // Address Low Byte
byte c;
for ( c = 0; c < length; c++)
Wire.send(data[c]);
Wire.endTransmission();
delay(10); // need some delay
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // Address High Byte
Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.receive();
return rdata;
}
// should not read more than 28 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // Address High Byte
Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
//int c = 0;
for ( int c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.receive();
}
[/code]
And for the rainbowduino:
[code]#include “Rainbow.h”
#include <Wire.h>
#include <avr/pgmspace.h>
#define EEPROM_ADDR 0x50
#define ID_BOARD 0x01
/*
Modifications:
- Transform it to serial ->OK
- Convert 8x8 to 5x5 matrix ->OK
- Send 6 bytes on communication instead of 5 ->OK
- Write in EEPROM ->OK
- Messages with a board ID (send 7 bytes instead of 5) -> OK
- implement shift for images
- implement light only one point -> OK
*/
//=============================================================
extern unsigned char dots_color[2][3][8][4]; //define Two Buffs (one for Display ,the other for receive data)
extern unsigned char GamaTab[16]; //define the Gamma value for correct the different LED matrix
extern unsigned char Prefabnicatel[5][3][8][4];
extern unsigned char ASCII_Char[52][8];
extern unsigned char ASCII_Number[10][8];
//=============================================================
unsigned char line,level;
unsigned char Buffprt=0;
unsigned char State=0;
unsigned char g8Flag1;
unsigned char RainbowCMD[7]={0,0,0,0,0,0, 0};
unsigned char toggle=0;
void setup()
{
_init();
// TESTS FOR EACH FUNCTION BEGIN HERE
/Serial.println(“Writing Test:”);
for (int i=0; i<32; i++){ // loop for first 20 slots
i2c_eeprom_write_byte(EEPROM_ADDR,i,77-i); // write address + 65 A or 97 a 77-i
Serial.print(". ");
delay(5); // NEED THIS DELAY! (tests suggest it can be as
// small as delay(3) – BBR
}/
/Serial.println("");
//delay(500);
Serial.println(“Reading Test:”);
for (int i=0; i<45; i++){ // loop for first 20 slots
Serial.print(i2c_eeprom_read_byte(EEPROM_ADDR, i),BYTE);
Serial.print(" ");
}/
}
void loop()
{
switch (State)
{
case waitingcmd:
if (Serial.available()>0) receiveEvent(1); //the one doesnt matter here
break;
case processing:
GetCMD();
//State=checking;
State=waitingcmd;
break;
/*case checking:
if(CheckRequest)
{
State=waitingcmd;
ClrRequest;
}
break;*/
default:
State=waitingcmd;
break;
}
}
ISR(TIMER2_OVF_vect) //Timer2 Service
{
TCNT2 = GamaTab[level]; // Reset a scanning time by gamma value table
flash_next_line(line,level); // sacan the next line in LED matrix level by level.
line++;
if(line>4) // when have scaned all LEC the back to line 0 and add the level
{
line=0;
level++;
if(level>15) level=0;
}
}
void init_timer2(void)
{
TCCR2A |= (1 << WGM21) | (1 << WGM20);
TCCR2B |= (1<<CS22); // by clk/64
TCCR2B &= ~((1<<CS21) | (1<<CS20)); // by clk/64
TCCR2B &= ~((1<<WGM21) | (1<<WGM20)); // Use normal mode
ASSR |= (0<<AS2); // Use internal clock - external clock not used in Arduino
TIMSK2 |= (1<<TOIE2) | (0<<OCIE2B); //Timer2 Overflow Interrupt Enable
TCNT2 = GamaTab[0];
sei();
}
void _init(void) // define the pin mode
{
DDRD=0xff;
DDRC=0xff;
DDRB=0xff;
PORTD=0;
PORTB=0;
//Wire.begin(4); // join i2c bus (address optional for master)
Wire.begin(); //i2c eeprom
Serial.begin(115200);
//Wire.onReceive(receiveEvent); // define the receive function for receiving data from master
//Wire.onRequest(requestEvent); // define the request function for the request from maseter
init_timer2(); // initial the timer for scanning the LED matrix
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void receiveEvent(int howMany)
{
unsigned char i=0;
while(Serial.available()>0)
{
RainbowCMD[i]=Serial.read();
i++;
}
if((i==7)&&(RainbowCMD[0]==‘R’)&&(RainbowCMD[1]==setPixelInImage || RainbowCMD[1]==getPixelInImage || RainbowCMD[6]==ID_BOARD)) State=processing;
else State=waitingcmd;
}
/*void requestEvent(void)
{
Wire.send(State);
if ((State==processing)||(State==checking)) SetRequest;
}*/
void DispshowPicture(void)
{
unsigned char pi,shifts;
unsigned char color=0,row=0,dots=0;
unsigned char temp;
unsigned char fir,sec;
shifts=((RainbowCMD[2]>>4)&0x0F);
pi=RainbowCMD[4];
RainbowCMD[1]=0;
for(color=0;color<3;color++)
{
for (row=0;row<8;row++)
{
for (dots=0;dots<4;dots++)
{
if (shifts&0x01)
{
temp = dots + (shifts>>1);
fir=pgm_read_byte(&(Prefabnicatel[pi][row][(temp<4)?(temp):(temp-4)]));
sec=pgm_read_byte(&(Prefabnicatel[pi][row][(temp<3)?(temp+1):(temp-3)]));
dots_color[((Buffprt+1)&1)][row][dots] = (fir<<4)|(sec>>4);
}
else
{
temp = dots + (shifts>>1);
dots_color[((Buffprt+1)&1)][row][dots] = pgm_read_byte(&(Prefabnicatel[pi][row][(temp<4)?(temp):(temp-4)]));
}
}
}
}
Buffprt++;
Buffprt&=1;
}
void DispshowChar(void)
{
unsigned char Col_Red,Col_Blue,Col_Green,shift,ASCII;
unsigned char tempword,color,row,dots,Num,tempdata,tempcol,AS;
shift=((RainbowCMD[2]>>4)&0x0F);
Col_Red=(RainbowCMD[2]&0x0F);
Col_Green=((RainbowCMD[3]>>4)&0x0F);
Col_Blue=(RainbowCMD[3]&0x0F);
ASCII=RainbowCMD[4];
RainbowCMD[1]=0;
if((ASCII>64)&&(ASCII<91)) AS=ASCII-65;
else if((ASCII>96)&&(ASCII<123)) AS=ASCII-71;
else if( (ASCII>='0')&&(ASCII<='9')) AS=ASCII-48;
for(color=0;color<3;color++)
{
if(color==0) tempcol=Col_Green;
else if(color==1) tempcol=Col_Red;
else if(color==2) tempcol=Col_Blue;
for (row=0;row<8;row++)
{
if( (ASCII>='0')&&(ASCII<='9'))
tempword=pgm_read_byte(&(ASCII_Number[AS][row]));
else
tempword=pgm_read_byte(&(ASCII_Char[AS][row]));
tempword=(shift<7)?(tempword<<shift):(tempword>>(shift-8));
for (dots=0;dots<4;dots++)
{
if((tempword<<(2*dots))&0x80)
{
tempdata&=0x0F;
tempdata|=(tempcol<<4);
}
else
{
tempdata&=0x0F;
}
if((tempword<<(2*dots+1))&0x80)
{
tempdata&=0xF0;
tempdata|=tempcol;
}
else
{
tempdata&=0xF0;
}
dots_color[((Buffprt+1)&1)][row][dots]=tempdata;
}
}
}
Buffprt++;
Buffprt&=1;
}
void DispshowColor(void)
{
unsigned char color=0,row=0,dots=0;
unsigned char Gr,Bl,Re;
Re=(RainbowCMD[2]&0x0F);
Gr=((RainbowCMD[3]>>4)&0x0F);
Bl=(RainbowCMD[3]&0x0F);
RainbowCMD[1]=0;
for(color=0;color<3;color++)
{
for (row=0;row<8;row++)
{
for (dots=0;dots<4;dots++)
{
switch (color)
{
case 0://green
dots_color[((Buffprt+1)&1)][row][dots]=( Gr|(Gr<<4));
break;
case 1://blue
dots_color[((Buffprt+1)&1)][row][dots]= (Bl|(Bl<<4));
break;
case 2://red
dots_color[((Buffprt+1)&1)][row][dots]= (Re|(Re<<4));
break;
default:
break;
}
}
}
}
Buffprt++;
Buffprt&=1;
}
void DispshowPicture2(void)
{
unsigned char pi;
signed char shifts;
unsigned char color=0,row=0,dots=0;
unsigned char temp;
unsigned char fir,sec;
unsigned int eeaddressX0, eeaddressX1, eeaddressX2;
shifts=((RainbowCMD[2]>>4)&0x0F); shifts-=5;
pi=RainbowCMD[4];
RainbowCMD[1]=0;
for(color=0;color<3;color++)
{
for (row=0;row<5;row++)
{
/for (dots=0;dots<3;dots++)
{/
//shift doesnt work now (maybe in the future if we want to add it)
//eeaddressX=((15*color)+(3*row)+(dots))+(pi*128); //15 bytes per color, 3 byte per row. Pages of 128
eeaddressX0=((15*color)+(3*row)+(0))+(pi*128); //15 bytes per color, 3 byte per row. Pages of 128
eeaddressX1=((15*color)+(3*row)+(1))+(pi*128); //15 bytes per color, 3 byte per row. Pages of 128
eeaddressX2=((15*color)+(3*row)+(2))+(pi*128); //15 bytes per color, 3 byte per row. Pages of 128
if(shifts==0){
dots_color[((Buffprt+1)&1)][row][0]= i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0);
dots_color[((Buffprt+1)&1)][row][1] = i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1);
dots_color[((Buffprt+1)&1)][row][2] = i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2);
}
//positives
else if(shifts==1){
dots_color[((Buffprt+1)&1)][row][0]=(i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 )>>4)&0x0F;
dots_color[((Buffprt+1)&1)][row][1] =((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )>>4)&0x0F));
dots_color[((Buffprt+1)&1)][row][2] =((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )>>4)&0x0F));
}
else if(shifts==2){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 );
dots_color[((Buffprt+1)&1)][row][2] =i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 );
}
else if(shifts==3){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =(i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 )>>4)&0x0F;
dots_color[((Buffprt+1)&1)][row][2] =((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0)<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1)>>4)&0x0F));
}
else if(shifts==4){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =0;
dots_color[((Buffprt+1)&1)][row][2] =i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 );
}
else if(shifts==5){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =0;
dots_color[((Buffprt+1)&1)][row][2] =0;
}
//negatives
else if(shifts==-1){
dots_color[((Buffprt+1)&1)][row][0]=((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )>>4)&0x0F));
dots_color[((Buffprt+1)&1)][row][1] =((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )>>4)&0x0F));
dots_color[((Buffprt+1)&1)][row][2] =0;
}
else if(shifts==-2){
dots_color[((Buffprt+1)&1)][row][0]=i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 );
dots_color[((Buffprt+1)&1)][row][1] =i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )&0xF0;
dots_color[((Buffprt+1)&1)][row][2] =0;
}
else if(shifts==-3){
dots_color[((Buffprt+1)&1)][row][0]= ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )>>4)&0x0F));
dots_color[((Buffprt+1)&1)][row][1] = 0; //because we have 5 leds per row
dots_color[((Buffprt+1)&1)][row][2] = 0;
}
else if(shifts==-4){
dots_color[((Buffprt+1)&1)][row][0]=i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )&0xF0;
dots_color[((Buffprt+1)&1)][row][1] =0;
dots_color[((Buffprt+1)&1)][row][2] =0;
}
else if(shifts==-5){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =0;
dots_color[((Buffprt+1)&1)][row][2] =0;
}
//}
}
}
Buffprt++;
Buffprt&=1;
}
void DispshowPicture3(void)
{
unsigned char pi, audioChannels;
signed char shifts;
unsigned char color=0,row=0,dots=0;
unsigned char temp;
unsigned char fir,sec;
unsigned int eeaddressX0, eeaddressX1, eeaddressX2;
shifts=((RainbowCMD[2]>>4)&0x0F); shifts-=5;
pi=RainbowCMD[4];
audioChannels=RainbowCMD[5];
RainbowCMD[1]=0;
for(color=0;color<3;color++)
{
for (row=0;row<5;row++)
{
/for (dots=0;dots<3;dots++)
{/
//shift doesnt work now (maybe in the future if we want to add it)
//eeaddressX=((15*color)+(3*row)+(dots))+(pi*128); //15 bytes per color, 3 byte per row. Pages of 128
eeaddressX0=((15*color)+(3*row)+(0))+(pi*128); //15 bytes per color, 3 byte per row. Pages of 128
eeaddressX1=((15*color)+(3*row)+(1))+(pi*128); //15 bytes per color, 3 byte per row. Pages of 128
eeaddressX2=((15*color)+(3*row)+(2))+(pi*128); //15 bytes per color, 3 byte per row. Pages of 128
if(shifts==0){
dots_color[((Buffprt+1)&1)][row][0]= i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0);
dots_color[((Buffprt+1)&1)][row][1] = i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1);
dots_color[((Buffprt+1)&1)][row][2] = i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2);
}
//positives
else if(shifts==1){
dots_color[((Buffprt+1)&1)][row][0]=(i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 )>>4)&0x0F;
dots_color[((Buffprt+1)&1)][row][1] =((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )>>4)&0x0F));
dots_color[((Buffprt+1)&1)][row][2] =((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )>>4)&0x0F));
}
else if(shifts==2){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 );
dots_color[((Buffprt+1)&1)][row][2] =i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 );
}
else if(shifts==3){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =(i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 )>>4)&0x0F;
dots_color[((Buffprt+1)&1)][row][2] =((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0)<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1)>>4)&0x0F));
}
else if(shifts==4){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =0;
dots_color[((Buffprt+1)&1)][row][2] =i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 );
}
else if(shifts==5){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =0;
dots_color[((Buffprt+1)&1)][row][2] =0;
}
//negatives
else if(shifts==-1){
dots_color[((Buffprt+1)&1)][row][0]=((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX0 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )>>4)&0x0F));
dots_color[((Buffprt+1)&1)][row][1] =((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )>>4)&0x0F));
dots_color[((Buffprt+1)&1)][row][2] =0;
}
else if(shifts==-2){
dots_color[((Buffprt+1)&1)][row][0]=i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 );
dots_color[((Buffprt+1)&1)][row][1] =i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )&0xF0;
dots_color[((Buffprt+1)&1)][row][2] =0;
}
else if(shifts==-3){
dots_color[((Buffprt+1)&1)][row][0]= ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX1 )<<4)&0xF0 | ((i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )>>4)&0x0F));
dots_color[((Buffprt+1)&1)][row][1] = 0; //because we have 5 leds per row
dots_color[((Buffprt+1)&1)][row][2] = 0;
}
else if(shifts==-4){
dots_color[((Buffprt+1)&1)][row][0]=i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX2 )&0xF0;
dots_color[((Buffprt+1)&1)][row][1] =0;
dots_color[((Buffprt+1)&1)][row][2] =0;
}
else if(shifts==-5){
dots_color[((Buffprt+1)&1)][row][0]=0;
dots_color[((Buffprt+1)&1)][row][1] =0;
dots_color[((Buffprt+1)&1)][row][2] =0;
}
//}
}
}
Buffprt++;
Buffprt&=1;
//audio switch code
switch(audioChannels){
case 0: digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break;
case 1: digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW); break;
case 2: digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, LOW); break;
case 3: digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, LOW); break;
case 4: digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, HIGH); break;
case 5: digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, HIGH); break;
case 6: digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break;
case 7: digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break;
}
}
void setOnePixel(void)
{
unsigned char color=0,row=0,dots=0;
unsigned char r, g, b, rowX, colX;
r=RainbowCMD[2]&0x0F;
g=(RainbowCMD[3]>>4)&0x0F;
b=RainbowCMD[3]&0x0F;
rowX=(RainbowCMD[5]>>4)&0x0F;
colX=RainbowCMD[5]&0x0F;
RainbowCMD[1]=0;
for(color=0;color<3;color++)
{
for (row=0;row<5;row++)
{
for (dots=0;dots<3;dots++)
{
unsigned char data;
switch (color){
case 0: data=g; break;
case 1: data=r; break;
case 2: data=b; break;
default: break;
}
if (row==rowX && dots==(int(colX/2))){
if(colX%2==0) dots_color[((Buffprt+1)&1)][row][dots]= (data<<4)&0xF0;
else dots_color[((Buffprt+1)&1)][row][dots]= data&0x0F;
}
else dots_color[((Buffprt+1)&1)][row][dots]= 0x00;
}
}
}
Buffprt++;
Buffprt&=1;
}
void setOnePixelWithAudio(void)
{
unsigned char color=0,row=0,dots=0;
unsigned char r, g, b, rowX, colX, audioChannels;
r=RainbowCMD[2]&0x0F;
g=(RainbowCMD[3]>>4)&0x0F;
b=RainbowCMD[3]&0x0F;
audioChannels=RainbowCMD[4];
rowX=(RainbowCMD[5]>>4)&0x0F;
colX=RainbowCMD[5]&0x0F;
RainbowCMD[1]=0;
for(color=0;color<3;color++)
{
for (row=0;row<5;row++)
{
for (dots=0;dots<3;dots++)
{
unsigned char data;
switch (color){
case 0: data=g; break;
case 1: data=r; break;
case 2: data=b; break;
default: break;
}
if (row==rowX && dots==(int(colX/2))){
if(colX%2==0) dots_color[((Buffprt+1)&1)][row][dots]= (data<<4)&0xF0;
else dots_color[((Buffprt+1)&1)][row][dots]= data&0x0F;
}
else dots_color[((Buffprt+1)&1)][row][dots]= 0x00;
}
}
}
Buffprt++;
Buffprt&=1;
//audio switch code
switch(audioChannels){
case 0: digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break;
case 1: digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW); break;
case 2: digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, LOW); break;
case 3: digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, LOW); break;
case 4: digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, HIGH); break;
case 5: digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, HIGH); break;
case 6: digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break;
case 7: digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break;
}
}
void setPixel(){
unsigned char color=0;
unsigned char r, g, b, pi, row, col;
unsigned int eeaddressX;
r=RainbowCMD[2]&0x0F;
g=(RainbowCMD[3]>>4)&0x0F;
b=RainbowCMD[3]&0x0F;
pi=RainbowCMD[4];
row=(RainbowCMD[5]>>4)&0x0F;
col=RainbowCMD[5]&0x0F;
byte aux;
//green, blue, red
for(color=0;color<3;color++){
eeaddressX=((row*3)+((int)col/2))+(pi*128)+(color*15);
aux=i2c_eeprom_read_byte(EEPROM_ADDR, eeaddressX );
byte data;
switch (color){
case 0: data=g; break;
case 1: data=r; break;
case 2: data=b; break;
default: break;
}
if(col%2==0) i2c_eeprom_write_byte(EEPROM_ADDR, eeaddressX, (data<<4)|(aux&0x0F));
else i2c_eeprom_write_byte(EEPROM_ADDR, eeaddressX, (aux&0xF0)|data);
delay(5);
}
}
void getPixel(){
//do nothing for now
}
void setAudio(){
unsigned char audioChannels;
audioChannels=RainbowCMD[4]&0x0F;
/pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);/
byte aux;
//audio switch code
switch(audioChannels){
case 0: digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break;
case 1: digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW); break;
case 2: digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, LOW); break;
case 3: digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, LOW); break;
case 4: digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, HIGH); break;
case 5: digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, HIGH); break;
case 6: digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break;
case 7: digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break;
}
}
void clearAudio(){
unsigned char audioChannels;
audioChannels=RainbowCMD[4]&0x0F;
/pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);/
//clear pin number
switch(audioChannels){
case 0: digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); break;
case 1: digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW); break;
case 2: digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, LOW); break;
case 3: digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, LOW); break;
case 4: digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, HIGH); break;
case 5: digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, HIGH); break;
case 6: digitalWrite(11, LOW); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break;
case 7: digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, HIGH); break;
}
}
void GetCMD(void)
{
switch(RainbowCMD[1])
{
case showPrefabnicatel:
DispshowPicture();
break;
case showPrefabnicate2:
DispshowPicture2();
break;
case showPrefabnicate3:
DispshowPicture3();
break;
case showChar:
DispshowChar();
break;
case showColor:
DispshowColor();
break;
case setPixelInImage:
showItsReceivingON();
setPixel();
showItsReceivingOFF();
break;
case getPixelInImage: //this one not yet implemented
getPixel();
break;
case setJustOnePixel:
setOnePixel();
break;
case setJustOnePixelWithAudio:
setOnePixelWithAudio();
break;
case setAudioChannel:
setAudio();
break;
case clearAudioChannel:
clearAudio();
break;
}
}
void showItsReceivingON(){
//one pixel to know its processing
unsigned char color=0,row=0,dots=0;
unsigned char r, g, b, rowX, colX;
r=0;
g=15;
b=0;
rowX=2;
colX=2;
for(color=0;color<3;color++){
for (row=0;row<5;row++){
for (dots=0;dots<3;dots++){
unsigned char data;
switch (color){
case 0: data=g; break;
case 1: data=r; break;
case 2: data=b; break;
default: break;
}
if (row==rowX && dots==(int(colX/2))){
if(colX%2==0) dots_color[((Buffprt+1)&1)][row][dots]= (data<<4)&0xF0;
else dots_color[((Buffprt+1)&1)][row][dots]= data&0x0F;
}
else dots_color[((Buffprt+1)&1)][row][dots]= 0x00;
}
}
}
Buffprt++;
Buffprt&=1;
}
void showItsReceivingOFF(){
//one pixel to know its processing
unsigned char color=0,row=0,dots=0;
unsigned char r, g, b, rowX, colX;
r=0;
g=0;
b=0;
rowX=2;
colX=2;
for(color=0;color<3;color++){
for (row=0;row<5;row++){
for (dots=0;dots<3;dots++){
dots_color[((Buffprt+1)&1)][row][dots]= 0x00;
}
}
}
Buffprt++;
Buffprt&=1;
}
//==============================================================
void shift_1_bit(unsigned char LS) //shift 1 bit of 1 Byte color data into Shift register by clock
{
if(LS){ shift_data_1; }
else { shift_data_0; }
clk_rising;
}
//==============================================================
void flash_next_line(unsigned char line,unsigned char level) // scan one line
{
disable_oe;
close_all_line;
open_line(line);
shift_24_bit(line,level);
enable_oe;
}
//==============================================================
void shift_24_bit(unsigned char line,unsigned char level) // display one line by the color level in buff
{
unsigned char color=0,row=0;
unsigned char data0=0,data1=0;
le_high;
for(color=0;color<3;color++)//GBR
{
for(row=0;row<4;row++)
{
if(row<2){
data1=dots_color[Buffprt][line][row]&0x0f;
data0=dots_color[Buffprt][line][row]>>4;
}
else if(row==2){
data1=0;
data0=dots_color[Buffprt][line][row]>>4;
}
else{
data1=0;data0=0;
}
if(data0>level) //gray scale,0x0f aways light
{
shift_1_bit(1);
}
else
{
shift_1_bit(0);
}
if(data1>level)
{
shift_1_bit(1);
}
else
{
shift_1_bit(0);
}
}
}
le_low;
}
//==============================================================
void open_line(unsigned char line) // open the scaning line
{
switch(line)
{
case 0:{ open_line0; break; }
case 1:{ open_line1; break; }
case 2:{ open_line2; break; }
case 3:{ open_line3; break; }
case 4:{ open_line4; break; }
case 5:{ open_line5; break; }
case 6:{ open_line6; break; }
case 7:{ open_line7; break; }
}
}
// External EEPROM - Wathc out this one does work for Microchip EEPROMs
//
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // Address High Byte
Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.send(rdata);
Wire.endTransmission();
}
// Address is a page address, 6-bit (63). More and end will wrap around
// But data can be maximum of 28 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddresspage >> 8)); // Address High Byte
Wire.send((int)(eeaddresspage & 0xFF)); // Address Low Byte
byte c;
for ( c = 0; c < length; c++)
Wire.send(data[c]);
Wire.endTransmission();
delay(10); // need some delay
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // Address High Byte
Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.receive();
return rdata;
}
// should not read more than 28 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // Address High Byte
Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
//int c = 0;
for ( int c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.receive();
}
[/code]