Trellis X/Y Library

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
gkuetemeyer
 
Posts: 23
Joined: Sat Jan 11, 2014 1:15 pm

Trellis X/Y Library

Post by gkuetemeyer »

Just coded a little Trellis X/Y library. Finding it useful, so posting it for others to use:

Here is the header file:

Code: Select all

// Adafruit_Trellis_XY.h

//  G. Kuetemeyer
//  03/20/2014
//  [email protected]

#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

void TrellisToXY(uint8_t trellisID, struct TileOffsets &TileOffsets, uint8_t *x, uint8_t *y);
uint8_t XYToTrellis(uint8_t numKeys, struct TileOffsets &TileOffsets, uint8_t xIn, uint8_t yIn);

//-----------------------------------------------------------
// struct for mapping Trellis Tile XY offsets
//
// example with 4 Tiles in a square (8X8 buttons/leds)
//
// -----
// |2|3|    layout for 8X8 matrix ----
// |0|1|    Don't forget to initialize Trellises in order matching layouty...
// -----
//
//
// struct TileOffsets XYOffsets {
//   { 0, 4, 0, 4 },
//   { 0, 0, 4, 4 }
//};

struct TileOffsets {
	uint8_t xOffsets[8];
	uint8_t yOffsets[8];
};

// array for mapping trellis IDs for a single Tile to X/Y coordinates
const uint8_t trellisIDs[16][2] = {
  { 0, 3 },
  { 1, 3 },
  { 2, 3 },
  { 3, 3 },
  { 0, 2 },
  { 1, 2 },
  { 2, 2 },
  { 3, 2 },
  { 0, 1 },
  { 1, 1 },
  { 2, 1 },
  { 3, 1 },
  { 0, 0 },
  { 1, 0 },
  { 2, 0 },
  { 3, 0 },
};


// array for mapping X/Y coordinates to single trellis Tile IDs
const uint8_t XYTrellisIDs[4][4] = {
  { 12, 13, 14, 15 },
  {  8,  9, 10, 11 },
  {  4,  5,  6,  7 },
  {  0,  1,  2,  3 },
};
Here is the .cpp file:

Code: Select all

// Adafruit_Trellis_XY.cpp
//
// Library for mapping Trellis Tile IDs to XY coordinates
// and mapping XY coordinates to Trellis Tile IDs
//  G. Kuetemeyer
//  03/20/2014
//  [email protected]

#include <Adafruit_Trellis_XY.h>

//----------------------------------------------------------------------
// map Trellis button/led ids to X/Y coordinates
void TrellisToXY(uint8_t trellisID, struct TileOffsets &TileOffsets, uint8_t *x, uint8_t *y) {
  uint8_t xOffset;
  uint8_t yOffset;
  
  // get trellis matrix offsets
  if(trellisID < 16) {
    xOffset = TileOffsets.xOffsets[0];
    yOffset = TileOffsets.yOffsets[0];
  }
  else if(trellisID > 15 && trellisID < 32) {
    trellisID = trellisID - 16;
    xOffset = TileOffsets.xOffsets[1];
    yOffset = TileOffsets.yOffsets[1];
  }
  else if(trellisID > 31 && trellisID < 48) {
    trellisID = trellisID - 32;
    xOffset = TileOffsets.xOffsets[2];
    yOffset = TileOffsets.yOffsets[2];
  }  
  else if(trellisID > 47 && trellisID < 64) {
    trellisID = trellisID - 48;
    xOffset = TileOffsets.xOffsets[3];
    yOffset = TileOffsets.yOffsets[3];
  }
  else if(trellisID > 63 && trellisID < 80) {
    trellisID = trellisID - 64;
    xOffset = TileOffsets.xOffsets[4];
    yOffset = TileOffsets.yOffsets[4];
  }
  else if(trellisID > 79 && trellisID < 96) {
    trellisID = trellisID - 80;
    xOffset = TileOffsets.xOffsets[5];
    yOffset = TileOffsets.yOffsets[5];
  }  
  else if(trellisID > 95 && trellisID < 112) {
    trellisID = trellisID - 96;
    xOffset = TileOffsets.xOffsets[6];
    yOffset = TileOffsets.yOffsets[6];
  }
  else if(trellisID > 111 && trellisID < 128) {
    trellisID = trellisID - 112;
    xOffset = TileOffsets.xOffsets[7];
    yOffset = TileOffsets.yOffsets[7];
  }	
  else {
    xOffset = TileOffsets.xOffsets[0];
    yOffset = TileOffsets.yOffsets[0];
  }
  
	// add in offsets
  *x = trellisIDs[trellisID][0] + xOffset;
  *y = trellisIDs[trellisID][1] + yOffset;    
}

//----------------------------------------------------------------------
// map X/Y coordinates to Trellis button/led id
// (admittedly brute force but really simple. 
// Just pass ids to TrellisToXY until we get a match
uint8_t XYToTrellis(uint8_t numKeys, struct TileOffsets &TileOffsets, uint8_t xIn, uint8_t yIn) {
  uint8_t xTest;
  uint8_t yTest;
  
  for(uint8_t i = 0; i < numKeys + 1; i++) {
    TrellisToXY(i, TileOffsets, &xTest, &yTest);
    if(xTest == xIn && yTest == yIn) {
      return i;
    }
  }
  return 0;  
}
Here is keywords.txt:

Code: Select all

# Syntax Coloring Map For Adafruit_Trellis_XY
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################
TileOffsets	KEYWORD1
xOffsets	KEYWORD1
yOffsets	KEYWORD1
trellisIDs	KEYWORD1
XYTrellisIDs	KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

TrellisToXY	KEYWORD2
XYToTrellis	KEYWORD2

#######################################
# Instances (KEYWORD2)
#######################################

#######################################
Constants (LITERAL1)
#######################################
Finally, here is a sample sketch -- just a modification of the standard test script:

Code: Select all

/*************************************************** 
  This is a test example for the Adafruit Trellis w/HT16K33 
  Added Adafruit_Trellis_XY library
  Print out X/Y values...
  
  G. Kuetemeyer
  03/20/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>

/*************************************************** 
  This example shows reading buttons and setting/clearing buttons in a loop
  Also sending X/Y values to serial port.

 ****************************************************/

#define MOMENTARY 0
#define LATCHING 1
// set the mode here
#define MODE LATCHING 


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;

//-----------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println("Trellis Demo");
  
  // INT pin requires a pullup
  pinMode(INTPIN, INPUT);
  digitalWrite(INTPIN, HIGH);
  
  // order is important here!! Start with tile address you want to
  // use as the first one, etc. 
  trellis.begin(0x72, 0x73, 0x70, 0x71);  // or four!

  // light up all the LEDs in order
  for (uint8_t i=0; i<numKeys; i++) {
    trellis.setLED(i);
    trellis.writeDisplay();    
    delay(50);
  }
  // then turn them off
  for (uint8_t i=0; i<numKeys; i++) {
    trellis.clrLED(i);
    trellis.writeDisplay();    
    delay(50);
  }
}

void loop() {
  delay(30); // 30ms delay is required, dont remove me!
  
  if (MODE == LATCHING) {
    // If a button was just pressed or released...
    if (trellis.readSwitches()) {
      // go through every button
      for (uint8_t i=0; i<numKeys; i++) {
        // if it was pressed...
	if (trellis.justPressed(i)) {
          // print out X/Y values
          Serial.println("----------");
	       Serial.print("v"); Serial.println(i);
          TrellisToXY(i, TileOffsets, &xVal, &yVal);
          Serial.print("x: "); Serial.println(xVal, DEC);
          Serial.print("y: "); Serial.println(yVal, DEC);
          // also print out Trellis ID #
          xyTrellisID = XYToTrellis(numKeys, TileOffsets, xVal, yVal);
          Serial.print("xyTrellisID: "); Serial.println(xyTrellisID, DEC);
	  // Alternate the LED
	  if (trellis.isLED(i)) {
	    trellis.clrLED(i);
          }
	  else {
	    trellis.setLED(i);
          }
        } 
      }
      // tell the trellis to set the LEDs we requested
      trellis.writeDisplay();
    }
  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Trellis X/Y Library

Post by adafruit_support_bill »

Cool! Thanks for sharing that :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”