Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "teensy_tlc5940.h"
- #define TTL 750
- #define yMax 16
- #define xMax 10
- #define S0 0
- #define S1 1
- #define S2 2
- #define S3 5
- //Variables associated with draw()
- int count = 0;
- int readyToFlip = 0; //Determines whether to read input or not.
- //initially false because it shouldn't wait for draw and should read inputs
- //Initial variables
- boolean piReady = false;
- char piRdyResponse[6] = {' ', ' ', ' ', ' ', ' ', '\0'};
- char inByte;
- unsigned long startOfNoSerial;
- bool serialAvailable;
- bool receivedData = false;
- //Input variables
- int packetPlacement = 0;
- int payloadByte = 0;
- int currentValue[5] = {0,0};
- boolean inPacket = false;
- int ledArray[10][16][6]; //switch to a char array, typecast every single time we send out
- /*Of the [8]
- -0,1,2 for first array
- -3,4,5 for second array
- -6 for checkbit
- -will be 1 when array is full (can draw array)
- -will be 0 when array isn't full (has been drawn)
- */
- int writeToLeftArray=0; /*
- - False, write to first array, pass to draw function, draw from first array, set writeToLeftArray to true (second array)
- - True, write to second array, pass to draw function, draw from second array, set writeToLeftArray to false (first array)
- */
- int shifter = 3;
- int updateCount = 0;
- //TLC and shift register variables
- int shiftRegCount = 0;
- int shiftSelect[8] = {1,2,4,8,16,32,64,128};
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200); // set the baud rate
- delay(2); //Delay to let both interfaces initialize serial
- //analogWriteResolution(12);
- initializeScreenToGradient();
- //Setting pinModes for sensor grid (digital pinouts for the MUX)
- pinMode(S0, OUTPUT);
- pinMode(S1, OUTPUT);
- pinMode(S2, OUTPUT);
- pinMode(S3, OUTPUT);
- // pinMode(4, OUTPUT); //can just ground this si ne it isa lways low
- //
- //Shift register initialization
- pinMode(7,OUTPUT); //Clock Pin
- pinMode(2,OUTPUT); //Latch Pin
- pinMode(8,OUTPUT); //Data Pin
- pinMode(6,OUTPUT); //~OE pin
- //digitalWrite(15,LOW);
- //TLC initialization
- initTLC5940();
- //readSensors(); //Read one full array and send to Pi intially so we have something to parse through when we get to the main loop
- }
- void loop() {
- // put your main code here, to run repeatedly:
- for(int i = 0; i < 100; i++) {
- bool sysReady = initiate();
- if(sysReady){
- readInput(); //read input from Pi
- }
- }
- if(receivedData) {
- readSensors(); //Send entire array to Pi
- }
- draw(); //send entire array from Pi to LED's
- }
- //========================READ SENSORS==========================
- void readSensors(){
- for(int i=0; i<yMax; i++){
- //
- //digitalWrite(S0,bitRead(i,S0));
- //digitalWrite(S1,bitRead(i,S1));
- //digitalWrite(S2,bitRead(i,S2));
- //digitalWrite(S3,bitRead(i,S3));
- //digitalWrite(4,0); we can just ground this
- for (int j=0; j<xMax; j++){
- //Serial.write(char(255));
- //Serial.write(char((i)*10 + (j) + 1));
- //Serial.write(char(i+j+1));
- //Serial.write(char(0));
- //Serial.send_now();
- }
- }
- Serial.print(char(255));
- Serial.send_now();
- }
- //==============================DRAW==================================
- void draw(){
- for(int j = 0; j<48; j++) {
- setGSData(j,ledArray[shiftRegCount][j%16][j/16]);
- }
- //Serial.println(j);
- //delay(50);
- //Send out values and shift the shift register
- sendGSData(shiftSelect[shiftRegCount]);
- //Increment count
- ++shiftRegCount;
- if(shiftRegCount > 7) {
- shiftRegCount = 0;
- }
- }
- //=========================READ INPUT FROM PI==============================
- void readInput() {
- inByte = Serial.read(); //reads first character in buffer from incoming serial data
- payloadByte = int(inByte); //typecasts inByte from a char to an int
- if (inPacket) { //If we're in a packet (have read start bit), then continue
- if(payloadByte == 0) { //Checks to see if we're reading the stop bit
- inPacket = false; //Sets inPacket to false, meaning we're not in a packet and will look for another start bit
- //Once we hit the stop bit, package has been read and we update the mask
- //ledArray[currentValue[0]][currentValue[1]][6]=255;
- if(++updateCount > 159) {
- updateCount = 0;
- if(writeToLeftArray == 0) {
- writeToLeftArray = 1;
- shifter = 3;
- }
- else {
- writeToLeftArray = 0;
- shifter = 0;
- }
- }
- }
- else {
- if(payloadByte == 255) {
- inPacket = true;
- packetPlacement = 0;
- }
- if(packetPlacement == 0) { //First piece of data in packet denote position on table
- int x = 0;
- int y = 0;
- if(payloadByte > 160) {
- payloadByte = 160;
- }
- xyconvert(payloadByte-1, &x, &y); //Converts position to x-y coordinates
- currentValue[0] = x;
- currentValue[1] = y;
- } //Next three bytes will denotes r,g,b brightness,
- else if(payloadByte == 1) { //
- payloadByte = 0;
- }
- else if(payloadByte == 254) {
- payloadByte = 255;
- }
- if(packetPlacement > 0 && packetPlacement < 4) {
- if(writeToLeftArray == 1){ //Determines which array to write to
- ledArray[currentValue[0]][currentValue[1]][packetPlacement-1] = map(payloadByte,0,255,0,4095); //Writing to first array
- }else{
- ledArray[currentValue[0]][currentValue[1]][packetPlacement+2] = map(payloadByte,0,255,0,4095); //Writing to second array
- }
- }
- if(packetPlacement < 0 || packetPlacement > 3) {
- Serial.println("Error: Incorrect data format");
- }
- if(++packetPlacement > 6) {
- inPacket = false;
- }
- }
- }
- else {
- if(payloadByte == 255) { //ASCII start bit
- inPacket = true;
- receivedData = true;
- packetPlacement = 0;
- }
- }
- //Logic to flip from reading to left to reading from right and vice versa for data input from serial
- }
- //================================XY CONVERT==========================
- int xyconvert(int in, int *x, int *y) {
- *y=in/(xMax);
- *x=in%(xMax);
- }
- //============================HANDSHAKE===============================================
- //Raspberry Pi handshake function
- bool piHandshake() {
- if(Serial.available()){
- for(int i=1; i<5; i++){
- piRdyResponse[i-1] = piRdyResponse[i];
- }
- char inByte = Serial.read();
- piRdyResponse[4] = inByte;
- if(strcmp(piRdyResponse, "Ready") == 0){ //As soon as the RPi says "Ready", it returns true
- Serial.println("Ready");
- serialAvailable=true;
- return true;
- }
- }
- return false;
- }
- //================================INITIATE FUNCTION====================================
- bool initiate() {
- if(Serial.available() && piReady==true){ //If there's data to be taken and piReady is good to go, read data
- serialAvailable=true;
- return true;
- }
- if(!(Serial.available()) && piReady==true){ //If there's no data and piReady is good to go, count
- if(serialAvailable==true){
- startOfNoSerial = millis();
- serialAvailable=false;
- }
- else if((unsigned long)(millis() - startOfNoSerial) >= TTL){
- initializeScreenToOn();
- delay(2);
- receivedData = false;
- Serial.println("False");
- piReady = false;
- }
- }
- if(piReady == false) { //If piReady isn't ready, check handshake.
- piReady = piHandshake();
- }
- return false;
- }
- void initializeScreenToGradient() {
- for(int i = 0; i<10; i++) { //Initialize array to all 0.
- for(int j = 0; j<16; j++) {
- for(int k = 0; k<6; k++){
- int temp = 0;
- if(k == 6) {
- temp = 0;
- }
- else {
- temp = map(i+j,0,8+9,0,4095);
- }
- ledArray[i][j][k] = temp;
- }
- }
- }
- }
- void initializeScreenToRevGradient() {
- for(int i = 0; i<10; i++) { //Initialize array to all 0.
- for(int j = 0; j<16; j++) {
- for(int k = 0; k<6; k++){
- int temp = 0;
- if(k == 6) {
- temp = 0;
- }
- else {
- temp = map(i*j,7*7,0,0,4095);
- }
- ledArray[i][j][k] = temp;
- }
- }
- }
- }
- void initializeScreenToTopLeft() {
- for(int i = 0; i<10; i++) { //Initialize array to all 0.
- for(int j = 0; j<16; j++) {
- for(int k = 0; k<6; k++){
- int temp = 0;
- if(k == 6) {
- temp = 0;
- }
- else if(i*j == 0) {
- temp = map(i*j,7*7,0,0,4095);
- }
- else {
- temp = 0;
- }
- ledArray[i][j][k] = temp;
- }
- }
- }
- }
- void initializeScreenToBottomRight() {
- for(int i = 0; i<10; i++) { //Initialize array to all 0.
- for(int j = 0; j<16; j++) {
- for(int k = 0; k<6; k++){
- int temp = 0;
- if(k == 6) {
- temp = 0;
- }
- else if(i+j == 7) {
- temp = 4095;
- }
- else {
- temp = 0;
- }
- ledArray[i][j][k] = temp;
- }
- }
- }
- }
- void initializeScreenToOff() {
- for(int i = 0; i<10; i++) { //Initialize array to all 0.
- for(int j = 0; j<16; j++) {
- for(int k = 0; k<6; k++){
- int temp = 0;
- if(k == 6) {
- temp = 0;
- }
- ledArray[i][j][k] = 0;
- }
- }
- }
- }
- void initializeScreenToOn() {
- for(int i = 0; i<10; i++) { //Initialize array to all 0.
- for(int j = 0; j<16; j++) {
- for(int k = 0; k<6; k++){
- int temp = 0;
- if(k == 6) {
- temp = 0;
- }
- ledArray[i][j][k] = 4095;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement