While I was hacking (carefully crafting) changes to how yield worked as per the other thread,

I came back to the code for T4.x where I would blindly enable yield to call off to the serialEventX function on every
Serial port you did a begin on, and then I would remove the call on the first time our default implementation was called as it served no useful function.

I did that because I did not know a way to find out if the sketch has provided their own serialEvent implementation.

Example sketches that do things like:
Code:
void serialEvent1() {
    while (Serial1.available()) Serial.write(Serial1.read());
}
But I think I figured out a way of detecting this...

What I just tried was to extract the default implementation for serialEvent1 out of HardwareSerial1.cpp and put it into a new file
serialEvent1.cpp.

Now in HardwareSerial.cpp I add the following:
Code:
//void serialEvent1() __attribute__((weak));
//void serialEvent1() {Serial1.disableSerialEvents(); }		// No use calling this so disable if called...
uint8_t serialEvent1_default __attribute__((weak)) PROGMEM = 0 ;
and in the new serialEvent1.cpp the file has:
Code:
#include <Arduino.h>
#include "HardwareSerial.h"
void serialEvent1() __attribute__((weak));
void serialEvent1() {Serial1.disableSerialEvents(); }		// No use calling this so disable if called...
uint8_t serialEvent1_default PROGMEM = 1;
So I tried adding to simple sketch:
Code:
  extern const uint8_t serialEvent1_default;
  Serial.printf("Default serialEvent1? %d\n", serialEvent1_default);
Which did not have a serialEvent1 and printed 1, when I add it it printed 0...

So I can create 9 new files like this Serial1-8 plus the USB 1, change my begin method to check for a flag like this and only setup to call the serialEvent if the users code actually makes use of it.

Does this make sense? Worth it?