Tempo Tapper  1.0.0
A simple library to implement a tempo tapper
tempo_tapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Patrick Pedersen
3 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13 
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  *
17  */
18 
38 #pragma once
39 
40 #include <stdint.h>
41 
42 #define S_TO_US 1000000
43 
44 #if defined(TT_TARGET_PLATFORM_POSIX)
45 
46 #include <sys/time.h>
47 typedef struct timeval tt_time_t;
48 
49 #elif defined(TT_TARGET_PLATFORM_ARDUINO)
50 
51 #include <Arduino.h>
52 typedef unsigned long tt_time_t;
53 
54 #else
55 
56 #error No target platform specified!
57 
58 #endif
59 
60 typedef float BPM_t; // Data type to store BPM values
61 
87 typedef struct tempo_tapper
88 {
89  tt_time_t prd_sum;
90  tt_time_t lst_t;
91  int taps;
92 } tempo_tapper;
93 
94 // Platform Specific
95 
107 void current_time(tt_time_t *time);
108 
117 void add_time(tt_time_t *a, tt_time_t *b, tt_time_t *res);
118 
127 void sub_time(tt_time_t *a, tt_time_t *b, tt_time_t *res);
128 
136 void reset_time(tt_time_t *time);
137 
146 unsigned long time_to_us(tt_time_t *time);
147 
148 // Common
149 
159 
170 unsigned long tt_period_us(tempo_tapper *tapper);
171 
182 void tt_tap(tempo_tapper *tapper);
183 
191 void tt_reset(tempo_tapper *tapper);
192 
201 BPM_t tt_bpm(tempo_tapper *tapper);
reset_time
void reset_time(tt_time_t *time)
Resets a time var to 0.
tt_period_us
unsigned long tt_period_us(tempo_tapper *tapper)
Returns the period of a tempo in microseconds.
Definition: tempo_tapper_common.cxx:37
add_time
void add_time(tt_time_t *a, tt_time_t *b, tt_time_t *res)
Adds two time values.
tt_tap
void tt_tap(tempo_tapper *tapper)
"Taps" the tempo tapper
Definition: tempo_tapper_common.cxx:46
tempo_tapper::prd_sum
tt_time_t prd_sum
Holds the sum of all measured/"tapped" periods.
Definition: tempo_tapper.h:89
tempo_tapper::lst_t
tt_time_t lst_t
Hold the clock time of the last tap.
Definition: tempo_tapper.h:90
tt_new
tempo_tapper * tt_new()
Creates a new tempo tapper instance.
Definition: tempo_tapper_common.cxx:61
tt_bpm
BPM_t tt_bpm(tempo_tapper *tapper)
Returns the tempo in BPM.
Definition: tempo_tapper_common.cxx:72
tt_reset
void tt_reset(tempo_tapper *tapper)
Resets the tempo tapper.
Definition: tempo_tapper_common.cxx:83
tempo_tapper::taps
int taps
Number of taps. The inital val is -1, meaning the 1st tap does not count.
Definition: tempo_tapper.h:91
tempo_tapper
struct tempo_tapper tempo_tapper
Tempo tapper struct.
sub_time
void sub_time(tt_time_t *a, tt_time_t *b, tt_time_t *res)
Subtracts two time values.
tempo_tapper
Tempo tapper struct.
Definition: tempo_tapper.h:87
time_to_us
unsigned long time_to_us(tt_time_t *time)
Returns a time value in microseconds.
current_time
void current_time(tt_time_t *time)
Receives the current clock time.