From 3939ed782dee1385d5a70c75eddef04fcfbeaae7 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Tue, 7 Jul 2020 18:06:21 +0200 Subject: [PATCH] 0.2.0 release --- LICENSE | 2 +- README.md | 44 ++++++++++++++ examples/microSeconds/microSeconds.ino | 56 ++++++++++++++++++ examples/milliSeconds/milliSeconds.ino | 47 +++++++++++++++ examples/seconds/seconds.ino | 81 ++++++++++++++++++++++++++ keywords.txt | 13 +++++ library.json | 21 +++++++ library.properties | 11 ++++ timing.h | 57 ++++++++++++++++++ 9 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 examples/microSeconds/microSeconds.ino create mode 100644 examples/milliSeconds/milliSeconds.ino create mode 100644 examples/seconds/seconds.ino create mode 100644 keywords.txt create mode 100644 library.json create mode 100644 library.properties create mode 100644 timing.h diff --git a/LICENSE b/LICENSE index 3d51ae3..95ba217 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Rob Tillaart +Copyright (c) 2011-2020 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 diff --git a/README.md b/README.md index 2de9639..a84848c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,46 @@ # timing + Arduino library with wrappers for seconds millis micros + +## Description + +A request often made on the Arduino forum and otherwise is an option to reset +the **millis()** function to 0 or another value. +This library provide wrapper classes around **millis()** and **micros()** +with the extra function to do this. +To implement this only a 4 byte offset is needed. + +These classes make it easy to make a simple stopwatch. + +## Interface + +The library has 3 classes that are very similar. +- microSeconds +- milliSeconds +- seconds (wrapper around millis() too) + +The interface of all three are very similar, so only one is described + +- **milliSeconds()** constructor, sets the offset so it starts at 0. +- **now()** returs the time elapsed since its 'zero moment', either construction +or by a set(0). +- **set(value)** sets the offset of the object. As it is possible to set it +to a non zero value it is easy to adjust the time. + +The classes are based upon **millis()** and **micros()** therefor have the same +restrictions as these functions with respect to overflow and accuracy. +Depending on e.g. interrupts millis and micros can drift. + +| class | overflow after | +|:----|:----|:----:| +|seconds | 49 days, 17:02:47.297 | +| milliSeconds | 49 days, 17:02:47.297 | +| microSeconds | 00 days 01:11:34.967296 | + +## Operation + +See examples + +## Todo +- test on ESP32 + diff --git a/examples/microSeconds/microSeconds.ino b/examples/microSeconds/microSeconds.ino new file mode 100644 index 0000000..5940113 --- /dev/null +++ b/examples/microSeconds/microSeconds.ino @@ -0,0 +1,56 @@ +// +// FILE: microSeconds.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2020 +// URL: https://github.com/RobTillaart/timing +// + +#include "timing.h" +#include "printHelpers.h" // https://github.com/RobTillaart/printHelpers + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + delay(10); + + microSeconds ms; // starts at zero + uint64_t big = fibonaci(91); // 91 is the max! + uint32_t x = micros(); + uint32_t y = ms.now(); + Serial.print("micros():\t"); + Serial.println(x); + Serial.print("ms.now(): \t"); + Serial.println(y); + Serial.print(" fib(91): \t"); + Serial.println(print64(big)); + + Serial.println("\nDone..."); +} + +void loop() +{ +} + +uint64_t fibonaci(uint32_t n) +{ + uint64_t p = 0; + uint64_t q = 1; + uint64_t t; + if (n == 0) return 0; + if (n == 1) return 1; + for (uint32_t i = 0; i < n; i++) + { + t = p; + p = q; + q += t; + // Serial.print(i); + // Serial.print("\t"); + // Serial.println(print64(q)); + } + return q; +} + +// -- END OF FILE -- diff --git a/examples/milliSeconds/milliSeconds.ino b/examples/milliSeconds/milliSeconds.ino new file mode 100644 index 0000000..348b52d --- /dev/null +++ b/examples/milliSeconds/milliSeconds.ino @@ -0,0 +1,47 @@ +// +// FILE: milliSeconds.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2020 +// URL: https://github.com/RobTillaart/timing +// + +#include "timing.h" + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + delay(10); + + milliSeconds mis; // starts at zero + for (uint32_t i = 0; i < 1000; i++) + { + Serial.print(i); + Serial.print("\t"); + Serial.println(i*i); + } + uint32_t x = mis.now(); + Serial.print("milliseconds:\t"); + Serial.println(x); + + mis.set(0); // starts at zero + for (uint32_t i = 0; i < 1000; i++) + { + Serial.print(i); + Serial.print("\t"); + Serial.println(i*i); + } + x = mis.now(); + Serial.print("milliseconds:\t"); + Serial.println(x); + + Serial.println("\nDone..."); +} + +void loop() +{ +} + +// -- END OF FILE -- diff --git a/examples/seconds/seconds.ino b/examples/seconds/seconds.ino new file mode 100644 index 0000000..cad42cc --- /dev/null +++ b/examples/seconds/seconds.ino @@ -0,0 +1,81 @@ +// +// FILE: seconds.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2020 +// URL: https://github.com/RobTillaart/timing +// + +#include "timing.h" + + +#if defined(ESP32) +#define MAXPRIMES 800 +const uint32_t mx = 1000000; +#else +#define MAXPRIMES 800 +const uint32_t mx = 15000; +#endif + + +uint16_t primes[MAXPRIMES]; +uint16_t idx = 0; +uint32_t x = 1; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.println("UNO (16Mhz) takes ~ 30 seconds..."); + Serial.println("ESP32 (240MHz) takes ~ 10 seconds..."); + + delay(10); + seconds sec; // starts at zero + while (x < mx) + { + nextPrime(); + } + Serial.print("Seconds:\t"); + Serial.println(sec.now()); + + delay(10); + x = 1; + sec.set(); // starts at zero + while (x < mx) + { + nextPrime(); + } + Serial.print("Seconds:\t"); + Serial.println(sec.now()); + + Serial.println("\nDone..."); +} + +void loop() +{ +} + +// sort of sieve. +int nextPrime() +{ + bool prime = true; + do + { + prime = true; + x++; + for (uint16_t i = 0; i < idx; i++) + { + if ( (x % primes[i]) == 0) + { + prime = false; + break; + } + } + } while (!prime); + if (idx < MAXPRIMES) primes[idx++] = x; + + return x; +} + +// -- END OF FILE -- \ No newline at end of file diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..c12cca7 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,13 @@ +# Syntax Coloring Map for timing + +# Datatypes (KEYWORD1) +microSeconds KEYWORD1 +milliSeconds KEYWORD1 +seconds KEYWORD1 + +# Methods and Functions (KEYWORD2) +now KEYWORD2 +set KEYWORD2 + +# Constants (LITERAL1) + diff --git a/library.json b/library.json new file mode 100644 index 0000000..ed462fa --- /dev/null +++ b/library.json @@ -0,0 +1,21 @@ +{ + "name": "timing", + "keywords": "seconds, milliSeconds, microSeconds, now, reset, zero", + "description": "Arduino library with wrapper classes for seconds millis micros. These wrappers allow to reset the value of the time.", + "authors": + [ + { + "name": "Rob Tillaart", + "email": "Rob.Tillaart@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/RobTillaart/timing" + }, + "version":"0.2.0", + "frameworks": "arduino", + "platforms": "*" +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..6f130e5 --- /dev/null +++ b/library.properties @@ -0,0 +1,11 @@ +name=timing +version=0.2.0 +author=Rob Tillaart +maintainer=Rob Tillaart +sentence=Arduino library with wrapper classes for seconds millis micros. +paragraph=These wrappers allow to reset the value of the time. +category=dataprocessing +url=https://github.com/RobTillaart/timing +architectures=* +includes=timing.h +depends= diff --git a/timing.h b/timing.h new file mode 100644 index 0000000..2e4768a --- /dev/null +++ b/timing.h @@ -0,0 +1,57 @@ +#pragma once +// +// FILE: timing.h +// AUTHOR: Rob Tillaart +// VERSION: 0.2.0 +// PURPOSE: Arduino library with wrapper classes for seconds millis micros +// URL: https://github.com/RobTillaart/timing +// +// HISTORY: +// 0.1.00 - 2011-01-04 initial version +// 0.1.01 - 2011-07-19 lib version +// 0.1.02 - 2015-03-02 move all to mymillis.h file so compiler can optimize +// 0.2.0 2020-07-07 renamed to timing.h + +// IDEAS +// char * toClock(); +// seconds.toClock() -> DD 12:34:56 +// milliSeconds.toClock() -> 12:23:45.123 +// microSeconds.toCLock() -> ??? + + +class microSeconds +{ +public: + microSeconds() { set(0); } + uint32_t now() { return micros() - _offset; } + void set(uint32_t val = 0UL) { _offset = micros() - val; } + +private: + uint32_t _offset = 0UL; +}; + + +class milliSeconds +{ +public: + milliSeconds() { set(0); }; + uint32_t now() { return millis() - _offset; }; + void set(uint32_t val = 0UL) { _offset = millis() - val; }; + +private: + uint32_t _offset = 0UL; +}; + + +class seconds +{ +public: + seconds() { set(0); } + uint32_t now() { return millis()/1000UL - _offset; } + void set(uint32_t val = 0UL) { _offset = millis()/1000UL - val; } + +private: + uint32_t _offset = 0UL; +}; + +// -- END OF FILE --