Skip to content

Commit 3f761af

Browse files
committed
analogWave
1 parent e845205 commit 3f761af

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

libraries/AnalogWave/analogWave.cpp

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include "analogWave.h"
2+
3+
void analogWave::init(pin_size_t pinNumber) {
4+
5+
buffer = nullptr;
6+
size = 0;
7+
index = 0;
8+
_amplitude = 1.1;
9+
10+
dac_bits = 0;
11+
12+
}
13+
14+
analogWave::analogWave(pin_size_t pinNumber) {
15+
;
16+
}
17+
18+
analogWave::analogWave(pin_size_t pinNumber, uint16_t *_buffer, uint32_t _size,
19+
uint32_t _offset) {
20+
21+
buffer = _buffer;
22+
size = _size;
23+
index = _offset;
24+
}
25+
26+
bool analogWave::freq(float freq_hz) {
27+
28+
freq_max = (dac_max_samp_rate / (double)size);
29+
if (freq_hz >= freq_max) {
30+
return false;
31+
}
32+
33+
float freq = freq_hz * size;
34+
float p = 1000000 / freq;
35+
36+
37+
38+
return true;
39+
}
40+
41+
bool analogWave::begin(float freq_hz) {
42+
43+
return true;
44+
}
45+
46+
template <typename T>
47+
void _update(uint16_t *src, T *dts, float _amp, uint32_t _off, uint32_t _size,
48+
uint8_t _res) {
49+
uint16_t src_index = _off;
50+
51+
for (uint16_t i = 0; i < _size; i++) {
52+
src_index = src_index % _size;
53+
uint16_t sample = *(src + src_index);
54+
src_index++;
55+
56+
if (_amp < 1) {
57+
sample = (uint16_t)((float)sample * _amp);
58+
}
59+
T value = (T)map(sample, 0, (1 << 16), 0, (1 << _res));
60+
*(dts + i) = value;
61+
}
62+
}
63+
64+
bool analogWave::update() {
65+
66+
uint8_t *local_samples8 = nullptr;
67+
uint16_t *local_samples16 = nullptr;
68+
69+
/*
70+
* calculate updated values on a new allocated buffer
71+
*/
72+
return true;
73+
}
74+
75+
uint32_t analogWave::num_of_sample() {
76+
return size;
77+
}
78+
79+
void analogWave::amplitude(float a) {
80+
if (a >= 0 && a <= 1) {
81+
_amplitude = a;
82+
}
83+
update();
84+
}
85+
86+
void analogWave::offset(uint32_t o) {
87+
if (o < size) {
88+
_offset = o;
89+
}
90+
update();
91+
}
92+
93+
void analogWave::_pre_sync() {
94+
;
95+
}
96+
97+
void analogWave::sync(analogWave &w) {
98+
;
99+
}
100+
101+
void analogWave::start() {
102+
;
103+
}
104+
105+
void analogWave::stop() {
106+
;
107+
}
108+
109+
110+
111+
void analogWave::sine(float freq_hz) {
112+
113+
;
114+
}
115+
116+
void analogWave::saw(float freq_hz) {
117+
;
118+
}
119+
120+
void analogWave::square(float freq_hz) {
121+
122+
;
123+
}

libraries/AnalogWave/analogWave.h

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#ifndef ARDUINO_ANALOG_WAVE
2+
#define ARDUINO_ANALOG_WAVE
3+
4+
#include "Arduino.h"
5+
6+
#define SAMPLES_FOR_PREDEFINED_WAVE 24
7+
#define WAVE_PRIORITY 1
8+
9+
class analogWave {
10+
private:
11+
FspTimer timer;
12+
volatile uint32_t pin;
13+
uint16_t *buffer;
14+
15+
uint8_t dac_bits;
16+
uint8_t *samples8;
17+
uint16_t *samples16;
18+
uint32_t size;
19+
volatile uint32_t index;
20+
volatile float _amplitude;
21+
volatile uint32_t _offset;
22+
23+
double freq_max;
24+
double dac_max_samp_rate;
25+
26+
27+
void init(pin_size_t p);
28+
/* update the output with the next sample value */
29+
bool update();
30+
31+
public:
32+
/* constructor 1 with only DAC pin (to be used when predefined waveform are used) */
33+
analogWave(pin_size_t pinNumber);
34+
/* constructor 2 with buffer to be used (sample in buffer are output ciclycally */
35+
analogWave(pin_size_t pinNumber, uint16_t *_buffer, uint32_t _size, uint32_t _offset);
36+
37+
/* begin the output of the waweform the frequency is the one the complete
38+
set on sample are output (all samples will be output in 1 period */
39+
bool begin(float freq_hz);
40+
/* updated the frequency used */
41+
bool freq(float freq_hz);
42+
/* make the current index in the buffer to "jump" of the specified quantity */
43+
void offset(uint32_t o);
44+
/* start the generation of sample */
45+
void start();
46+
/* stop the genration of sample */
47+
void stop();
48+
/* every sample is multiplied for this factor must be a value between 0 and 1 */
49+
void amplitude(float amplitude);
50+
/* get the number of sample used */
51+
uint32_t num_of_sample();
52+
53+
void _pre_sync();
54+
55+
void sync(analogWave &w);
56+
57+
void sine(float freq_hz);
58+
void square(float freq_hz);
59+
void saw(float freq_hz);
60+
};
61+
62+
#endif

0 commit comments

Comments
 (0)