Advertisement
Guest User

7x7 version of Arduinome

a guest
Jun 19th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.94 KB | None | 0 0
  1. /*
  2.  * Hirsty_Matrix_7x7_RGB - Arduino-based 7x7 interactive panel based on Grumpy Mike's  Arduinome project.
  3.  *
  4.  * Arduinome_4x4_RGB - Arduino Based Monome-like device with a 4 X 4 matrix of fully fadeable RGB LEDs
  5.  * By Mike Cook (mike_k_cook at yahoo dot co dot uk) 22/10/08 based on the following:-
  6.  *
  7.  * Alex Leone - acleone ~AT~ u.washington.edu for the basis of the TL5940 Libiary
  8.  * "ArduinomeFirmware" - Arduino Based Monome Clone by Owen Vallis & Jordan Hochenbaum 06/16/2008
  9.  *
  10.  * --------------------------------------------------------------------------
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * --------------------------------------------------------------------------
  21.  *
  22.  * Please DO NOT email monome with technical questions and/or help regarding this code or clone.  
  23.  * They are in NO WAY responsible or affiliated with this project other than they were our inspiration
  24.  * and we used many of their methods and pulled from their code.
  25.  *
  26.  * Additionally, while we are availble and willing to help as much as possible, we too CANNOT be held
  27.  * responsible for anything you do with this code.  Please feel free to report any bugs, suggestions
  28.  * or improvements to us as they are all welcome.  Again, we cannot be held responsible for any damages
  29.  * or harm caused by the use or misuse of this code or our instructions.  Thank you for understanding.
  30.  * --------------------------------------------------------------------------
  31.  *
  32.  * Links:
  33.  * http://bricktable.wordpress.com - Our website - Click "Arduino Monome Project" on the Navigation Menu on the right.
  34.  * www.monome.org - the "original" monome and our inspiration
  35.  * www.flickr.com/photos/unsped/2283428540/in/photostream/
  36.  
  37.  * http://play-collective.net/blog/archives/category/arduinomonomergb
  38.  *
  39.  *
  40.  * www.thebox.myzen.co.uk - for detailes of the 4 by 4 RGB - schematic and construction Mike Cook (Grumpy Mike)
  41.  */
  42.  
  43.  
  44. #include <avr/pgmspace.h>
  45. #include <TLC5940Multiplex.h>
  46.  
  47. static int t;
  48. static int L=0;
  49. int incomingByte = 0;   // for incoming serial data
  50. int redOn=0xfff, greenOn=0x20, blueOn=0x00, redOff=0x000, greenOff=0, blueOff = 0;  // colours for on and off states from the monome
  51. int  IntensityVal, DisplayVal, ShutdownModeVal, address, state = 0 ;
  52. boolean ShutdownModeChange = false, WaitingForAddress = true;
  53. byte byte0, byte1, x, y, z;
  54. byte keyState[4][4];
  55.  
  56.  
  57. /* This sketch and hardware uses only the first 12 LED drivers in a TLC5940 however the software will support all 16
  58.  * The first value in each row is the value for the last LED on the last TLC5940.
  59.  * Each value is 12 bits (0-4095) or 3 nibbles.  21 LEDs (1 row) * 12 bits  = 252 bits. Round up to 256 to get 32 bytes per row.
  60.  * Each of the numbers in the array below is 1 byte (0-255) in hex. (Ex. 0xff = 255)
  61.  *
  62.  *  | 1st byte | 2nd byte | 3rd byte | 4th byte | 5th byte | 6th byte | ...
  63.  *  | LED 16 value   | LED 15 value  | LED 14 value   | LED 13 value  | ...
  64.  */
  65.  
  66. // Change this array initilisation to get a diffrent switch on pattern
  67.  
  68.  static uint8_t ledBuffer[] = {
  69.        
  70.     // Row 7 - CYAN
  71.     // 32     31     30     29     28     27     26     25     24     23     22    21     20     19
  72.        0x0f,  0xff,  0x00,  0x0f,  0xff,  0xff,  0xf0,  0x00,  0xff,  0xff,  0xff, 0x00,  0x0f,  0xff,  
  73.     //    | L21-B      | L20-R | L19-G      | L18-B | L17-R      | L16-G | L15-B     | L14-R | L13-G      
  74.     //    | RGB 7                           | RGB 6                      | RGB 5
  75.     // 18     17     16     15     14     13     12     11     10     9      8      7      6      5      4      3      2      1
  76.        0xff,  0xf0,  0x00,  0xff,  0xff,  0xff,  0x00,  0x0f,  0xff,  0xff,  0xf0,  0x00,  0xff,  0xff,  0xff,  0x00,  0x0f,  0xff,
  77.     //   | l12-B | L11-R      | L10-G | L9-B       | L8-R  | L7-G       | L6-B  | L5-R       | L4-G  | L3-B       | L2-R  | L1-G  
  78.     //   | RGB 4                      | RGB 3                           | RGB 2                      | RGB 1
  79.    
  80.     // Row 6 - VIOLET
  81.     // 32     31     30     29     28     27     26     25     24     23     22    21     20     19
  82.        0x0f,  0xff,  0xff,  0xf0,  0x00,  0xff,  0xff,  0xff,  0x00,  0x0f,  0xff, 0xff,  0xf0,  0x00,  
  83.     //    | L21-B      | L20-R | L19-G      | L18-B | L17-R      | L16-G | L15-B     | L14-R | L13-G      
  84.     //    | RGB 7                           | RGB 6                      | RGB 5
  85.     // 18     17     16     15     14     13     12     11     10     9      8      7      6      5      4      3      2      1
  86.        0xff,  0xff,  0xff,  0x00,  0x0f,  0xff,  0xff,  0xf0,  0x00,  0xff,  0xff,  0xff,  0x00,  0x0f,  0xff,  0xff,  0xf0,  0x00,
  87.     //   | l12-B | L11-R      | L10-G | L9-B       | L8-R  | L7-G       | L6-B  | L5-R       | L4-G  | L3-B       | L2-R  | L1-G  
  88.     //   | RGB 4                      | RGB 3                           | RGB 2                      | RGB 1
  89.  
  90.     // Row 5 - YELLOW
  91.     // 32     31     30     29     28     27     26     25     24     23     22    21     20     19
  92.        0x00,  0x00,  0xff,  0xff,  0xff,  0x00,  0x0f,  0xff,  0xff,  0xf0,  0x00, 0xff,  0xff,  0xff,  
  93.     //    | L21-B      | L20-R | L19-G      | L18-B | L17-R      | L16-G | L15-B     | L14-R | L13-G      
  94.     //    | RGB 7                           | RGB 6                      | RGB 5
  95.     // 18     17     16     15     14     13     12     11     10     9      8      7      6      5      4      3      2      1
  96.        0x00,  0x0f,  0xff,  0xff,  0xf0,  0x00,  0xff,  0xff,  0xff,  0x00,  0x0f,  0xff,  0xff,  0xf0,  0x00,  0xff,  0xff,  0xff,
  97.     //   | l12-B | L11-R      | L10-G | L9-B       | L8-R  | L7-G       | L6-B  | L5-R       | L4-G  | L3-B       | L2-R  | L1-G  
  98.     //   | RGB 4                      | RGB 3                           | RGB 2                      | RGB 1
  99.  
  100.     // Row 4 (Bottom) - BLUE
  101.     // 32     31     30     29     28     27     26     25     24     23     22    21     20     19
  102.        0x0f,  0xff,  0x00,  0x00,  0x00,  0xff,  0xf0,  0x00,  0x00,  0x0f,  0xff, 0x00,  0x00,  0x00,  
  103.     //    | L21-B      | L20-R | L19-G      | L18-B | L17-R      | L16-G | L15-B     | L14-R | L13-G      
  104.     //    | RGB 7                           | RGB 6                      | RGB 5
  105.     // 18     17     16     15     14     13     12     11     10     9      8      7      6      5      4      3      2      1
  106.        0xff,  0xf0,  0x00,  0x00,  0x0f,  0xff,  0x00,  0x00,  0x00,  0xff,  0xf0,  0x00,  0x00,  0x0f,  0xff,  0x00,  0x00,  0x00,
  107.     //   | l12-B | L11-R      | L10-G | L9-B       | L8-R  | L7-G       | L6-B  | L5-R       | L4-G  | L3-B       | L2-R  | L1-G  
  108.     //   | RGB 4                      | RGB 3                           | RGB 2                      | RGB 1
  109.                              
  110.        
  111.     // Row 3 - GREEN
  112.     // 32     31     30     29     28     27     26     25     24     23     22    21     20     19
  113.        0x00,  0x00,  0x00,  0x0f,  0xff,  0x00,  0x00,  0x00,  0xff,  0xf0,  0x00, 0x00,  0x0f,  0xff,  
  114.     //    | L21-B      | L20-R | L19-G      | L18-B | L17-R      | L16-G | L15-B     | L14-R | L13-G      
  115.     //    | RGB 7                           | RGB 6                      | RGB 5
  116.     // 18     17     16     15     14     13     12     11     10     9      8      7      6      5      4      3      2      1
  117.        0x00,  0x00,  0x00,  0xff,  0xf0,  0x00,  0x00,  0x0f,  0xff,  0x00,  0x00,  0x00,  0xff,  0xf0,  0x00,  0x00,  0x0f,  0xff,
  118.     //   | l12-B | L11-R      | L10-G | L9-B       | L8-R  | L7-G       | L6-B  | L5-R       | L4-G  | L3-B       | L2-R  | L1-G  
  119.     //   | RGB 4                      | RGB 3                           | RGB 2                      | RGB 1
  120.        
  121.        
  122.     // Row 2 - RED
  123.     // 32     31     30     29     28     27     26     25     24     23     22    21     20     19
  124.        0x00,  0x00,  0xff,  0xf0,  0x00,  0x00,  0x0f,  0xff,  0x00,  0x00,  0x00, 0xff,  0xf0,  0x00,  
  125.     //    | L21-B      | L20-R | L19-G      | L18-B | L17-R      | L16-G | L15-B     | L14-R | L13-G      
  126.     //    | RGB 7                           | RGB 6                      | RGB 5
  127.     // 18     17     16     15     14     13     12     11     10     9      8      7      6      5      4      3      2      1
  128.        0x00,  0x0f,  0xff,  0x00,  0x00,  0x00,  0xff,  0xf0,  0x00,  0x00,  0x0f,  0xff,  0x00,  0x00,  0x00,  0xff,  0xf0,  0x00,
  129.     //   | l12-B | L11-R      | L10-G | L9-B       | L8-R  | L7-G       | L6-B  | L5-R       | L4-G  | L3-B       | L2-R  | L1-G  
  130.     //   | RGB 4                      | RGB 3                           | RGB 2                      | RGB 1      
  131.        
  132.        
  133.     // Row 1 - Test Pattern (red, green, blue, white, yellow, violet, cyan)
  134.     // 32     31     30     29     28     27     26     25     24     23     22    21     20     19
  135.        0x0f,  0xff,  0x00,  0x0f,  0xff,  0xff,  0xff,  0xff,  0x00,  0x00,  0x00, 0xff,  0xff,  0xff,  
  136.     //    | L21-B      | L20-R | L19-G      | L18-B | L17-R      | L16-G | L15-B     | L14-R | L13-G      
  137.     //    | RGB 7                           | RGB 6                      | RGB 5
  138.     // 18     17     16     15     14     13     12     11     10     9      8      7      6      5      4      3      2      1
  139.        0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0xff,  0xf0,  0x00,  0xff,  0xf0,  0x00
  140.     //   | l12-B | L11-R      | L10-G | L9-B       | L8-R  | L7-G       | L6-B  | L5-R       | L4-G  | L3-B       | L2-R  | L1-G  
  141.     //   | RGB 4                      | RGB 3                           | RGB 2                      | RGB 1
  142.    
  143. };
  144.  
  145. uint8_t dotClockBuffer[] = {
  146.    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00
  147.   // 0x00, 0x00, 0x00,  0x00, 0x00, 0x00,  0x00, 0x00, 0x00,  0x00, 0x00, 0x00
  148.  };
  149.  
  150. void setup () {
  151.   int i;
  152.   pinMode(5, OUTPUT);
  153.   pinMode(18, OUTPUT); // Set analog pin 4 to act as a digital output
  154.  
  155.   // MacOS users - set this to 57600
  156.   // Windows users - you'll need to hack monomeSerial
  157.   // to make it run at 9600
  158.   // Serial.begin(9600);
  159.   Serial.begin(57600);    // Debug speed
  160.   Tlcm.init();
  161.   Tlcm.resetTimers();
  162.   Tlcm.startMultiplexing(ledBuffer);
  163.  //  Tlcm.setDCs(dotClockBuffer);  // I can't get this to work but it's here if it did
  164.     // clear out key state
  165.     for(int i=0; i<4; i++){
  166.       for(int j=0; j<4; j++){
  167.         keyState[i][j] = (byte) 0;
  168.       }
  169.     }
  170.   // make sure to turn off the lights if you want here.
  171.  
  172. }  
  173.  
  174.  
  175. void loop () {
  176.  
  177.   checkSerial();
  178.   // do the commands in serial
  179.  
  180.   //showBuffer(); // Debug
  181.  
  182.   if (ShutdownModeChange) {
  183.     ShutdownModeChange = false;
  184.   }
  185.  
  186.   // check for button presses
  187.   buttonCheck();  // monome output
  188.   //buttonPressed();  // change LEDs for testing
  189. }
  190.  
  191. void onLED( int x, int y)
  192. {
  193.    x^=3; // change order of x in row
  194.    Tlcm.set(ledBuffer, x*3   , y, redOn);
  195.    Tlcm.set(ledBuffer, x*3 +1, y, greenOn);
  196.    Tlcm.set(ledBuffer, x*3 +2, y, blueOn);
  197. }
  198. void offLED( int x, int y)
  199. {  x^=3; // change order of x in row
  200.    Tlcm.set(ledBuffer, x*3   , y, redOff);
  201.    Tlcm.set(ledBuffer, x*3 +1, y, greenOff);
  202.    Tlcm.set(ledBuffer, x*3 +2, y, blueOff);
  203. }
  204.  
  205. // for debug
  206. void showBuffer()
  207. { int i,j;
  208.    for(j=0; j<7; j++){
  209.      for(i=0; i<42; i++){
  210.        Serial.print(ledBuffer[i+j*42], HEX);
  211.        Serial.print(" ");
  212.      }
  213.      Serial.println(" ");
  214.    }
  215. }
  216.  
  217. void clearRows() {
  218.   for(int i=0; i<7; i++){
  219.   for(int j=0; j<42; j++) ledBuffer[j + i*42 ] = 0;
  220.   }
  221. }
  222.  
  223. void setRows() {
  224.    for(int i=0; i<7; i++){
  225.   for(int j=0; j<42; j++) ledBuffer[j + i*42 ] = 0xff;
  226. }
  227. }
  228. // test routine see if a buton is pressed turn on LED if it is
  229. void buttonPressed(){
  230.    // read push button
  231.    for(int j=0; j<4; j++){
  232.    for(int i=0; i<4; i++){
  233.     if(Tlcm.readPush(i,j) != 0) {
  234.  //   Serial.print(i);
  235.  //   Serial.print(j);
  236.  //   Serial.print(" ");
  237.     onLED(i,j);
  238.     if(j==0 && i==0) showBuffer();
  239.           }
  240.           else {
  241.           // offLED(i,j);
  242.         }
  243.      }
  244.    }
  245. }
  246.  
  247. // handle incomming message and act on it
  248. void checkSerial() {
  249.   do
  250.   {
  251.     if (Serial.available())
  252.     {
  253.       if (WaitingForAddress)   // address is the first byte of the two byte command
  254.       {
  255.         byte0 = Serial.read();
  256.         address = byte0 >> 4;
  257.         WaitingForAddress = false;
  258.         switch(address)  // do one byte commands
  259.         {
  260.          case 9: // clear command
  261.          if((byte0 & 1) ==0) clearRows(); else setRows();
  262.         WaitingForAddress = true; // next byte is a new command
  263.         break;
  264.         }        
  265.       }
  266.  
  267.       if (Serial.available())
  268.       {
  269.         WaitingForAddress = true;  // the next byte is the first byte of the next command
  270.         byte1 = Serial.read();    // read the second byte of this command
  271.  
  272.         switch(address)
  273.         {
  274.         case 2:  // led command
  275.           state = byte0 & 15;
  276.           x = (byte1 >> 4) & 0x03;  // mask so we don't go over the 4 by 4 grid
  277.           y = (byte1 & 15) & 0x03;
  278.  
  279.           if (state == 0){
  280.             offLED(x,y);
  281.           }
  282.           else {
  283.           onLED(x,y);
  284.           }
  285.           break;
  286.  
  287.         case 3:   // led intensity command RGB packed into 12 bits following the message
  288.           redOn =   ((byte0 & 0xf) << 4) | byte1;
  289.           greenOn = ((byte0 & 0xf) << 4) | byte1;
  290.           blueOn =  ((byte0 & 0xf) << 4) | byte1;        
  291.           break;
  292.         case 4:  // led test command
  293.           if( (byte1 & 1) == 0) { setRows(); } else { clearRows(); }      
  294.           break;
  295.         case 5:  // enable ADC command - but we don't do this
  296.           break;
  297.         case 6: // shutdown command - not sure what the monome is expected to do here
  298.           ShutdownModeChange = true;
  299.           ShutdownModeVal= byte1 & 15;
  300.           break;
  301.         case 7:  // led row command
  302.           y = byte0 & 0x03; // mask this value so we don't write to an invalid address.
  303.           z = byte1;
  304.           x = 0;
  305.           for(byte i = 1; i<0x10; i <<= 1 ){
  306.           if( (i & z) != 0) { onLED(x,y);} else  { offLED(x,y);}
  307.           x++;
  308.           }
  309.           break;
  310.         case 8:  // coloum command
  311.           x = byte0 & 0x03; // mask this value so we don't write to an invalid address.
  312.           z = byte1;
  313.           y = 0;
  314.           for(byte i = 1; i<0x10; i <<= 1){
  315.           if( (i & z) != 0) { onLED(x,y); } else  { offLED(x,y);}
  316.           y++;
  317.           }
  318.           break;
  319.           default:
  320.           break;
  321.           // extra colour setting commands
  322.         case 13:   // set red led intensity command
  323.           redOn = byte1 | ((byte0 & 0x0f) << 8);
  324.           break;
  325.         case 14:   // set green led intensity command  
  326.           greenOn = byte1 | ((byte0 & 0x0f) << 8);
  327.           break;
  328.         case 15:   // set blue led intensity command  
  329.           blueOn = byte1 | ((byte0 & 0x0f) << 8);        
  330.           break;
  331.          
  332.         } // end switch(address)
  333.       } // end if (Serial.available()
  334.     } // end if (Serial.available();
  335.   } // end do
  336.   while (Serial.available() > 16);
  337. }
  338.  
  339. void buttonCheck()
  340. { byte t;
  341.   // read push button array and send message if anything changed from last time
  342.    for(byte j=0; j<4; j++){
  343.    for(byte i=0; i<4; i++){
  344.     if(Tlcm.readPush(i,j) != keyState[i][j] ) {
  345.       // a key has changed
  346.        keyState[i][j] = Tlcm.readPush(i,j);
  347.        // send on or off if pressed
  348.         if(keyState[i][j] == 0) { Serial.print(0x0,BYTE); } else { Serial.print(0x1,BYTE); }
  349.         // send x - y coordnate of switch that has changed in the second byte
  350.        t = (i << 4) | j;
  351.        Serial.print(t,BYTE);
  352.           }
  353.      }
  354.    }
  355.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement