Advertisement
Guest User

ArduinoCode

a guest
Mar 29th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.55 KB | None | 0 0
  1. #include "teensy_tlc5940.h"
  2. #define TTL 750
  3. #define yMax 16
  4. #define xMax 10
  5.  
  6. #define S0 0
  7. #define S1 1
  8. #define S2 2
  9. #define S3 5
  10.  
  11. //Variables associated with draw()
  12. int count = 0;
  13. int readyToFlip = 0;  //Determines whether to read input or not.
  14.                              //initially false because it shouldn't wait for draw and should read inputs
  15.  
  16. //Initial variables
  17. boolean piReady = false;
  18. char piRdyResponse[6] = {' ', ' ', ' ', ' ', ' ', '\0'};
  19. char inByte;
  20. unsigned long startOfNoSerial;
  21. bool serialAvailable;
  22. bool receivedData = false;
  23. //Input variables
  24. int packetPlacement = 0;
  25. int payloadByte = 0;
  26. int currentValue[5] = {0,0};
  27. boolean inPacket = false;
  28. int ledArray[10][16][6];   //switch to a char array, typecast every single time we send out
  29.                             /*Of the [8]
  30.                                 -0,1,2 for first array
  31.                                 -3,4,5 for second array
  32.                                 -6 for checkbit
  33.                                   -will be 1 when array is full (can draw array)
  34.                                   -will be 0 when array isn't full (has been drawn)
  35.                             */
  36. int writeToLeftArray=0;  /*
  37.                             -  False, write to first array, pass to draw function, draw from first array, set writeToLeftArray to true (second array)
  38.                             -  True,  write to second array, pass to draw function, draw from second array, set writeToLeftArray to false (first array)
  39.                             */
  40. int shifter = 3;
  41.  
  42. int updateCount = 0;
  43.                            
  44. //TLC and shift register variables
  45. int shiftRegCount = 0;
  46. int shiftSelect[8] = {1,2,4,8,16,32,64,128};
  47.  
  48. void setup() {
  49.  // put your setup code here, to run once:
  50.  Serial.begin(115200); // set the baud rate
  51.  delay(2); //Delay to let both interfaces initialize serial
  52.  //analogWriteResolution(12);
  53.  initializeScreenToGradient();
  54.  //Setting pinModes for sensor grid (digital pinouts for the MUX)
  55.  
  56. pinMode(S0, OUTPUT);
  57. pinMode(S1, OUTPUT);
  58. pinMode(S2, OUTPUT);
  59. pinMode(S3, OUTPUT);
  60. // pinMode(4, OUTPUT);  //can just ground this si ne it isa lways low
  61. //
  62.  
  63.   //Shift register initialization
  64.   pinMode(7,OUTPUT);  //Clock Pin
  65.   pinMode(2,OUTPUT);  //Latch Pin
  66.   pinMode(8,OUTPUT);  //Data Pin
  67.   pinMode(6,OUTPUT); //~OE pin
  68.   //digitalWrite(15,LOW);
  69.   //TLC initialization
  70.   initTLC5940();
  71.  
  72.  
  73.  //readSensors();  //Read one full array and send to Pi intially so we have something to parse through when we get to the main loop
  74. }
  75.  
  76. void loop() {
  77.  // put your main code here, to run repeatedly:
  78.    for(int i = 0; i < 100; i++) {
  79.    bool sysReady = initiate();
  80.    if(sysReady){
  81.      readInput();      //read input from Pi
  82.    }
  83.  }
  84.     if(receivedData) {
  85.      readSensors();  //Send entire array to Pi
  86.    }
  87.  draw();       //send entire array from Pi to LED's
  88. }
  89.  
  90. //========================READ SENSORS==========================
  91. void readSensors(){
  92.   for(int i=0; i<yMax; i++){
  93.     //
  94.     //digitalWrite(S0,bitRead(i,S0));
  95.     //digitalWrite(S1,bitRead(i,S1));
  96.     //digitalWrite(S2,bitRead(i,S2));
  97.     //digitalWrite(S3,bitRead(i,S3));
  98.     //digitalWrite(4,0);        we can just ground this
  99.    
  100.     for (int j=0; j<xMax; j++){
  101.         //Serial.write(char(255));
  102.         //Serial.write(char((i)*10 + (j) + 1));
  103.         //Serial.write(char(i+j+1));
  104.         //Serial.write(char(0));
  105.         //Serial.send_now();
  106.     }
  107.   }
  108.   Serial.print(char(255));
  109.   Serial.send_now();
  110. }
  111.  
  112. //==============================DRAW==================================
  113. void draw(){
  114.   for(int j = 0; j<48; j++) {
  115.       setGSData(j,ledArray[shiftRegCount][j%16][j/16]);
  116.   }
  117.     //Serial.println(j);
  118.     //delay(50);  
  119.   //Send out values and shift the shift register
  120.     sendGSData(shiftSelect[shiftRegCount]);
  121.   //Increment count
  122.    ++shiftRegCount;
  123.    if(shiftRegCount > 7) {
  124.       shiftRegCount = 0;
  125.    }
  126. }
  127.  
  128. //=========================READ INPUT FROM PI==============================
  129. void readInput() {
  130.    inByte = Serial.read();  //reads first character in buffer from incoming serial data
  131.    payloadByte = int(inByte);  //typecasts inByte from a char to an int
  132.    if (inPacket) {  //If we're in a packet (have read start bit), then continue
  133.      if(payloadByte == 0) {  //Checks to see if we're reading the stop bit
  134.          inPacket = false;      //Sets inPacket to false, meaning we're not in a packet and will look for another start bit
  135.          //Once we hit the stop bit, package has been read and we update the mask
  136.          //ledArray[currentValue[0]][currentValue[1]][6]=255;
  137.          if(++updateCount > 159) {
  138.            updateCount = 0;
  139.            if(writeToLeftArray == 0) {
  140.              writeToLeftArray = 1;
  141.              shifter = 3;
  142.            }
  143.            else {
  144.               writeToLeftArray = 0;
  145.               shifter = 0;
  146.            }
  147.          }
  148.      }
  149.      else {
  150.          if(payloadByte == 255) {  
  151.            inPacket = true;
  152.            packetPlacement = 0;
  153.          }
  154.          if(packetPlacement == 0) {                  //First piece of data in packet denote position on table
  155.              int x = 0;
  156.              int y = 0;
  157.              if(payloadByte > 160) {
  158.                payloadByte = 160;
  159.              }
  160.              xyconvert(payloadByte-1, &x, &y);       //Converts position to x-y coordinates
  161.              currentValue[0] = x;
  162.              currentValue[1] = y;
  163.          }                                           //Next three bytes will denotes r,g,b brightness,
  164.          else if(payloadByte == 1) {     //                      
  165.              payloadByte = 0;
  166.          }
  167.          else if(payloadByte == 254) {
  168.              payloadByte = 255;
  169.          }
  170.          
  171.          if(packetPlacement > 0 && packetPlacement < 4) {
  172.            if(writeToLeftArray == 1){                                                                  //Determines which array to write to
  173.              ledArray[currentValue[0]][currentValue[1]][packetPlacement-1] = map(payloadByte,0,255,0,4095);            //Writing to first array
  174.            }else{
  175.              ledArray[currentValue[0]][currentValue[1]][packetPlacement+2] = map(payloadByte,0,255,0,4095);            //Writing to second array
  176.            }
  177.          }
  178.          if(packetPlacement < 0 || packetPlacement > 3) {
  179.              Serial.println("Error: Incorrect data format");
  180.          }
  181.          if(++packetPlacement > 6) {
  182.            inPacket = false;
  183.          }    
  184.      }
  185.    }
  186.    else {
  187.      if(payloadByte == 255) {  //ASCII start bit
  188.          inPacket = true;
  189.          receivedData = true;
  190.          packetPlacement = 0;
  191.      }
  192.    }
  193.  //Logic to flip from reading to left to reading from right and vice versa for data input from serial
  194. }
  195.  
  196. //================================XY CONVERT==========================
  197. int xyconvert(int in, int *x, int *y) {
  198.  *y=in/(xMax);
  199.  *x=in%(xMax);
  200. }
  201.  
  202. //============================HANDSHAKE===============================================
  203. //Raspberry Pi handshake function
  204. bool piHandshake() {
  205.  if(Serial.available()){
  206.    for(int i=1; i<5; i++){
  207.      piRdyResponse[i-1] = piRdyResponse[i];
  208.    }
  209.    char inByte = Serial.read();
  210.    piRdyResponse[4] = inByte;
  211.    if(strcmp(piRdyResponse, "Ready") == 0){ //As soon as the RPi says "Ready", it returns true
  212.      Serial.println("Ready");
  213.      serialAvailable=true;
  214.      return true;
  215.    }
  216.  }
  217.  return false;
  218. }
  219.  
  220. //================================INITIATE FUNCTION====================================
  221. bool initiate() {
  222.  if(Serial.available() && piReady==true){  //If there's data to be taken and piReady is good to go, read data
  223.    serialAvailable=true;
  224.    return true;
  225.  }
  226.  if(!(Serial.available()) && piReady==true){  //If there's no data and piReady is good to go, count
  227.    if(serialAvailable==true){
  228.      startOfNoSerial = millis();
  229.      serialAvailable=false;
  230.    }
  231.    else if((unsigned long)(millis() - startOfNoSerial) >= TTL){
  232.      initializeScreenToOn();
  233.      delay(2);
  234.      receivedData = false;
  235.      Serial.println("False");
  236.      piReady = false;
  237.    }
  238.  }
  239.  if(piReady == false) {           //If piReady isn't ready, check handshake.
  240.    piReady = piHandshake();
  241.  }    
  242.  return false;
  243. }
  244.  
  245. void initializeScreenToGradient() {
  246.  for(int i = 0; i<10; i++) {      //Initialize array to all 0.
  247.      for(int j = 0; j<16; j++) {
  248.          for(int k = 0; k<6; k++){
  249.            int temp = 0;
  250.              if(k == 6) {
  251.              temp = 0;
  252.              }
  253.              else {
  254.              temp = map(i+j,0,8+9,0,4095);  
  255.              }
  256.              ledArray[i][j][k] = temp;
  257.          }
  258.      }  
  259.  }
  260. }
  261. void initializeScreenToRevGradient() {
  262.  for(int i = 0; i<10; i++) {      //Initialize array to all 0.
  263.      for(int j = 0; j<16; j++) {
  264.          for(int k = 0; k<6; k++){
  265.            int temp = 0;
  266.              if(k == 6) {
  267.              temp = 0;
  268.              }
  269.              else {
  270.              temp = map(i*j,7*7,0,0,4095);  
  271.              }
  272.              ledArray[i][j][k] = temp;
  273.          }
  274.      }  
  275.  }
  276. }
  277. void initializeScreenToTopLeft() {
  278.  for(int i = 0; i<10; i++) {      //Initialize array to all 0.
  279.      for(int j = 0; j<16; j++) {
  280.          for(int k = 0; k<6; k++){
  281.            int temp = 0;
  282.              if(k == 6) {
  283.              temp = 0;
  284.              }
  285.              else if(i*j == 0) {
  286.              temp = map(i*j,7*7,0,0,4095);  
  287.              }
  288.              else {
  289.                temp = 0;
  290.              }
  291.              ledArray[i][j][k] = temp;
  292.          }
  293.      }  
  294.  }
  295. }
  296. void initializeScreenToBottomRight() {
  297.  for(int i = 0; i<10; i++) {      //Initialize array to all 0.
  298.      for(int j = 0; j<16; j++) {
  299.          for(int k = 0; k<6; k++){
  300.            int temp = 0;
  301.              if(k == 6) {
  302.              temp = 0;
  303.              }
  304.              else if(i+j == 7) {
  305.              temp = 4095;  
  306.              }
  307.              else {
  308.                temp = 0;
  309.              }
  310.              ledArray[i][j][k] = temp;
  311.          }
  312.      }  
  313.  }
  314. }
  315. void initializeScreenToOff() {
  316.  for(int i = 0; i<10; i++) {      //Initialize array to all 0.
  317.      for(int j = 0; j<16; j++) {
  318.          for(int k = 0; k<6; k++){
  319.            int temp = 0;
  320.              if(k == 6) {
  321.              temp = 0;
  322.              }
  323.              ledArray[i][j][k] = 0;
  324.          }
  325.      }  
  326.  }
  327. }
  328. void initializeScreenToOn() {
  329.  for(int i = 0; i<10; i++) {      //Initialize array to all 0.
  330.      for(int j = 0; j<16; j++) {
  331.          for(int k = 0; k<6; k++){
  332.            int temp = 0;
  333.              if(k == 6) {
  334.              temp = 0;
  335.              }
  336.              ledArray[i][j][k] = 4095;
  337.          }
  338.      }  
  339.  }  
  340.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement