Code: Select all
/***************************************************
* This is an implementation of the 40h Protocol used by the Monome
* ----> http://monome.org
*
* Brian Lightfoot
* 03.21.2014
* [email protected]
*
*
* Designed specifically to work with the Adafruit Trellis
* ----> https://www.adafruit.com/products/1616
* ----> https://www.adafruit.com/products/1611
*
*
*
*
*
****************************************************/
#include <Wire.h>
#include <Adafruit_Trellis.h>
#include <Adafruit_Trellis_XY.h>
Adafruit_Trellis matrix0 = Adafruit_Trellis();
Adafruit_Trellis matrix1 = Adafruit_Trellis();
Adafruit_Trellis matrix2 = Adafruit_Trellis();
Adafruit_Trellis matrix3 = Adafruit_Trellis();
Adafruit_TrellisSet trellis = Adafruit_TrellisSet(&matrix0, &matrix1, &matrix2, &matrix3);
#define NUMTRELLIS 4
#define numKeys (NUMTRELLIS * 16)
#define INTPIN A2
//-----------------------------------------------------------
// define offsets for 4X4 Tiles
struct TileOffsets TileOffsets =
{
{
0, 4, 0, 4, 0, 0, 0, 0
}
,
{
0, 0, 4, 4, 0 , 0, 0, 0
}
};
uint8_t xVal;
uint8_t yVal;
uint8_t xyTrellisID;
// These variables are used to handle the ADC messages, and store which ports are currently enabled,
// and which are not.
byte ADCnum;
byte ADCstate;
byte ADCEnableState[4];
int ShutdownModeVal, address, command = 0 ;
boolean ShutdownModeChange = false, WaitingCommand = true;
byte byte0, byte1, row = 0;
byte keyState[8];
byte x;
byte y;
long nextRefresh;
int chessboard[8][8] =
{
{
12, 8, 4, 0, 44, 40, 36, 32
}
,
{
13, 9, 5, 1, 45, 41, 37, 33
}
,
{
14, 10, 6, 2, 46, 42, 38, 34
}
,
{
15, 11, 7, 3, 47, 43, 39, 35
}
,
{
28, 24, 20, 16, 60, 56, 52, 48
}
,
{
29, 25, 21, 17, 61, 57, 53, 49
}
,
{
30, 26, 22, 18, 62, 58, 54, 50
}
,
{
31, 27, 23, 19, 63, 59, 55, 51
}
};
void setup()
{
Serial.begin(115200);
pinMode(INTPIN, INPUT);
digitalWrite(INTPIN, HIGH);
trellis.begin(0x72, 0x73, 0x70, 0x71);
for (uint8_t i = 0; i < 8; i++)
{
for (uint8_t j = 0; j < 8; j++)
{
trellis.setBrightness(j);
trellis.setLED(chessboard[i][j]);
trellis.writeDisplay();
delay(50);
}
trellis.setBrightness(1);
}
for (uint8_t i = 0; i < numKeys; i++)
{
trellis.clrLED(i);
}
trellis.writeDisplay();
}
void onLED( byte x, byte y)
{
byte xMask = 1 << x;
trellis.setLED(chessboard[y][x]);
}
void offLED( byte x, byte y)
{
byte xMask = (1 << x) ^ 0xff;
trellis.clrLED(chessboard[y][x]);
}
void clearRows()
{
for(byte y = 0; y < 8; y++) chessboard[8][y] = 0;
}
void setRows()
{
for(byte y = 0; y < 8; y++) chessboard[8][y] = 0xff;
}
void checkSerial()
{
byte x, y, state;
do
{
if (Serial.available())
{
if (WaitingCommand) // command is the first byte of the two byte command
{
byte0 = Serial.read();
command = byte0 >> 4;
WaitingCommand = false;
switch(command) // do one byte commands
{
case 9: // clear command
if((byte0 & 1) == 0) clearRows();
else setRows();
WaitingCommand = true; // next byte is a new command
break;
}
if((command > 9) || (command < 2)) WaitingCommand = true; // command outside range so must be an error
}
else
{
WaitingCommand = true; // the next byte is the first byte of the next command
byte1 = Serial.read(); // read the second byte of this command
switch(command)
{
case 2: // LED command
state = byte0 & 0x0f; // is the command for on or off
x = ((byte1 >> 4) & 0x07); // mask so we don't go over the 8 by 8 grid
y = ((byte1 & 15) & 0x07);
if (state == 0)
{
offLED(x, y);
}
else
{
onLED(x, y);
}
break;
case 3: // led intensity command
break;
case 4: // led test command
if( (byte1 & 1) == 0)
{
setRows();
}
else
{
clearRows();
}
break;
case 5: // enable ADC command - but we don't do this
break;
case 6: // shutdown command - not sure what the monome is expected to do here
ShutdownModeChange = true;
ShutdownModeVal = byte1 & 15;
break;
case 7: // led row command
y = byte0 & 0x07; // mask this value so we don't write to an invalid address.
x = 0;
for(byte i = 0x1; i != 0x0; i <<= 1 )
{
if( (i & byte1) != 0)
{
onLED(x, y);
}
else
{
offLED(x, y);
}
x++;
}
break;
case 8: // coloum command
x = byte0 & 0x07; // mask this value so we don't write to an invalid address.
y = 0;
for(byte i = 0x1; i != 0x0; i <<= 1 )
{
if( (i & byte1) != 0)
{
onLED(x, y);
}
else
{
offLED(x, y);
}
y++;
}
break;
default:
onLED(7, 7);
break;
} // end switch(command)
} // end of else in if(WaitingCommand)
} // end of if(Serial.available();
} // end of do
while (Serial.available() > 8); // keep on going if we have a lot of commands stacked up
}
void checkButtons()
{
if (trellis.readSwitches())
{
for (uint8_t i = 0; i < numKeys; i++)
{
if (trellis.justPressed(i))
{
// button debounce
delay(50);
int s = 1;
TrellisToXY(i, TileOffsets, &xVal, &yVal);
Serial.write((0 << 4) | (s & 15));
Serial.write((yVal << 4) | (xVal & 15));
}
if (trellis.justReleased(i))
{
int s = 0;
TrellisToXY(i, TileOffsets, &xVal, &yVal);
Serial.write((0 << 4) | (s & 15));
Serial.write((yVal << 4) | (xVal & 15));
}
}
}
}
void sendADC(int port, int value) {
Serial.write((1 << 4) | ((port << 2) & 0x0C) | ((value >> 8) & 0x03));
Serial.write(value & 0xFF);
}
int current[4];
int previous[4];
int tolerance = 7;
void checkADCs()
{
for (int adcCounter = 1; adcCounter < 3; adcCounter++)
{
if (ADCEnableState[adcCounter] != 0)
{
current[adcCounter] = analogRead(adcCounter);
if (abs(previous[adcCounter]-current[adcCounter]) > tolerance)
{
previous[adcCounter] = current[adcCounter];
sendADC(adcCounter,current[adcCounter]);
}
}
}
}
void loop()
{
checkButtons();
checkSerial();
checkADCs();
trellis.writeDisplay();
}
I dont know what to do. Ive read about flashing a serial number to the arduino or something about 40h protocol. What am i missing? i honestly dont know whats going on because the code seems to work for everyone else.sketch_jul24a:42: error: variable 'TileOffsets TileOffsets' has initializer but incomplete type
sketch_jul24a.ino: In function 'void checkButtons()':
sketch_jul24a:277: error: 'TrellisToXY' was not declared in this scope
sketch_jul24a:284: error: 'TrellisToXY' was not declared in this scope