Go Down

Topic: Anyone got some cool Sine Wave animation already written? (Read 576 times)previous topic - next topic

mbferguson

Nov 19, 2018, 03:20 amLast Edit: Nov 19, 2018, 03:28 am by mbferguson
I'm working on a GUI for a transducer that uses ultrasound to detect flaws within metal. I'm trying to spice up the display a little bit with some fancy animations. I figured I'd check on here and see if you guys had anything you'd already worked on.

I'm using the Adafruit 2.8" TFT Touch Shield.

Here's a picture of what it currently looks like just to give you an idea of what I'm working with.

I thought it'd be cool if there was a sine wave wobbling up and down in between these lines in the middle. Or, something else that'd make my professor go "ooh good job Michael". Any advice about how I could go about doing this would be appreciated as well. There don't seem to be any good functions in the TFT library to draw weird shapes like a sine wave, let alone making it continuously move across the screen.

I suppose it could be done simply enough with the Arduino - Trigonometric Functions, although that would be slightly complicated at first for the setCursor().

Nick_Pyner

There don't seem to be any good functions in the TFT library to draw weird shapes like a sine wave
Really? I'm sure you will find sine waves are standard examples in most libraries.

mbferguson

Really? I'm sure you will find sine waves are standard examples in most libraries.
TFT has: lines, rectangles, circles

david_prentice

Go on.   GFX has points.    Math.h has trig functions.    You simply plot the points.
Look at the standard UTFT example.    Then use regular GFX methods to implement a similar display.

Show that you have studied working code and can adapt it to your requurements i.e. post your code.

David.

mbferguson

#4
Nov 19, 2018, 08:57 amLast Edit: Nov 19, 2018, 09:22 am by mbferguson
Go on.   GFX has points.    Math.h has trig functions.    You simply plot the points.
Look at the standard UTFT example.    Then use regular GFX methods to implement a similar display.

Show that you have studied working code and can adapt it to your requurements i.e. post your code.

David.
Dont even get me started on UTFT. WHAT A JOKE!!! I spent like 1.5 hour tryna get that mofo to work. White screen. White screen. White screen. Never again. And before you assume I didn't know how to read the READ_MEs, I tried literally EVERY single model of 2.8'' for the function it says to change depending on your screen size. I even tried the ones that weren't listed in the documentation and still turned blue like ILI9341_8 and ILI9341_16. The only reason I could think it didn't work is because my LCD is an offbrand from Elegoo. I tried restarting my computer 100 times, unplugging the usb from the arduino and computer 100 times, and i even went go take dump and it still didn't work. On top of that, all the google links ran dry and I was left with purple hopelessness.

david_prentice

Your photo shows a Blue 2.8 inch Mcufriend shield.
I presumed that you might be using MCUFRIEND_kbv library.    And could try the UTFTGLUE example.

The important point is to learn how to read foreign code.   Just to see "how someone else has done it".

Confession.    I have not studied the UTFT code.   Just observed the result and glanced at the UTFT sketch code.

I may have written the UTFTGLUE class but that does not mean I have looked at everything that might "be run" with the class.

Even if you are not using MCUFRIEND_kbv, you can still "read" the UTFT example.

David.

mbferguson

Your photo shows a Blue 2.8 inch Mcufriend shield.
I presumed that you might be using MCUFRIEND_kbv library.    And could try the UTFTGLUE example.

The important point is to learn how to read foreign code.   Just to see "how someone else has done it".

Confession.    I have not studied the UTFT code.   Just observed the result and glanced at the UTFT sketch code.

I may have written the UTFTGLUE class but that does not mean I have looked at everything that might "be run" with the class.

Even if you are not using MCUFRIEND_kbv, you can still "read" the UTFT example.

David.
I don't think it will be too hard to make a simple plot, just takes some thinking is all. The point of my project isn't to make a sine wave on the screen, I thought it would be a nice addition to fill up some empty space. Anyways, since I've got your attention.. a lot of these libraries on github don't have an explicit 'function' list or documentation on how to utilize them. Is it expected that people will look through the cpp files themselves... kind of like you are suggesting now???

david_prentice

No,   I have never provided extensive documentation.   MCUFRIEND_kbv  only does the hardware e.g. read_ID(),   pushColors(),  vertScroll(), ...

All the graphics come from Adafruit_GFX.h e.g. circles, lines, ...
All the text printing come from Print.h

You read the appropriate GFX docs and tutorials.   Quite honestly,   circles, triangles, ... are pretty intuitive from GFX.    Things like setTextColor() or printing float expressions with different number of decimal places are not so obvious.

My approach is always to run all the examples from a library first.    It gives me some idea about the capabilities.    I can then adapt the example code to implement an individual sketch.

You don't have to read any library CPP code at all.    Just the example INO sketches.
But mostly it is just running and observing the examples.
Yes,   I tend to read the H files rather than tutorials when I want to see overloaded method arguments.

This Forum works quite well.    You can quote library examples.    You can explain what you want to do.
Several library authors give very good answers and suggestions e.g. olikraus for U8g2lib,   bperrybap for hd44780,  bodmer for TFT_eSPI, ...

Incidentally,    your suggested animation requires software intervention with most TFT hardware.
Some OLED hardware can do it all by itself !

David.

mbferguson

Hey you were right :^). I did not know that my 'Adafruit/Elegoo' ripoff  thing was gonna work with Mcufriend library because it is not a Mcufriend shield. The examples seem to be working pretty good though. WHO NEEDS UTFT???

Nick_Pyner

#9
Nov 20, 2018, 12:36 amLast Edit: Nov 20, 2018, 12:39 am by Nick_Pyner
WHO NEEDS UTFT???
Well, that's a long list, but might start with those who want to draw sinewaves and, when they do, they do it with ease... The main reason for UTFT being so good is that it has a comprehensive instruction manual.
I use UTFT and raised the point because it does sine waves so easily, and I simply assumed other libraries had similar examples.

david_prentice

#10
Nov 20, 2018, 01:13 amLast Edit: Nov 20, 2018, 01:32 am by david_prentice
I am intrigued.   What is so special about the code?
Code: [Select]
`    // Draw a moving sinewave    x = 1;    for (int i = 1; i < (318 * 20); i++)  //draw 20 iterations    {        x++;        if (x == 319)     //start again            x = 1;        if (i > 319)       //rub out the previous sinewave         {            if ((x == 159) || (buf[x - 1] == 119)) // ?axes                 myGLCD.setColor(0, 0, 255);  //yes, draw BLUE axis            else                myGLCD.setColor(0, 0, 0);    //else rub out i.e. draw BLACK point            myGLCD.drawPixel(x, buf[x - 1]); //replace the previous point        }        myGLCD.setColor(0, 255, 255);        //new graph is in CYAN        y = 119 + (sin(((i * 1.1) * 3.14) / 180) * (90 - (i / 100)));        myGLCD.drawPixel(x, y);              //plot the new sinewave point        buf[x - 1] = y;                      //remember the point for next pass    }`
You would use exactly the same algorithm in GFX.    Except that it looks like
Code: [Select]
`    // Draw a moving sinewave    x = 1;    for (int i = 1; i < (318 * 20); i++)  //draw 20 iterations    {        uint16_t color = BLACK;           //for rubout        x++;        if (x == 319)     //start again            x = 1;        if (i > 319)       //rub out the previous sinewave         {            if ((x == 159) || (buf[x - 1] == 119)) // ?axes                 color = BLUE;  //yes, draw BLUE axis            tft.drawPixel(x, buf[x - 1], color); //replace the previous point        }        y = 119 + (sin(((i * 1.1) * 3.14) / 180) * (90 - (i / 100)));        tft.drawPixel(x, y, CYAN);              //plot the new sinewave point        buf[x - 1] = y;                      //remember the point for next pass    }`

Untested.   The same algorithm.   I just added comments to UTFT.   And typed GFX version into my Browser.
UTFT sets the colour in a separate method.   GFX uses colour as an argument to each graphics method.   (which is a bit more intuitive)

David.

mbferguson

I am intrigued.   What is so special about the code?

David.
I was looking at the mcufriend examples, the GLUE_DEMO_320x240 specifically. It works correctly on my display, but it mentions in the code that UTFTGLUE only works with UTFT installed. I don't have UTFT installed as I mentioned earlier... so what is going on here?

david_prentice

My apologies.   The
Quote
// This program requires the UTFT library (8bit mode)
is part of the original UTFT example.

I just took a regular UTFT example and replaced the UTFT include, constructor and Font declaration with :
Code: [Select]
`// if I want to use a GLUE class that implements the UTFT API// with the Adafruit classes,   I MUST include those headers// because the Arduino Java does not look at nested includes !#include <Adafruit_GFX.h>#include <UTFTGLUE.h>              // class methods are in hereUTFTGLUE myGLCD(0x0154, A2, A1, A3, A4, A0);// Declare which fonts we will be using#if !defined(SmallFont)extern uint8_t SmallFont[];    //.kbv GLUE defines as GFXFont ref#endif`

You can do the same with most of the UTFT examples.     You do not touch any of the "actual sketch statements".
The program gets compiled with the UTFTGLUE class which simply calls regular GFX class methods.
It does not use or need UTFT in any way.

David.

Go Up