Jump to content

Sharing A Spinner


David Kibble

Recommended Posts

Hi All.

Not sure if this is the right place to share code, but please find attached some simple code to create a 'spinner'. By spinner I mean a visual indicator of the system being busy or processing, much like the hour glass in old Windows or the rotating circle in Linux, Android etc. It's fairly basic but does the job for me. The code is hopefully self explanatory but just to give a quick summary. The spinner is displayed by a gfxThread which is created the first time you call showSpinner(...), that function takes two params, one is a Boolean (bool_t) which controls if the spinner is made visible or not and the second is a GHandle for the parent under which the spinner is drawn. There are functions to control, colour, size and style of the spinner. I've coded 3 styles, a spinning pie segment, a spinning arc segment and a horizontal bar "knight rider" style. If for any reason you need to stop the thread which runs the spinner just call stopSpinnerThread(), this may save resources on very low spec hardware instead of just hiding the spinner using showSpinner(FALSE,...). I've left a few printfs in there so you can see the thread startup and exit. Just take them out if you don't want them.

To use the code just include "spinner.h" in your code, add the spinner.c to your source code folder, add spinner.c to your makefile and then just call showSpinner(TRUE,<parent>) from your code. You can pass NULL for <parent> in which case my code redraws the whole display, e.g. showSpinner(TRUE,NULL); The spinner will run until you call showSpinner(FALSE,<parent>). The speed of the spinner is hard coded, but you can easily change it by altering the usleep(...) in spinner.c line 149. The code has been written and tested on Linux host and targets and uses the default thread stack size with low priority. You can experiment with these values if you need to.

You'll need to enable a few things in your gfxconf.h
#define GDISP_NEED_CIRCLE                       TRUE
#define GDISP_NEED_ARC                            TRUE
#define GDISP_NEED_MULTITHREAD            TRUE

Good luck.

Dave

spinner.c

spinner.h

Link to comment
Share on other sites

Hello Dave,

Thank you very much for sharing your code. Things like that are always appreciated. I'm sure that this will help somebody in the future.
I haven't taken a look at your code yet but judging from your description you are using an actual thread for the spinner. Have you considered using a GTimer instead?

I've moved this topic to the User Projects section of this forum.

Link to comment
Share on other sites

Hello Dave,

If you're using the GWIN module you're already using the GTIMER module as well. Both the GWIN and the GINPUT module have the GTIMER module as dependencies. This means that you can use a GTIMER in your spinner without pulling additional dependencies in.
A GTIMER uses a lot less resources than a thread, both in terms of memory and CPU power.

Also, you might want to consider taking your spinner and creating a custom widget out of it. The process is very easy, especially in this case as the spinner doesn't take any user input and doesn't fire any events. It's what we call a window, not a widget (that's explained in the corresponding documentation).
Having a dedicated widget (window!) for this would mean that the window manager can control the visibility, that it can become a child of a parent container and that you can have multiple instances of it. Furthermore, it would be easy to apply styles, move it around and stuff like that.
Creating a custom widget is explained here.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...