Skip to content

Commit 6cd38a0

Browse files
Change in the interrupt processing flow.
AttachInterrupt() should be called for a capable class (digital input is the only class from the classic HAL instances). ProcessInterrupt() function will be called from the HalInstance class to handle the interrupt notification. That function could register it's HalInstance object to be called from the normal run with the EBF ProcessInterrupt(), which will use the messaging system to call the HalInstance Process() function.
1 parent adf95cc commit 6cd38a0

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

examples/Digital-Interrupts/Digital-Interrupts.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,22 @@ void setup()
116116
// Built-in LED on digital output #13 will be used
117117
led.Init(13);
118118

119-
// Initialize button on digital input 2
119+
// Initialize button on digital input 2 for any change of the value
120120
// Internal pullup resistor will be used to minimize bouncing
121121
button.Init(2, onButtonChange, EBF_DigitalInput::InterruptMode::MODE_CHANGE, true);
122122

123-
// Since interrupts processing is enabled for that example, EBF will try to use
124-
// hardware interrupts for the button digital input since it's using interrupt
125-
// enabled line 2. On Arduino UNO lines 2 and 3 are capable to produce interrupts
123+
// AttachInterrupt() should be called for the button instance to utilize
124+
// the hardware interrupt line based on the configuration passed in Init() function
125+
// On Arduino UNO lines 2 and 3 are capable to produce interrupts.
126126
// When interrupts are used, polling is disabled on those pins
127127
// Pay attention that there is no change in the initialization of the digital input
128128
// object. The same onButtonChange function will be called.
129+
button.AttachInterrupt();
129130
}
130131

131132
void loop()
132133
{
133134
// Let EBF to do all the processing
134135
// Your logic should be done in the callback functions
135136
EBF.Process();
136-
}
137+
}

src/Core/EBF_DigitalInput.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ uint8_t EBF_DigitalInput::Init(
2828

2929
if (callbackFunc != NULL) {
3030
lastValue = digitalRead(pinNumber);
31+
} else {
32+
// No callback. No need to poll in that case
33+
pollIntervalMs = EBF_NO_POLLING;
34+
}
3135

36+
return EBF_OK;
37+
}
38+
39+
uint8_t EBF_DigitalInput::AttachInterrupt()
40+
{
3241
#ifdef EBF_USE_INTERRUPTS
3342
uint8_t interruptNumber = NOT_AN_INTERRUPT;
3443

@@ -43,14 +52,12 @@ uint8_t EBF_DigitalInput::Init(
4352
// No need to poll
4453
pollIntervalMs = EBF_NO_POLLING;
4554
}
55+
56+
return EBF_OK;
4657
}
4758
#endif
48-
} else {
49-
// No callback. No need to poll in that case
50-
pollIntervalMs = EBF_NO_POLLING;
51-
}
5259

53-
return EBF_OK;
60+
return EBF_INVALID_STATE;
5461
}
5562

5663
void EBF_DigitalInput::SetPollInterval(uint32_t ms)
@@ -128,6 +135,17 @@ uint8_t EBF_DigitalInput::Process()
128135
return EBF_OK;
129136
}
130137

138+
void EBF_DigitalInput::ProcessInterrupt()
139+
{
140+
// This function is called from ISR.
141+
// For DigitalInput, call the callback function to pass the pcontrol to user
142+
// The user can call EBF.ProcessInterrupt() function to pass the control
143+
// back to the EBF, which will call user callback again from normal run
144+
ProcessCallback();
145+
146+
// The interrupt register is cleared by the arduino code
147+
}
148+
131149
uint8_t EBF_DigitalInput::GetValue()
132150
{
133151
return digitalRead(pinNumber);

src/Core/EBF_DigitalInput.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ class EBF_DigitalInput : protected EBF_HalInstance {
2727
uint8_t pinNumber,
2828
EBF_CallbackType callbackFunc,
2929
InterruptMode isrMode = InterruptMode::MODE_CHANGE,
30-
bool internelPullup = false
30+
bool internalPullup = false
3131
);
32+
// Call to attach the device to an interrupt line
33+
uint8_t AttachInterrupt();
3234

3335
uint8_t GetValue();
3436
uint8_t GetLastValue();
@@ -37,6 +39,7 @@ class EBF_DigitalInput : protected EBF_HalInstance {
3739

3840
protected:
3941
uint8_t Process();
42+
void ProcessInterrupt();
4043
virtual void ProcessCallback() { callbackFunc(); }
4144
EBF_CallbackType callbackFunc;
4245
uint8_t pinNumber;

src/Core/EBF_HalInstance.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class EBF_HalInstance {
3838
uint8_t Init(HAL_Type type, uint8_t id);
3939
// Virtual function that will be called to process the data
4040
virtual uint8_t Process() = 0;
41-
// Virtual function that will be called to process the callback function
42-
virtual void ProcessCallback() { }
41+
// Virtual function that will be called to process the interrupt
42+
virtual void ProcessInterrupt() { }
4343

4444
static uint8_t GetNumberOfInstances();
4545
uint32_t GetPollingInterval();

src/Core/EBF_Logic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ void EBF_Logic::HandleIsr(uint8_t interruptNumber)
318318
if (pHalIsr[interruptNumber] != NULL) {
319319
// Next process call is done from ISR
320320
isRunFromISR = 1;
321-
pHalIsr[interruptNumber]->ProcessCallback();
321+
pHalIsr[interruptNumber]->ProcessInterrupt();
322322
isRunFromISR = 0;
323323
}
324324
}

0 commit comments

Comments
 (0)