mirror of
				https://github.com/0xFEEDC0DE64/arduino-esp32.git
				synced 2025-11-04 08:01:38 +01:00 
			
		
		
		
	
		
			
	
	
		
			48 lines
		
	
	
		
			784 B
		
	
	
	
		
			Arduino
		
	
	
	
	
	
		
		
			
		
	
	
			48 lines
		
	
	
		
			784 B
		
	
	
	
		
			Arduino
		
	
	
	
	
	
| 
								 | 
							
								#include <Arduino.h>
							 | 
						||
| 
								 | 
							
								#include <FunctionalInterrupt.h>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define BUTTON1 16
							 | 
						||
| 
								 | 
							
								#define BUTTON2 17
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class Button
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								public:
							 | 
						||
| 
								 | 
							
									Button(uint8_t reqPin) : PIN(reqPin){
							 | 
						||
| 
								 | 
							
										pinMode(PIN, INPUT_PULLUP);
							 | 
						||
| 
								 | 
							
										attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
							 | 
						||
| 
								 | 
							
									};
							 | 
						||
| 
								 | 
							
									~Button() {
							 | 
						||
| 
								 | 
							
										detachInterrupt(PIN);
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									void IRAM_ATTR isr() {
							 | 
						||
| 
								 | 
							
										numberKeyPresses += 1;
							 | 
						||
| 
								 | 
							
										pressed = true;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									void checkPressed() {
							 | 
						||
| 
								 | 
							
										if (pressed) {
							 | 
						||
| 
								 | 
							
											Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
							 | 
						||
| 
								 | 
							
											pressed = false;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								private:
							 | 
						||
| 
								 | 
							
									const uint8_t PIN;
							 | 
						||
| 
								 | 
							
								    volatile uint32_t numberKeyPresses;
							 | 
						||
| 
								 | 
							
								    volatile bool pressed;
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Button button1(BUTTON1);
							 | 
						||
| 
								 | 
							
								Button button2(BUTTON2);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void setup() {
							 | 
						||
| 
								 | 
							
								    Serial.begin(115200);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void loop() {
							 | 
						||
| 
								 | 
							
									button1.checkPressed();
							 | 
						||
| 
								 | 
							
									button2.checkPressed();
							 | 
						||
| 
								 | 
							
								}
							 |