28 lines
886 B
C++
28 lines
886 B
C++
#include "generator.h"
|
|
#include "pwmclass.h"
|
|
#include "utils.h"
|
|
|
|
static constexpr unsigned W_TB = 8u;
|
|
static constexpr double AMPL = MAXPWM >> 1;
|
|
static constexpr int ULEN = 1 << W_TB;
|
|
|
|
static constexpr uint16_t u16_sin (const int x) {
|
|
const double a = (double (x) * D_PI) / double (ULEN);
|
|
const double s = AMPL * (1.0 + 0.96 * sincos (a, true));
|
|
return i_round (s);
|
|
}
|
|
static const TABLE<uint16_t, ULEN> sin_tab (u16_sin);
|
|
|
|
Generator::Generator (const unsigned f) noexcept : OneWay<uint16_t>(),
|
|
freq (f), base(0u), incr (0u), ms_count (0u) {
|
|
}
|
|
uint16_t Generator::step() {
|
|
const uint16_t v = sin_tab [base >> 24];
|
|
base += incr;
|
|
return v;
|
|
}
|
|
unsigned int Generator::Send(uint16_t * const ptr, const unsigned int len) {
|
|
for (unsigned n=0u; n<len; n++) ptr [n] = step();
|
|
if (ms_count) ms_count -= 1u; // průchod zde je za 1 ms přesně
|
|
return len;
|
|
}
|