Skip to content

Commit

Permalink
Move the remaining bits of mozzi_rand.cpp to other locations
Browse files Browse the repository at this point in the history
While at it, use explict precision types.
  • Loading branch information
tfry-git committed Jan 4, 2024
1 parent 2fa17a4 commit 49af0d5
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 224 deletions.
6 changes: 5 additions & 1 deletion MozziGuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "CircularBuffer.h"
#include "MozziGuts.h"
#include "mozzi_analog.h"
//#include "mozzi_utils.h"
#include "internal/mozzi_rand_p.h"
#include "AudioOutput.h"

// Forward declarations of functions to be provided by platform specific implementations
Expand Down Expand Up @@ -274,6 +274,10 @@ void startMozzi(int control_rate_hz) {
startAudio();
}

uint32_t MozziRandPrivate::x=132456789;
uint32_t MozziRandPrivate::y=362436069;
uint32_t MozziRandPrivate::z=521288629;

////// END initialization ///////

// reduce Macro leakage
Expand Down
8 changes: 4 additions & 4 deletions MozziGuts_impl_AVR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,15 @@ static long longRandom()
}
#endif

void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
void MozziRandPrivate::autoSeed() {
ADCSRA &= ~ (1 << ADIE); // adc Disable Interrupt, re-enable at end
// this attempt at remembering analog_reference stops it working
// maybe needs a delay after changing analog reference in longRandom (Arduino reference suggests this)
// because the analog reads return 0
//uint8_t analog_reference_orig = ADMUX&192; // analog_reference is high 2 bits of ADMUX, store this because longRandom sets it to internal
*x = longRandom();
*y = longRandom();
*z = longRandom();
x = longRandom();
y = longRandom();
z = longRandom();
//analogReference(analog_reference_orig); // change back to original
ADCSRA |= (1 << ADIE); // adc re-Enable Interrupt
}
Expand Down
8 changes: 4 additions & 4 deletions MozziGuts_impl_ESP32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ void stopMozzi() {
//// END AUDIO OUTPUT code ///////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
*x = esp_random();
*y = esp_random();
*z = esp_random();
void MozziRandPrivate::autoSeed() {
x = esp_random();
y = esp_random();
z = esp_random();
}
//// END Random seeding ////////

Expand Down
8 changes: 4 additions & 4 deletions MozziGuts_impl_ESP8266.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ void stopMozzi() {

//// BEGIN Random seeding ////////
#include <esp8266_peri.h>
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
*x = RANDOM_REG32;
void MozziRandPrivate::autoSeed() {
x = RANDOM_REG32;
// TODO: The XORs may not be needed, but for lack of documentation (that I could find), let's assume RANDOM_REG32
// itself might not get updated on every read. NOTE: x, y, and z are initialized to non-zero, before this.
*y = *y ^ RANDOM_REG32;
*z = *z ^ RANDOM_REG32;
y = y ^ RANDOM_REG32;
z = z ^ RANDOM_REG32;
}
//// END Random seeding ////////
2 changes: 1 addition & 1 deletion MozziGuts_impl_MBED.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void stopMozzi() {
////// END audio output code //////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
void MozziRandPrivate::autoSeed() {
#warning Automatic random seedings is not implemented on this platform
}
//// END Random seeding ////////
Expand Down
2 changes: 1 addition & 1 deletion MozziGuts_impl_RENESAS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void stopMozzi() {
//// END AUDIO OUTPUT code ///////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
void MozziRandPrivate::autoSeed() {
#warning Automatic random seedings is not implemented on this platform
}
//// END Random seeding ////////
2 changes: 1 addition & 1 deletion MozziGuts_impl_RP2040.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void stopMozzi() {
////// END audio output code //////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
void MozziRandPrivate::autoSeed() {
#warning Automatic random seedings is not implemented on this platform
}
//// END Random seeding ////////
Expand Down
2 changes: 1 addition & 1 deletion MozziGuts_impl_SAMD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void stopMozzi() {
//// END AUDIO OUTPUT code ///////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
void MozziRandPrivate::autoSeed() {
#warning Automatic random seedings is not implemented on this platform
}
//// END Random seeding ////////
8 changes: 4 additions & 4 deletions MozziGuts_impl_STM32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void stopMozzi() {
//// END AUDIO OUTPUT code ///////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
void MozziRandPrivate::autoSeed() {
// Unfortunately the internal temp sensor on STM32s does _not_ appear to create a lot of noise.
// Ironically, the calls to calibrate help induce some random noise. You're still fairly likely to produce two equal
// random seeds in two subsequent runs, however.
Expand All @@ -150,12 +150,12 @@ void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
uint32_t ci;
} conv;
conv.cf = adc.readTemp();
*x=*x^conv.ci;
x=x^conv.ci;
adc.calibrate();
conv.cf = adc.readTemp();
*y=*y^conv.ci;
y=y^conv.ci;
adc.calibrate();
conv.cf = adc.readTemp();
*z=*z^conv.ci;
z=z^conv.ci;
}
//// END Random seeding ////////
2 changes: 1 addition & 1 deletion MozziGuts_impl_STM32duino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void stopMozzi() {
//// END AUDIO OUTPUT code ///////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
void MozziRandPrivate::autoSeed() {
#warning Automatic random seedings is not implemented on this platform
}
//// END Random seeding ////////
2 changes: 1 addition & 1 deletion MozziGuts_impl_TEENSY.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void stopMozzi() {
//// END AUDIO OUTPUT code ///////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
void MozziRandPrivate::autoSeed() {
#warning Automatic random seedings is not implemented on this platform
}
//// END Random seeding ////////
4 changes: 2 additions & 2 deletions MozziGuts_impl_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ void stopMozzi() {
////// END audio output code //////

//// BEGIN Random seeding ////////
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
// Add here code to initialize the values pointed to by x, y, and z to some random values
void MozziRandPrivate::autoSeed() {
// Add here code to initialize the values of MozziRandPrivate::x, MozziRandPrivate::y, and MozziRandPrivate::z to some random values
// This doesn't need to be crypographically safe. If nothing better is available, e.g. try reading an internal temperature sensor
// in order to get some noise. It also doesn't have to be fast.
// It *should* however ensure that rand() sequences will differ across reboots, after randSeed() has been called.
Expand Down
181 changes: 0 additions & 181 deletions mozzi_rand.cpp

This file was deleted.

Loading

0 comments on commit 49af0d5

Please sign in to comment.