3
votes

I am in the process of making an event driven GUI in an embedded system. I just finished implementing the widget graphics and touchscreen functionality.

My question is how to / tips on implementing this in C and on an embedded system.

This is how I was thinking in VERY general "pseudo" code:

mainloop()
{
    <All initializations etc.>

    eventloop();
}

eventloop()
{
    eventhandler();
    sleep_low_power_uc_mode();
}

touchscreen_interrupt_service_routine()
{
    int * x, *y;
    eventtype event = TOUCHSCREEN_CLICK;
    get_XY_coordinate(x, y);
    post_event(*x, *y, event);
}

eventhandler()
{
    int * x, *y;
    eventtype * event;
    static int current_state;
    get_event(x, y, event);
    if(event != NO_EVENT)
    {
        handle_events(*x, *y, *event, current_state);
    }
}

handle_events(int x, int y, eventtype event, int * current_state)
{
    int buttonID, i=0;
    buttonID = check_if_button_pressed(x, y, current_state);
    while(buttons[i].enabled != FALSE)
    {
        if(buttonID == buttons[i].ID)
        {
            call_buttons_respective_handler();
        }
    }
}

Here I assume that I have a touchscreen which will wake up my micro controller controlled device with a hardware interrupt. The eventloop() is a never ending event loop that will handle any events then go to sleep until next touchscreen interrupt. The touchscreen interrupt service routine will get the X and Y coordinate from the touchscreen and post an event with the post_event() function. The event_handler() function, which is a function within the eventloop() function, will get events (if any) and call the handle_events() function. The handle_events() function check if a button (for example) has been pressed with the given event, X and Y coordinates and return a non-zero buttonID if a button has been pressed. Then the next is to loop through the array of buttons and check of a identical buttonID and call that buttons handler.

Does what I have tried to describe make any sense in a event driven programming manner? Any thoughts are very welcome (and please bear with me as I am new to this).

2
Yes. This is the basic way in which event driven systems are implemented. - Linuxios
It looks really good so far. One thing I find handy with no RTOS is a job queue/scheduler. You could have the event handler in the job queue but also do other stuff there too, as well as periodic tasks that you don't want to do in an ISR. Then your main loop just polls the job queue and different jobs kick off other jobs, like updating the screen. - c.fogelklou
It seems that you are developing your own event-driven framework. The elements you still need is event queuing, and most likely state machines. Perhaps you could get some inspiration from the existing open source frameworks of this type, such as QP-nano or QP/C, both programmed in C and suitable for low end microcontrollers. Regarding integration of such a framework with embedded GUI I would also recommend an Application Note "QP™ and emWin Embedded GUI" at state-machine.com/emwin/AN_QP_and_emWin.pdf - Miro Samek
@Miro: You are right. I am in the learning process and I feel that before I use any libraries for things, I want to build my own simple programs that has the core concepts. This way I will feel comfortable using ready made libraries as you have given an example of above. Thanks for the helpful application note! - uniquenamehere

2 Answers

1
votes

The answer would really depend on what platform you are developing for. An embedded RTOS for a Microchip controller is going to have one set of constraints, whereas an RTOS for an ARM solution will be completely different. You should clarify what hardware, or at a minimum, what micro controller, you are designing for.

0
votes

You can Program most of event on Interrupt based on timer, then you can invoke each interrupt after 2 or 5 ms which means all your event can be monitor simultaneously(psuedo RTOS).