aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSelene ToyKeeper2018-06-23 02:10:12 -0600
committerSelene ToyKeeper2018-06-23 02:10:12 -0600
commite2f73d62df6e7b79483b571cd744701e233cf94a (patch)
tree9404bf0c3b815c333c06c73ec1f93c28a7a6ea57
parentDon't auto-shutoff indicator LED when entering standby mode. (diff)
downloadanduril-e2f73d62df6e7b79483b571cd744701e233cf94a.tar.gz
anduril-e2f73d62df6e7b79483b571cd744701e233cf94a.tar.bz2
anduril-e2f73d62df6e7b79483b571cd744701e233cf94a.zip
Moved pseudo_rand() into its own header, and made it gather entropy from ADC readings to improve randomness.
Adjusted candle mode to use lower bits instead of upper bits, because the lower bits are more random. (also, the lower-bit method is slightly smaller in ROM)
-rw-r--r--spaghetti-monster/anduril/anduril.c36
-rw-r--r--spaghetti-monster/anduril/anduril.txt3
-rw-r--r--spaghetti-monster/fsm-adc.c5
-rw-r--r--spaghetti-monster/fsm-random.c33
-rw-r--r--spaghetti-monster/fsm-random.h29
-rw-r--r--spaghetti-monster/spaghetti-monster.h2
6 files changed, 81 insertions, 27 deletions
diff --git a/spaghetti-monster/anduril/anduril.c b/spaghetti-monster/anduril/anduril.c
index ca8a138..2ee18e8 100644
--- a/spaghetti-monster/anduril/anduril.c
+++ b/spaghetti-monster/anduril/anduril.c
@@ -253,11 +253,6 @@ volatile uint8_t strobe_type = 4;
// bike mode config options
volatile uint8_t bike_flasher_brightness = MAX_1x7135;
-#ifdef USE_PSEUDO_RAND
-volatile uint8_t pseudo_rand_seed = 0;
-uint8_t pseudo_rand();
-#endif
-
#ifdef USE_CANDLE_MODE
uint8_t triangle_wave(uint8_t phase);
#endif
@@ -821,16 +816,16 @@ uint8_t strobe_state(EventPtr event, uint16_t arg) {
set_level(brightness);
// wave1: slow random LFO
- if ((arg & 1) == 0) candle_wave1 += pseudo_rand()&1;
+ if ((arg & 1) == 0) candle_wave1 += pseudo_rand() & 1;
// wave2: medium-speed erratic LFO
candle_wave2 += candle_wave2_speed;
// wave3: erratic fast wave
- candle_wave3 += pseudo_rand()%37;
+ candle_wave3 += pseudo_rand() % 37;
// S&H on wave2 frequency to make it more erratic
- if ((pseudo_rand()>>2) == 0)
- candle_wave2_speed = pseudo_rand()%13;
+ if ((pseudo_rand() & 0b00111111) == 0)
+ candle_wave2_speed = pseudo_rand() % 13;
// downward sawtooth on wave2 depth to simulate stabilizing
- if ((candle_wave2_depth > 0) && ((pseudo_rand()>>2) == 0))
+ if ((candle_wave2_depth > 0) && ((pseudo_rand() & 0b00111111) == 0))
candle_wave2_depth --;
// random sawtooth retrigger
if ((pseudo_rand()) == 0) {
@@ -839,9 +834,9 @@ uint8_t strobe_state(EventPtr event, uint16_t arg) {
candle_wave2 = 0;
}
// downward sawtooth on wave3 depth to simulate stabilizing
- if ((candle_wave3_depth > 2) && ((pseudo_rand()>>3) == 0))
+ if ((candle_wave3_depth > 2) && ((pseudo_rand() & 0b00011111) == 0))
candle_wave3_depth --;
- if ((pseudo_rand()>>1) == 0)
+ if ((pseudo_rand() & 0b01111111) == 0)
candle_wave3_depth = 5;
}
#endif
@@ -1475,17 +1470,6 @@ void indicator_blink(uint8_t arg) {
#endif
-#ifdef USE_PSEUDO_RAND
-uint8_t pseudo_rand() {
- static uint16_t offset = 1024;
- // loop from 1024 to 4095
- offset = ((offset + 1) & 0x0fff) | 0x0400;
- pseudo_rand_seed += 0b01010101; // 85
- return pgm_read_byte(offset) + pseudo_rand_seed;
-}
-#endif
-
-
#ifdef USE_CANDLE_MODE
uint8_t triangle_wave(uint8_t phase) {
uint8_t result = phase << 1;
@@ -1688,7 +1672,7 @@ void loop() {
//rand_time = 1 << (pseudo_rand() % 7);
rand_time = pseudo_rand() & 63;
brightness = 1 << (pseudo_rand() % 7); // 1, 2, 4, 8, 16, 32, 64
- brightness += 1 << (pseudo_rand()&0x03); // 2 to 80 now
+ brightness += 1 << (pseudo_rand() & 0x03); // 2 to 80 now
brightness += pseudo_rand() % brightness; // 2 to 159 now (w/ low bias)
if (brightness > MAX_LEVEL) brightness = MAX_LEVEL;
set_level(brightness);
@@ -1717,8 +1701,8 @@ void loop() {
// turn the emitter off,
// for a random amount of time between 1ms and 8192ms
// (with a low bias)
- rand_time = 1<<(pseudo_rand()%13);
- rand_time += pseudo_rand()%rand_time;
+ rand_time = 1 << (pseudo_rand() % 13);
+ rand_time += pseudo_rand() % rand_time;
set_level(0);
nice_delay_ms(rand_time);
diff --git a/spaghetti-monster/anduril/anduril.txt b/spaghetti-monster/anduril/anduril.txt
index e74ce87..9cbef68 100644
--- a/spaghetti-monster/anduril/anduril.txt
+++ b/spaghetti-monster/anduril/anduril.txt
@@ -161,6 +161,7 @@ TODO:
* refactor to make config modes smaller
* move all config menus to four clicks
* candle mode timer, with three clicks to add 30 minutes
- - diagram updates for 3-click special actions
+ * diagram updates for 3-click special actions
- candle mode: smoother adjustments?
- make sunset mode timer and brightness configurable?
+ - make beacon mode actually sleep between pulses
diff --git a/spaghetti-monster/fsm-adc.c b/spaghetti-monster/fsm-adc.c
index 6e9f19b..2ec630c 100644
--- a/spaghetti-monster/fsm-adc.c
+++ b/spaghetti-monster/fsm-adc.c
@@ -109,6 +109,11 @@ ISR(ADC_vect) {
uint16_t measurement = ADC; // latest 10-bit ADC reading
+ #ifdef USE_PSEUDO_RAND
+ // real-world entropy makes this a true random, not pseudo
+ pseudo_rand_seed += measurement;
+ #endif
+
adc_step = (adc_step + 1) & (ADC_STEPS-1);
#ifdef USE_LVP
diff --git a/spaghetti-monster/fsm-random.c b/spaghetti-monster/fsm-random.c
new file mode 100644
index 0000000..1f83bce
--- /dev/null
+++ b/spaghetti-monster/fsm-random.c
@@ -0,0 +1,33 @@
+/*
+ * fsm-random.c: Random number generator for SpaghettiMonster.
+ *
+ * Copyright (C) 2017 Selene ToyKeeper
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef FSM_RANDOM_C
+#define FSM_RANDOM_C
+
+#ifdef USE_PSEUDO_RAND
+uint8_t pseudo_rand() {
+ static uint16_t offset = 1024;
+ // loop from 1024 to 4095
+ offset = ((offset + 1) & 0x0fff) | 0x0400;
+ pseudo_rand_seed += 0b01010101; // 85
+ return pgm_read_byte(offset) + pseudo_rand_seed;
+}
+#endif
+
+#endif
diff --git a/spaghetti-monster/fsm-random.h b/spaghetti-monster/fsm-random.h
new file mode 100644
index 0000000..720f6f2
--- /dev/null
+++ b/spaghetti-monster/fsm-random.h
@@ -0,0 +1,29 @@
+/*
+ * fsm-random.h: Random number generator for SpaghettiMonster.
+ *
+ * Copyright (C) 2017 Selene ToyKeeper
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef FSM_RANDOM_H
+#define FSM_RANDOM_H
+
+#ifdef USE_PSEUDO_RAND
+uint8_t pseudo_rand();
+// TODO: test without "volatile", in case it's not needed
+volatile uint8_t pseudo_rand_seed = 0;
+#endif
+
+#endif
diff --git a/spaghetti-monster/spaghetti-monster.h b/spaghetti-monster/spaghetti-monster.h
index 5599bca..08690b4 100644
--- a/spaghetti-monster/spaghetti-monster.h
+++ b/spaghetti-monster/spaghetti-monster.h
@@ -34,6 +34,7 @@
#include "fsm-pcint.h"
#include "fsm-standby.h"
#include "fsm-ramping.h"
+#include "fsm-random.h"
#ifdef USE_EEPROM
#include "fsm-eeprom.h"
#endif
@@ -71,6 +72,7 @@ void loop();
#include "fsm-pcint.c"
#include "fsm-standby.c"
#include "fsm-ramping.c"
+#include "fsm-random.c"
#ifdef USE_EEPROM
#include "fsm-eeprom.c"
#endif