KrisKasprzak / ILI9341_t3_controls

Graphical slider controls for TFT LCD type displays
30 stars 64 forks source link

Issues with CCGraph - Plotting Points and Colors on multiple Graphs #8

Open mjs513 opened 1 month ago

mjs513 commented 1 month ago

Hi Kris Been hacking on your controls library so I can use one of the new RA8876 parallel display libraries that we've been working on for the Teensy 4.1.

Since most of libraries are derivatives of the ILI9341_t3 library most of the commands were the same.

To get it to work with the new lib I changed the #include to:

#if __has_include("ILI9341_t3.h")
#include <ILI9341_t3.h>
#elif __has_include("NT35510_t4x_p.h")
#include <Teensy_Parallel_GFX.h>
#include <NT35510_t4x_p.h>
#define ILI9341_t3 NT35510_t4x_p
#elif __has_include("RA8876_t41_p.h")
#include <RA8876_common.h>
#include <RA8876_t41_p.h>
#define ILI9341_t3 RA8876_t41_p
#endif

Tried the 800x 600 NT33510 lib as well but think there are issues with it. But the RA8876 works for the most part. But did have to change your drawFillRoundRect and fillroundrect functions to match whats in the RA8876 lib to:

        #if __has_include("RA8876_t41_p.h")
        d->fillRoundRect(x - (w/2)+(bt/2), y - (h/2)+(bt/2), w-bt, h-bt, min(w, h) / 4, min(w, h) / 4, disablecolorfill);
        d->drawRoundRect(x - (w/2)+(bt/2), y - (h/2)+(bt/2), w-bt, h-bt, min(w, h) / 4, min(w, h) / 4, disablecolortext);
        #else
                d->fillRoundRect(x - (w/2)+(bt/2), y - (h/2)+(bt/2), w-bt, h-bt, min(w,h)/4, disablecolorfill);
                d->drawRoundRect(x - (w/2)+(bt/2), y - (h/2)+(bt/2), w-bt, h-bt, min(w,h)/4-(bt/2), disablecolortext);
        #endif
            }

Not sure yet if I have to do that for other display libs we have yet.

Also in your latest version you are using measureTextWidth which we never ported over to any of the other libs so I used

        #if __has_include("RA8876_t41_p.h")
        uint16_t _txtWidth = d->getFontWidth() * strlen(label);
                d->setCursor(x - _txtWidth, y - (_txtWidth/2));
        #else
                d->setCursor(x - (d->measureTextWidth(label)/2), y - (d->measureTextHeight(label)/2));
        #endif

That still needs work for multiple lines but will get there.

Next Post has the issues. This was just the disclaimer

mjs513 commented 1 month ago

Ran into 2 problems. If I have multiple graphs (3) in my test case only the first graph will plot marker size of 1 - the other 2 show nothing unless I change there markersize to 2??? I have a hack that works but not sure if thats the best way:

            if ( pdia[cID] > 1){
                d->fillCircle(XPoint, YPoint, pdia[cID],  dc[cID]);
                // d->fillRect(XPoint -  pdia[cID]/2, YPoint-  pdia[cID]/2, pdia[cID],   pdia[cID], dc[cID]);
            } 
      else if ( pdia[cID] == 1){    //added this - seems to resolve the issue
        d->drawPixel(XPoint, YPoint,  dc[cID]);
      }  

But the other problem I can't figure out is that if I have:

  mxmyID = MyGraph.add("Mxy", C_RED);
  mxmzID = MyGraph.add("Mxz", C_GREEN);
  mymzID = MyGraph.add("Myz", C_BLUE);  

only the plot for mxmyID will show RED - the other 2 plots are plotted in BLACK. If I put in some debug statements dc[ID] shows correctly in CGraph::add but when I do a dump in CGraph::plot of the dc array dc[0] is red but 1 and 2 show 0? Now sure how its getting its loosing the colors. This is what is looks like before my markersize 1 fix IMG_1451

Any help woult be appreciated it

mjs513 commented 1 month ago

Oh here is the whole sketch

/***************** config display and graphs **********************/

#include "RA8876_Config_8080.h"
#include <RA8876_common.h>
#include <RA8876_t41_p.h>

#include <font_Arial.h>           // custom fonts that ships with ILI9341_t3.h
#include <ILI9341_t3_Controls.h>

#include <ILI9341_t3_Controls.h>
#include <font_Arial.h>           // custom fonts that ships with ILI9341_t3.h

// you must create and pass fonts to the function
#define FONT_TITLE Arial_16
#define FONT_DATA Arial_10

// defines for graph location and scales
#define X_ORIGIN    75
#define Y_ORIGIN    250
#define X_ORIGIN2    75
#define Y_ORIGIN2    550
#define X_ORIGIN3    350
#define Y_ORIGIN3    550

#define X_WIDE        200
#define Y_HIGH        200
#define X_LOSCALE   -75
#define X_HISCALE   75
#define X_INC       25
#define Y_LOSCALE   -75
#define Y_HISCALE   75
#define Y_INC       25

#define C_WHITE         0xFFFF
#define C_BLACK         0x0000
#define C_GREY          0xC618
#define C_BLUE          0x001F
#define C_RED           0xF800
#define C_GREEN         0x07E0
#define C_CYAN          0x07FF
#define C_MAGENTA       0xF81F
#define C_YELLOW        0xFFE0  
#define C_TEAL            0x0438       
#define C_ORANGE      0xDC00          
#define C_PINK        0xF81F
#define C_PURPLE          0x801F
#define C_DKGREY      0x52AA

#define TEXTCOLOR C_WHITE
#define GRIDCOLOR C_GREY
#define AXISCOLOR C_YELLOW
#define BACKCOLOR C_BLACK
#define PLOTCOLOR C_DKGREY

// used to monitor elaspsed time
unsigned long oldTime;

// create a variable for each data data point
float mx, my, mz;
float ax, ay, az;

// create an ID for each data to be plotted
int mxmyID, mxmzID, mymzID;

// create the display object
RA8876_t41_p Display = RA8876_t41_p(RA8876_8080_DC,RA8876_8080_CS,RA8876_8080_RESET);

// create the cartesian coordinate graph object
CGraph MyGraph(&Display, X_ORIGIN, Y_ORIGIN, X_WIDE, Y_HIGH, X_LOSCALE, X_HISCALE, X_INC, Y_LOSCALE, Y_HISCALE, Y_INC);
CGraph MyGraph1(&Display, X_ORIGIN2, Y_ORIGIN2, X_WIDE, Y_HIGH, X_LOSCALE, X_HISCALE, X_INC, Y_LOSCALE, Y_HISCALE, Y_INC);
CGraph MyGraph2(&Display, X_ORIGIN3, Y_ORIGIN3, X_WIDE, Y_HIGH, X_LOSCALE, X_HISCALE, X_INC, Y_LOSCALE, Y_HISCALE, Y_INC);

/************************ Configure MPU9250 *******************************/

// Include Modified Bolderflight invensense-imu library
#include "mpu9250.h"

/* Mpu9250 object */
bfs::Mpu9250 imu;

//Gravity
#define   G   9.80665

// scale factors
float accelScale, gyroScale;
float magScale[3];

char cmd;
float values1[10];
char str[128];

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

void setup() {

 /* Serial to display data */
  while(!Serial && millis() < 5000) {}
  Serial.begin(115200);

  // If Teensy 4.x fails print Crashreport to serial monitor
  if (CrashReport) {
      Serial.print(CrashReport);
      Serial.println("Press any key to continue");
      while (Serial.read() != -1) {
      }
      while (Serial.read() == -1) {
      }
      while (Serial.read() != -1) {
      }
  }

  // fire up the display
  Display.setBusWidth(RA8876_8080_BUS_WIDTH); // RA8876_8080_BUS_WIDTH is defined in
                                          // src/RA8876_Config_8080.h. 
  Display.begin(BUS_SPEED); // RA8876_8080_BUS_WIDTH is defined in
                        // src/RA8876_Config_8080.h. Default is 20MHz.   Display.setRotation(1);
  Display.fillScreen(C_BLACK);
  Display.setRotation(0);

  Display.fillScreen(C_BLACK);

  // initialize the graph object
  MyGraph.init("MX-MY", "", "", TEXTCOLOR, GRIDCOLOR, AXISCOLOR, BACKCOLOR, PLOTCOLOR, FONT_TITLE, FONT_DATA);
  MyGraph1.init("MX-MZ", "", "", TEXTCOLOR, GRIDCOLOR, AXISCOLOR, BACKCOLOR, PLOTCOLOR, FONT_TITLE, FONT_DATA);
  MyGraph2.init("MY-MZ", "", "", TEXTCOLOR, GRIDCOLOR, AXISCOLOR, BACKCOLOR, PLOTCOLOR, FONT_TITLE, FONT_DATA);

  // use the add method to create a plot for each data
  // PlotID = MyGraph.Add(data title, data color);
  // 
  //VoltID = MyGraph.add("Volts", VOLTSCOLOR);
  //SinID = MyGraph.add("sin(x)", SINCOLOR);
  mxmyID = MyGraph.add("Mxy", C_RED);
  mxmzID = MyGraph.add("Mxz", C_GREEN);
  mymzID = MyGraph.add("Myz", C_BLUE);  

  MyGraph.setMarkerSize(mxmyID, 1); 
  MyGraph1.setMarkerSize(mxmzID, 1); 
  MyGraph2.setMarkerSize(mymzID, 1);

  // MyGraph.setYAxis(-1.5, 2.5, 0.5);  // reset the y axis at any time in your program
  // MyGraph.showTitle(false);          //  hide the title--good for big graph in tight space
  // MyGraph.showLegend(false);         //  hide the legend--good for big graph in tight space
  // MyGraph.showXScale(false);         //  hide the x scale--good for big graph in tight space
  // MyGraph.showYScale(false);         //  hide the y scale--good for big graph in tight space

  // these call are all optional
  MyGraph.drawGraph();      // draw empty graph if you have a long delay before any plottable data
  MyGraph1.drawGraph();     // draw empty graph if you have a long delay before any plottable data
  MyGraph2.drawGraph();     // draw empty graph if you have a long delay before any plottable data

  /**************************** MPU-9250 *********************************/
  /* Start the I2C bus */
  Wire2.begin();
  Wire2.setClock(400000);
  /* I2C bus,  0x68 address */
  imu.Config(&Wire2, bfs::Mpu9250::I2C_ADDR_PRIM);
  /* Initialize and configure IMU */
  if (!imu.Begin()) {
    Serial.println("Error initializing communication with IMU");
    while(1) {}
  }
  /* Set the sample rate divider */
  // rate = 1000 / (srd + 1)
  // = 1000/20 = 50 hz
  // = 100 hz
  if (!imu.ConfigSrd(9)) {
    Serial.println("Error configured SRD");
    while(1) {}
  }

  /* Accelerometer options
    ACCEL_RANGE_2G
    ACCEL_RANGE_4G
    ACCEL_RANGE_8G
    ACCEL_RANGE_16G (Default)
  */
  //imu.ConfigAccelRange(bfs::Mpu9250::ACCEL_RANGE_16G);
  /* Gyroscope Options
    GYRO_RANGE_250DPS
    GYRO_RANGE_500DPS
    GYRO_RANGE_1000DPS
    GYRO_RANGE_2000DPS
  */
  //imu.ConfigGyroRange(bfs::Mpu9250::GYRO_RANGE_2000DPS);

  //Get MPU sensitivity values
  imu.getScales(&accelScale, &gyroScale, magScale);
  Serial.print("Accelerometer Scale: "); Serial.println(accelScale, 5);
  Serial.print("Gyro Scale: "); Serial.println(gyroScale, 5);
  Serial.println("Magnetometer Scales:");
  Serial.print("\tMagx: "); Serial.println(magScale[0],5);
  Serial.print("\tMagx: "); Serial.println(magScale[1],5);
  Serial.print("\tMagx: "); Serial.println(magScale[2],5);

}

void loop() {
  char cmd = '0';
  if(Serial.available()) {
    cmd = Serial.read();
    switch(cmd) {
      case 'v':  // Send handshake
        {
          //sprintf(str, "FreeIMU library by FREQ: LIB_VERSION: %s", FREEIMU_LIB_VERSION);
          Serial.print("OK....");
          Serial.print('\n');
          break;
        }
      case 'x': //No calibration
        {
          uint16_t count = 10000;
          uint16_t i_count = 0;
          //for(uint8_t i=0; i<count; i++) {
          while(i_count < count) {
            if(imu.Read(values1)){
              i_count += 1;
              MyGraph.setX(values1[6]);
              MyGraph.plot(mxmyID, values1[7]);
              MyGraph1.setX(values1[6]);
              MyGraph1.plot(mxmzID, values1[8]);
              MyGraph2.setX(values1[8]);
              MyGraph2.plot(mymzID, values1[7]);
              //sprintf(str, "%f,%f,%f,%f,%f,%f,%f,%f,%f,", values1[0], values1[1], values1[2], values1[3], values1[4], values1[5], values1[6], values1[7], values1[8]);
              //Serial.println(str);
              if((i_count % 500) == 0) Serial.print(".");
            }
            delay(5);
          }
          Serial.println(" Done....");
          break;
        }
      default:
        break;
    }
    while (Serial.read() != -1)
        ;  // lets strip the rest out
  }
}