I know this can be done with less code!!

I'm making a midi sequencer with the seeedstudio TFT shield.
I made a little gird for the sequencer and need the squares to toggle between black and blue to show if it's active or not. I have a way to place the square in the correct spot but I can't figure out how to do it efficiently. I need to be able to toggle them at any time...

Heres the chunch of code that plots the square.
I'll put the whole thing in the attachment.

I assume I would have to iterate over it somehow and change the the values in the if statements but I'm still kindof a noob and I'm not entirely sure what I'm doing.
Thanks for the help!

//GRID TOGGLES
if (x > 0 && x < 60 && y > 180 && y < 240) //first square
{
//have to have way to tell if it's already selected or not
if (seqBool[0]==0)
{
//fill square
Tft.fillRectangle(1, 181, 59,59,CYAN); //slightly smaller square fits inside blue grid square. hense 61 instead of 60
seqBool[0]=1;
}

else
{
Tft.fillRectangle(1, 181, 59,59,BLACK); //slightly smaller square fits inside blue grid square. hense 61 instead of 60
seqBool[0]=0;
}
}

tftSequencer.ino (10.7 KB)

I assume I would have to iterate over it somehow

Yes. A for loop would be good. Actually a pair of them for handling the rows and the columns.

and change the the values in the if statements

Yes, based on the loop indices.

but I'm still kindof a noob and I'm not entirely sure what I'm doing.

Try something. If it doesn't work, and you can't figure out why, we'll be here all week.

I ran into the same 'slow' fillRectangle problem with the 3.2 TFT display.. fillRectangle is a slow function..
drawRectangle is fast if u can use that instead..
U might check the TFT.h and see if theres any notes on speeding up fill functions

I see nothing in 'TFTv2.h' that suggests a way to speed it up, but as PaulS say's looping the drawing of like sized rectangles can be simplified and made easier to read.

{
    int16_t     _left;
    int16_t     _top;
    int16_t     _width;
    int16_t     _height;

    rect_t(int16_t left = 0, int16_t top = 0, int16_t width = 0, int16_t height = 0)
        : _left(left), _top(top)
        , _width(width), _height(height)
    { }

    rect_t& operator= (const rect_t& rhs) 
    {
        _left   = rhs._left;
        _top    = rhs._top;
        _width  = rhs._width;
        _height = rhs._height;

        return *this;
    }

    rect_t& offset(int16_t dx, int16_t dy)
    {
        _width   += dx;
        _height  += dy;

        return *this;
    }

    int16_t left() const    { return _left; }
    int16_t top() const     { return  _top; }
    int16_t width() const   { return _width; }
    int16_t height() const  { return _height; }
};


void drawGrid()
{
    // ... allocate an instance of rect_t named 'rect' ...
    // ... 'rect' will be initialized a to 0, 0 and a width and height of 12, 60 ...

    rect_t  rect(0, 0, 12, 60);

    for ( size_t iCol = 0; iCol < 3; iCol++ )
    {
        // ... allocate another instance of 'rect_t' named 'rc' and initialize it with the existing rectangle 'rect' ...
        rect_t  rc = rect;

        // ... offset instance 'rc' by 'iCol' times its width ...
        rc.offset(iCol * rc.left(), 0);
        Tft.drawRectangle(rc.left(), rc.top(), rc.width(), rc.height(), BLUE);
    }
    ... 
}

this likely wont work for u, but u might find similar--
http://forum.arduino.cc/index.php?topic=140111.30

i should have included this link--

theres some good ideas there

I've recently been using the Adafruit GFX and MCUFRIEND_kbv libraries and find them extremely fast on the UNO. For reasons I don't fully understand, the routines seem about half as fast on a Mega board.

BTW, I have this 3.95" TFT color display

http://www.ebay.com/itm/322009754655?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

working on the UNO with the libraries above and it is very nice. However, I am running out of Flash, hence the try with the Mega, but the graphics are not as fast as I'd like. I'd really like to get the code moved to a Teensy 3.2, but have not been able to get the libraries to compile with it. If there are any Teensy experts out there who have a good graphics engine, I'd love to hear about it.

I'm having trouble trying to figure this out with for loops.
It works fine if I type it ALL out but that takes forever and is hard to read.

ZachhacZ:
I'm having trouble trying to figure this out with for loops.
It works fine if I type it ALL out but that takes forever and is hard to read.

Reply #3

lloyddean:
Reply #3

Sorry I failed to notice that...
It's a little over my head could you explain whats going on?