Libreria Gestione Pulsanti

Buongiorno a tutti,

stavo realizzando una libreria per la gestione dei pulsanti, in pratica intercetta gli stati di pressione, rilascio e cambiamento di stato; dopo di che esegue le funzioni che gli venivano puntate come puntatori.
Eseguendola ho notato che funziona soltanto quando abilito il debug che mi sono creato nella libreria.

file .h:

#ifndef MBUTTON.h
#define	MBUTTON.h

#if defined(ARDUINO) && ARDUINO>=100
	#include "arduino.h"
#else
	#include "WProgram.h"
#endif // defined(ARDUINO) && ARDUINO>=100

#include <stddef.h>

typedef void(*funRelease)(long long tmp_pressButton);
typedef void(*funPress)();

class mButton
{
private:
	int pin;	//Pin relativo al pulsante
	int oldState;
	
	long long dbTime = 25;	//Tempo di debounce
	long long time = 0;
	long long timePress = 0;
	long long readInput = 100;
	long long timeRead = 0;

	bool pulUp = 0;
	bool chekPress = false;

	funRelease buttonRelease;
	funPress buttonPress;
	funPress buttonChange;

public:
	bool begin(int btPin, long long btDbTime = 25, bool btPulUp = 0, funRelease funButtonRelease = NULL, funPress funButtonPress = NULL, funPress funButtonChange = NULL);
	void checkStateButton();
};
#endif // !MBUTTON.h

file .cpp:

#include "mButton.h"
#define DEBUG_BUTTON //COMMENTATNDO LA RIGA SI DISABILITA IL DEBUG SU SERIALE DELLA LIBRERIA

bool mButton::begin(int btPin, long long btDbTime = 25, bool btPulUp = 0, funRelease funButtonRelease = NULL, funPress funButtonPress = NULL, funPress funButtonChange = NULL) {
	oldState = (btPulUp == 1) ? 0 : 1;
	pin = btPin;
	dbTime = btDbTime;
	buttonChange = funButtonChange;
	buttonPress = funButtonPress;
	buttonRelease = funButtonRelease;
	pulUp = btPulUp;

#ifdef DEBUG_BUTTON
	Serial.println("\tDEBUG CLASS BUTTON:");
	Serial.print("\t\t-PIN:\t");
	Serial.println(pin);
	Serial.print("\t\t-OLD STATE:\t");
	Serial.println(oldState);
	Serial.print("\t\t-PULLUP:\t");
	Serial.println(pulUp);
	Serial.print("\t\t-DEBOUNCE TIME:\t");
	Serial.println((int)dbTime);
#endif // DEBUG_BUTTON


	if (buttonChange == NULL && buttonPress == NULL && buttonRelease == NULL) {
#ifdef DEBUG_BUTTON
		Serial.println("\tERRORE:");
		Serial.print("\t\t-Passare almeno una funzione");
#endif // DEBUG_BUTTON

		return false;
	}
	chekPress = false;

	pinMode(pin, (pulUp == 1) ? INPUT_PULLUP : INPUT);

#ifdef DEBUG_BUTTON
	Serial.println("\tPulsante inizializzato Correttamente");
#endif // DEBUG_BUTTON

	return true;

}

void mButton::checkStateButton() {
	if ((millis() - timeRead) > readInput) {
		int stateButton = digitalRead(pin);
#ifdef DEBUG_BUTTON
		Serial.print(millis());
		Serial.print(" - STATO INGRESSO: ");
		Serial.println(stateButton);
		Serial.print(millis());
		Serial.print(" - STATO PRECEDENTE: ");
		Serial.println(oldState);
		Serial.print(millis());
		Serial.print(" - CHECKPRESS: ");
		Serial.println(chekPress);
#endif // DEBUG_BUTTON

		if ((!chekPress) && (oldState != stateButton)) {
			chekPress = !chekPress;
			time = millis();
#ifdef DEBUG_BUTTON
			Serial.print(millis());
			Serial.print(" - TIME: ");
			Serial.println((int)time);
#endif // DEBUG_BUTTON
		}

		if (chekPress) {
#ifdef DEBUG_BUTTON
			Serial.print(millis());
			Serial.print(" - CONDITION: ");
			Serial.println((millis() - time) > dbTime);
#endif // DEBUG_BUTTON
			if ((millis() - time) > dbTime) {
#ifdef DEBUG_BUTTON
				Serial.print(millis());
				Serial.print(" - CONDITION_2: ");
				Serial.println(oldState != stateButton);
#endif // DEBUG_BUTTON
				if (oldState != stateButton) {

					if (buttonChange != NULL) buttonChange();

					if (stateButton == !pulUp) {
						timePress = millis();
						if (buttonPress != NULL) buttonPress();
					}

					if ((stateButton == pulUp) && (buttonPress != NULL)) buttonRelease((long long)(millis() - timePress));

					oldState = stateButton;
				}

			}
			chekPress = false;
			timeRead = millis();
		}
	}
}

e adesso il file .ino di esempio:

#include <mButton.h>

mButton	pulsante;

int value = 0;

void setup() {
	Serial.begin(9600);
	Serial.println("TEST");
	pulsante.begin(7, 25, true, release, press, NULL);
}

void press() {
	Serial.print(millis());
	Serial.println(" - Pulsante Premuto");
	value = 1;
}

void release(long long t) {
	Serial.print(millis());
	Serial.print(" - Rilasciato. Pressione durata: ");
	Serial.print ((int)t);
	Serial.println(" ms");
	value = (int)(t/1000);
}


// the loop function runs over and over again until power down or reset
void loop() {
	pulsante.checkStateButton();
}

nella funzione di classe checkStateButton() ho inserito un ritado nella lettura di 100 ms.

Come mai se non decommento
Dipende dal fatto che non ho usato alcun delay subito dopo la lettura dell'ingresso e la verifica della pressione?

Vi ringrazio in anticipo per le risposte ed i consigli.

Ho risolto…
Se può essere d'aiuto a qualcuno posto il link a GitHub con i codici funzionanti: Libreria mButton
Sono aperto a consigli e opinioni.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.