Skip to content

Commit

Permalink
adjust Arduino examples to use interrupts when applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Apr 5, 2024
1 parent 492ddb8 commit 3f10fc2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
23 changes: 21 additions & 2 deletions examples/absolute_mode/absolute_mode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
// an object to hold data reported by the Cirque trackpad
AbsoluteReport data;

#if DR_PIN != PINNACLE_SW_DR
// track the interrupts with our own IRQ flag
volatile bool isDataReady = false;

void interruptHandler() {
isDataReady = true;
}
#endif // using DR_PIN

void setup() {
Serial.begin(115200);
while (!Serial) {
Expand All @@ -30,6 +39,10 @@ void setup() {
Serial.println(F("CirquePinnacle/examples/absolute_mode"));
trackpad.setDataMode(PINNACLE_ABSOLUTE);
trackpad.absoluteModeConfig(1); // set count of z-idle packets to 1
#if DR_PIN != PINNACLE_SW_DR
// pinMode() is already called by trackpad.begin()
attachInterrupt(digitalPinToInterrupt(DR_PIN), interruptHandler, FALLING);
#endif // using DR_PIN
Serial.println(F("\n*** Enter 'M' to measure and print raw data."));
Serial.println(F("*** Enter 'T' to measure and print trigonometric calculations.\n"));
Serial.println(F("Touch the trackpad to see the data."));
Expand All @@ -43,14 +56,20 @@ raw data (false) or trigonometry data (true)
*/
bool onlyShowTrigVals = false;

#ifndef M_PI
#if defined(M_PI) && !defined(PI)
#define PI M_PI
#else
#endif
#ifndef PI
#define PI 3.14159
#endif

void loop() {
#if DR_PIN == PINNACLE_SW_DR // not using DR_PIN
if (trackpad.available()) {
#else // using interruptHandler()
if (isDataReady) {
isDataReady = false; // reset our IRQ flag
#endif // using DR_PIN
trackpad.read(&data);

// datasheet recommends clamping the axes value to reliable range
Expand Down
40 changes: 34 additions & 6 deletions examples/anymeas_mode/anymeas_mode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ void compensate() {
}
}

// track the interrupts with our own IRQ flag
volatile bool isDataReady = false;

// a flag to control iteration of our loop()
bool waitingForInterrupt = false;
// the index number used to iterate through our vectorDeterminants array used in loop()
unsigned int vectorIndex = 0;

void interruptHandler() {
isDataReady = true;
}

void setup() {
Serial.begin(115200);
while (!Serial) {
Expand All @@ -63,20 +75,36 @@ void setup() {
trackpad.setDataMode(PINNACLE_ANYMEAS);
trackpad.anymeasModeConfig();
compensate();
// pinMode() is already called by trackpad.begin()
attachInterrupt(digitalPinToInterrupt(DR_PIN), interruptHandler, FALLING);
Serial.println(F("starting in 5 seconds..."));
delay(5000);
}

void loop() {
for (uint8_t i = 0; i < variousVectors_size; i++) {
int16_t measurement = trackpad.measureAdc(vectorDeterminants[i].toggle,
vectorDeterminants[i].polarity);
measurement -= compensations[i];
if (!isDataReady && !waitingForInterrupt) {
trackpad.startMeasureAdc(
vectorDeterminants[vectorIndex].toggle,
vectorDeterminants[vectorIndex].polarity);
waitingForInterrupt = true;
} else if (isDataReady) {
isDataReady = false; // reset our IRQ flag
waitingForInterrupt = false; // allow iteration to continue

int16_t measurement = trackpad.getMeasureAdc();
measurement -= compensations[vectorIndex];
Serial.print(F("meas"));
Serial.print(i);
Serial.print(vectorIndex);
Serial.print(F(":"));
Serial.print(measurement);
Serial.print(F(" \t"));

// increment our loop iterator
if (vectorIndex < (variousVectors_size - 1)) {
vectorIndex++;
} else {
vectorIndex = 0;
Serial.println();
}
}
Serial.println();
}
19 changes: 19 additions & 0 deletions examples/relative_mode/relative_mode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
// an object to hold data reported by the Cirque trackpad
RelativeReport data;

#if DR_PIN != PINNACLE_SW_DR
// track the interrupts with our own IRQ flag
volatile bool isDataReady = false;

void interruptHandler() {
isDataReady = true;
}
#endif // using DR_PIN

void setup() {
Serial.begin(115200);
while (!Serial) {
Expand All @@ -29,11 +38,21 @@ void setup() {
Serial.println(F("CirquePinnacle/examples/relative_mode"));
trackpad.setDataMode(PINNACLE_RELATIVE);
trackpad.relativeModeConfig(); // uses default config
#if DR_PIN != PINNACLE_SW_DR
// pinMode() is already called by trackpad.begin()
attachInterrupt(digitalPinToInterrupt(DR_PIN), interruptHandler, FALLING);
#endif // using DR_PIN
Serial.println(F("Touch the trackpad to see the data."));
}

void loop() {

#if DR_PIN == PINNACLE_SW_DR // not using DR_PIN
if (trackpad.available()) {
#else // using interruptHandler()
if (isDataReady) {
isDataReady = false; // reset our IRQ flag
#endif // using DR_PIN
trackpad.read(&data);
Serial.print(F("Left:"));
Serial.print(data.buttons & 1);
Expand Down
21 changes: 19 additions & 2 deletions examples/usb_mouse/usb_mouse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
// an object to hold data reported by the Cirque trackpad
RelativeReport data;

#if DR_PIN != PINNACLE_SW_DR
// track the interrupts with our own IRQ flag
volatile bool isDataReady = false;

void interruptHandler() {
isDataReady = true;
}
#endif // using DR_PIN

const uint32_t interval = 750; // milliseconds used to blink LED
uint32_t lastLedChange = 0; // millis() since last digitalWrite()
bool ledState = false; // last state sent to digitalWrite()
Expand All @@ -31,15 +40,23 @@ void setup() {
trackpad.setDataMode(PINNACLE_RELATIVE); // ensure mouse mode is enabled
// tell the Pinnacle ASIC to rotate the orientation of the axis data by +90 degrees
trackpad.relativeModeConfig(true, true); // (enable taps, rotate90)

#if DR_PIN != PINNACLE_SW_DR
// pinMode() is already called by trackpad.begin()
attachInterrupt(digitalPinToInterrupt(DR_PIN), interruptHandler, FALLING);
#endif // using DR_PIN

digitalWrite(LED, LOW);
lastLedChange = millis();
}

void loop() {

if (trackpad.available()) { // is there new data?
#if DR_PIN == PINNACLE_SW_DR // not using DR_PIN
if (trackpad.available()) {
#else // using interruptHandler()
if (isDataReady) {
isDataReady = false; // reset our IRQ flag
#endif // using DR_PIN

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

0 comments on commit 3f10fc2

Please sign in to comment.