Skip to main content

decode_event

Function decode_event 

Source
pub fn decode_event(payload: &[u8], layout: &Layout) -> Option<DeviceEvent>
Expand description

Decode one wire payload (the change-frame, not including transport frame) into a typed event. Returns None if the payload isn’t a recognized JUCE change-frame at all.

Examples found in repository?
examples/cli.rs (line 351)
326fn handle_body(
327    body: Vec<u8>,
328    layout: &Shared<Option<Layout>>,
329    root: &Shared<Option<Node>>,
330    watch: &AtomicBool,
331) {
332    // 0x02 = JUCE fullSync. Build a fresh Layout from the parsed tree so
333    // subsequent change-frames decode against an up-to-date address map.
334    if body.first() == Some(&0x02) {
335        if let Some(parsed_root) = parse_valuetree(&body) {
336            match Layout::from_full_sync(&parsed_root) {
337                Ok(new_layout) => {
338                    println!(
339                        "\n[layout] fullSync received: {} model, {} faders, {} sources, {} mixes",
340                        model_label(new_layout.model()),
341                        new_layout.fader_count(),
342                        new_layout.source_count(),
343                        new_layout.mix_count_per_source(),
344                    );
345                    *layout.lock().unwrap() = Some(new_layout);
346                    *root.lock().unwrap() = Some(parsed_root);
347                    if watch.load(Ordering::Relaxed) {
348                        // InitialState bursts can flood the terminal; print only
349                        // the count, not every property.
350                        if let Some(l) = layout.lock().unwrap().as_ref() {
351                            if let Some(DeviceEvent::InitialState(events)) = decode_event(&body, l)
352                            {
353                                println!("[initial-state] {} properties", events.len());
354                            }
355                        }
356                    }
357                    reprompt();
358                }
359                Err(e) => {
360                    println!("\n[layout] fullSync decode failed: {e:?}");
361                    reprompt();
362                }
363            }
364        }
365        return;
366    }
367
368    let guard = layout.lock().unwrap();
369    let Some(l) = guard.as_ref() else {
370        if watch.load(Ordering::Relaxed) {
371            println!("\n[event] (no layout yet, {} bytes)", body.len());
372            reprompt();
373        }
374        return;
375    };
376    if let Some(event) = decode_event(&body, l) {
377        if watch.load(Ordering::Relaxed) {
378            print_event(&event);
379        }
380    }
381}