Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
arduino-esp32
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
arduino-esp32
Commits
1e3717bc
Unverified
Commit
1e3717bc
authored
Feb 15, 2023
by
Rodrigo Garcia
Committed by
GitHub
Feb 15, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds pseudo random numbers generation (#7848)
parent
8873adb1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
4 deletions
+26
-4
cores/esp32/Arduino.h
cores/esp32/Arduino.h
+11
-3
cores/esp32/WMath.cpp
cores/esp32/WMath.cpp
+15
-1
No files found.
cores/esp32/Arduino.h
View file @
1e3717bc
...
...
@@ -137,9 +137,19 @@ typedef unsigned int word;
void
setup
(
void
);
void
loop
(
void
);
// The default is using Real Hardware random number generator
// But when randomSeed() is called, it turns to Psedo random
// generator, exactly as done in Arduino mainstream
long
random
(
long
);
long
random
(
long
,
long
);
#endif
// Calling randomSeed() will make random()
// using pseudo random like in Arduino
void
randomSeed
(
unsigned
long
);
// Allow the Application to decide if the random generator
// will use Real Hardware random generation (true - default)
// or Pseudo random generation (false) as in Arduino MainStream
void
useRealRandomGenerator
(
bool
useRandomHW
);
#endif
long
map
(
long
,
long
,
long
,
long
,
long
);
#ifdef __cplusplus
...
...
@@ -207,8 +217,6 @@ void setToneChannel(uint8_t channel = 0);
void
tone
(
uint8_t
_pin
,
unsigned
int
frequency
,
unsigned
long
duration
=
0
);
void
noTone
(
uint8_t
_pin
);
// WMath prototypes
long
random
(
long
);
#endif
/* __cplusplus */
#include "pins_arduino.h"
...
...
cores/esp32/WMath.cpp
View file @
1e3717bc
...
...
@@ -29,10 +29,22 @@ extern "C" {
}
#include "esp32-hal-log.h"
// Allows the user to choose between Real Hardware
// or Software Pseudo random generators for the
// Arduino random() functions
static
bool
s_useRandomHW
=
true
;
void
useRealRandomGenerator
(
bool
useRandomHW
)
{
s_useRandomHW
=
useRandomHW
;
}
// Calling randomSeed() will force the
// Pseudo Random generator like in
// Arduino mainstream API
void
randomSeed
(
unsigned
long
seed
)
{
if
(
seed
!=
0
)
{
srand
(
seed
);
s_useRandomHW
=
false
;
}
}
...
...
@@ -46,7 +58,9 @@ long random( long howbig )
if
(
howbig
<
0
)
{
return
(
random
(
0
,
-
howbig
));
}
return
esp_random
()
%
howbig
;
// if randomSeed was called, fall back to software PRNG
uint32_t
val
=
(
s_useRandomHW
)
?
esp_random
()
:
rand
();
return
val
%
howbig
;
}
long
random
(
long
howsmall
,
long
howbig
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment