2021-01-29 06:31:58 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: DEVRANDOM.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-10-31 14:50:00 -04:00
|
|
|
// VERSION: 0.2.1
|
2021-12-15 13:48:19 -05:00
|
|
|
// PURPOSE: Arduino library for a /dev/random stream - useful for testing
|
2021-01-29 06:31:58 -05:00
|
|
|
// URL: https://github.com/RobTillaart/DEVRANDOM
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
|
|
|
2022-10-31 14:50:00 -04:00
|
|
|
#define DEVRANDOM_LIB_VERSION (F("0.2.1"))
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2022-07-03 14:27:42 -04:00
|
|
|
#define DEVRANDOM_MODE_RANDOM 0
|
|
|
|
#define DEVRANDOM_MODE_DIGITALREAD 1
|
|
|
|
#define DEVRANDOM_MODE_ANALOGREAD 2
|
|
|
|
#define DEVRANDOM_MODE_MARSAGLIA 3
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DEVRANDOM : public Stream
|
|
|
|
{
|
|
|
|
public:
|
2022-07-03 14:27:42 -04:00
|
|
|
DEVRANDOM();
|
|
|
|
DEVRANDOM(const char * str);
|
|
|
|
DEVRANDOM(const uint32_t value);
|
|
|
|
DEVRANDOM(const float value);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-07-03 14:27:42 -04:00
|
|
|
int available();
|
|
|
|
int peek();
|
|
|
|
int read();
|
2022-10-31 14:50:00 -04:00
|
|
|
|
|
|
|
// keep CI happy as parent class flush is virtual.
|
2022-07-03 14:27:42 -04:00
|
|
|
void flush();
|
2022-10-31 14:50:00 -04:00
|
|
|
|
|
|
|
// for reseeding, including via print() and println().
|
2022-07-03 14:27:42 -04:00
|
|
|
size_t write(const uint8_t data);
|
|
|
|
size_t write(const uint8_t * buffer, size_t size);
|
2021-12-15 13:48:19 -05:00
|
|
|
|
2022-07-03 14:27:42 -04:00
|
|
|
// build in random is the default mode.
|
|
|
|
void useRandom();
|
|
|
|
void useDigitalRead(uint8_t pin);
|
|
|
|
void useAnalogRead(uint8_t pin);
|
|
|
|
void useMarsaglia();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-07-03 14:27:42 -04:00
|
|
|
uint8_t getMode();
|
2021-12-15 13:48:19 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-10-31 14:50:00 -04:00
|
|
|
// OBSOLETE
|
|
|
|
// void useAR(uint8_t pin) { useAnalogRead(pin); };
|
|
|
|
// void useHW(uint8_t pin) { useDigitalRead(pin); };
|
|
|
|
// void useSW() { useRandom(); };
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
2022-07-03 14:27:42 -04:00
|
|
|
uint8_t _next = 0;
|
|
|
|
uint32_t _seed = 0;
|
|
|
|
uint8_t _mode = 0;
|
|
|
|
uint8_t _pin = 0;
|
2022-10-31 14:50:00 -04:00
|
|
|
|
|
|
|
// Marsaglia 'constants'
|
2022-07-03 14:27:42 -04:00
|
|
|
uint32_t _m_w = 1;
|
|
|
|
uint32_t _m_z = 2;
|
|
|
|
|
|
|
|
int _rnd();
|
|
|
|
int _digitalRead();
|
|
|
|
int _analogRead();
|
|
|
|
uint32_t _marsaglia();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-07-03 14:27:42 -04:00
|
|
|
};
|
2021-12-15 13:48:19 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
// -- END OF FILE --
|
2021-12-15 13:48:19 -05:00
|
|
|
|