2024-03-03 21:25:22 +01:00
|
|
|
#include "gpio.h"
|
|
|
|
#include "usartclass.h"
|
|
|
|
#include "print.h"
|
|
|
|
#include "adcclass.h"
|
2024-03-04 11:23:10 +01:00
|
|
|
#include "oneway.h"
|
2024-03-03 21:25:22 +01:00
|
|
|
//////////////////////////////////////
|
2024-03-04 11:23:10 +01:00
|
|
|
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.
|
|
|
|
* */
|
2024-03-03 21:25:22 +01:00
|
|
|
//////////////////////////////////////
|
|
|
|
static AdcClass adc;
|
2024-03-04 11:23:10 +01:00
|
|
|
static Process out;
|
2024-03-03 21:25:22 +01:00
|
|
|
int main () {
|
2024-03-04 11:23:10 +01:00
|
|
|
adc.attach(out);
|
2024-03-03 21:25:22 +01:00
|
|
|
for (;;) {
|
2024-03-04 11:23:10 +01:00
|
|
|
out.pass();
|
2024-03-03 21:25:22 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|