Button counter with interrupt - button

I want to increment a variable every second when an interrupt is triggered.
Code on esp32, esp-idf.
I have connected a button, when the button is pressed I want to count the number of seconds.
I did this using polling function, but I want to learn how to do it with interrupt, so counting and polling only when the button is pressed and not checking every second for a button pushed.
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#define ESP_INTR_FLAG_DEFAULT 0
#define BLINK_LED 13
#define GPIO_INPUT_IO_0 33
int buttonCount = 0;
int i = 0;
SemaphoreHandle_t xSemaphore = NULL;
TaskHandle_t printVariableTask = NULL;
void printVariable(void *pvParameter) {
int a = (int) pvParameter;
while (1) {
printf("A is a: %d \n", a++);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
// interrupt service routine, called when the button is pressed
void IRAM_ATTR button_isr_handler(void* arg) {
// notify the button task
xSemaphoreGiveFromISR(xSemaphore, NULL);
}
// task that will react to button clicks
void button_task(void* arg) {
// infinite loop
for(;;) {
// wait for the notification from the ISR
if(xSemaphoreTake(xSemaphore,portMAX_DELAY) == pdTRUE) {
int buttonState = gpio_get_level(GPIO_INPUT_IO_0);
while(buttonState == 1){ //code stucks here!!!!
buttonCount++;
printf("GPIO_INPUT_IO_0 %d\n", buttonState);
printf("Button pressed! %d \n", i++);
gpio_set_level(BLINK_LED, buttonState);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
}
}
void app_main()
{
// create the binary semaphore
xSemaphore = xSemaphoreCreateBinary();
// configure button and led pins as GPIO pins
gpio_pad_select_gpio(GPIO_INPUT_IO_0);
gpio_pad_select_gpio(BLINK_LED);
// set the correct direction
gpio_set_direction(GPIO_INPUT_IO_0, GPIO_MODE_INPUT);
gpio_set_direction(BLINK_LED, GPIO_MODE_OUTPUT);
// enable interrupt on falling (1->0) edge for button pin
gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_POSEDGE);
// start the task that will handle the button
xTaskCreate(button_task, "button_task", 2048, NULL, 10, NULL);
// install ISR service with default configuration
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
// attach the interrupt service routine
gpio_isr_handler_add(GPIO_INPUT_IO_0, button_isr_handler, NULL);
int pass = 25;
xTaskCreate(&printVariable, "printVariable", 2048, (void*) pass, 5, &printVariableTask);
}
It works, but when the code enter in the while(buttonState == 1) the loop never ends.
What am I doing wrong?

not sure if this is still and issue for you but try making a new global variable as a flag and set it to 1 in the interrupt routine when you want to start counting. In your loop look for that flag to be set to 1 and start incrementing. When you detect the button is no longer being pressed set the flag to 0.
Also you're never resetting your button state within the button task while loop. That's why your Button State is always coming back as 1.
Actually looking at it further I think you might just need to sample your input level level outside of your if statement within the for(;;) loop. I think this because (I believe) your if in the button task wont be called on a falling edge?

Related

How to make animation in *.gif run only once in Qt? [duplicate]

I'm using QMovie and gif to create explosion after collision. The problem is that my gif is looping over and over, I've checked loopcount status and it returns -1 (infinite). How to display my gif just one time?
#include "Bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "Enemy.h"
#include "Game.h"
#include <typeinfo>
#include "levels.h"
extern Game * game; // there is an external global object called game
int Bullet::killed = 0;
int Bullet::missed = 0;
double Bullet::accurancy = 0;
Bullet::Bullet(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
// draw graphics
setPixmap(QPixmap(":/images/res/images/bullets/bullet.png"));
missed++; // increse missed when bullet is created
movie = new QMovie(":/images/res/images/effects/explosion/64x48.gif");
processLabel = new QLabel;
processLabel->setMovie(movie);
// make/connect a timer to move() the bullet every so often
QTimer * timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
// start the timer
timer->start(2);
}
void Bullet::move(){
// get a list of all the items currently colliding with this bullet
QList<QGraphicsItem *> colliding_items = collidingItems();
// if one of the colliding items is an Enemy, destroy both the bullet and the enemy
for (int i = 0, n = colliding_items.size(); i < n; ++i){
if (typeid(*(colliding_items[i])) == typeid(Enemy)){
// increase the score
game->score->increase();
//play explosion animation
movie->start();
movie->setSpeed(180);
processLabel->setAttribute(Qt::WA_NoSystemBackground);
processLabel->setGeometry(QRect(x()-15,y()-15,64,48));
scene()->addWidget(processLabel);
qDebug() << movie->loopCount();
//connect(movie,SIGNAL(finished()),movie,SLOT(stop()));
// remove them from the scene (still on the heap)
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
// delete them from the heap to save memory
delete colliding_items[i];
delete this;
killed++;
missed--; // decrese missed if bullet colide with enemy
if((killed+1) % 9 == 0)
{
game->level->Levels::incrementLevels();
game->score->Score::addToSum(); /// TODO
}
//qDebug() << "Already killed: " << killed;
//qDebug() << "Already missed: " << missed;
// return (all code below refers to a non existint bullet)
return;
}
}
// if there was no collision with an Enemy, move the bullet forward
setPos(x(),y()-1);
// if the bullet is off the screen, destroy it
if (pos().y() < 0){
scene()->removeItem(this);
delete this;
}
}
I had the same question and didn't find anything, so here's my solution:
connect the signal "frameChanged(int)" to:
void Bullet::frameChanged_Handler(int frameNumber) {
if(frameNumber == (movie->frameCount()-1)) {
movie->stop();
}
}
If you want to run X times the loop you just have to add a static counter to know how many times you've passed the last frame:
void Bullet::frameChanged_Handler(int frameNumber) {
static int loopCount = 0;
if(frameNumber == (movie->frameCount()-1)) {
loopCount++;
if(loopCount >= MAX_LOOP_TIMES)
movie->stop();
}
}
and of course, connect with this:
connect(movie, SIGNAL(frameChanged(int)), this, SLOT(frameChanged_Handler(int)));
That's it... Hope it can help you.

How to implement long press key event for stm32f429i discovery board using keil?

I interfaced a keypad to stm32f429i board and am able to display respective button of keypad on LCD screen of board. Short press is working fine but I have no idea how to implement long press key event. Can someone please help me?
Edit: As suggested by PeterJ_01, using a Timer interrupt is the most elegant way. Do not use the GPIO interrupt for buttons!
If you are using the Hardware Abstraction Layer (HAL) Library, you can do something like this inside your GPIO interrupt:
static int timestamp_pressed = -1; // -1 means not pressed
if (timestamp_pressed == -1) {
// user just pressed the button
timestamp_pressed = HAL_GetTick(); // milliseconds since startup
} else if (HAL_GetTick() - timestamp_pressed > 2000) {
// 2000 milliseconds = 2 seconds are over
// << your code
timestamp_pressed = -1;
}
If you are not using HAL and don't want to use a regular timer interrupt (from TIM1, TIM2, TIM3, ...) you can consider using the SysTick timer (with an interrupt or without). There is plenty information about it in the internet ;)
Yes - you need to use the timer interrupt. Here is the link and the library:
https://www.diymat.co.uk/arm-three-function-click-double-and-long-click-button-library-timer-interrupt-driven/
An implementation inspired from STM32F746G discovery board example.
if(HAL_GPIO_ReadPin(Push_GPIO_Port,Push_Pin) != RESET)
{
HAL_Delay(10);//debounce
timestamp_butpressed = HAL_GetTick();
while (HAL_GPIO_ReadPin(Push_GPIO_Port,Push_Pin) != RESET);
timestamp_butreleased = HAL_GetTick();
//Is button pressed enouph to run the program?
if(timestamp_butreleased>timestamp_butpressed?//This condition prevent max value to cause problem
((timestamp_butreleased-timestamp_butpressed)>2000)://normal condition
((4294967295-timestamp_butpressed)+timestamp_butreleased)>2000)//glitchy conditioin handle
{
//application runs here or flag handling heppens here
}
}
Another triky implementation (long press triggers as it reaches required time):
if(HAL_GPIO_ReadPin(Push_GPIO_Port,Push_Pin) != RESET)
{
HAL_Delay(10);
while(HAL_GPIO_ReadPin(Push_GPIO_Port,Push_Pin) == RESET)
{
HAL_Delay(5);
i++
if(i==100)break;
}
if(i<100)
{
short pressed
}
else
{
long pressed
}
}
A functional implementation:
uint8_t pushbot(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) != RESET)
{
uint32_t timestamp_butpressed=0;
uint32_t timestamp_butreleased=0;
HAL_Delay(10);//debounce
timestamp_butpressed = HAL_GetTick();
while (HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) != RESET);
timestamp_butreleased = HAL_GetTick();
//Is button pressed enouph to run the program?
if(timestamp_butreleased>timestamp_butpressed?//This condition prevent max value to cause problem
((timestamp_butreleased-timestamp_butpressed)>1000)://normal condition
((4294967295-timestamp_butpressed)+timestamp_butreleased)>1000)//glitchy conditioin handle
{
return 2;// long pressed
}
else
{
return 1;// short pressed
}
}
else
{
return 0;// not pressed
}
}

Arduino - How to control piezo with button?

What I am trying to achieve is a button that acts as an on/off switch for playing a tune on a piezo. Initially I want the piezo not to make any sound until the user presses a button, and then they can turn the piezo off by pressing the same button. I'm wondering if anyone can help me figure out what code I need to add to this to get this kind of button functionality.
This is a modified version of the toneMelody sketch that comes with Arduino IDE, I have a button attached to Pin 12, and a Piezo to Pin 8.
#include "pitches.h"
// notes in the melody:
int melody[] ={
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NA5,NE5,0,NE5,
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,NF5,0,NE5,0,NC5,NF5,0,NE5,0,NC5,
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,NB4,NB4,0,NC5,0,NG5,
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,
NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,ND5,ND5,NE5,0,NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,NG5,ND5,0,
NE5,NF5,NE5,ND5,ND5,NC5
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,
8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,16,8,16,8,8,16,8,16,8,
4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,
4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,2,
8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,4,4,8,8,8,4,2,8,8,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,4,1
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 240; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.
}
The way I would use a button would be like this. I added comments above and below were I added stuff. The out come of this will be that when you press the button the whole song will play then stop until you press the button again. You can see what kind of button I am taking about here http://arduino.cc/en/Tutorial/Button it also gives you some example code, for you to look at just incase I messed up, if I did mess up
just message me and I will show you how to fix.
#include "pitches.h"
// notes in the melody:
int melody[] ={
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,
NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND
5,NE5,ND5,0,NC5,NE5,NG5,NG5,NA5,NE5,0,NE5,
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND
5,NE5,ND5,NF5,0,NE5,0,NC5,NF5,0,NE5,0,NC5,
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,NB4,NB4,0,NC5,
0,NG5,
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,
NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,
NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,ND5,ND5,NE5,0,NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,NG5,ND5,0,
NE5,NF5,NE5,ND5,ND5,NC5
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,
8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,16,8,16,8,8,16,8,16,8,
4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,
4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,2,
8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,4,4,8,8,8,4,2,8,8,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,4,1
};
//int to identify what digital pin the button is hooked up to
// can Chang 2 if put button on a different pin
int buttonPin = 2;
void setup() {
//says button pin is a input
pinMode(buttonPin, INPUT);
// I might say make a int = to the pin you have
// your sound maker on, name it soundMakerPin, and the uncomment the line below
// pinMode(soundMakerPin, OUTPUT);
}
void loop(){
// read the button
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// play song
song();
}
}
//function named song, call this and your song should play
void song(){
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 240; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}

File changes handle, QSocketNotifier disables due to invalid socket

I am developing for a touch screen and need to detect touch events to turn the screen back on. I am using Qt and sockets and have run into an interesting issue.
Whenever my QSocketNotifier detects the event it sends me infinite notices about it. Therefore I need to close and open the event file to cycle the notifier (inputNotifier in the below code). The problem usually arises several minutes after the device has been running and the file (inputDevice) suddenly changes it's handle from 24 to something else (usually 17).
I am not sure what to do, because the initial connect statement is linked to the initial Notifier pointer. If I create a new Notifier using the new handle, the connect is invalid. As far as I can tell there is no option to set a new socket value on a running QSocketNotifier. Suggestions? The relevant code is below:
#include "backlightcontroller.h"
#include <QTimer>
#include <QFile>
#include <syslog.h>
#include <QDebug>
#include <QSocketNotifier>
BacklightController::BacklightController(QObject *parent) :
QObject(parent)
{
backlightActive = true;
// setup timer
trigger = new QTimer;
trigger->setSingleShot(false);
connect(trigger, SIGNAL(timeout()), SLOT(deactivateBacklight()));
idleTimer = new QTimer;
idleTimer->setInterval(IDLE_TIME * 1000);
idleTimer->setSingleShot(false);
connect(idleTimer, SIGNAL(timeout()), SIGNAL(idled()));
idleTimer->start();
// setup socket notifier
inputDevice = new QFile(USERINPUT_DEVICE);
if (!inputDevice->open(QIODevice::ReadOnly))
{
syslog (LOG_ERR, "Input file for Backlight controller could not been opened.");
}
else
{
inputNotifier = new QSocketNotifier(inputDevice->handle(), QSocketNotifier::Read);
inputNotifier->setEnabled(true);
connect(inputNotifier, SIGNAL(activated(int)), SLOT(activateBacklight()));
}
qDebug()<<"backlight socket: "<<inputNotifier->socket();
// read out settings-file
QString intensity = Global::system_settings->getValue("BelatronUS_backlight_intensity");
if (intensity.length() == 0) intensity = "100";
QString duration = Global::system_settings->getValue("BelatronUS_backlight_duration");
if (duration.length() == 0) duration = "180";
QString always_on = Global::system_settings->getValue("BelatronUS_backlight_always_on");
if (always_on.length() == 0) always_on = "0";
setIntensity(intensity.toInt());
setDuration(duration.toInt());
if (always_on == "0")
setAlwaysOn(false);
else
setAlwaysOn(true);
}
BacklightController::~BacklightController()
{
trigger->stop();
inputNotifier->setEnabled(false);
inputDevice->close();
delete trigger;
delete inputDevice;
delete inputNotifier;
}
void BacklightController::setCurrentIntensity(int intensity)
{
// adapt backlight intensity
QFile backlightFile("/sys/class/backlight/atmel-pwm-bl/brightness");
if (!backlightFile.open(QIODevice::WriteOnly))
{
syslog (LOG_ERR, "Backlight intensity file could not been opened.");
}
else
{
QString intensityString = QString::number(TO_BRIGHTNESS(intensity));
if (backlightFile.write(
qPrintable(intensityString), intensityString.length()
) < intensityString.length())
{
syslog (LOG_ERR, "Backlight intensity could not been changed.");
}
backlightFile.close();
}
}
void BacklightController::resetNotifier()
{
inputDevice->close();
if (!inputDevice->open(QIODevice::ReadOnly))
{
syslog (LOG_ERR, "BacklightController::%s: Input file could not been opened.", __FUNCTION__);
}
qDebug()<<"reset, handle: "<<inputDevice->handle();
//inputNotifier=QSocketNotifier(inputDevice->handle(), QSocketNotifier::Read);
// restart timer after user input
idleTimer->start();
}
void BacklightController::activateBacklight()
{
// only activate backlight, if it's off (avoid to useless fileaccess)
if (!backlightActive)
{
setCurrentIntensity(_intensity);
backlightActive = true;
emit backlightActivated();
}
// restart backlight timeout, but only if we don't want the backlight to shine all the time
if (!_alwaysOn)
trigger->start();
// reset notifier to be able to catch the next userinput
resetNotifier();
}
void BacklightController::deactivateBacklight()
{
// don't turn it off, if it's forced on
if (!_alwaysOn)
{
if (backlightActive)
{
// only deactivate backlight, if it's on (avoid to useless fileaccess)
setCurrentIntensity(BACKLIGHT_INTENSITY_OFF);
backlightActive = false;
emit backlightDeactivated();
}
}
qDebug()<<"trigger stopping";
trigger->stop();
}
void BacklightController::setIntensity(int intensity)
{
if (intensity > 100)
intensity = 100;
else if (intensity < 0)
intensity = 0;
_intensity = intensity;
// write new intensity to file if it's active at the moment
if (backlightActive)
{
setCurrentIntensity(_intensity);
trigger->start();
}
}
void BacklightController::setDuration(int duration)
{
if (duration < 1)
duration = 1;
_duration = duration;
trigger->setInterval(_duration * MS_IN_SEC);
// reset trigger after changing duration
if (backlightActive)
{
trigger->start();
}
}
void BacklightController::setAlwaysOn(bool always_on)
{
_alwaysOn = always_on;
// tell the timer what to to now
if (_alwaysOn)
{
this->activateBacklight();
trigger->stop();
}
else
{
trigger->start();
}
}
I seem to have found a working solution for now. It's not the greatest so if there are better solutions I would be interested to hear them. The reason I did not think of this before is because I thought if I had a new connect statement in a function it would have limited scope as the function ended.
The solution was to simply check for an occurrence of the handle change in the file and then create a new pointer for the notifier using that handle. Then re-enable the notifier because it has likely been disabled by now and then create a new connect statement for the pointer. This is the code I used, added just below the closing and reopening of the event file:
if(inputDevice->handle()!=inputNotifier->socket()){
inputNotifier = new QSocketNotifier(inputDevice->handle(), QSocketNotifier::Read);
inputNotifier->setEnabled(true);
connect(inputNotifier, SIGNAL(activated(int)), SLOT(activateBacklight()));
}

How to create a count down time with Qt?

All developer could you show me how to create a count down time with c++ Qt? If you can, you should to show me a source code.
You could use something like that. It calls timeOutSlot every second.
#define TIMEOUT 60
...
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(timeOutSlot()));
timer->start(1000);
...
void timeOutSlot()
{
static int time = TIMEOUT;
time--; // decrement counter
if (time==0) // countdown has finished
{
// timeout
}
else // re-start counter
{
time->start(1000);
}
}

Resources