Skip to content

Commit

Permalink
0.2.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Jul 7, 2020
1 parent 29d07fb commit 3939ed7
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

56 changes: 56 additions & 0 deletions examples/microSeconds/microSeconds.ino
Original file line number Diff line number Diff line change
@@ -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 --
47 changes: 47 additions & 0 deletions examples/milliSeconds/milliSeconds.ino
Original file line number Diff line number Diff line change
@@ -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 --
81 changes: 81 additions & 0 deletions examples/seconds/seconds.ino
Original file line number Diff line number Diff line change
@@ -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 --
13 changes: 13 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -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)

21 changes: 21 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -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": "*"
}
11 changes: 11 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name=timing
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
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=
57 changes: 57 additions & 0 deletions timing.h
Original file line number Diff line number Diff line change
@@ -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 --

0 comments on commit 3939ed7

Please sign in to comment.