Skip to content

Commit 47d385f

Browse files
committed
Move auto random seeding to hardware specific implementation files
(This is expected to increase flash size, marginally, but the effect will mostly go away once implementation and usage are once again in the same compilation unit.)
1 parent dc2428f commit 47d385f

12 files changed

+143
-83
lines changed

MozziGuts_impl_AVR.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,49 @@ void stopMozzi() {
377377
// Timer1.isrCallback();
378378
// }
379379
//// END AUDIO OUTPUT code ///////
380+
381+
//// BEGIN Random seeding ////////
382+
#if defined (__AVR_ATmega644P__)
383+
384+
// a less fancy version for gizduino (__AVR_ATmega644P__) which doesn't know INTERNAL
385+
static long longRandom()
386+
{
387+
return ((long)analogRead(0)+63)*(analogRead(1)+97); // added offsets in case analogRead is 0
388+
}
389+
390+
#else
391+
392+
/*
393+
longRandom(), used as a seed generator, comes from:
394+
http://arduino.cc/forum/index.php/topic,38091.0.html
395+
// AUTHOR: Rob Tillaart
396+
// PURPOSE: Simple Random functions based upon unreliable internal temp sensor
397+
// VERSION: 0.1
398+
// DATE: 2011-05-01
399+
//
400+
// Released to the public domain, use at own risk
401+
//
402+
*/
403+
static long longRandom()
404+
{
405+
//analogReference(INTERNAL);
406+
unsigned long rv = 0;
407+
for (uint8_t i=0; i< 32; i++) rv |= ((analogRead(8)+1171) & 1L) << i; // added 1171 in case analogRead is 0
408+
return rv;
409+
}
410+
#endif
411+
412+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
413+
ADCSRA &= ~ (1 << ADIE); // adc Disable Interrupt, re-enable at end
414+
// this attempt at remembering analog_reference stops it working
415+
// maybe needs a delay after changing analog reference in longRandom (Arduino reference suggests this)
416+
// because the analog reads return 0
417+
//uint8_t analog_reference_orig = ADMUX&192; // analog_reference is high 2 bits of ADMUX, store this because longRandom sets it to internal
418+
*x = longRandom();
419+
*y = longRandom();
420+
*z = longRandom();
421+
//analogReference(analog_reference_orig); // change back to original
422+
ADCSRA |= (1 << ADIE); // adc re-Enable Interrupt
423+
}
424+
425+
//// END Random seeding ////////

MozziGuts_impl_ESP32.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,12 @@ void stopMozzi() {
161161
}
162162
//// END AUDIO OUTPUT code ///////
163163

164+
//// BEGIN Random seeding ////////
165+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
166+
*x = esp_random();
167+
*y = esp_random();
168+
*z = esp_random();
169+
}
170+
//// END Random seeding ////////
171+
164172
#undef ESP_SAMPLE_SIZE // only used inside this file

MozziGuts_impl_ESP8266.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,13 @@ void stopMozzi() {
136136
#endif
137137

138138
//// END AUDIO OUTPUT code ///////
139+
140+
//// BEGIN Random seeding ////////
141+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
142+
*x = RANDOM_REG32;
143+
// TODO: The XORs may not be needed, but for lack of documentation (that I could find), let's assume RANDOM_REG32
144+
// itself might not get updated on every read. NOTE: x, y, and z are initialized to non-zero, before this.
145+
*y = *y ^ RANDOM_REG32;
146+
*z = *z ^ RANDOM_REG32;
147+
}
148+
//// END Random seeding ////////

MozziGuts_impl_MBED.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,11 @@ void stopMozzi() {
221221
#endif
222222
////// END audio output code //////
223223

224+
//// BEGIN Random seeding ////////
225+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
226+
#warning Automatic random seedings is not implemented on this platform
227+
}
228+
//// END Random seeding ////////
229+
224230
#undef CHUNKSIZE
225231
#undef US_PER_AUDIO_TICK

MozziGuts_impl_RENESAS.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,9 @@ void stopMozzi() {
168168
#endif
169169
}
170170
//// END AUDIO OUTPUT code ///////
171+
172+
//// BEGIN Random seeding ////////
173+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
174+
#warning Automatic random seedings is not implemented on this platform
175+
}
176+
//// END Random seeding ////////

MozziGuts_impl_RP2040.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,5 +266,11 @@ void stopMozzi() {
266266
}
267267
////// END audio output code //////
268268

269+
//// BEGIN Random seeding ////////
270+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
271+
#warning Automatic random seedings is not implemented on this platform
272+
}
273+
//// END Random seeding ////////
274+
269275
#undef MOZZI_RP2040_BUFFERS
270276
#undef MOZZI_RP2040_BUFFER_SIZE

MozziGuts_impl_SAMD.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,9 @@ void stopMozzi() {
138138
interrupts();
139139
}
140140
//// END AUDIO OUTPUT code ///////
141+
142+
//// BEGIN Random seeding ////////
143+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
144+
#warning Automatic random seedings is not implemented on this platform
145+
}
146+
//// END Random seeding ////////

MozziGuts_impl_STM32.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,24 @@ void stopMozzi() {
138138
}
139139

140140
//// END AUDIO OUTPUT code ///////
141+
142+
//// BEGIN Random seeding ////////
143+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
144+
// Unfortunately the internal temp sensor on STM32s does _not_ appear to create a lot of noise.
145+
// Ironically, the calls to calibrate help induce some random noise. You're still fairly likely to produce two equal
146+
// random seeds in two subsequent runs, however.
147+
adc.enableInternalReading();
148+
union {
149+
float cf;
150+
uint32_t ci;
151+
} conv;
152+
conv.cf = adc.readTemp();
153+
*x=*x^conv.ci;
154+
adc.calibrate();
155+
conv.cf = adc.readTemp();
156+
*y=*y^conv.ci;
157+
adc.calibrate();
158+
conv.cf = adc.readTemp();
159+
*z=*z^conv.ci;
160+
}
161+
//// END Random seeding ////////

MozziGuts_impl_STM32duino.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,9 @@ void stopMozzi() {
171171
}
172172

173173
//// END AUDIO OUTPUT code ///////
174+
175+
//// BEGIN Random seeding ////////
176+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
177+
#warning Automatic random seedings is not implemented on this platform
178+
}
179+
//// END Random seeding ////////

MozziGuts_impl_TEENSY.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,9 @@ void stopMozzi() {
112112
interrupts();
113113
}
114114
//// END AUDIO OUTPUT code ///////
115+
116+
//// BEGIN Random seeding ////////
117+
void autoRandomSeeds(uint32_t *x, uint32_t *y, uint32_t *z) {
118+
#warning Automatic random seedings is not implemented on this platform
119+
}
120+
//// END Random seeding ////////

0 commit comments

Comments
 (0)