Skip to content

Instantly share code, notes, and snippets.

@yashi
Created September 19, 2023 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashi/e9a3955d83682eec279742dda398dd77 to your computer and use it in GitHub Desktop.
Save yashi/e9a3955d83682eec279742dda398dd77 to your computer and use it in GitHub Desktop.
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/zbus/zbus.h>
LOG_MODULE_REGISTER(app);
ZBUS_CHAN_DEFINE(acc_data_chan,
int,
NULL,
NULL, /* User data */
ZBUS_OBSERVERS(my_listener, my_subscriber),
ZBUS_MSG_INIT(42)
);
static void listener_cb(const struct zbus_channel *chan)
{
const int *msg = zbus_chan_const_msg(chan);
const char *name = k_thread_name_get(k_current_get());
//LOG_INF("From listener (%s) -> msg %d", name, *msg);
}
ZBUS_LISTENER_DEFINE(my_listener, listener_cb);
ZBUS_SUBSCRIBER_DEFINE(my_subscriber, 4);
static void subscriber_func(void)
{
const struct zbus_channel *chan;
const char *name = k_thread_name_get(k_current_get());
while (!zbus_sub_wait(&my_subscriber, &chan, K_FOREVER)) {
int val;
if (&acc_data_chan == chan) {
zbus_chan_read(&acc_data_chan, &val, K_MSEC(500));
//LOG_INF("From subscriber (%s) -> msg %d", name, val);
}
}
}
K_THREAD_DEFINE(subscriber, CONFIG_MAIN_STACK_SIZE, subscriber_func, NULL, NULL, NULL, 3, 0, 0);
int main(void)
{
int val = {100};
while (1) {
zbus_chan_pub(&acc_data_chan, &val, K_SECONDS(1));
val += 1;
k_sleep(K_SECONDS(1));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment