2024-03-03 21:25:22 +01:00
|
|
|
#include "gpio.h"
|
2024-03-07 13:55:30 +01:00
|
|
|
#include "usart.h"
|
2024-03-03 21:25:22 +01:00
|
|
|
#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;
|
2024-03-07 13:55:30 +01:00
|
|
|
Usart serial;
|
2024-03-04 11:23:10 +01:00
|
|
|
Print cout;
|
|
|
|
FIFO<uint16_t, 8> data;
|
|
|
|
int pass_cnt;
|
2024-03-04 14:09:45 +01:00
|
|
|
unsigned average;
|
2024-03-04 11:23:10 +01:00
|
|
|
public:
|
|
|
|
explicit Process () noexcept : OneWay (),
|
2024-03-04 14:09:45 +01:00
|
|
|
led (GPIOD, 4), serial (115200u), cout (DEC), data(), pass_cnt (0), average (0u) {
|
2024-03-04 11:23:10 +01:00
|
|
|
cout += serial;
|
|
|
|
}
|
|
|
|
unsigned Send (uint16_t * const ptr, const unsigned len) override {
|
2024-03-04 14:09:45 +01:00
|
|
|
// Klouzavý průměr s exponenciálním zapomínáním lépe drží stálou hodnotu pro zobrazení.
|
|
|
|
// Průměr je rozšířen na 12-bit (0 - 4096) násobením hodnoty 4.
|
|
|
|
for (unsigned n=0; n<len; n++) average = (15u * average + 4u * ptr [n]) >> 4;
|
|
|
|
data.Write (static_cast<uint16_t> (average));
|
2024-03-04 11:23:10 +01:00
|
|
|
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á
|
2024-03-04 14:09:45 +01:00
|
|
|
* pevnou frekvenci vzorkování 10kHz,
|
2024-03-04 11:23:10 +01:00
|
|
|
* 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;
|
|
|
|
}
|