Repository navigation
Expand file tree
/
Copy pathSwitch.cpp
More file actions
202 lines (177 loc) · 7.32 KB
/
Copy pathSwitch.cpp
File metadata and controls
202 lines (177 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "WHSR.h"
#if defined(ARDUINO_ARDUINO_NANO33BLE)
#include <nrf_gpio.h> // pulls in CMSIS header with NRF_P0/NRF_P1 and GPIO_PIN_CNF_PULL_Msk
#endif
// For copy & paste
#if defined(ARDUINO_AVR_NANO)
#elif defined(ARDUINO_ARDUINO_NANO33BLE)
#endif
#define SWITCH_ADC_PULLUP D11 // D11, Pin 14, BINT
#define SWITCH_PIN A2 // A2 = D16 = ADC[2], BUT
// switchInterruptState lifecycle (separate from SWITCH_INTERRUPT_STATE hardware flag in WHSR.h)
#define SWITCH_PRESS_NONE 0 // No press detected
#define SWITCH_PRESS_WAIT 1 // ISR fired, ADC read pending (Nano 33 BLE)
#define SWITCH_PRESS_READY 2 // ADC read done, value available for sketch
#define SWITCH_PRESS_DONE 3 // Value consumed by sketch
// Das ist nicht das gleiche wie switchInterruptState:
volatile unsigned char switchInterruptAktiv = SWITCH_PRESS_NONE;
volatile boolean warInISR = 0;
//
// Initialisiert die Kollisionserkennung
//
void WHSR::initSwitches(void)
{
// Switch-Messschaltung aktivieren
pinMode(SWITCH_ADC_PULLUP, OUTPUT);
digitalWrite(SWITCH_ADC_PULLUP, HIGH);
}
//
// Liest die Taster für die Kollisionserkennung aus
//
unsigned char WHSR::readSwitches(void)
{
if (switchInterruptAktiv == SWITCH_PRESS_READY)
{
switchInterruptAktiv = SWITCH_PRESS_DONE;
return switchValue;
}
#if defined(ARDUINO_AVR_NANO)
else
{
DoCheckADCMode(SWITCH_ADC);
long adcValue = mySensorValues[SWITCH_ADC];
adcValue = adcValue == 0 ? 1 : adcValue; // Don't divide by zero
return (unsigned char)(((10160000L / adcValue - 10000L) * SwitchFactor + 5000L) / 10000);
}
#elif defined(ARDUINO_ARDUINO_NANO33BLE)
if (switchInterruptAktiv == SWITCH_PRESS_WAIT)
{
switchInterruptOff(); // disable interrupt while we do the ADC read and processing
// Restore D11 HIGH so the resistor divider is active, then read A2.
// (The ISR set D11 LOW to prevent re-triggering; we need it HIGH for measurement.)
digitalWrite(SWITCH_ADC_PULLUP, HIGH);
delay(2);
long adcValue = analogRead(SWITCH_PIN);
DebugSerial_print("SWITCH_ADC Interrupt: ");
DebugSerial_println(adcValue);
adcValue = adcValue == 0 ? 1 : adcValue;
switchValue = (unsigned char)(((10160000L / adcValue - 10000L) * SwitchFactor + 5000L) / 10000);
switchInterruptOn(NULL, FALLING); // re-arm: sets D11 HIGH and re-attaches interrupt, resets state to PRESS_NONE
switchInterruptAktiv = SWITCH_PRESS_DONE;
return switchValue;
}
// Direct polling read (interrupt mode not active)
long adcValue = analogRead(SWITCH_PIN); // use pin constant, not channel index
DebugSerial_print("SWITCH_ADC ohne Interrupt: ");
DebugSerial_println(adcValue);
adcValue = adcValue == 0 ? 1 : adcValue; // Don't divide by zero
switchInterruptAktiv = SWITCH_PRESS_DONE;
return (unsigned char)(((10160000L / adcValue - 10000L) * SwitchFactor + 5000L) / 10000);
#endif
}
//
// Turn On the PinChange Interrupt
//
void WHSR::switchInterruptOn(void (*isrfunction)(), PinStatus triggerOn)
{
// Messschaltung deaktivieren, SWITCH_PIN wird zum digitalen Eingang
digitalWrite(SWITCH_ADC_PULLUP, LOW);
delay(2);
#if defined(ARDUINO_AVR_NANO)
byte pin = SWITCH_PIN;
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
#elif defined(ARDUINO_ARDUINO_NANO33BLE)
// Interrupt for switches
// Use INPUT (no internal pullup): SWITCH_ADC_PULLUP (D11) already provides external pullup to A2.
// Adding INPUT_PULLUP would lower the effective pullup impedance and prevent
// high-resistance switches from pulling A2 below the LOW threshold.
DebugSerial_println("attachInterrupt");
pinMode(SWITCH_PIN, INPUT);
if(isrfunction == NULL) {
DebugSerial_println("Using default switchISR");
isrfunction = switchISR;
} else {
DebugSerial_println("Using custom switchISR");
}
attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), isrfunction, triggerOn);
// mbed's InterruptIn enables the internal pullup (~8-13kOhm) by default.
// With that pullup active, switches above ~5k cannot pull A2 below VIL (0.99V),
// so only the lowest-resistance switch triggers FALLING.
// Fix: clear only the PULL bits [3:2] of PIN_CNF. Do NOT touch SENSE bits [17:16] —
// the previous attempt used nrf_gpio_cfg_input() which also wrote SENSE=NOSENSE,
// disabling the interrupt entirely if mbed uses the PORT-event (SENSE) mechanism.
{
uint32_t p = (uint32_t)digitalPinToPinName(SWITCH_PIN);
NRF_GPIO_Type* port = (p < 32u) ? NRF_P0 : NRF_P1;
port->PIN_CNF[p & 0x1Fu] &= ~GPIO_PIN_CNF_PULL_Msk; // clear pull = NoPull
}
#endif
switchInterruptAktiv = SWITCH_PRESS_NONE;
switchInterruptState = SWITCH_INTERRUPT_IDLE;
}
//
// Turn Off the PinChange Interrupt
//
void WHSR::switchInterruptOff(void)
{
// Messschaltung aktivieren, SWITCH_PIN wird zum analogen Eingang
digitalWrite(SWITCH_ADC_PULLUP, HIGH);
delay(2);
#if defined(ARDUINO_AVR_NANO)
byte pin = SWITCH_PIN;
PCICR &= ~bit (digitalPinToPCICRbit(pin)); // disable interrupt for the group
#elif defined(ARDUINO_ARDUINO_NANO33BLE)
detachInterrupt(digitalPinToInterrupt(SWITCH_PIN));
#endif
switchInterruptAktiv = SWITCH_PRESS_NONE;
switchInterruptState = SWITCH_INTERRUPT_NONE;
}
//
// Return true if Switch was pressed
//
bool WHSR::switchAvailable()
{
#if defined(ARDUINO_ARDUINO_NANO33BLE)
if (warInISR) {
DebugSerial_print("WarInISR, switchInterruptAktiv: ");
DebugSerial_println(switchInterruptAktiv);
warInISR = false;
}
// PRESS_WAIT: ISR fired, readSwitches() still needs to do the ADC read
// PRESS_READY: ADC read done, value is waiting for the sketch
return switchInterruptAktiv == SWITCH_PRESS_WAIT || switchInterruptAktiv == SWITCH_PRESS_READY;
#else
return switchInterruptAktiv == SWITCH_PRESS_READY;
#endif
}
//
// Pin Change Interrupt Service Routine
//
void WHSR::switchInterruptSeviceRoutine(void)
{
noInterrupts();
warInISR = true;
#if defined(ARDUINO_ARDUINO_NANO33BLE)
// Mirror AVR behaviour as closely as possible:
// - On press: immediately disable interrupt and set D11 LOW (measurement mode),
// then flag PRESS_WAIT so readSwitches() does the ADC read from main-loop context
// (analogRead/delay are not safe in ISR context).
// detachInterrupt() and digitalWrite() are safe to call from ISR context on mbed.
switchInterruptAktiv = SWITCH_PRESS_WAIT;
#else
switchInterruptAktiv = SWITCH_PRESS_WAIT;
// Ausschalten, damit der analoge Wert gelesen werden kann
switchInterruptOff();
switchValue = 0;
//unsigned long startMillis = millis(); //Aktuellen Zeitstempel speichern
//while (switchValue == 0 && (millis() - startMillis < 1000))
switchValue = readSwitches();
// Wieder einschalten
switchInterruptOn(NULL, FALLING); // re-arm: sets D11 HIGH and re-attaches interrupt (same as AVR)
switchInterruptAktiv = SWITCH_PRESS_READY;
// Interrupts werden nach ISR aktiviert
#endif
interrupts();
}