Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion platforms/Zephyr/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ CONFIG_STM32_ENABLE_DEBUG_SLEEP_STOP=y
# Display drivers
CONFIG_DISPLAY=y

# Needed for random number generation primitives
CONFIG_ENTROPY_GENERATOR=y

# Needed for getting runtime memory stats
Expand Down
7 changes: 7 additions & 0 deletions src/Primitives/emulated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ def_prim(random_int, NoneToOneU32) {
return true;
}

def_prim(random_set_seed, oneToNoneI32) {
srand(arg0.uint32);
pop_args(1);
return true;
}

// call callback test function (temporary)
def_prim(test, oneToNoneU32) {
uint32_t fidx = arg0.uint32;
Expand Down Expand Up @@ -593,6 +599,7 @@ void install_primitives(Interpreter *interpreter) {
install_primitive(millis);
install_primitive(micros);
install_primitive(random_int);
install_primitive(random_set_seed);

install_primitive(print_int);
install_primitive(print_string);
Expand Down
15 changes: 13 additions & 2 deletions src/Primitives/zephyr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/kernel.h>
#include <zephyr/random/random.h>
#include <zephyr/sys/util_macro.h>

#if IS_ENABLED(CONFIG_WIFI)
Expand All @@ -32,6 +31,7 @@
#include <cstdio>
#include <cstring>
#include <optional>
#include <random>

#include "../Memory/mem.h"
#include "../Utils/macros.h"
Expand Down Expand Up @@ -176,8 +176,18 @@ def_prim(noTone, NoneToNoneU32) {

#endif

// Ideally, we would use rand and srand here but since ESP-IDF and Zephyr both
// define a function called "random" it causes a linker error.
static std::minstd_rand rng;

def_prim(random_int, NoneToOneU32) {
pushInt32(sys_rand32_get());
pushInt32(rng());
return true;
}

def_prim(random_set_seed, oneToNoneI32) {
rng.seed(arg0.uint32);
pop_args(1);
return true;
}

Expand Down Expand Up @@ -654,6 +664,7 @@ void install_primitives(Interpreter *interpreter) {
install_primitive(chip_digital_read);
install_primitive(millis);
install_primitive(random_int);
install_primitive(random_set_seed);
install_primitive(print_string);
install_primitive(print_int);
install_primitive(abort);
Expand Down
Loading