#include #include "generators.hpp" #include "filters.hpp" constexpr int DAC_PIN = A2; constexpr uint16_t SAMPLING_RATE_HZ = 8000; constexpr unsigned long SAMPLE_US = 1000000 / SAMPLING_RATE_HZ; #ifndef LED_BUILTIN #define LED_BUILTIN PC13 #endif unsigned long last_time_us = 0; int16_t val; bool sample_computed = false; constexpr uint32_t hz_to_micros(uint32_t hz) { return 1000000 / hz; } void setup() { delay(3000); } void loop() { unsigned long time_us = micros(); unsigned long delta = time_us - last_time_us; if (!sample_computed) { if (delta < SAMPLE_US) { constexpr uint32_t period = hz_to_micros(412); val = sine(time_us % period); sample_computed = true; } } else { if (delta >= SAMPLE_US) { last_time_us = time_us; analogWrite(DAC_PIN, val); sample_computed = false; } } delayMicroseconds(1); }