Hello,
Could someone please clarify how the interrupts for this library work?
I am trying to recognize a button press in my vehicle, and create an interrupt that increments a counter every time a particular can message is received.
Code:
// New can object using can bus 0 on teensy.
FlexCAN_T4<CAN0, RX_SIZE_256, TX_SIZE_16> can1;
CAN_message_t canMsg;
CAN_message_t msgInt;
void initCanT4(void){
can1.begin();
can1.setBaudRate(100E3);
can1.setMaxMB(1);
can1.setMB(MB0, RX, STD);
//REJECT ANY CAN MESSAGE THAT IS NOT IN THE LIST
can1.setMBFilter(REJECT_ALL);
// MAILBOX 0 Acts as a hardware interrupt (For button presses ONLY)
can1.enableMBInterrupt(MB0, true);
//Every time there is a new message, sniff the packet
can1.onReceive(MB0,canInterruptSniff);
can1.setMBFilter(MB0, 0x1D6); // Hardware interrupt, steering wheel button
}
Code:
void canInterruptSniff(const CAN_message_t &msgInt) {
Serial.print("INTERRUPTED CAN1 ");
Serial.print("MB: "); Serial.print(msgInt.mb);
Serial.print(" ID: 0x"); Serial.print(msgInt.id, HEX );
Serial.print(" EXT: "); Serial.print(msgInt.flags.extended );
Serial.print(" LEN: "); Serial.print(msgInt.len);
Serial.print(" DATA: ");
for ( uint8_t i = 0; i < 8; i++ ) {
Serial.print(msgInt.buf[i]); Serial.print(" ");
}
Serial.print(" TS: "); Serial.println(msgInt.timestamp);
Serial.println("END READ INTERRUPT CAN MSG");
if (lastClickTime - millis() > 800) { //Essentially, this is to eliminate "bouncing of the button press"
parseCanInterruptedMessage(msgInt.id, msgInt.buf, msgInt.len);
}
}
Code:
unsigned volatile long lastClickTime = 0;
volatile int clickCount = 0;
static uint32_t STEERING_WHEEL_ID = 470; //1D6 HEX
void parseCanInterruptedMessage(uint32_t id, const uint8_t message[], uint8_t messageLength){
if (id == STEERING_WHEEL_ID){
Serial.println("STEERING WHEEL BUTTON!!!!!");
if (message[0] == 192 && message[1] == 13 && lastClickTime - millis() > 800){ // IF the particular voice button is pressed...
lastClickTime = millis(); // Reset button press timer
Serial.println("STEERING WHEEL BUTTON!!!!!****************PRESSED!");
clickCount += 1;
}
}
}
So I want to run the above two functions whenever a can bus message with ID 470 is received, no matter what the microcontroller is doing. In this case, the interrupt would increment a counter, and the teensy would be running a while loop checking for the number of clicks. The interrupt should increment the number of clicks. My current code does not trigger an interrupt to increment the counter though.
Code:
void checkNumClicks(void){
static unsigned long max_delay = 3000;
Serial.println("");
Serial.print("internal Fn click Counter: ");
Serial.println(clickCount);
while((millis() - lastClickTime < max_delay) && clickCount == 1){
if (clickCount >= 2){
//returnVal = 2;
clickCount = 0;
Serial.println("DOUBLECLICK************************");
lastClickTime = millis();
break;
}
}
if ((clickCount == 1) && (millis() - lastClickTime >= max_delay)){
clickCount = 0;
Serial.println("***********************SINGLECLICK");
advanceScreen();
lastClickTime = millis();
//returnVal = 1;
//lastClickTime = millis();
}
}
I am hoping there is a very simple solution I am overlooking. The code works perfectly with a traditional "pin rising" interrupt and a standard button.