Skip to content

Instantly share code, notes, and snippets.

@philippe86220
Last active March 11, 2026 13:33
Show Gist options
  • Select an option

  • Save philippe86220/f4a51e0d458ea632a26ff512086cfe93 to your computer and use it in GitHub Desktop.

Select an option

Save philippe86220/f4a51e0d458ea632a26ff512086cfe93 to your computer and use it in GitHub Desktop.
Multithread UNO-Q

Arduino_RouterBridge.h

1. About Bridge.provide()

In one of my diagnostic versions of the Python script I wrote:

ok = Bridge.provide("adc_data", on_adc)
print(f"Bridge.provide returned: {ok}", flush=True)

This was actually unnecessary on my part. Bridge.provide() simply registers the callback on the MPU side and does not return a meaningful value for the application logic. The assignment to ok was only a quick diagnostic check to see whether the framework exposed any status information.

In practice the correct usage is simply:

Bridge.provide("adc_data", on_adc)

So there is no real return value to consider on the MPU side for this function.


2. Difference between notify() and call()

My tests also clarified the behavioral difference between the two mechanisms.


Bridge.notify()

Bridge.notify("adc_data", adc);

This is a one-way event notification:

  • MCU → MPU
  • the Python callback is triggered
  • no value is expected back

So the MCU simply sends the information and continues execution.


Bridge.call()

auto r = Bridge.call("adc_data", adc);

This is a remote procedure call (RPC) between the MCU and the MPU.

Two pieces of information can be obtained on the MCU side.

First, the success status of the RPC transaction:

int pyValue;
bool ok = r.result(pyValue);

ok indicates whether the Bridge transaction itself completed successfully.

Second, if the Python callback explicitly returns a value:

def on_adc(value):
    print(f"ADC: {value:#x}", flush=True)
    return value

then the MCU can retrieve that value through pyValue.


Example output from my tests:

RPC success: 1 Python returned: 0x6A Sending data to MPU: 0x6A

This confirms that:

  • the MCU successfully called the Python callback
  • the callback executed on the MPU
  • the returned value was correctly transferred back to the MCU.

Summary

  • Bridge.notify() → asynchronous event, no return value expected
  • Bridge.call() → RPC mechanism, possible return value from Python

In your original example, notify() is actually the appropriate mechanism because the Python side only prints the ADC value and does not need to return anything.


For completeness, here is the same example adapted from your original program but using Bridge.call() so that the Python callback explicitly returns a value to the MCU. In your original example, notify() remains the correct choice since the Python side only prints the ADC value and does not need to return anything.


Console Serial Monitor:

RPC success: 1. Python returned: 0x6A. Sending data to MPU: 0x6A.

import time
import threading
from arduino.app_utils import *
LED1_R = "/sys/class/leds/red:user/brightness"
LED2_G = "/sys/class/leds/green:wlan/brightness"
print("Python is coming up", flush=True)
def on_adc(value):
print(f"ADC: {value:#x}", flush=True)
return value
Bridge.provide("adc_data", on_adc)
def set_led(led, value):
with open(led, "w") as f:
f.write(f"{value}\n")
def blink_led1():
while True:
set_led(LED1_R, 1)
time.sleep(0.5)
set_led(LED1_R, 0)
time.sleep(0.5)
def blink_led2():
while True:
set_led(LED2_G, 1)
time.sleep(4)
set_led(LED2_G, 0)
time.sleep(4)
t1 = threading.Thread(target=blink_led1, daemon=True)
t2 = threading.Thread(target=blink_led2, daemon=True)
t1.start()
t2.start()
print("Starting App.run()", flush=True)
App.run()
#include <Arduino_RouterBridge.h>
void setup()
{
Bridge.begin();
Monitor.begin();
delay(5000);
analogReadResolution(10);
}
void loop()
{
int adc = analogRead(A0);
auto r = Bridge.call("adc_data", adc);
int pyValue = -1;
bool ok = r.result(pyValue);
Monitor.print("RPC success: ");
Monitor.println(ok);
Monitor.print("Python returned: 0x");
Monitor.println(pyValue, HEX);
Monitor.print("Sending data to MPU: 0x");
Monitor.println(adc, HEX);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment