Skip to content

Attach rx callback for HardwareSerial #2708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
marshfolx opened this issue Apr 8, 2025 · 1 comment
Open

Attach rx callback for HardwareSerial #2708

marshfolx opened this issue Apr 8, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@marshfolx
Copy link

marshfolx commented Apr 8, 2025

I'm using freertos, and I have a task listening serial messages. When the rx buffer is empty, I want the task to be blocked, or "sleep", and a rx callback will return the task to running state after several bytes have arrived. Otherwise the listening task will constantly occupies CPU time, leaves no process opportunity for other tasks, unless the priority of the task is low enough.

The callback function would be like:

void rx_callback(HardwareSerial *serial_instance) {
    if(serial_instance->available() > 5) {
        if( the_task_is_blocked_waiting_message ) {
            wake_up_the_task();
        }
    }
}

It would be more convenient to define the rx_callback function as:

void rx_callback(HardwareSerial *serial_instance, void * ptr_to_callback_object) {
    if(serial_instance->available() > 5) {
        if( the_task_is_blocked_waiting_message ) {
            auto *ptr = reinterpret_cast<CallbackObjectType*>(ptr_to_callback_obejct)
            ptr->wake_up_the_task();
        }
    }
}

The value of ptr_to_callback_object is stored in a member of HardwareSerial. This way, the callback function can directly get the handle to the task. And the code of callback function can be reused in different projects without modification.

@marshfolx marshfolx added the enhancement New feature or request label Apr 8, 2025
@fpistm
Copy link
Member

fpistm commented Apr 8, 2025

Hi @marshfolx
Feel free to provide a PR then it could be reviewed and discussed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants