lib_xcore select on case channel input example Topic is solved

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
aclassifier
Respected Member
Posts: 510
Joined: Wed Apr 25, 2012 8:52 pm

lib_xcore select on case channel input example

Post by aclassifier »

I have found no example in the XMOS examples of this, like in XMOS XTC Tools User Guide, or the YouTube Technical Guide to C for xcore. Is there?

Since I haven't installed XTC yet, my guess is that the XC code:

Code: Select all

void dsp_task_y (
    chanend ch_logger,
    // Other params
    )
{
    task_y_ctx_t ctx; // Context
    // Init etc.
    while (1) {
        [[ordered]] // Excludes  [[combinable]]
        select {
            // Other channel or timer or or cases
            case ch_logger :> ctx.next_curve_logger_state : { 
                // Handle the select case channel event
            } break;
        // No common code here
    }
    // Unreachable
} // dsp_task_y
might look something like this in lib_xcore (excluding ordered for now):

Code: Select all

DECLARE_JOB(dsp_task_y, (chanend_t));
void dsp_task_y (chanend_t ch_logger) {
    SELECT_RES(
        CASE_THEN(ch_logger, on_logger_input))
        {
            on_logger_input: {
                // Handle logger input event
                continue;
            }
        }
    }
}
Can I make up my own tag like on_logger_input?

The reason I ask is that I downloaded the AI-based editor Cursor the other day, and gave it my present project. I zoomed in on some coincidental code and asked it to make lib_xcore code out of it.

Even if it was correctly able to tell me which task that sent on this channel (ie. the other side), on this one it gave me busy polling and even made up some lib_xcore calls.

I have documented this in An encounter with AI-powered Cursor text editor and XC. There you may see the full chat which I had with Claude-3.5-Sonnet (by reading in a graphics file).

I assume there will be corrections here in my attempt at lib_xcore. I hope it would be ok if I could use that more correct pseudo-code in the blog note. (Blog disclaimer)
--
Øyvind Teig
Trondheim (Norway)
https://www.teigfam.net/oyvind/home/
View Solution
User avatar
xhuw
Verified
Active Member
Posts: 39
Joined: Wed May 22, 2024 2:36 pm

Post by xhuw »

Hey Øyvind Teig,

I haven't tried compiling your example but your lib_xcore looks good to me.

Regarding `on_logger_input,` the answer to your question is yes! You can make up your own tag. After macro expansion, this is a goto label; nothing magic is going on. I found it very useful when understanding SELECT_RES to look at the preprocessor output so I could see how it works.

The two snippets provided are not the same as your XC is ordered. You would need to use `SELECT_RES_ORDERED` to get the same behaviour in C:

https://www.xmos.com/documentation/XM-0 ... ES_ORDERED

My experience with Claude/chatgpt/copilot is that it will struggle with writing xcore specific code as there isn't enough xc/lib_xcore on the web for it to have been trained on. I saw you mention Claude apologising for being wrong in your blog. In my experience the AI will apologise for being wrong even when it was right, if you tell it that it was wrong. The customer is always right I suppose ;)

Huw
XMOS Software Engineer

Image
User avatar
aclassifier
Respected Member
Posts: 510
Joined: Wed Apr 25, 2012 8:52 pm

Post by aclassifier »

Thanks a lot! I have updated in the note. Search for "xhuw".

About the user-defined tag. I mentioned in the note, we did somethink like this, see https://www.teigfam.net/oyvind/pub/CPA2009/paper.pdf Listing 4.

I was so happy to get a response on this, because, sitting alone with 13k lines of my own XC code, it's ok to think that there would be a place to ask when the time comes.
--
Øyvind Teig
Trondheim (Norway)
https://www.teigfam.net/oyvind/home/