void checkButtons() {
if (trellis.readSwitches()) {
for (uint8_t i=0; i<numKeys; i++) {
if (trellis.justPressed(i)) {
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));
}
}
}
}
#include <Wire.h>
#include "Adafruit_Trellis.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)
int ShutdownModeVal, address, command=0 ;
boolean ShutdownModeChange = false, WaitingCommand = true;
byte byte0, byte1, row = 0;
byte keyState[8];
long nextRefresh;
int chessboard[8][8] = {
{16, 17, 18, 19, 0, 1, 2, 3},
{20, 21, 22, 23, 4, 5, 6, 7},
{24, 25, 26, 27, 8, 9, 10, 11},
{28, 29, 30, 31, 12, 13, 14, 15},
{32, 33, 34, 35, 48, 49, 50, 51},
{36, 37, 38, 39, 52, 53, 54, 55},
{40, 41, 42, 43, 56, 57, 58, 59},
{44, 45, 46, 47, 60, 61, 62, 63}
};
#define INTPIN 5
void setup() {
Serial.begin(9600);
Serial.println("Trellis Hello World :-)");
pinMode(INTPIN, INPUT);
digitalWrite(INTPIN, HIGH);
trellis.begin(0x70, 0x71, 0x72, 0x73);
for (uint8_t i=0; i<8; i++) {
for (uint8_t j=0; j<8; j++) {
trellis.setLED(chessboard[i][j]);
trellis.writeDisplay();
delay(50);
}
}
for (uint8_t i=0; i<numKeys; i++) {
trellis.clrLED(i);
}
trellis.writeDisplay();
}
void onLED( byte x, byte y) {
x = 7 - x; // swap x axis to componsate for the position of the USB connector
y = 7 - y; // swap y axis to componsate for the position of the USB connector
byte xMask = 1 << x;
trellis.setLED(chessboard[x][y]);
trellis.writeDisplay();
}
void offLED( byte x, byte y) {
x = 7 - x; // swap x axis to componsate for the position of the USB connector
y = 7 - y; // swap y axis to componsate for the position of the USB connector
byte xMask = (1 << x) ^ 0xff;
trellis.clrLED(chessboard[x][y]);
trellis.writeDisplay();
}
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 loop() {
checkSerial();
}
if (trellis.readSwitches()) {
// go through every button
for (uint8_t i=0; i<numKeys; i++) {
// if it was pressed...
if (trellis.justPressed(i)) {
switch(i);
case 16:
x=0; y=0;
break;
case 17:
x=0; y=1;
break;
*// create a case statment 64 times??? going to look ugly.
}
//Serial x y press goes here
}
}
column_count = 4;
if (trellis.justPressed(i)) {
x = i % column_count;
y = floor( i / column_count);
/***************************************************
* This is an implementation of the 40h Protocol used by the Monome
* ----> http://monome.org
* Large portions of this sketch were taken from www.thebox.myzen.co.uk Econome Arduinome 8x8
*-->http://www.thebox.myzen.co.uk
* Brian Lightfoot
* 03.21.2014
* brianlightfoot at gmail dot com
* www.thebox.myzen.co.uk
*
*
* Designed specifically to work with the Adafruit Trellis
* ----> https://www.adafruit.com/products/1616
* ----> https://www.adafruit.com/products/1611
* This Sketch needs Trellis X/Y library to compile -----> http://forums.adafruit.com/viewtopic.php?f=47&t=51444&p=259870 hilit=Trellis#p259870
*
*
* Currently only works with Serial-pyio http://sourceforge.net/p/serial-pyio/git/ci/master/tree/
* Has been tested on the Raspberry Pi running Raspbian & on the a windows based machine running Windows XP
*
* TODO:
*
* Button's need debouncing ***Priority #1***
* Add accelerometer support for tilt control.
* serialosc support ----> http://monome.org/docs/app:serialosc
****************************************************/
#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;
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)) {
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 loop() {
delay(30);
checkButtons();
checkSerial();
trellis.writeDisplay();
}
#if defined(USB_SERIAL)
#define VENDOR_ID 0x03eb
#define PRODUCT_ID 0x2ff7
#define DEVICE_CLASS 2 // 2 = Communication Class
#define MANUFACTURER_NAME {'A','r','d','u','i','n','o','m','e'}
#define MANUFACTURER_NAME_LEN 7
#define PRODUCT_NAME {'a','4','0','h','-','0','0','1'}
#define PRODUCT_NAME_LEN 8
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_USB_BUFFERS 12
#define NUM_INTERFACE 2
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1
#define CDC_ACM_ENDPOINT 2
#define CDC_RX_ENDPOINT 3
#define CDC_TX_ENDPOINT 4
#define CDC_ACM_SIZE 16
#define CDC_RX_SIZE 64
#define CDC_TX_SIZE 64
#define CONFIG_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
#define ENDPOINT2_CONFIG ENDPOINT_TRANSIMIT_ONLY
#define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_ONLY
#define ENDPOINT4_CONFIG ENDPOINT_TRANSIMIT_ONLY