kupakod

z Klima, 1 dzień temu, napisane w C++, wyświetlone 6 razy. [paste_expire] 5 dni.
URL https://pastebin.k4be.pl/view/818a573b Udostępnij
Pobierz wklejkę lub Pokaż surowy tekst
  1. #include <Arduino.h>
  2. #include <hardware/watchdog.h>
  3. #include <muTimer.h>
  4. #include "pico/multicore.h"
  5. #include <Smoothed.h>
  6.  
  7.  
  8. #define SAMPLEPERIODUS 200UL   // Okres
  9.  
  10. muTimer Decay = muTimer();
  11. muTimer Decay2 = muTimer();
  12. bool decay_2_in;
  13. bool decay_2_out;
  14.  
  15. const unsigned long BPM_INTERVAL = 60000;  // 1 minuta
  16. unsigned long beatCount = 0;
  17. unsigned long lastBeatTime = 0;
  18. float bpm = 0;
  19. const int HISTORY_SIZE = 5;  // Liczba ostatnich uderzen do analizy
  20. unsigned long beatIntervals[HISTORY_SIZE] = {0};
  21. int beatIndex = 0;
  22. bool stable = false;  // Flaga czy sygnal ok
  23.  
  24. //beat led clk
  25. const int LED_PIN = 2;
  26. unsigned long beatInterval;  // Odstęp trig/trig w ms
  27. bool BPM_CLK_LED;
  28. muTimer LEDDecay = muTimer();
  29. muTimer LEDBPM = muTimer();
  30.  
  31.  
  32. //vu meter
  33.  
  34. const int NUM_BARS = 10;  // Liczba VU
  35. float peakLevel = 0;       // Przechowuje (peak hold)
  36. unsigned long peakHoldTime = 500;  // Podtrzymanie poziomu (ms)
  37. unsigned long lastPeakUpdate = 0;  // Ostatni peak hold
  38. unsigned long lastUpdateTime = 0;  // Ostatni czas VU
  39.  
  40.  
  41.  
  42. Smoothed <float> myBPM;
  43.  
  44.  
  45. muTimer co_250ms = muTimer();
  46. bool trig_co_250ms;
  47. muTimer co_100ms = muTimer();
  48. bool trig_co_100ms;
  49. muTimer co_1s = muTimer();
  50. bool trig_co_1s;
  51.  
  52. #define serial_debug
  53. #define RS232_RX 5
  54. #define RS232_TX 4
  55.  
  56. /*
  57. DEBUG PINY:
  58. DEBUG POM(SWDIO) / CZAR(GND) / ZOL(SCLK)
  59. RS232 CZAR(GND) / POM(TX) / ZOL(RX)
  60. */
  61.  
  62. //VARIABLES
  63. unsigned int running_counter;
  64. unsigned int running_counter_c1;
  65. bool beat_x ;
  66.  
  67.  
  68. ///
  69.  
  70. //
  71. // *20 - 200Hz Single Pole Bandpass IIR Filter
  72. //
  73. float bassFilter(float sample) {
  74.   static float xv[3] = {0.0f, 0.0f, 0.0f};
  75.   static float yv[3] = {0.0f, 0.0f, 0.0f};
  76.  
  77.   xv[0] = xv[1];
  78.   xv[1] = xv[2];
  79.   xv[2] = sample / 3.0f;  //* regulacja pod sile sygnalu
  80.  
  81.   yv[0] = yv[1];
  82.   yv[1] = yv[2];
  83.   yv[2] = (xv[2] - xv[0])
  84.          + (-0.7960060012f * yv[0])
  85.          + (1.7903124146f * yv[1]);
  86.   return yv[2];
  87. }
  88.  
  89. //
  90. //* 10Hz Single Pole Lowpass IIR Filter
  91. //
  92. float envelopeFilter(float sample) {
  93.   static float xv[2] = {0.0f, 0.0f};
  94.   static float yv[2] = {0.0f, 0.0f};
  95.  
  96.   xv[0] = xv[1];
  97.   xv[1] = sample / 50.0f;
  98.  
  99.   yv[0] = yv[1];
  100.   yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]);
  101.   return yv[1];
  102. }
  103.  
  104. //
  105. //* 1.7 - 3.0Hz Single Pole Bandpass IIR Filter
  106. //
  107. float beatFilter(float sample) {
  108.   static float xv[3] = {0.0f, 0.0f, 0.0f};
  109.   static float yv[3] = {0.0f, 0.0f, 0.0f};
  110.  
  111.   xv[0] = xv[1];
  112.   xv[1] = xv[2];
  113.   xv[2] = sample / 2.7f;
  114.  
  115.   yv[0] = yv[1];
  116.   yv[1] = yv[2];
  117.   yv[2] = (xv[2] - xv[0])
  118.          + (-0.7169861741f * yv[0])
  119.          + (1.4453653501f * yv[1]);
  120.   return yv[2];
  121. }
  122.  
  123.  
  124. ///
  125.  
  126.  
  127. // SETUP NA RDZENIU 1
  128. void setup1() {
  129.   #ifdef serial_debug
  130.   Serial2.println("1: Port Szeregowy Gotowy!");
  131.   #endif
  132.  
  133.   delay(1000);
  134. }
  135.  
  136. // PETLA LOOP NA RZENIU 1
  137. void loop1() {
  138.   delay(500);
  139.   running_counter_c1 = running_counter_c1 + 1;
  140. }
  141.  
  142.  
  143. ///funkcje
  144.  
  145. unsigned long getAverageInterval() {
  146.     unsigned long sum = 0;
  147.     for (int i = 0; i < HISTORY_SIZE; i++) {
  148.         sum += beatIntervals[i];
  149.     }
  150.     return sum / HISTORY_SIZE;
  151. }
  152.  
  153. bool isStable() {
  154.     unsigned long avg = getAverageInterval();
  155.     for (int i = 0; i < HISTORY_SIZE; i++) {
  156.         if (beatIntervals[i] == 0) return false;  // Nie mamy jeszcze....
  157.         if (abs((long)beatIntervals[i] - (long)avg) > 100) return false;  // Maksymalne odchylenie...
  158.     }
  159.     return true;
  160. }
  161.  
  162.  
  163.  
  164.  
  165. //SETUP -- SETUP -- SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP
  166. void setup() {
  167.    
  168.    //pin i/o
  169.    pinMode(LED_BUILTIN, OUTPUT);
  170.    pinMode(LED_PIN, OUTPUT);
  171.    
  172.    //adc res
  173.    analogReadResolution(12);
  174.  
  175.  
  176.   //Serial_DEBUG
  177.   #ifdef serial_debug
  178.   Serial2.setTX(RS232_TX);
  179.   Serial2.setRX(RS232_RX);
  180.   Serial2.begin(115200);
  181.   Serial2.println("0: Port Szeregowy Gotowy!");
  182.   #endif
  183.  
  184.   //timery
  185.   Decay.delayOffTrigger(1, 100);
  186.   LEDDecay.delayOffTrigger(1, 50);
  187.   co_100ms.cycleTrigger(100);
  188.   co_250ms.cycleTrigger(250);
  189.   co_1s.cycleTrigger(1000);
  190.  
  191.  
  192.   //filtry
  193.   myBPM.begin(SMOOTHED_AVERAGE, 50);
  194. //wdt
  195.   watchdog_enable(8000, false);
  196.  
  197.  
  198.  
  199. }//setup end.
  200. //SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP --SETUP -- SETUP
  201.  
  202.  
  203.  
  204. //LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP
  205. void loop() {
  206.  
  207.  ///
  208.   static unsigned int sampleCounter = 0;
  209.   static unsigned long lastSampleTime = 0;
  210.   unsigned long currentTime = micros();
  211.  
  212.   // Co: 5000Hz (co 200 µs)
  213.   if (currentTime - lastSampleTime < SAMPLEPERIODUS) return;
  214.   lastSampleTime = currentTime;
  215.  
  216.   // Odczyt A0
  217.   // Offset: dla 12-bit ADC (0-4095) ~ 2048
  218.   float sample = (float)analogRead(A0) - 2048.0f;
  219.  
  220.   // Basofiltr
  221.   float value = bassFilter(sample);
  222.   if (value < 0) value = -value;
  223.  
  224.   // Obwiednia
  225.   float envelope = envelopeFilter(value);
  226.  
  227.   sampleCounter++;
  228.   // Co 200  (~25Hz) wykonujemy filtr beat <-> ustawiamy stan LED
  229.   if (sampleCounter >= 200) {
  230.     float beat = beatFilter(envelope);
  231.  
  232.     // Odczyt A1 do ustawienia progu
  233.     float thresh = 0.02f * (float)analogRead(A1);
  234.  
  235.     // Jeżeli wykryty beat > prog, LED
  236.     bool beat_detect;
  237.     if (beat > thresh)
  238.        beat_detect = true;
  239.       //digitalWrite(LED_BUILTIN, HIGH);
  240.     else
  241.      //digitalWrite(LED_BUILTIN, LOW);
  242.      beat_detect = false;
  243.     sampleCounter = 0;
  244.  
  245.       beat_x = Decay.delayOff(beat_detect, 100);
  246.  
  247.   }
  248.  
  249.   digitalWrite(LED_BUILTIN, beat_x);
  250.  
  251.  ///
  252.    static bool lastState = false;
  253.    
  254.     if (beat_x && !lastState) {  // Wykrycie zbocza narastajacego
  255.         unsigned long currentTime = millis();
  256.        
  257.         if (lastBeatTime > 0) {
  258.             unsigned long interval = currentTime - lastBeatTime;
  259.             beatIntervals[beatIndex] = interval;
  260.             beatIndex = (beatIndex + 1) % HISTORY_SIZE;
  261.            
  262.             if (isStable()) {
  263.                 bpm = 60000.0 / getAverageInterval();
  264.                 stable = true;
  265.             } else {
  266.                 stable = false;
  267.             }
  268.         }
  269.        
  270.         lastBeatTime = currentTime;
  271.     }
  272.    
  273.     lastState = beat_x;
  274.  ///
  275.  
  276. ///
  277.  
  278.  
  279.    
  280. ///
  281.  beatInterval = 60000 / bpm;
  282.  
  283. //LEDDecay.cycleTrigger(beatInterval);
  284. if(beat_x == true){
  285. BPM_CLK_LED =  LEDBPM.delayOff(LEDDecay.cycleTrigger(beatInterval), 10);
  286. }
  287. if(beat_x == false){
  288. BPM_CLK_LED = false;
  289. }
  290.  
  291.  
  292.  
  293.  
  294.  if (beat_x == true and BPM_CLK_LED == true){
  295. decay_2_in = true;
  296.  }
  297.  else{
  298. decay_2_in = false;
  299.  }
  300.  
  301. decay_2_out =  Decay2.delayOff(decay_2_in, 100);
  302.  
  303. if(beat_x == false){
  304. decay_2_out = false;
  305. }
  306.  
  307. digitalWrite(LED_PIN, decay_2_out);
  308.  
  309. ///vu meter
  310.  
  311.  // Aktualizacja co 50ms bez delay()
  312.     if (currentTime - lastUpdateTime >= 50) {
  313.         lastUpdateTime = currentTime;
  314.  
  315.         // 1. Odczytaj
  316.         int rawValue = analogRead(A0);
  317.  
  318.         // 2. Mapuj
  319.         float level = map(rawValue, 2000, 2250, 0, NUM_BARS);
  320.  
  321.         // 3. Peak Hold
  322.         if (level > peakLevel) {
  323.             peakLevel = level;
  324.             lastPeakUpdate = currentTime;  // Zapisujemy czas aktualizacji
  325.         }
  326.  
  327.         // 4. Sprawdzam > czas peak hold
  328.         if (currentTime - lastPeakUpdate > peakHoldTime) {
  329.             peakLevel -= 0.2;  // Powolne opadanie peak hold
  330.             if (peakLevel < 0) peakLevel = 0; // Antyminus
  331.         }
  332.  
  333.         // 5. Wyświetlenie VU
  334.         Serial.print("VU: [");
  335.         for (int i = 0; i < NUM_BARS; i++) {
  336.             if (i < level) {
  337.                 Serial.print("#");  // Aktualny poziom
  338.             } else {
  339.                 Serial.print(" ");  // Puste miejsca
  340.             }
  341.         }
  342.  
  343.         // 6. Wyświetlenie Peak
  344.         if (peakLevel >= 1) {
  345.             int peakPos = min(NUM_BARS - 1, (int)peakLevel);
  346.             if (peakPos >= (int)level) {
  347.                 Serial.print(" *");  // Jak poza tunning
  348.             }
  349.         }
  350.  
  351.         Serial.println("]");  // Japierdole z tym....
  352.     }
  353.  
  354.  
  355.  
  356. //co 100ms
  357. if(co_100ms.cycleTrigger(100)){
  358.    trig_co_100ms = not(trig_co_100ms);
  359.   // digitalWrite(LED_BUILTIN, trig_co_100ms);
  360.  
  361.   myBPM.add(bpm);
  362. }//co 100ms end.
  363.  
  364. //co 1s
  365. if(co_1s.cycleTrigger(1000)){
  366.    trig_co_1s = not(trig_co_1s);
  367.    running_counter = running_counter + 1;
  368.  
  369.  
  370.   //SERIAL DEBUG SERIAL DEBUG SERIAL DEBUG SERIAL DEBUG
  371.   #ifdef serial_debug
  372.   //Serial2.print("0:running counter: ");
  373.   //Serial2.print(running_counter);
  374.   //Serial2.print("\n");
  375.   //Serial2.print("1:running counter: ");
  376.   //Serial2.print(running_counter_c1);
  377.   //Serial2.print("\n");
  378.  
  379.   Serial2.print("Treshold: ");
  380.   Serial2.print(analogRead(A1));
  381.   Serial2.print("\n");
  382.   Serial2.print("Signal: ");
  383.   Serial2.print(analogRead(A0));
  384.   Serial2.print("\n");
  385.   Serial2.print("BPM: ");
  386.   Serial2.print(myBPM.get());
  387.   Serial2.print(" STABLE: ");
  388.   Serial2.print(stable);
  389.   Serial2.print("\n");
  390.   Serial2.print("INTERVAL: ");
  391.   Serial2.print(beatInterval);
  392.   Serial2.print("\n");
  393.  
  394.   #endif
  395.  
  396.  
  397. }//co 1s end.
  398.  
  399. //WDT ! WDT ! WDT ! WDT ! WDT ! WDT ! WDT ! WDT !
  400. watchdog_update();
  401. }//loop end.
  402. //LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP - LOOP
  403.  
  404.  
  405.  

odpowiedź "kupakod"

Tutaj możesz odpowiedzieć na wklejkę z góry

captcha