Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Adafruit TFT ILI9341 for Visualizer

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    4

    Adafruit TFT ILI9341 for Visualizer

    Currently working on a visualizer, music is coming from a Bluetooth module that is currently connected to speakers. Just trying to get it to be like a screensaver, where after a few seconds of not touching the screen (where there are currently buttons not tied to anything) to a visualizer going along with the music... I don't know where to continue from the buttons screen.

    Code:
    #include "Adafruit_GFX.h"
    #include "Adafruit_ILI9341.h"
    #include "TouchScreen.h"
    #include "Buttons.h"
    #include <SPI.h>
    #include <stdint.h>
    
    #define BACK_CLR ILI9341_BLACK
    #define numButt 6
    Button b[numButt];
    
    Button bSingle[numButt];           // single screen
    int bSize = sizeof(Button);
    #define TFT_CS_0 10
    #define TFT_DC_0 6
    #define TFT_RST 0
    #define numScr 1
    #define XP 3
    #define YP 1
    #define XM 2
    #define YM 4
    Adafruit_ILI9341 tft = { Adafruit_ILI9341(TFT_CS_0, TFT_DC_0, TFT_RST)};    // screen
    // measured 282 ohms accross the X plate
    TouchScreen ts = { TouchScreen(XP, YP, XM, YM, 298)};      // touch screen
    
    void setup() {
      Serial.begin(9600);
      
      tft.begin();
      tft.fillScreen(BACK_CLR);
      // initialise buttons on screen
      int xOffset = 30;                                 // offset from x edge
      int yOffset = 30;                                 // offset from y edge
      int numBX = 2;                                    // number of buttons along the x axis per screen
      int numBY = 3;                                    // number of buttons along the y axis per screen
      int xDiff = (tft.width()) / numBX;             // distance between buttons on x axis per screen
      int yDiff = (tft.height()) / numBY;            // distance between buttons on y axis per screen
      for (int i = 0; i < numBX; i++) {
        for (int j = 0; j < numBY; j++) {
          b[j+numBY * i] = B_Init_Def(b[j + numBY * i],xOffset + i * xDiff,yOffset + j * yDiff);
          
        }
      }
      for (int i = 0; i < numButt; i++) {
        tft.fillRoundRect(b[i].x, b[i].y, b[i].width, b[i].height, b[i].r, b[i].clr);
      }
      
    }
    
    void loop() {
     static boolean set = false;
     if (!set) {
       set = true;
       setupSingle();
     }
     singleScreen();
    
    }
     void setupSingle() {
      digitalWrite(LED_BUILTIN,1);
    
      // fill other screens with grey, this may not be possible
      for (int i=0; i < numScr; i++)
        if (i != 1)
          tft.fillScreen(0x8410);
      tft.fillScreen(BACK_CLR);
      
      // initialise buttons on left screen
      int bHeight = DEF_B_HEIGHT - 40;                   // height of buttons
      int bWidth = DEF_B_WIDTH - 42;                     // width of buttons
      int xOffset = 20;                                 // offset from x edge
      int yOffset = 30;                                 // offset from y edge
      int numBX = 2;                                    // number of buttons along the x axis per screen
      int numBY = 3;                                    // number of buttons along the y axis per screen
      int xDiff = (tft.width()) / numBX;    // distance between buttons on x axis per screen
      int yDiff = (tft.height()) / numBY;   // distance between buttons on y axis per screen
    
      // place buttons in an even grid formation
      for (int i = 0; i < numBX; i++) {
        for (int j = 0; j < numBY; j++) {
          bSingle[j + numBY * i] = B_Init(bSingle[j + numBY * i], xOffset + i * xDiff, yOffset + j * yDiff, bHeight, bWidth, 5, DEF_B_CLR, 1);
        }
      }
      
      
      // Draw to screens
      for (int i = 0; i < numButt; i++) {
        tft.fillRoundRect(bSingle[i].x, bSingle[i].y, bSingle[i].width, bSingle[i].height, bSingle[i].r, bSingle[i].clr);
      }
     }
    
     void singleScreen() {
      static int led = 1;
      static int cnt = 0;
      
      if (cnt++ > 200) {
        digitalWrite(LED_BUILTIN, led ^= 1);
        cnt = 0;
      }
      
      // If touchscreen pressed
      static int fCnt = 0;
      static int lastX = 0, lastY = 0;
    
    
      // Read from touchscreens
      TSPoint p = ts.getPoint();
    
      // When touch detected    
      if (p.z > ts.pressureThreshhold) {
        
        // scale from 0->1023 to tft.width
        p.x = map(p.x, 920, 120, tft.width(), 0);
        p.y = map(p.y, 935, 75, tft.height(), 0);
    
        // button press to change color
        if (!digitalRead(5))
        {
          // only when pressed, not continuously
          if (fCnt > 30) {
            for (int i = 0; i < numButt; i++) {
              if (isPressed(bSingle[i], p.x, p.y)) {
    
                // if red, change to green
                if (bSingle[i].clr == 0xF81F) {
                  bSingle[i].clr = ILI9341_GREEN;
                  Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" connected");
    
                  // all other green buttons change red
                  for (int j = 0; j  < numButt; j++) {
                    if (j != i) {
                      if (bSingle[j].clr == ILI9341_GREEN) {
                        bSingle[j].clr = ILI9341_RED;
                        tft.fillRoundRect(bSingle[j].x, bSingle[j].y, bSingle[j].width, bSingle[j].height, bSingle[j].r, bSingle[j].clr);
                        Serial.print("Channel "); Serial.print(j, DEC); Serial.println(" disconnected");
                      }
                    }
                  }
                }
                else {
                  // change to red if green
                  bSingle[i].clr = 0xF800;//random(0xFFFF);
                  Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" disconnected");
                }
    
                // redraw pressed button
                tft.fillRoundRect(bSingle[i].x, bSingle[i].y, bSingle[i].width, bSingle[i].height, bSingle[i].r, bSingle[i].clr);
              }
            }
          }
        } else {
    
          // allow button to be moved
          for (int i = 0; i < numButt; i++) {
    
            // button must move a small distance before it registers
            if (abs(lastX - p.x) > 5 || abs(lastY - p.y) > 5) {
    
              // first time button pressed
              if (fCnt > 5) {
                lastX = p.x;
                lastY = p.y;
              }
    
              // remove last button, print new
              if (isPressed(bSingle[i], p.x, p.y)) {
                tft.fillRoundRect(bSingle[i].x, bSingle[i].y, bSingle[i].width, bSingle[i].height, bSingle[i].r, BACK_CLR);
                tft.fillRoundRect(bSingle[i].x += p.x - lastX, bSingle[i].y += p.y - lastY, bSingle[i].width, bSingle[i].height,bSingle[i].r, bSingle[i].clr);
    
                lastX = p.x;
                lastY = p.y;
              }
            }
          }
        }
        fCnt = 0;
      } else {
        fCnt++;
      }
    }

  2. #2
    Junior Member
    Join Date
    Mar 2020
    Posts
    4
    I should update this with some clarifying. I just need to figure out how to do the visualizer. I thought it would be cool to use the touchscreen to visually show changes in the music that gets played.
    Code changes still in progress, but so far reduced buttons because 6 were too many.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •