Skip to content

Commit bba7b40

Browse files
committed
fix reset pin for portenta machine control
1 parent 1f5ca5a commit bba7b40

File tree

9 files changed

+346
-185
lines changed

9 files changed

+346
-185
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ The procedure:
3535
* `Arduino GIGA R1 WiFi`: short the pin 7 to GND until the led turns off
3636
* `Arduino Nano RP2040 Connect`: short the pin 2 to 3.3V until the led turns off
3737
* `Arduino Portenta H7`: short the pin 0 to GND until the led turns off
38+
* `Portenta Machine Control`: plug the device to a 24V power source, short the pin Digital Inputs 0 to 24VOUT until the led (`LED_BUILTIN`) turns off
3839
* Other boards: short the pin 2 to GND until the led turns off
39-
* `Portenta Machine Control`: currently the reset procedure is not available
4040

4141
### More on the reconfiguration pin
4242
Internally, the pin indicated in the procedure is set as `INPUT_PULLUP` (except for `Arduino Opta` ) and it's attached to an ISR fired on every change of the pin's status.

examples/NetworkConfiguratorDemo/NetworkConfiguratorDemo.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off
2323
* - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off
2424
* - Portenta H7: short the pin 0 to GND until the led turns off
25-
* - Portenta Machine Control: the reset is not available
25+
* - Portenta Machine Control: plug the device to a 24V power source, short the pin Digital Inputs 0
26+
* to 24VOUT until the led (`LED_BUILTIN`) turns off
2627
* - Other boards: short the pin 2 to GND until the led turns off
2728
*
2829
* In this sketch the BLE and Serial interfaces are always enabled and ready for accepting

src/Arduino_NetworkConfigurator.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ NetworkConfiguratorClass::NetworkConfiguratorClass(ConnectionHandler &connection
4141
_receivedEvent = NetworkConfiguratorEvents::NONE;
4242
_optionUpdateTimer.begin(NC_UPDATE_NETWORK_OPTIONS_TIMER_ms); //initialize the timer before calling begin
4343
_agentsManager = &AgentsManagerClass::getInstance();
44-
_resetInput = &ResetInput::getInstance();
44+
_resetInput = &ResetInputBase::getInstance();
4545
_ledFeedback = &LEDFeedbackClass::getInstance();
4646
}
4747

@@ -123,7 +123,8 @@ NetworkConfiguratorStates NetworkConfiguratorClass::update() {
123123
* - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off
124124
* - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off
125125
* - Portenta H7: short the pin 0 to GND until the led turns off
126-
* - Portenta Machine Control: the reset is not available
126+
* - Portenta Machine Control: plug the device to a 24V power source, short the pin Digital Inputs 0
127+
* to 24VOUT until the led (`LED_BUILTIN`) turns off
127128
* - Other boards: short the pin 2 to GND until the led turns off
128129
*/
129130

@@ -210,7 +211,7 @@ void NetworkConfiguratorClass::setReconfigurePin(int pin) {
210211
_resetInput->setPin(pin);
211212
}
212213

213-
void NetworkConfiguratorClass::addReconfigurePinCallback(ResetInput::ResetInputCallback callback) {
214+
void NetworkConfiguratorClass::addReconfigurePinCallback(ResetInputBase::ResetInputCallback callback) {
214215
_resetInput->setPinChangedCallback(callback);
215216
}
216217

src/Arduino_NetworkConfigurator.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <connectionHandlerModels/settings.h>
1717
#include <Arduino_TimedAttempt.h>
1818
#include <Arduino_KVStore.h>
19-
#include "utility/ResetInput.h"
19+
#include "utility/ResetInputBase.h"
2020
#include "utility/LEDFeedback.h"
2121

2222
/**
@@ -59,7 +59,8 @@ enum class NetworkConfiguratorStates { ZERO_TOUCH_CONFIG,
5959
* - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off
6060
* - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off
6161
* - Portenta H7: short the pin 0 to GND until the led turns off
62-
* - Portenta Machine Control: the reset is not available
62+
* - Portenta Machine Control: plug the device to a 24V power source, short the pin Digital Inputs 0
63+
* to 24VOUT until the led (`LED_BUILTIN`) turns off
6364
* - Other boards: short the pin 2 to GND until the led turns off
6465
*
6566
*/
@@ -129,7 +130,7 @@ class NetworkConfiguratorClass {
129130
* interrupt on the reconfiguration pin is fired.
130131
* @param callback Pointer to the callback function.
131132
*/
132-
void addReconfigurePinCallback(ResetInput::ResetInputCallback callback);
133+
void addReconfigurePinCallback(ResetInputBase::ResetInputCallback callback);
133134

134135
/**
135136
* @brief Checks if a specific configuration agent is enabled.
@@ -165,7 +166,7 @@ class NetworkConfiguratorClass {
165166
static inline models::NetworkSetting _networkSetting;
166167
bool _connectionHandlerIstantiated;
167168
bool _configInProgress;
168-
ResetInput *_resetInput;
169+
ResetInputBase *_resetInput;
169170
LEDFeedbackClass *_ledFeedback;
170171
/* Timeout instances */
171172
// Timeout for connection attempt

src/utility/ResetInput.cpp

Lines changed: 0 additions & 121 deletions
This file was deleted.

src/utility/ResetInput.h

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,83 @@
88

99
#pragma once
1010

11-
#include "Arduino.h"
12-
#include <functional>
11+
#include "ANetworkConfigurator_Config.h"
12+
#include "ResetInputBase.h"
13+
#include "LEDFeedback.h"
1314

14-
#define DISABLE_PIN -1
15-
16-
/**
17-
* @class ResetInput
18-
* @brief A singleton class to handle input of the reset functionality with interrupt-based monitoring.
19-
*
20-
* This class provides methods to configure and monitor a reset input pin. It allows
21-
* setting up a custom callback function to be executed when the pin status changes.
22-
*/
23-
class ResetInput{
15+
class ResetInput : public ResetInputBase {
2416
public:
25-
/**
26-
* @typedef ResetInput::ResetInputCallback
27-
* @brief A type definition for the callback function to be executed on pin status change.
28-
*/
29-
typedef std::function<void()> ResetInputCallback;
30-
/**
31-
* @brief Get the singleton instance of the ResetInput class.
32-
* @return A reference to the ResetInput instance.
33-
*/
34-
static ResetInput& getInstance();
35-
/**
36-
* @brief Initialize the reset input by setting up the interrupt pin.
37-
*/
38-
void begin();
39-
/**
40-
* @brief Check if the reset event has been fired.
41-
* @return True if the event is fired, otherwise false.
42-
*/
43-
bool isEventFired();
44-
/**
45-
* @brief Set a custom callback function to be called when the pin status changes.
46-
* This function must be called before invoking the `begin` method.
47-
* @param callback The custom callback function to be executed.
48-
*/
49-
void setPinChangedCallback(ResetInputCallback callback);
50-
/**
51-
* @brief Set the pin to be monitored for reset events.
52-
* By default, the pin is set as INPUT_PULLUP.
53-
* Use the value DISABLE_PIN to disable the pin and the reset procedure.
54-
* This function must be called before invoking the `begin` method.
55-
* @param pin The pin number to be monitored. The pin must
56-
* be in the list of digital pins usable for interrupts.
57-
* Please refer to the Arduino documentation for more details:
58-
* https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/
59-
*/
60-
void setPin(int pin);
17+
static ResetInput &getInstance() {
18+
static ResetInput instance;
19+
return instance;
20+
}
21+
22+
void begin() override {
23+
if(_pin == DISABLE_PIN){
24+
return;
25+
}
26+
#ifdef ARDUINO_OPTA
27+
pinMode(_pin, INPUT);
28+
#else
29+
pinMode(_pin, INPUT_PULLUP);
30+
#endif
31+
pinMode(LED_RECONFIGURE, OUTPUT);
32+
digitalWrite(LED_RECONFIGURE, LED_OFF);
33+
attachInterrupt(digitalPinToInterrupt(_pin),_pressedCallback, CHANGE);
34+
}
35+
36+
bool isEventFired() override {
37+
if(_startPressed != 0){
38+
#if defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_SAMD_MKRWIFI1010)
39+
LEDFeedbackClass::getInstance().stop();
40+
#endif
41+
if(micros() - _startPressed > RESET_HOLD_TIME){
42+
digitalWrite(LED_RECONFIGURE, LED_OFF);
43+
_expired = true;
44+
}
45+
}
46+
47+
return _fireEvent;
48+
}
49+
6150
private:
62-
/**
63-
* @brief Private constructor to enforce the singleton pattern.
64-
*/
65-
ResetInput();
66-
static inline ResetInputCallback _pressedCustomCallback;
67-
static inline int _pin;
51+
ResetInput() {
52+
_pin = PIN_RECONFIGURE;
53+
_expired = false;
54+
_startPressed = 0;
55+
_fireEvent = false;
56+
_pressedCustomCallback = nullptr;
57+
}
6858
static inline volatile bool _expired;
6959
static inline volatile bool _fireEvent;
7060
static inline volatile uint32_t _startPressed;
7161
/**
7262
* @brief Internal callback function to handle pin press events.
7363
*/
74-
static void _pressedCallback();
64+
static void _pressedCallback() {
65+
#if defined(ARDUINO_NANO_RP2040_CONNECT)
66+
if(digitalRead(_pin) == HIGH){
67+
#else
68+
if(digitalRead(_pin) == LOW){
69+
#endif
70+
#if !defined(ARDUINO_NANO_RP2040_CONNECT) && !defined(ARDUINO_SAMD_MKRWIFI1010)
71+
LEDFeedbackClass::getInstance().stop();
72+
#endif
73+
_startPressed = micros();
74+
digitalWrite(LED_RECONFIGURE, LED_ON);
75+
} else {
76+
digitalWrite(LED_RECONFIGURE, LED_OFF);
77+
if(_startPressed != 0 && _expired){
78+
_fireEvent = true;
79+
}else{
80+
LEDFeedbackClass::getInstance().restart();
81+
}
82+
_startPressed = 0;
83+
}
84+
85+
if (_pressedCustomCallback) {
86+
_pressedCustomCallback();
87+
}
88+
}
7589
};
90+

0 commit comments

Comments
 (0)