C

Qt Quick Ultralite interrupt_handler Example

Demonstrates how to handle input interrupts in Qt Quick Ultralite.

Overview

The interrupt_handler example shows how to handle input interrupts, such as touch screen or button inputs, in Qt Quick Ultralite. It has a simple QML UI, where a box moves from left to right. The screen changes its background color on touch and a button press on the device pauses and resumes the movement.

Target platforms

Code overview

The code demonstrates how to transfer data from interrupt handlers. While the input data in this example is a simple integer, it can be also something more complicated, such as data from a thermometer.

HWButtonInput class

HWButtonInput class provides a C++ interface, which is used to communicate the button press events to interrupt_handler.qml. It derives both Qul::Singleton and Qul::EventQueue.

typedef int HWButtonEvent;

class HWButtonInput : public Qul::Singleton<HWButtonInput>, public Qul::EventQueue<HWButtonEvent>
{
public:
    Qul::Signal<void(int button)> buttonEvent;

    void onEvent(const HWButtonEvent &inputEvent);
};

Button events are sent to QML by emitting buttonEvent() signal inside onEvent(), which is triggered when the event queue handles an event:

void HWButtonInput::onEvent(const HWButtonEvent &inputEvent)
{
    buttonEvent(inputEvent);
}
board_config.h and board_config.cpp

board_config.h contains only a declaration of ConfigureBoard() function:

#pragma once

void ConfigureBoard();

This is a board-specific function that has a different implementation for each board. You can find all board-specific implementations in the interrupt_handler example directory, under each of the supported board's directory.

The following board_config.cpp is an implementation for STM32F769i:

#include "stm32f769i_discovery.h"
#include "board_config.h"
#include "interrupt_queue.h"

void ConfigureBoard()
{
    BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
}

extern "C" void EXTI0_IRQHandler(void)
{
    HAL_GPIO_EXTI_IRQHandler(USER_BUTTON_PIN);
    HWButtonInput::instance().postEventFromInterrupt(0);
}

ConfigureBoard() is implemented to configure a user button found on the STM32F769i-Discovery board, using the BSP_PB_Init() function from STM32Cube F7 SDK.

EXTI0_IRQHandler() is an interrupt handler that is triggered on each button press. The handler function runs STM32 HAL's own interrupt handler and then posts an event to the HWButtonInput instance's event queue. In this example, the event is just an integer with a value of 0.

main.cpp

There are two main.cpp files, one each for BareMetal and FreeRTOS. Both are located under the interrupt_handler/os directory in baremetal and freertos directories respectively.

#include "board_config.h"
#include "interrupt_handler.h"

#include <qul/application.h>
#include <qul/qul.h>

int main()
{
    Qul::initPlatform();
    ConfigureBoard();
    Qul::Application app;
    static interrupt_handler item;
    app.setRootItem(&item);
    app.exec();
    return 0;
}

main() function configures the board, configures a button on that board, initializes the Qt Quick Ultralite application, and runs the application.

Note: The shown main.cpp is for BareMetal configurations. FreeRTOS configuration adds FreeRTOS-specific configuration code. See Using Qt Quick Ultralite with FreeRTOS.

interrupt_handler.qml

interrupt_handler.qml declares the UI, which is shown on the screen.

import QtQuick 2.15

Rectangle {
    id: root
    color: "white"

    Rectangle {
        id: rect
        anchors.verticalCenter: parent.verticalCenter
        width: 50; height: 50
        color: "black"
    }

    Timer {
        id: timer
        interval: 10
        running: true
        repeat: true
        onTriggered: {
            rect.x = ((rect.x + rect.width + 5) % (root.width + rect.width)) - rect.width
        }
    }

The MouseArea QML type processes touch inputs, and in this example it is filling the whole screen. When the area is touched, the color of the background rectangle, root, is changed from white to yellow. The color is changed back to white when the screen is not touched.

    MouseArea {
        id: ta
        anchors.fill: parent
        onPressedChanged: {
            root.color = pressed ? "yellow" : "white"
        }
    }

HWButtonInput.onButtonEvent is the slot for buttonEvent signal. When the signal is emitted, the timer is paused or resumed depending on the timer's current state. Pausing the timer stops rect from moving.

    HWButtonInput.onButtonEvent: {
        timer.running = !timer.running
    }

Files:

Available under certain Qt licenses.
Find out more.