mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
0.1.3 DEVRANDOM
This commit is contained in:
parent
79ba7528d3
commit
962377b7ba
@ -2,6 +2,10 @@ compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
- leonardo
|
||||
- due
|
||||
- zero
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
- m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
@ -4,10 +4,14 @@ name: Arduino CI
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
arduino_ci:
|
||||
runTest:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: Arduino-CI/action@master
|
||||
# Arduino-CI/action@v0.1.1
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
- run: |
|
||||
gem install arduino_ci
|
||||
arduino_ci.rb
|
||||
|
@ -2,21 +2,22 @@
|
||||
//
|
||||
// FILE: DEVRANDOM.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.2
|
||||
// PURPOSE: Arduino library for a /dev/random stream - usefull for testing
|
||||
// VERSION: 0.1.3
|
||||
// PURPOSE: Arduino library for a /dev/random stream - useful for testing
|
||||
// URL: https://github.com/RobTillaart/DEVRANDOM
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2020-06-23 initial version
|
||||
// 0.1.1 2020-12-18 add arduino-ci + unit tests
|
||||
// 0.1.1 2020-12-18 add Arduino-CI + unit tests
|
||||
// + getMode() + flush()
|
||||
// 0.1.2 2021-01-15 add constructors with seed.
|
||||
// 0.1.3 2021-12-15 update library.json, license, minor edits
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define DEVRANDOM_LIB_VERSION (F("0.1.2"))
|
||||
#define DEVRANDOM_LIB_VERSION (F("0.1.3"))
|
||||
|
||||
|
||||
#define DEVRANDOM_MODE_SW 0
|
||||
@ -35,6 +36,7 @@ public:
|
||||
_pin = 0;
|
||||
};
|
||||
|
||||
|
||||
DEVRANDOM(const char * str)
|
||||
{
|
||||
this->print(str);
|
||||
@ -43,17 +45,19 @@ public:
|
||||
_pin = 0;
|
||||
};
|
||||
|
||||
DEVRANDOM(const uint32_t val)
|
||||
|
||||
DEVRANDOM(const uint32_t value)
|
||||
{
|
||||
this->print(val);
|
||||
this->print(value);
|
||||
_next = random(256);
|
||||
_mode = 0;
|
||||
_pin = 0;
|
||||
};
|
||||
|
||||
DEVRANDOM(const float val)
|
||||
|
||||
DEVRANDOM(const float value)
|
||||
{
|
||||
this->print(val, 6);
|
||||
this->print(value, 6);
|
||||
_next = random(256);
|
||||
_mode = 0;
|
||||
_pin = 0;
|
||||
@ -64,6 +68,8 @@ public:
|
||||
|
||||
|
||||
int peek() { return _next; };
|
||||
|
||||
|
||||
int read()
|
||||
{
|
||||
uint8_t x = _next;
|
||||
@ -110,29 +116,31 @@ private:
|
||||
|
||||
int _hardware()
|
||||
{
|
||||
uint8_t val = 0;
|
||||
uint8_t value = 0;
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
val <<= 1;
|
||||
// TODO register optimized read for speed? ==> Not portable
|
||||
if (digitalRead(_pin)) val++;
|
||||
value <<= 1;
|
||||
if (digitalRead(_pin)) value++;
|
||||
}
|
||||
return val ^ _seed;
|
||||
return value ^ _seed;
|
||||
}
|
||||
|
||||
|
||||
int _analog()
|
||||
{
|
||||
uint8_t val = 0;
|
||||
uint8_t value = 0;
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
val <<= 1;
|
||||
if (analogRead(_pin) & 1) val++;
|
||||
value <<= 1;
|
||||
if (analogRead(_pin) & 1) value++;
|
||||
}
|
||||
return val ^ _seed;
|
||||
return value ^ _seed;
|
||||
}
|
||||
};
|
||||
|
||||
// TODO alternative random nummer generator so all platforms behave same.
|
||||
|
||||
// TODO alternative random number generator so all platforms behave same.
|
||||
// Marsaglia ?
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020-2021 Rob Tillaart
|
||||
Copyright (c) 2020-2022 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -1,27 +1,31 @@
|
||||
|
||||
[![Arduino CI](https://github.com/RobTillaart/DEVRANDOM/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
[![Arduino-lint](https://github.com/RobTillaart/DEVRANDOM/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DEVRANDOM/actions/workflows/arduino-lint.yml)
|
||||
[![JSON check](https://github.com/RobTillaart/DEVRANDOM/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DEVRANDOM/actions/workflows/jsoncheck.yml)
|
||||
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DEVRANDOM/blob/master/LICENSE)
|
||||
[![GitHub release](https://img.shields.io/github/release/RobTillaart/DEVRANDOM.svg?maxAge=3600)](https://github.com/RobTillaart/DEVRANDOM/releases)
|
||||
|
||||
|
||||
# DEVRANDOM
|
||||
|
||||
Arduino library to wrap a random generator in a stream
|
||||
Arduino library to wrap a random generator in a stream.
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
The library implements a stream class that mimics the /dev/random
|
||||
device of a linux system. It can be used for testing with streams.
|
||||
device of a Linux system. It can be used for testing with streams.
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
### Constructor
|
||||
|
||||
- **DEVRANDOM()** Constructor
|
||||
- **DEVRANDOM(char \* str)** Constructor
|
||||
- **DEVRANDOM(uint32_t val)** Constructor
|
||||
- **DEVRANDOM(float val)** Constructor
|
||||
- **DEVRANDOM()** Constructor.
|
||||
- **DEVRANDOM(char \* str)** Constructor, seeding with a string of characters.
|
||||
- **DEVRANDOM(uint32_t value)** Constructor, seeding with a integer value.
|
||||
- **DEVRANDOM(float value)** Constructor, seeding with a float value.
|
||||
|
||||
|
||||
### Streeam interface
|
||||
|
||||
@ -41,14 +45,14 @@ does have some effect);
|
||||
- **useSW()** use a software random number generator. This is the default.
|
||||
By default the build-in random number generator is used.
|
||||
This can be replaced by a RNG of your choice.
|
||||
- **useHW(pin)** use digitalRead to read 8 bits from a defined pin.
|
||||
- **useHW(uint8_t pin)** use digitalRead to read 8 bits from a defined pin.
|
||||
One can build a hardware RNG that flips between 0 and 1 very rapidly and unpredictably.
|
||||
Connect this signal to the pin and it will be read and generate a random byte.
|
||||
The seed value from the write is used as an XOR byte.
|
||||
- **useAR(pin)** use the analogRead to read 8 bits
|
||||
This can be fed with an analog noise source.
|
||||
- **useAR(uint8_t pin)** use the analogRead to read 8 bits
|
||||
This can be fed with an analogue noise source.
|
||||
The seed value from the write is used as a XOR byte.
|
||||
- **getMode()** returns the source of randomness => 0=SW, 1=HW, 2=AR (see above).
|
||||
- **getMode()** returns the source of randomness => 0 = SW, 1 = HW, 2 = AR (see above).
|
||||
|
||||
As **write()** reseeds the RNG, printing to **DEVRANDOM** will also reseed the RNG.
|
||||
E.g. **dr.println("Hello world");** would reseed it too.
|
||||
@ -72,3 +76,9 @@ As shown in the example one can use fscanf to read larger datatypes,
|
||||
However float is not supported standard in the fscanf by UNO and strings (%s) generate garbage.
|
||||
So a password generator is a bit more difficult (and a good exercise).
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- improve documentation
|
||||
- add Marsaglia PRG
|
||||
-
|
||||
|
@ -7,6 +7,7 @@
|
||||
// (c) : MIT
|
||||
//
|
||||
|
||||
|
||||
#include "DEVRANDOM.h"
|
||||
|
||||
DEVRANDOM dr;
|
||||
@ -14,6 +15,7 @@ DEVRANDOM dr;
|
||||
uint32_t i = 0, start, stop;
|
||||
volatile int x;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -85,6 +87,7 @@ void setup()
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
i++;
|
||||
@ -97,4 +100,6 @@ void loop()
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -22,13 +22,15 @@ char pw[PASSWORD_SIZE + 1];
|
||||
|
||||
DEVRANDOM dr;
|
||||
|
||||
char c1[] = "QWERTYUIOPLMKJNBHGVCFDXSZA";
|
||||
char c2[] = "zaqwertyuioplmnbvcxsdfghjk";
|
||||
char c3[] = "1598746230";
|
||||
char c4[] = "!@#$%^&*()_+-={}|[]\\:;<>?,./";
|
||||
// PROGMEM ?
|
||||
const char c1[] = "QWERTYUIOPLMKJNBHGVCFDXSZA";
|
||||
const char c2[] = "zaqwertyuioplmnbvcxsdfghjk";
|
||||
const char c3[] = "1598746230";
|
||||
const char c4[] = "!@#$%^&*()_+-={}|[]\\:;<>?,./";
|
||||
|
||||
int mx = 0;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -40,6 +42,7 @@ void setup()
|
||||
dr.useSW(); // for proof of concept this is OK
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.print('\t');
|
||||
@ -59,6 +62,7 @@ void loop()
|
||||
delay(5);
|
||||
}
|
||||
|
||||
|
||||
int generatePassword()
|
||||
{
|
||||
int idx = 0;
|
||||
@ -99,4 +103,6 @@ int generatePassword()
|
||||
return count;
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -9,12 +9,14 @@
|
||||
|
||||
// USE WITH SERIAL PLOTTER
|
||||
|
||||
|
||||
#include "DEVRANDOM.h"
|
||||
|
||||
DEVRANDOM dr;
|
||||
|
||||
volatile int x, y, z;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -24,6 +26,7 @@ void setup()
|
||||
Serial.println("SOFTWARE\tDIGITAL\tANALOG");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
dr.useSW();
|
||||
@ -41,4 +44,6 @@ void loop()
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,16 +1,24 @@
|
||||
# Syntax Coloring Map For DEVRANDOM
|
||||
# Syntax Colouring Map For DEVRANDOM
|
||||
|
||||
# Datatypes (KEYWORD1)
|
||||
|
||||
# Data types (KEYWORD1)
|
||||
DEVRANDOM KEYWORD1
|
||||
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
useAR KEYWORD2
|
||||
useHW KEYWORD2
|
||||
useSW KEYWORD2
|
||||
getMode KEYWORD2
|
||||
|
||||
|
||||
# Instances (KEYWORD2)
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
#DEVRANDOM_LIB_VERSION LITERAL1
|
||||
|
||||
DEVRANDOM_MODE_SW LITERAL1
|
||||
DEVRANDOM_MODE_HW LITERAL1
|
||||
DEVRANDOM_MODE_AR LITERAL1
|
||||
|
||||
|
@ -15,8 +15,9 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/DEVRANDOM.git"
|
||||
},
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
"platforms": "*",
|
||||
"headers": "DEVRANDOM.h"
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=DEVRANDOM
|
||||
version=0.1.2
|
||||
version=0.1.3
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library to wrap a random generator in a stream
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "DEVRANDOM_LIB_VERSION: %s\n", (char *) DEVRANDOM_LIB_VERSION);
|
||||
}
|
||||
|
||||
|
||||
@ -37,10 +38,16 @@ unittest_teardown()
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constants)
|
||||
{
|
||||
assertEqual(0, DEVRANDOM_MODE_SW);
|
||||
assertEqual(1, DEVRANDOM_MODE_HW);
|
||||
assertEqual(2, DEVRANDOM_MODE_AR);
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constructor)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", DEVRANDOM_LIB_VERSION);
|
||||
|
||||
DEVRANDOM dr;
|
||||
|
||||
assertEqual(DEVRANDOM_MODE_SW, dr.getMode());
|
||||
|
Loading…
Reference in New Issue
Block a user