RISC-V/adc/main.cpp
2024-03-04 11:23:10 +01:00

50 lines
1.3 KiB
C++

#include "gpio.h"
#include "usartclass.h"
#include "print.h"
#include "adcclass.h"
#include "oneway.h"
//////////////////////////////////////
class Process : public OneWay {
GpioClass led;
UsartClass serial;
Print cout;
FIFO<uint16_t, 8> data;
int pass_cnt;
public:
explicit Process () noexcept : OneWay (),
led (GPIOD, 4), serial (115200u), cout (DEC), data(), pass_cnt (0) {
cout += serial;
}
unsigned Send (uint16_t * const ptr, const unsigned len) override {
unsigned sum = 0u;
for (unsigned n=0; n<len; n++) sum += ptr [n];
sum >>= 4;
data.Write (static_cast<uint16_t> (sum));
return len;
}
void pass () {
uint16_t avg;
if (!data.Read (avg)) return;
cout << "pass: " << pass_cnt << ", avg = " << static_cast<int>(avg) << EOL;
pass_cnt += 1;
const bool b = pass_cnt & 1;
led << b;
}
};
//////////////////////////////////////
/* Tohle DEMO je složitější. ADC má
* pevnou frekvenci vzorkování 1kHz,
* vzorky se průměrují v přerušení a
* vypisují (v jiném přerušení) na
* sériový port.
* */
//////////////////////////////////////
static AdcClass adc;
static Process out;
int main () {
adc.attach(out);
for (;;) {
out.pass();
}
return 0;
}