Skip to content

Commit

Permalink
update Linux 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 0daec6f commit 492ddb8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
23 changes: 21 additions & 2 deletions examples/linux/absolute_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <cmath> // sqrt(), pow(), atan2(), M_PI
#include <iostream> // cout, endl, cin
#include <iomanip> // setprecision()
#include <CirquePinnacle/CirquePinnacle.h> // trackpad object, absoluteClampAxis()
#include <CirquePinnacle/CirquePinnacle.h> // trackpad object

#ifdef USE_SW_DR // if using PINNACLE_SW_DR
#define DR_PIN PINNACLE_SW_DR
Expand All @@ -27,6 +27,15 @@ PinnacleTouchI2C trackpad(DR_PIN);
// an object to hold data reported by the Cirque trackpad
AbsoluteReport data;

#ifndef USE_SW_DR
// track the interrupts with our own IRQ flag
volatile bool isDataReady = false;
void interruptHandler()
{
isDataReady = true;
}
#endif // !defined(USE_SW_DR)

/*
Showing all the printed output below will slow down the board's ability to
read() data from the trackpad in a timely manner (resulting in data loss).
Expand All @@ -45,8 +54,13 @@ bool setup()
<< std::endl;
trackpad.setDataMode(PINNACLE_ABSOLUTE);
trackpad.absoluteModeConfig(1); // set count of z-idle packets to 1
#ifndef USE_SW_DR // if using PINNACLE_SW_DR
#ifndef USE_SW_DR // if not using PINNACLE_SW_DR
std::cout << "-- Using HW DataReady pin." << std::endl;

// pull in arduino-like namespace
namespace arduino = cirque_pinnacle_arduino_wrappers;
// setup the interrupt handler
arduino::attachInterrupt(DR_PIN, arduino::FALLING, &interruptHandler);
#endif
#ifndef USE_I2C
std::cout << "-- Using SPI interface." << std::endl;
Expand All @@ -68,7 +82,12 @@ bool setup()

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

// datasheet recommends clamping the axes value to reliable range
Expand Down
55 changes: 45 additions & 10 deletions examples/linux/anymeas_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ void compensate()
accumulatedValue = 0;
while (sweep < 5) // take 5 measurements and average them for a bit lower noise compensation value
{
int16_t value = trackpad.measureAdc(vectorDeterminants[i].toggle, vectorDeterminants[i].polarity);
int16_t value = trackpad.measureAdc(
vectorDeterminants[i].toggle,
vectorDeterminants[i].polarity);
sweep++;
accumulatedValue += value;
}
Expand All @@ -54,6 +56,19 @@ 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;
}

bool setup()
{
if (!trackpad.begin()) {
Expand All @@ -63,16 +78,21 @@ bool setup()
std::cout << "CirquePinnacle/examples/linux/anymeas_mode" << std::endl;
trackpad.setDataMode(PINNACLE_ANYMEAS);
trackpad.anymeasModeConfig();
#ifndef USE_SW_DR // if using PINNACLE_SW_DR

std::cout << "-- Using HW DataReady pin." << std::endl;
#endif

#ifndef USE_I2C
std::cout << "-- Using SPI interface." << std::endl;
#else
std::cout << "-- Using I2C interface." << std::endl;
#endif
compensate();

// pull in arduino-like namespace
namespace arduino = cirque_pinnacle_arduino_wrappers;
// setup the interrupt handler
arduino::attachInterrupt(DR_PIN, arduino::FALLING, &interruptHandler);

for (uint8_t i = 5; i; --i) {
std::cout << "starting in " << (unsigned int)i << "second" << (i < 1 ? 's' : ' ') << '\r';
sleep(1);
Expand All @@ -83,14 +103,29 @@ bool setup()

void loop()
{
for (unsigned int i = 0; i < variousVectors_size; i++) {
int16_t measurement = trackpad.measureAdc(
vectorDeterminants[i].toggle,
vectorDeterminants[i].polarity);
measurement -= compensations[i];
std::cout << "meas" << i << ": " << measurement << "\t";
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];
std::cout << "meas" << vectorIndex << ": " << measurement << "\t";

// increment our loop iterator
if (vectorIndex < (variousVectors_size - 1)) {
vectorIndex++;
}
else {
vectorIndex = 0;
std::cout << std::endl;
}
}
std::cout << std::endl;
}

int main()
Expand Down
22 changes: 21 additions & 1 deletion examples/linux/relative_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ PinnacleTouchI2C trackpad(DR_PIN);
// an object to hold data reported by the Cirque trackpad
RelativeReport data;

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

void interruptHandler()
{
isDataReady = true;
}
#endif // !defined(USE_SW_DR)

bool setup()
{
if (!trackpad.begin()) {
Expand All @@ -33,8 +43,13 @@ bool setup()
}
std::cout << "CirquePinnacle/examples/linux/relative_mode\n"
<< std::endl;
#ifndef USE_SW_DR // if using PINNACLE_SW_DR
#ifndef USE_SW_DR // if not using PINNACLE_SW_DR
std::cout << "-- Using HW DataReady pin." << std::endl;

// pull in arduino-like namespace
namespace arduino = cirque_pinnacle_arduino_wrappers;
// setup the interrupt handler
arduino::attachInterrupt(DR_PIN, arduino::FALLING, &interruptHandler);
#endif
#ifndef USE_I2C
std::cout << "-- Using SPI interface." << std::endl;
Expand All @@ -47,7 +62,12 @@ bool setup()

void loop()
{
#ifdef USE_SW_DR
if (trackpad.available()) {
#else // using interruptHandler()
if (isDataReady) {
isDataReady = false; // reset our IRQ flag
#endif
trackpad.read(&data);
std::cout << "Left:" << (unsigned int)(data.buttons & 1)
<< " Right:" << (unsigned int)(data.buttons & 2)
Expand Down

0 comments on commit 492ddb8

Please sign in to comment.