Skip to content
This repository was archived by the owner on Aug 21, 2019. It is now read-only.

Commit 928a24c

Browse files
DAS_v0.4.2
added functions: - long random(long howbig) - long random(long howsmall, long howbig) - long long millis()
1 parent 6691fd8 commit 928a24c

1 file changed

Lines changed: 47 additions & 4 deletions

File tree

das.h

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#pragma once
22
#include "olcConsoleGameEngine.h"
3+
#include <random>
4+
#include <algorithm>
35

46
// Danilo's Arduino Simulation namespace
57

68
namespace das
79
{
10+
/**
11+
Swaps variables with XOR (withouth bonus variable)
12+
*/
813
void Swap(int &x, int &y) {
914
x ^= y;
1015
y ^= x;
@@ -62,7 +67,7 @@ namespace das
6267
to call its constructor with different parameters.
6368
6469
@author Danilo Novakoviæ
65-
@version 0.4.1 4/29/2018
70+
@version 0.4.2 5/2/2018
6671
*/
6772
class ArduinoSimulation_uC32 : private olcConsoleGameEngine
6873
{
@@ -97,6 +102,8 @@ namespace das
97102
condition_variable m_cvDeamonThreads[NUM_DEAMON_THREADS];
98103
condition_variable m_cvScheduler;
99104
mutex m_muxPin;
105+
106+
chrono::steady_clock::time_point m_tpSimulationStarted;
100107
private:
101108
/**
102109
@note: Set 'm_bLearningMode' to off if you don't wish to be thrown unecessary exceptions
@@ -132,6 +139,8 @@ namespace das
132139
m_readyThreads[i] = 0;
133140
}
134141

142+
m_tpSimulationStarted = chrono::steady_clock::now();
143+
135144
executeSoftReset(RUN_SKETCH_ON_BOOT);
136145
setup();
137146

@@ -454,10 +463,10 @@ namespace das
454463

455464
protected:
456465
/**
457-
@note: this version of delay can be used for debuging, although it is not supported by
458-
Arduino (it is custom made)
466+
@note: this version of delay can be used for debuging, although it is not supported by
467+
Arduino (it is custom made)
459468
*/
460-
void delay(unsigned int ms, const wchar_t* debug_msg)
469+
void delay(unsigned int ms, const wchar_t* debug_msg)
461470
{
462471
unique_lock<mutex> ulock(m_muxPin);
463472
int thread_id = m_activeThread;
@@ -477,6 +486,40 @@ namespace das
477486
--m_readyThreads[thread_id];
478487
}
479488
}
489+
public:
490+
/**
491+
Returns number less then 'howbig'
492+
*/
493+
long random(long howbig)
494+
{
495+
if (howbig > 0) {
496+
return random(0, howbig - 1);
497+
}
498+
return 0;
499+
}
500+
501+
/**
502+
Returns number from closed interval [howsmall, howbig]
503+
*/
504+
long random(long howsmall, long howbig)
505+
{
506+
if (howsmall > howbig) {
507+
std::swap(howsmall, howbig);
508+
}
509+
static default_random_engine generator((unsigned int) chrono::system_clock::now().time_since_epoch().count());
510+
uniform_int_distribution<long> distribution(howsmall, howbig);
511+
return distribution(generator);
512+
}
513+
514+
/**
515+
Returns number of milliseconds passed from the start of the simulation
516+
*/
517+
long long millis()
518+
{
519+
auto timePassed = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - m_tpSimulationStarted);
520+
return timePassed.count();
521+
}
522+
480523

481524
};
482525

0 commit comments

Comments
 (0)