Skip to content

Commit df1da8d

Browse files
committed
adjust Arduino examples to use interrupts when applicable
1 parent 927861e commit df1da8d

File tree

4 files changed

+93
-10
lines changed

4 files changed

+93
-10
lines changed

examples/absolute_mode/absolute_mode.ino

+21-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
1616
// an object to hold data reported by the Cirque trackpad
1717
AbsoluteReport data;
1818

19+
#if DR_PIN != PINNACLE_SW_DR
20+
// track the interrupts with our own IRQ flag
21+
volatile bool isDataReady = false;
22+
23+
void interruptHandler() {
24+
isDataReady = true;
25+
}
26+
#endif // using DR_PIN
27+
1928
void setup() {
2029
Serial.begin(115200);
2130
while (!Serial) {
@@ -30,6 +39,10 @@ void setup() {
3039
Serial.println(F("CirquePinnacle/examples/absolute_mode"));
3140
trackpad.setDataMode(PINNACLE_ABSOLUTE);
3241
trackpad.absoluteModeConfig(1); // set count of z-idle packets to 1
42+
#if DR_PIN != PINNACLE_SW_DR
43+
// pinMode() is already called by trackpad.begin()
44+
attachInterrupt(digitalPinToInterrupt(DR_PIN), interruptHandler, FALLING);
45+
#endif // using DR_PIN
3346
Serial.println(F("\n*** Enter 'M' to measure and print raw data."));
3447
Serial.println(F("*** Enter 'T' to measure and print trigonometric calculations.\n"));
3548
Serial.println(F("Touch the trackpad to see the data."));
@@ -43,14 +56,20 @@ raw data (false) or trigonometry data (true)
4356
*/
4457
bool onlyShowTrigVals = false;
4558

46-
#ifndef M_PI
59+
#if defined(M_PI) && !defined(PI)
4760
#define PI M_PI
48-
#else
61+
#endif
62+
#ifndef PI
4963
#define PI 3.14159
5064
#endif
5165

5266
void loop() {
67+
#if DR_PIN == PINNACLE_SW_DR // not using DR_PIN
5368
if (trackpad.available()) {
69+
#else // using interruptHandler()
70+
if (isDataReady) {
71+
isDataReady = false; // reset our IRQ flag
72+
#endif // using DR_PIN
5473
trackpad.read(&data);
5574

5675
// datasheet recommends clamping the axes value to reliable range

examples/anymeas_mode/anymeas_mode.ino

+34-6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ void compensate() {
4848
}
4949
}
5050

51+
// track the interrupts with our own IRQ flag
52+
volatile bool isDataReady = false;
53+
54+
// a flag to control iteration of our loop()
55+
bool waitingForInterrupt = false;
56+
// the index number used to iterate through our vectorDeterminants array used in loop()
57+
unsigned int vectorIndex = 0;
58+
59+
void interruptHandler() {
60+
isDataReady = true;
61+
}
62+
5163
void setup() {
5264
Serial.begin(115200);
5365
while (!Serial) {
@@ -63,20 +75,36 @@ void setup() {
6375
trackpad.setDataMode(PINNACLE_ANYMEAS);
6476
trackpad.anymeasModeConfig();
6577
compensate();
78+
// pinMode() is already called by trackpad.begin()
79+
attachInterrupt(digitalPinToInterrupt(DR_PIN), interruptHandler, FALLING);
6680
Serial.println(F("starting in 5 seconds..."));
6781
delay(5000);
6882
}
6983

7084
void loop() {
71-
for (uint8_t i = 0; i < variousVectors_size; i++) {
72-
int16_t measurement = trackpad.measureAdc(vectorDeterminants[i].toggle,
73-
vectorDeterminants[i].polarity);
74-
measurement -= compensations[i];
85+
if (!isDataReady && !waitingForInterrupt) {
86+
trackpad.startMeasureAdc(
87+
vectorDeterminants[vectorIndex].toggle,
88+
vectorDeterminants[vectorIndex].polarity);
89+
waitingForInterrupt = true;
90+
} else if (isDataReady) {
91+
isDataReady = false; // reset our IRQ flag
92+
waitingForInterrupt = false; // allow iteration to continue
93+
94+
int16_t measurement = trackpad.getMeasureAdc();
95+
measurement -= compensations[vectorIndex];
7596
Serial.print(F("meas"));
76-
Serial.print(i);
97+
Serial.print(vectorIndex);
7798
Serial.print(F(":"));
7899
Serial.print(measurement);
79100
Serial.print(F(" \t"));
101+
102+
// increment our loop iterator
103+
if (vectorIndex < (variousVectors_size - 1)) {
104+
vectorIndex++;
105+
} else {
106+
vectorIndex = 0;
107+
Serial.println();
108+
}
80109
}
81-
Serial.println();
82110
}

examples/relative_mode/relative_mode.ino

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
1515
// an object to hold data reported by the Cirque trackpad
1616
RelativeReport data;
1717

18+
#if DR_PIN != PINNACLE_SW_DR
19+
// track the interrupts with our own IRQ flag
20+
volatile bool isDataReady = false;
21+
22+
void interruptHandler() {
23+
isDataReady = true;
24+
}
25+
#endif // using DR_PIN
26+
1827
void setup() {
1928
Serial.begin(115200);
2029
while (!Serial) {
@@ -29,11 +38,21 @@ void setup() {
2938
Serial.println(F("CirquePinnacle/examples/relative_mode"));
3039
trackpad.setDataMode(PINNACLE_RELATIVE);
3140
trackpad.relativeModeConfig(); // uses default config
41+
#if DR_PIN != PINNACLE_SW_DR
42+
// pinMode() is already called by trackpad.begin()
43+
attachInterrupt(digitalPinToInterrupt(DR_PIN), interruptHandler, FALLING);
44+
#endif // using DR_PIN
3245
Serial.println(F("Touch the trackpad to see the data."));
3346
}
3447

3548
void loop() {
49+
50+
#if DR_PIN == PINNACLE_SW_DR // not using DR_PIN
3651
if (trackpad.available()) {
52+
#else // using interruptHandler()
53+
if (isDataReady) {
54+
isDataReady = false; // reset our IRQ flag
55+
#endif // using DR_PIN
3756
trackpad.read(&data);
3857
Serial.print(F("Left:"));
3958
Serial.print(data.buttons & 1);

examples/usb_mouse/usb_mouse.ino

+19-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
1212
// an object to hold data reported by the Cirque trackpad
1313
RelativeReport data;
1414

15+
#if DR_PIN != PINNACLE_SW_DR
16+
// track the interrupts with our own IRQ flag
17+
volatile bool isDataReady = false;
18+
19+
void interruptHandler() {
20+
isDataReady = true;
21+
}
22+
#endif // using DR_PIN
23+
1524
const uint32_t interval = 750; // milliseconds used to blink LED
1625
uint32_t lastLedChange = 0; // millis() since last digitalWrite()
1726
bool ledState = false; // last state sent to digitalWrite()
@@ -31,15 +40,23 @@ void setup() {
3140
trackpad.setDataMode(PINNACLE_RELATIVE); // ensure mouse mode is enabled
3241
// tell the Pinnacle ASIC to rotate the orientation of the axis data by +90 degrees
3342
trackpad.relativeModeConfig(true, true); // (enable taps, rotate90)
34-
43+
#if DR_PIN != PINNACLE_SW_DR
44+
// pinMode() is already called by trackpad.begin()
45+
attachInterrupt(digitalPinToInterrupt(DR_PIN), interruptHandler, FALLING);
46+
#endif // using DR_PIN
3547

3648
digitalWrite(LED, LOW);
3749
lastLedChange = millis();
3850
}
3951

4052
void loop() {
4153

42-
if (trackpad.available()) { // is there new data?
54+
#if DR_PIN == PINNACLE_SW_DR // not using DR_PIN
55+
if (trackpad.available()) {
56+
#else // using interruptHandler()
57+
if (isDataReady) {
58+
isDataReady = false; // reset our IRQ flag
59+
#endif // using DR_PIN
4360

4461
// save buttons' previous state before getting updates
4562
uint8_t prevButtonStates = data.buttons; // for edge detection

0 commit comments

Comments
 (0)