Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pico: use pico_rand for random API #811

Merged
merged 1 commit into from
Mar 6, 2023
Merged
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
2 changes: 1 addition & 1 deletion 32blit-pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ target_sources(BlitHalPico INTERFACE
${CMAKE_CURRENT_LIST_DIR}/usb_descriptors.c
)

target_link_libraries(BlitHalPico INTERFACE hardware_dma hardware_pio hardware_pwm hardware_spi pico_multicore pico_stdlib pico_unique_id tinyusb_device)
target_link_libraries(BlitHalPico INTERFACE hardware_dma hardware_pio hardware_pwm hardware_spi pico_multicore pico_stdlib pico_unique_id pico_rand tinyusb_device)
target_include_directories(BlitHalPico INTERFACE
${CMAKE_CURRENT_LIST_DIR} # for tusb_config
${CMAKE_CURRENT_LIST_DIR}/../3rd-party/fatfs
Expand Down
22 changes: 3 additions & 19 deletions 32blit-pico/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <stdio.h>
#include <random>
#include <cstdio>

#include "hardware/structs/rosc.h"
#include "hardware/vreg.h"
#include "pico/binary_info.h"
#include "pico/multicore.h"
#include "pico/rand.h"
#include "pico/stdlib.h"

#include "audio.hpp"
Expand All @@ -26,24 +26,8 @@ static uint32_t now() {
return to_ms_since_boot(get_absolute_time());
}

static uint32_t get_random_seed() {
uint32_t seed = 0;

// use the hardware random bit to seed
for(int i = 0; i < 32; i++) {
seed <<= 1;
seed |= rosc_hw->randombit & 1;
sleep_us(1); // don't read too fast
}

return seed;
}

static uint32_t random() {
static std::mt19937 random_generator(get_random_seed());
static std::uniform_int_distribution<uint32_t> random_distribution;

return random_distribution(random_generator);
return get_rand_32();
}

static void debug(const char *message) {
Expand Down