Hi Everyone!
I have two modes, Relative and Absolute that I need help switching . I want to use Serial read or some kind of button press to switch between these two modes. I wasn't sure how to structure the code in the proper way.
Do I use a single generic setup() and have a choice prompted using serial.read in loop() to agument the intial setup using a fucntion like setupRelative() or setupAbsolute()?
//Absolute mode
#include <CirquePinnacle.h>
#define SS_PIN 0
#define DR_PIN 1
PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
// If using I2C, then use the following line (not the line above)
// PinnacleTouchI2C trackpad(DR_PIN);
// an object to hold data reported by the Cirque trackpad
AbsoluteReport data;
void setup() {
Serial.begin(115200);
while (!Serial) {
// wait till Serial monitor is opened
}
if (!trackpad.begin()) {
Serial.println(F("Cirque Pinnacle not responding!"));
while (true) {
// hold program in infinite loop
}
}
Serial.println(F("CirquePinnacle/examples/absolute_mode"));
trackpad.setDataMode(PINNACLE_ABSOLUTE);
trackpad.absoluteModeConfig(1); // set count of z-idle packets to 1
Serial.println(F("Touch the trackpad to see the data."));
}
void loop() {
if (trackpad.available()) {
trackpad.read(&data);
Serial.print(F("Left:"));
Serial.print(data.buttons & 1);
Serial.print(F(" Right:"));
Serial.print(data.buttons & 2);
Serial.print(F(" Middle:"));
Serial.print(data.buttons & 4);
Serial.print(F("\tX:"));
Serial.print(data.x);
Serial.print(F("\tY:"));
Serial.println(data.y);
}
}
//Relative Mode
#include <CirquePinnacle.h>
#define SS_PIN 0
#define DR_PIN 1
PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
// If using I2C, then use the following line (not the line above)
// PinnacleTouchI2C trackpad(DR_PIN);
// an object to hold data reported by the Cirque trackpad
RelativeReport data;
void setup() {
Serial.begin(115200);
while (!Serial) {
// wait till Serial monitor is opened
}
if (!trackpad.begin()) {
Serial.println(F("Cirque Pinnacle not responding!"));
while (true) {
// hold program in infinite loop
}
}
Serial.println(F("CirquePinnacle/examples/relative_mode"));
trackpad.setDataMode(PINNACLE_RELATIVE);
trackpad.relativeModeConfig(); // uses default config
Serial.println(F("Touch the trackpad to see the data."));
}
void loop() {
if (trackpad.available()) {
trackpad.read(&data);
Serial.print(F("Left:"));
Serial.print(data.buttons & 1);
Serial.print(F(" Right:"));
Serial.print(data.buttons & 2);
Serial.print(F(" Middle:"));
Serial.print(data.buttons & 4);
Serial.print(F("\tX:"));
Serial.print(data.x);
Serial.print(F("\tY:"));
Serial.print(data.y);
Serial.print(F("\tScroll:"));
Serial.println(data.scroll);
}
}
This is my attempt. I get data in absolute mode but not when swtich to relative mode. Something about declaring
AbsoluteReport data; vs RelativeReport data;
I am not sure how to switch this before setup() but it appears I have to declare it from the beginning. I can declare it also in the void loop() but cant seem to swtich it from the serial read.
#include <CirquePinnacle.h>
#define SS_PIN 0
#define DR_PIN 1
PinnacleTouchSPI trackpad(DR_PIN, SS_PIN);
// If using I2C, then use the following line (not the line above)
// PinnacleTouchI2C trackpad(DR_PIN);
// an object to hold data reported by the Cirque trackpad
RelativeReport data;
void setup() {
Serial.begin(115200);
while (!Serial) {
// wait till Serial monitor is opened
}
if (!trackpad.begin()) {
Serial.println(F("Cirque Pinnacle not responding!"));
while (true) {
// hold program in infinite loop
}
}
}
void loop() {
while (Serial.available()) {
Serial.println(F("CirquePinnacle/examples/absolute_mode vs relative_mode"));
Serial.println(F("\n*** Enter 'R' for relatve"));
Serial.println(F("*** Enter 'A' for absolute.\n"));
char input = Serial.read();
if (input == 'r' || input == 'R') {
// an object to hold data reported by the Cirque trackpad
RelativeReport data;
Serial.println(F("CirquePinnacle/examples/relative_mode"));
trackpad.setDataMode(PINNACLE_RELATIVE);
trackpad.relativeModeConfig(); // uses default config
} else if (input == 'a' || input == 'A') {
// an object to hold data reported by the Cirque trackpad
AbsoluteReport data;
Serial.println(F("CirquePinnacle/examples/absolute_mode"));
trackpad.setDataMode(PINNACLE_ABSOLUTE);
trackpad.absoluteModeConfig(1); // set count of z-idle packets to 1
}
}
if (trackpad.available()) {
trackpad.read(&data);
Serial.print(F("Left:"));
Serial.print(data.buttons & 1);
Serial.print(F(" Right:"));
Serial.print(data.buttons & 2);
Serial.print(F(" Middle:"));
Serial.print(data.buttons & 4);
Serial.print(F("\tX:"));
Serial.print(data.x);
Serial.print(F("\tY:"));
Serial.println(data.y);
}
}