-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuncGenerator.cpp
More file actions
48 lines (42 loc) · 970 Bytes
/
funcGenerator.cpp
File metadata and controls
48 lines (42 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "funcGenerator.h"
// constructor
funcGenerator::funcGenerator(int t, float f, float a) {
type = t;
frequency = f;
amplitude = a;
}
// setters
void funcGenerator::setFrequency(float f){
this->frequency = f;
}
void funcGenerator::setAmplitude(float a){
this->amplitude = a;
}
void funcGenerator::setType(int t){
this->type = constrain(t,0,2);
}
// the generator's main task
float funcGenerator::compute() {
float result;
float phase = 2.0 * PI * this->frequency * (float)millis() / 1000.0; // goes from 0 to 2pi with specified frequency
switch (type) {
case 0: // sine
result = this->amplitude * sin(phase);
break;
case 1: // cosine
result = this->amplitude * cos(phase);
break;
case 2: // square wave
if (fmod(phase, 2*PI) < PI) {
result = this->amplitude;
}
else {
result = 0;
}
break;
default:
result = 0;
break;
}
return (result);
}