Advertisement
Attilator

OLED_Needle_VUmeter_u8g2_STEREO_2_TypDisp_v1

Oct 12th, 2021
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  originál původně pro knihovnu adafruit, přepsáno na dispL pro Attila Csergo
  3.  *  bitmapa generovana nově pomocí online nástroje BMP to XBM
  4.  */
  5.  
  6. #include <U8g2lib.h>
  7. #include <SPI.h>
  8.  
  9. U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI  dispL(U8G2_R0,/*cs=*/ 10, /*dc=*/  9, /* reset=*/ 8);
  10. // propojeni je: clock=13, data=11, cs=10, dc=9, reset=8);
  11. U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI dispR(U8G2_R0, /* D0 clock=*/ 3, /* D1 data=*/ 4, /* cs=*/ 7, /* dc=*/ 6, /* reset=*/ 5);
  12.  
  13. int analogInputL = A0;                 // analog input for outside audio source Left channel
  14. int analogInputR = A1;                 // analog input for outside audio source Right channel
  15. int hMeter = 65;                      // horizontal center for needle animation
  16. int vMeter = 85;                      // vertical center for needle animation (outside of dislay limits)
  17. int rMeter = 80;                      // length of needle animation or arch of needle travel
  18.  
  19. const int sampleWindow = 20;          // sample window width in mS (50 mS = 20Hz)
  20. unsigned int sampleL, sampleR;
  21.  
  22. static const unsigned char PROGMEM VUMeter[] = {
  23.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  26.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30.   0x00, 0x00, 0x60, 0xC0, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  31.   0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x90, 0x20, 0x01, 0x84, 0x04, 0x00,
  32.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x19, 0x10, 0x60, 0xC0,
  33.   0x01, 0x84, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34.   0x25, 0x08, 0x90, 0x00, 0x01, 0x84, 0x04, 0xE0, 0x00, 0x00, 0x00, 0x00,
  35.   0x00, 0x00, 0x00, 0x00, 0x25, 0x08, 0x60, 0xC0, 0x00, 0x04, 0x03, 0x00,
  36.   0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8E, 0x01, 0x25, 0x08, 0x00, 0x00,
  37.   0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
  38.   0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x3C, 0x00, 0x00,
  39.   0x00, 0x00, 0x5C, 0x02, 0x00, 0x00, 0x40, 0x80, 0x00, 0x02, 0x01, 0xE0,
  40.   0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x42, 0x02, 0x00, 0x10, 0x40, 0x80,
  41.   0x10, 0x02, 0x01, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x9E, 0x01,
  42.   0x20, 0x10, 0x40, 0x80, 0x10, 0x81, 0x08, 0x00, 0x00, 0x20, 0x00, 0x00,
  43.   0x00, 0x00, 0x00, 0x00, 0x20, 0x10, 0x40, 0x80, 0x10, 0x81, 0x88, 0x20,
  44.   0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x40, 0x80,
  45.   0x10, 0x81, 0x84, 0x20, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x21,
  46.   0x40, 0x20, 0xF0, 0xFF, 0xFF, 0xC3, 0x47, 0x20, 0x00, 0x00, 0x10, 0x00,
  47.   0x00, 0x00, 0x00, 0x43, 0x80, 0xE0, 0x0F, 0x00, 0x00, 0xDC, 0x7F, 0x10,
  48.   0x02, 0x02, 0x10, 0x00, 0x00, 0x7F, 0x00, 0x46, 0x80, 0x1F, 0x00, 0x00,
  49.   0x00, 0xC0, 0xFF, 0x17, 0x02, 0x01, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x84,
  50.   0x78, 0x00, 0x20, 0x00, 0x01, 0x00, 0xFE, 0x7F, 0x01, 0x01, 0x10, 0x00,
  51.   0x00, 0x00, 0xC0, 0x8C, 0x07, 0x00, 0x20, 0x00, 0x01, 0x20, 0x80, 0xFF,
  52.   0x83, 0x00, 0x10, 0x00, 0x00, 0x00, 0xE0, 0x78, 0x00, 0x02, 0x00, 0x00,
  53.   0x00, 0x20, 0x00, 0xF8, 0x5F, 0x00, 0x10, 0x00, 0x00, 0x00, 0xE0, 0x0F,
  54.   0x00, 0x02, 0xDC, 0xE0, 0x06, 0x00, 0x00, 0x80, 0xFF, 0x00, 0x00, 0x00,
  55.   0x00, 0x00, 0xC0, 0x01, 0x00, 0x00, 0x2C, 0x81, 0x09, 0x33, 0x03, 0x00,
  56.   0xFC, 0x03, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0xC0, 0x0C, 0x30, 0x41,
  57.   0x09, 0xCA, 0x04, 0x00, 0xE0, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x02,
  58.   0x00, 0x13, 0xDC, 0x40, 0x06, 0xCA, 0x04, 0x00, 0x00, 0x7F, 0x00, 0x00,
  59.   0x00, 0x80, 0x01, 0x04, 0x80, 0x13, 0x00, 0x00, 0x00, 0x32, 0x03, 0x00,
  60.   0x00, 0xFC, 0x01, 0x00, 0x00, 0x60, 0x00, 0x00, 0xC0, 0x0C, 0x00, 0x00,
  61.   0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x07, 0x00, 0x00, 0x10, 0x00, 0x30,
  62.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00,
  63.   0x00, 0x0C, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64.   0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x02, 0x48, 0x00, 0x00, 0x00, 0x00,
  65.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x05, 0x30,
  66.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67.   0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x40, 0x40, 0x0C, 0x03, 0x00, 0x00,
  68.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xC0, 0x60,
  69.   0x0C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
  70.   0x00, 0x00, 0x80, 0x31, 0x0C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71.   0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x1B, 0x0C, 0x03, 0x00, 0x00,
  72.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x0E,
  73.   0x98, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
  74.   0x00, 0x00, 0x00, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  101.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  102.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108.   0x00, 0x00, 0x00, 0x00, };
  109.  
  110. void setup(void) {
  111.   Serial.begin(9600);
  112.   pinMode(analogInputL, INPUT);
  113.   pinMode(analogInputR, INPUT);
  114.   dispL.begin();
  115.   dispR.begin();
  116.   dispL.setBitmapMode(0);
  117.   dispR.setBitmapMode(0);
  118. }
  119.  
  120. void loop(void) {
  121.   unsigned long startMillis = millis();                    // start of sample window
  122.   unsigned int SignalMaxL = 0;
  123.   unsigned int SignalMaxR = 0;
  124.  
  125.   while ( millis() - startMillis < sampleWindow ){
  126.     sampleL = analogRead(analogInputL);
  127.     sampleR = analogRead(analogInputR);
  128.       if (sampleL > SignalMaxL) SignalMaxL = sampleL;   // saves just the max L levels
  129.       if (sampleR > SignalMaxR) SignalMaxR = sampleR;   // saves just the max R levels
  130.      }
  131.  
  132. // Linear mapping of input Max levels
  133. int MeterValueL = map(SignalMaxL,190,900,-34,40);
  134. int MeterValueR = map(SignalMaxR,190,900,-34,40);
  135.  
  136. // Logar mapping of input Max levels
  137. MeterValueL = 25.7856*log10(MeterValueL+35)-34;
  138. MeterValueR = 25.7856*log10(MeterValueR+35)-34;
  139.  
  140.  
  141. // testing different input values indication (-34 ~ 0%; 0 ~ 70%; +12 ~ 100%; +34 ~ +5)
  142. //  MeterValueR = -5;
  143.  
  144.   int a1 = (hMeter + (sin(MeterValueL / 57.296) * rMeter));
  145.   int a2 = (vMeter - (cos(MeterValueL / 57.296) * rMeter));
  146.   int a3 = (hMeter + (sin(MeterValueR / 57.296) * rMeter));
  147.   int a4 = (vMeter - (cos(MeterValueR / 57.296) * rMeter));
  148.  
  149.   dispL.clearBuffer();
  150.   dispL.drawXBMP(0,0, 128, 64, VUMeter);
  151.   dispL.drawLine(a1, a2, hMeter, vMeter);
  152. // dispL.setFont(u8g2_font_ncenB08_tr);
  153.   dispL.sendBuffer();
  154.  
  155.   dispR.clearBuffer();
  156.   dispR.drawXBMP(0,0, 128, 64, VUMeter);
  157.   dispR.drawLine(a3, a4, hMeter, vMeter);
  158.   //  dispR.drawLine(a3, a4, hMeter, vMeter);
  159.   // dispR.setFont(u8g2_font_ncenB08_tr);
  160.   dispR.sendBuffer();  
  161.  
  162. // testprint key values
  163.     Serial.print("sampleL: "); Serial.print(sampleL); Serial.print(" ");
  164. //    Serial.print("sampleR: "); Serial.print(sampleR); Serial.print("   ");
  165.     Serial.print("SignalMaxL: "); Serial.print(SignalMaxL); Serial.print(" ");
  166.     Serial.print("SignalMaxR: "); Serial.print(SignalMaxR); Serial.print(" ");
  167. //    Serial.print("midstepL: "); Serial.print(midstepL); Serial.print(" ");
  168. //    Serial.print("PeaktoPeakL: ");  Serial.print(PeaktoPeakL);   Serial.print("    ");    
  169.     Serial.print("MeterValueL: "); Serial.print(MeterValueL); Serial.println("    ");
  170. //    Serial.print("a3: ");      Serial.print(a3);       Serial.print(" ");
  171. //    Serial.print("a4: ");      Serial.println(a4);
  172.      
  173. // delay(1000);
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement