RISC-V/V203/hello/generator.cpp

33 lines
997 B
C++
Raw Normal View History

2024-08-17 14:27:07 +02:00
#include "generator.h"
2024-08-21 14:03:50 +02:00
#include "pwmclass.h"
2024-08-17 14:27:07 +02:00
#include "utils.h"
static constexpr unsigned W_TB = 8u;
2024-08-21 14:03:50 +02:00
static constexpr double AMPL = MAXPWM >> 1;
2024-08-17 14:27:07 +02:00
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);
extern void print_sinus_table (const TABLE<uint16_t, ULEN> & tab);
Generator::Generator (const unsigned f) noexcept : OneWay(),
freq (f), base(0u), incr (0u), ms_count (0u) {
#ifdef __linux__
print_sinus_table(sin_tab);
#endif
}
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;
}