Arduino PulseWire Transceiver Library
Loading...
Searching...
No Matches
IRProtocolDetector.h
1#pragma once
2#include <Arduino.h>
3
4#include "IRProtocol.h"
5
6namespace pulsewire {
7
16 public:
18 reset();
19 _instance = this;
20 _multiProtocol.setCallback(onProtocolDetected, this);
21 }
22
24 bool begin(uint8_t pin) {
25 if (_pin != -1) end(); // Ensure any existing interrupt is detached
26 _pin = pin;
27 pinMode(_pin, INPUT);
28 attachInterrupt(digitalPinToInterrupt(_pin), interruptHandler, CHANGE);
29 return true;
30 }
31
33 void end() {
34 reset();
36 }
37
38 IRProtocolEnum getDetectedProtocol() const { return _detectedProtocol; }
39
40 const char* getDetectedProtocolStr() const { return toStr(_detectedProtocol); }
41
42 void reset() { _detectedProtocol = IRProtocolEnum::Unknown; }
43
44 protected:
45 static IRProtocolDetector* _instance;
46 uint8_t _pin = -1;
47 IRMultiProtocol _multiProtocol;
48 volatile IRProtocolEnum _detectedProtocol = IRProtocolEnum::Unknown;
49 volatile uint32_t _lastEdge = 0;
50
51 static void interruptHandler() {
52 if (_instance) _instance->handleInterrupt();
53 }
54
55 static void onProtocolDetected(IRProtocolEnum proto, IRProtocol& info,
56 void* ref) {
57 IRProtocolDetector* detector = static_cast<IRProtocolDetector*>(ref);
58 if (detector) detector->_detectedProtocol = proto;
59 }
60
61
62 void handleInterrupt() {
63 bool level = digitalRead(_pin);
64 uint32_t now = micros();
65 uint32_t duration = now - _lastEdge;
66 _lastEdge = now;
67 OutputEdge edge{level, duration};
68 _multiProtocol.detect(edge);
69 }
70};
71
72// Static instance pointer definition
73IRProtocolDetector* IRProtocolDetector::_instance = nullptr;
74
75} // namespace pulsewire
void setCallback(void(*callback)(IRProtocolEnum, IRProtocol &, void *ref), void *ref)
Definition IRProtocol.h:378
ProtocolDetector: Uses interrupt edge logic to detect IR protocol by testing all known preambles.
bool begin(uint8_t pin)
Starts protocol detection by attaching an interrupt to the specified pin.
void end()
Stops protocol detection by detaching the interrupt.
Small, header-only vector replacement for non-STL environments.
Definition Vector.h:29