1+ /*
2+ *---------------------------------------------------------------------------------
3+ *
4+ * Copyright (c) 2025, SparkFun Electronics Inc.
5+ *
6+ * SPDX-License-Identifier: MIT
7+ *
8+ *---------------------------------------------------------------------------------
9+ */
10+
11+ #include < Arduino.h>
12+ // Implementation file for the I2C communication class of the library.
13+ #include " sfDevFPC2534IComm.h"
14+
15+ // When in I2C comm mode, an interrupt pin from the FPC2534 is used to signal when
16+ // data is available to read. We manage this here.
17+ //
18+ // For the ISR interrupt handler
19+ // static volatile bool data_available = false;
20+ static volatile bool data_available = false ;
21+
22+ static bool isISRInitialized = false ;
23+
24+ // --------------------------------------------------------------------------------------------
25+ // standard ISR handler - no param version
26+ static void the_isr_cb ()
27+ {
28+ // This is the interrupt callback function
29+ // It will be called when the IRQ pin goes high
30+ // We can use this to signal that data is available
31+
32+ data_available = true ;
33+ }
34+
35+ // --------------------------------------------------------------------------------------------
36+ // ISR handler with param version ()
37+ static void the_isr_cb_arg (void *arg)
38+ {
39+ // set the data available flag in the instance
40+ if (arg != nullptr )
41+ static_cast <sfDevFPC2534IComm *>(arg)->setISRDataAvailable ();
42+ }
43+
44+ // --------------------------------------------------------------------------------------------
45+ // method used to set the IRS Handler by a sub-class
46+ void sfDevFPC2534IComm::initISRHandler (uint32_t interruptPin)
47+ {
48+ // Some platforms (ESP32 , RP2040) support passing an argument to the ISR handler.
49+ // If so, use that method to pass in "this" pointer to the handler. Which allows
50+ // us to set the data_available flag in the instance, rather than a static/global flag
51+ // and possibly support multiple sensors at the same time.
52+
53+ pinMode (interruptPin, INPUT);
54+ #if defined(ESP32)
55+
56+ attachInterruptArg (interruptPin, the_isr_cb_arg, (void *)this , RISING);
57+ _usingISRParam = true ;
58+
59+ #elif defined(ARDUINO_ARCH_RP2040)
60+
61+ attachInterruptParam (interruptPin, the_isr_cb_arg, RISING, (void *)this );
62+ _usingISRParam = true ;
63+
64+ #else
65+
66+ // Setup the interrupt handler for non-parameter version (older arduino impls)
67+ attachInterrupt (interruptPin, the_isr_cb, RISING);
68+
69+ isISRInitialized = true ;
70+ _usingISRParam = false ;
71+ #endif
72+ }
73+ // --------------------------------------------------------------------------------------------
74+ void sfDevFPC2534IComm::setISRDataAvailable (void )
75+ {
76+ _dataAvailable = true ;
77+ }
78+ // --------------------------------------------------------------------------------------------
79+ // method used to clear the data available flag
80+ void sfDevFPC2534IComm::clearISRDataAvailable (void )
81+ {
82+ // Are we using the ISR param method?
83+ if (_usingISRParam)
84+ _dataAvailable = false ;
85+ else if (isISRInitialized)
86+ data_available = false ;
87+ }
88+
89+ // --------------------------------------------------------------------------------------------
90+ // Data available ?
91+ bool sfDevFPC2534IComm::isISRDataAvailable (void )
92+ {
93+ // Are we using the ISR param method?
94+ if (_usingISRParam)
95+ return _dataAvailable;
96+
97+ // Nope, using the static ISR and static flag in this file (this only supports one instance)
98+ if (!isISRInitialized)
99+ return false ;
100+
101+ return data_available;
102+ }
0 commit comments