Advertisement
Attilator

VU_Meter_Physics_SH1106

Apr 18th, 2022
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.91 KB | None | 0 0
  1. //https://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  2.  
  3. #include <EEPROM.h>
  4. #include <SPI.h>
  5.  
  6. #define testbit(data,bitno) ((data>>bitno)&0x001) //Byte manipulation macros
  7. #define bit_set(var,bitno) ((var) |= 1 << (bitno))
  8. #define bit_clr(var,bitno) ((var) &= ~(1 << (bitno)))
  9.  
  10. #define SSD1306 0 // Definice displeje SSD1306
  11. #define SSD1309 1 // displej SSD1309
  12. #define SH1106 2 // Displej SH1106
  13.  
  14. #define disp_type SH1106
  15. //int disp_type=SH1106; // Tady je konkretni vyber
  16. //int disp_type=SSD1309; // Tady je konkretni vyber
  17. //int disp_type=SSD1306; // Tady je konkretni vyber
  18.  
  19. // nadefinovani PINu konkr. displeje
  20. #define RST 4 //PD4
  21. #define DC 3 //PD3
  22. #define CS0 2 //PD2 Left Display Chip Select
  23. #define CS1 7 //PD7 Right Display Chip Select
  24. #define CK 6 //PD6 SCK 13
  25. #define DA 5 //PD5 MOSI 11
  26.  
  27. #define Plus 6 //increase brightness
  28. #define Minus 5 //decrease brightness
  29. #define BTN 8 //Mode select button
  30.  
  31. #define A0 14 //Left Audio Input
  32. #define A1 15 //Right Audio Input
  33.  
  34. #define LED 13
  35.  
  36. #define CMD 0
  37. #define DATA 1
  38.  
  39. //Display in normal or inverted pixels
  40. #define NORM 0
  41. #define INV 1
  42.  
  43. #define Disp0 0 //Left Display
  44. #define Disp1 1 //Right Display
  45.  
  46. char PHYS=1; //enable needle mass spring physics response, 0 no physics, 1 underdamped, 2 overdamped
  47. // p ,i
  48. // 0.2,0.8 - lots of overshoot/oscillations around setpoint, slower settling, underdamped
  49. // 0.5,0.1 - quicker settling, little overshoot/oscillations, overdamped
  50. double p_gain=0.2;
  51. double i_gain=0.8;
  52.  
  53. unsigned char brt=207; //Display Brightness
  54.  
  55. unsigned char dispbuff[1026]={128,64}; //Display Buffer to be sent to OLED
  56.  
  57. const char Rbackground[] PROGMEM = {128,64, //Right VU Meter Background
  58. 0x00,0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x80,
  59. 0x00,0xCE,0x0A,0x0B,0x80,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x0F,0x08,0x80,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0B,0x8A,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x8A,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x8A,0x0B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x8F,0x08,0x00,0x00,0x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0A,0x0E,0x00,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x0A,0x0F,0x00,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x0F,0x08,0x00,0x0F,0x08,0x0F,0x00,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62. 0x00,0x04,0x04,0x04,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x30,0x0C,0x03,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x04,0x1F,0x04,0x04,0x00,
  63. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x02,0x02,0xF2,0x92,0x92,0x62,0x04,0x08,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  64. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,0x3C,0x04,0x08,0x30,0x42,0x45,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  65. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  66. };
  67.  
  68. const char Lbackground[] PROGMEM = {128,64, //Left VU Meter Background
  69. //const int Lbackground[] PROGMEM = {128,64, // Pomohlo od Warnings
  70. 0x00,0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x80,
  71. 0x00,0xCE,0x0A,0x0B,0x80,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x0F,0x08,0x80,0x0F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x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x3B,0x00,
  73. 0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x0A,0x0B,0x00,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x02,0x0F,0x00,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x0A,0x0E,0x00,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x0A,0x0F,0x00,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x0F,0x08,0x00,0x0F,0x08,0x0F,0x00,0x0F,0x08,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  74. 0x00,0x04,0x04,0x04,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x30,0x0C,0x03,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x04,0x1F,0x04,0x04,0x00,
  75. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x02,0x02,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,0x4F,0x48,0x48,0x48,0x48,0x48,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  78. };
  79.  
  80. const char SmallFont[] PROGMEM = {0x05,0x08, //5x7 Small 8 bit Font
  81. 0x00,0x00,0x00,0x00,0x00, // ' ' , 480 bytes
  82. 0x00,0x00,0x5F,0x00,0x00, // !
  83. 0x00,0x07,0x00,0x07,0x00, // "
  84. 0x14,0x7F,0x14,0x7F,0x14, // #
  85. 0x24,0x2A,0x7F,0x2A,0x12, // $
  86. 0x23,0x13,0x08,0x64,0x62, // %
  87. 0x36,0x49,0x55,0x22,0x50, // &
  88. 0x00,0x05,0x03,0x00,0x00, // '
  89. 0x00,0x1C,0x22,0x41,0x00, // (
  90. 0x00,0x41,0x22,0x1C,0x00, // )
  91. 0x14,0x08,0x3E,0x08,0x14, // *
  92. 0x08,0x08,0x3E,0x08,0x08, // +
  93. 0x00,0x50,0x30,0x00,0x00, // ,
  94. 0x08,0x08,0x08,0x08,0x08, // -
  95. 0x00,0x60,0x60,0x00,0x00, // .
  96. 0x40,0x20,0x10,0x08,0x04, // /
  97.  
  98. 0x3E,0x51,0x49,0x45,0x3E, //0
  99. 0x00,0x42,0x7F,0x40,0x00, //1
  100. 0x42,0x61,0x51,0x49,0x46, //2
  101. 0x22,0x49,0x49,0x49,0x36, //3
  102. 0x18,0x14,0x12,0x7F,0x10, //4
  103. 0x27,0x45,0x45,0x45,0x39, //5
  104. 0x3E,0x49,0x49,0x49,0x32, //6
  105. 0x01,0x01,0x71,0x09,0x07, //7
  106. 0x36,0x49,0x49,0x49,0x36, //8
  107. 0x26,0x49,0x49,0x49,0x3E, //9
  108. 0x00,0x36,0x36,0x00,0x00, // :
  109. 0x00,0x56,0x36,0x00,0x00, // ;
  110. 0x08,0x14,0x22,0x41,0x00, // <
  111. 0x14,0x14,0x14,0x14,0x14, // =
  112. 0x41,0x22,0x14,0x08,0x00, // >
  113. 0x02,0x01,0x51,0x09,0x06, // ?
  114.  
  115. 0x32,0x49,0x79,0x41,0x3E, // @
  116. 0x7C,0x12,0x11,0x12,0x7C, //A
  117. 0x7F,0x49,0x49,0x49,0x36, //B
  118. 0x3E,0x41,0x41,0x41,0x22, //C
  119. 0x7F,0x41,0x41,0x41,0x3E, //D
  120. 0x7F,0x49,0x49,0x49,0x41, //E
  121. 0x7F,0x09,0x09,0x09,0x01, //F
  122. 0x3E,0x41,0x49,0x49,0x7A, //G
  123. 0x7F,0x08,0x08,0x08,0x7F, //H
  124. 0x00,0x41,0x7F,0x41,0x00, //I
  125. 0x20,0x41,0x41,0x3F,0x01, //J
  126. 0x7F,0x08,0x14,0x22,0x41, //K
  127. 0x7F,0x40,0x40,0x40,0x40, //L
  128. 0x7F,0x02,0x0C,0x02,0x7F, //M
  129. 0x7F,0x04,0x08,0x10,0x7F, //N
  130. 0x3E,0x41,0x41,0x41,0x3E, //O
  131.  
  132. 0x7F,0x09,0x09,0x09,0x06, //P
  133. 0x3E,0x41,0x51,0x21,0x5E, //Q
  134. 0x7F,0x09,0x19,0x29,0x46, //R
  135. 0x26,0x49,0x49,0x49,0x32, //S
  136. 0x01,0x01,0x7F,0x01,0x01, //T
  137. 0x3F,0x40,0x40,0x40,0x3F, //U
  138. 0x1F,0x20,0x40,0x20,0x1F, //V
  139. 0x3F,0x40,0x38,0x40,0x3F, //W
  140. 0x63,0x14,0x08,0x14,0x63, //X
  141. 0x07,0x08,0x70,0x08,0x07, //Y
  142. 0x61,0x51,0x49,0x45,0x43, //Z
  143. 0x00,0x7F,0x41,0x41,0x00, // [
  144. 0x15,0x16,0x7C,0x16,0x15, // Yen
  145. 0x00,0x41,0x41,0x7F,0x00, // ]
  146. 0x04,0x02,0x01,0x02,0x04, // ^
  147. 0x40,0x40,0x40,0x40,0x40, // _
  148.  
  149. 0x00,0x01,0x02,0x04,0x00, // `
  150. 0x20,0x54,0x54,0x54,0x78, //a
  151. 0x7F,0x48,0x44,0x44,0x38, //b
  152. 0x38,0x44,0x44,0x44,0x20, //c
  153. 0x38,0x44,0x44,0x48,0x7F, //d
  154. 0x38,0x54,0x54,0x54,0x18, //e
  155. 0x08,0x7E,0x09,0x01,0x02, //f
  156. 0x0C,0x52,0x52,0x52,0x3E, //g
  157. 0x7F,0x08,0x04,0x04,0x78, //h
  158. 0x00,0x44,0x7D,0x40,0x00, //i
  159. 0x20,0x40,0x44,0x3D,0x00, //j
  160. 0x7F,0x10,0x28,0x44,0x00, //k
  161. 0x00,0x41,0x7F,0x40,0x00, //l
  162. 0x7C,0x04,0x18,0x04,0x78, //m
  163. 0x7C,0x08,0x04,0x04,0x78, //n
  164. 0x38,0x44,0x44,0x44,0x38, //o
  165.  
  166. 0x7C,0x14,0x14,0x14,0x08, //p
  167. 0x08,0x14,0x14,0x18,0x7C, //q
  168. 0x7C,0x08,0x04,0x04,0x08, //r
  169. 0x48,0x54,0x54,0x54,0x24, //s
  170. 0x04,0x3F,0x44,0x40,0x20, //t
  171. 0x3C,0x40,0x40,0x20,0x7C, //u
  172. 0x1C,0x20,0x40,0x20,0x1C, //v
  173. 0x3C,0x40,0x38,0x40,0x3C, //w
  174. 0x44,0x28,0x10,0x28,0x44, //x
  175. 0x0C,0x50,0x50,0x50,0x3C, //y
  176. 0x44,0x64,0x54,0x4C,0x44, //z
  177. 0x00,0x08,0x36,0x41,0x00, // {
  178. 0x00,0x00,0x7F,0x00,0x00, // |
  179. 0x00,0x41,0x36,0x08,0x00, // }
  180. 0x08,0x08,0x2A,0x1C,0x08, // ->
  181. 0x08,0x1C,0x2A,0x08,0x08};// <-
  182. // Reset displeje
  183. void dispRST(void){
  184. delay(10);
  185. digitalWrite(RST,LOW);
  186. delay(10);
  187. digitalWrite(RST,HIGH);
  188. delay(10);
  189. }
  190.  
  191. // Přenos bytu přes sběrnici SPI do displeje
  192. void spi_byte(char b){ //only for bitbanged SPI implementation
  193. for (int i = 0; i < 8; i++) {
  194. //PORTD&=~(1<<CK); //digitalWrite(CK, LOW);
  195. //if((b >> (7 - i) & 0b1)){PORTD|=1<<DA;}else{PORTD&=~(1<<DA);} //digitalWrite(DA, (b >> (7 - i) & 0b1));
  196. //PORTD|=1<<CK; //digitalWrite(CK, HIGH);
  197. }
  198. }
  199.  
  200. void spi_start(bool ch){
  201. if(!ch){
  202. PORTD&=~(1<<CS0); //digitalWrite(CS, LOW);
  203. }else{
  204. PORTD&=~(1<<CS1);
  205. }
  206. }
  207.  
  208. void spi_end(){
  209. PORTD|=1<<CS0; //digitalWrite(CS, HIGH);
  210. PORTD|=1<<CS1;
  211. //PORTD&=~(1<<CK); //digitalWrite(CK, LOW);
  212. //PORTD&=~(1<<DA); //digitalWrite(DA, LOW);
  213. PORTD&=~(1<<DC); //digitalWrite(DC, LOW);
  214. }
  215.  
  216. void spi_write(bool D_C, char b, bool ch){
  217. if(D_C){PORTD|=1<<DC;}else{PORTD&=~(1<<DC);} //digitalWrite(DC,D_C);
  218. spi_start(ch);
  219. //spi_byte(b);
  220. SPI.transfer(b);
  221. spi_end();
  222. }
  223.  
  224. void disp_init(bool ch){
  225. spi_write(CMD,0xAE,ch); //Set display off
  226. spi_write(CMD,0xD5,ch); //Set display clock divide ratio
  227. spi_write(CMD,0x80,ch); //Set display oscillator freq
  228. spi_write(CMD,0xA8,ch); //Set multiplex ratio
  229. spi_write(CMD,0x3F,ch);
  230. spi_write(CMD,0xD3,ch); //Set display offset
  231. spi_write(CMD,0x00,ch);
  232. spi_write(CMD,0x40,ch); //Set display start line
  233. //spi_write(CMD,0x8D,ch); //Set charge pump
  234. //spi_write(CMD,0x14,ch);
  235. spi_write(CMD,0xA1,ch); //Set segment remap
  236. spi_write(CMD,0xC8,ch); //Set com output scan dir
  237. spi_write(CMD,0xDA,ch); //Set com pins hardware config
  238. spi_write(CMD,0x12,ch);
  239. spi_write(CMD,0x81,ch); //Set contrast control
  240. spi_write(CMD,0xCF,ch);
  241. spi_write(CMD,0xD9,ch); //Set precharge period
  242. spi_write(CMD,0xF1,ch);
  243. spi_write(CMD,0xDB,ch); //Set vcomh deselect level
  244. spi_write(CMD,0x40,ch);
  245. spi_write(CMD,0xA4,ch); //Set entire display on/off
  246. spi_write(CMD,0xA6,ch); //Set normal/inverse display
  247. spi_write(CMD,0x20,ch); //Set memory addressing mode
  248. spi_write(CMD,0x00,ch);
  249. spi_write(CMD,0xAF,ch); //Set display on
  250. }
  251.  
  252. void brightness(unsigned char b){
  253. for(int i=0;i<2;i++){
  254. spi_write(CMD,0x81,i); //Set contrast control
  255. spi_write(CMD,b,i);
  256. }
  257. }
  258.  
  259. void printchar(char c, int font, bool inv, bool ch){
  260. unsigned char w,h=0;
  261. w = pgm_read_byte_near(font);
  262. h = pgm_read_byte_near(font + 1);
  263. for(int i=0;i<5;i++){
  264. spi_write(DATA,pgm_read_byte_near(font+2+(w*h/8)*(c-32)+i),ch);
  265. }
  266. spi_write(DATA,0x00,ch);
  267. }
  268.  
  269. void writeString(String str, bool ch){
  270. unsigned char len = str.length();
  271. unsigned char p=0;
  272. while(p<len){
  273. printchar(str.charAt(p),SmallFont,NORM,ch);
  274. p++;
  275. }
  276. }
  277.  
  278. void clr(bool ch){
  279. if(disp_type==SSD1306||disp_type==SSD1309){
  280. for(int i=0;i<8192;i++){
  281. spi_write(DATA,0x00,ch);
  282. }
  283. }else{
  284. for(int j=0;j<8;j++){
  285. set_xy(0,j,ch);
  286. for(int i=0;i<128;i++){
  287. spi_write(DATA,0x00,ch);
  288. }
  289. }
  290. }
  291. set_xy(0,0,ch);
  292. }
  293.  
  294. void set_xy(char x, char y, bool ch){
  295. if(disp_type==SSD1306||disp_type==SSD1309){
  296. spi_write(CMD,0x21,ch); //set x
  297. spi_write(CMD,0x00+x,ch);
  298. spi_write(CMD,0x7F,ch);
  299. spi_write(CMD,0xB0+y,ch); //set y
  300. }else{
  301. x+=2;
  302. spi_write(CMD,0xB0|y,ch); //set y
  303. //spi_write(CMD,y);
  304. spi_write(CMD,x&0x0F,ch); //set x
  305. spi_write(CMD,((x>>4)&0x0F)|0x10,ch);
  306. }
  307. }
  308.  
  309. void drawBMP(int x, int y, unsigned long bmp, unsigned char elem, bool inv, bool ch){ //write bitmap image to location X,Y on screen, can specify array bmp and elem index of image
  310. unsigned char w=*(char*)bmp; //must cast to obtain value in ram given the address
  311. unsigned char h=*(char*)(bmp+1);
  312.  
  313. for(int j=0;j<h/8;j++){
  314. set_xy(x,y+j,ch);
  315. for(int i=0;i<w;i++){
  316. //writeI2C(OLEDaddr,DAT,0,(*(char*)(bmp+2+j*w+i+w*(h/8)*elem))^(inv*0xFF),0);
  317. spi_write(DATA,(*(char*)(bmp+2+j*w+i+w*(h/8)*elem))^(inv*0xFF),ch);
  318. }
  319. }
  320. }
  321.  
  322. void drawline(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1, bool ch){
  323. if(abs(y1-y0)<abs(x1-x0)){
  324. if(x0>x1){
  325. plotLineLow(x1,y1,x0,y0);
  326. }else{
  327. plotLineLow(x0,y0,x1,y1);
  328. }
  329. }else{
  330. if(y0>y1){
  331. plotLineHigh(x1,y1,x0,y0);
  332. }else{
  333. plotLineHigh(x0,y0,x1,y1);
  334. }
  335. }
  336.  
  337. digitalWrite(9,HIGH);
  338. drawBMP(0,0,dispbuff,0,NORM,ch);
  339. digitalWrite(9,LOW);
  340. }
  341.  
  342. void plotLineLow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1){
  343. char dx=x1-x0;
  344. char dy=y1-y0;
  345. char yi=1;
  346. if(dy<0){
  347. yi=-1;
  348. dy=-dy;
  349. }
  350. char d=(2*dy)-dx;
  351. char y=y0;
  352. for(int x=x0;x<=x1;x++){
  353. dispbuff[2+x+128*(y/8)]|=1<<(y%8);
  354. if(d>0){
  355. y=y+yi;
  356. d=d+(2*(dy-dx));
  357. }else{
  358. d=d+2*dy;
  359. }
  360. }
  361. }
  362.  
  363. void plotLineHigh(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1){
  364. char dx=x1-x0;
  365. char dy=y1-y0;
  366. char xi=1;
  367. if(dx<0){
  368. xi=-1;
  369. dx=-dx;
  370. }
  371. char d=(2*dx)-dy;
  372. char x=x0;
  373. for(int y=y0;y<=y1;y++){
  374. dispbuff[2+x+128*(y/8)]|=1<<(y%8);
  375. if(d>0){
  376. x=x+xi;
  377. d=d+(2*(dx-dy));
  378. }else{
  379. d=d+2*dx;
  380. }
  381. }
  382. }
  383.  
  384. void setup() {
  385. pinMode(LED,OUTPUT);
  386. pinMode(A0,INPUT);
  387. pinMode(A1,INPUT);
  388. pinMode(BTN,INPUT_PULLUP);
  389. pinMode(Plus,INPUT_PULLUP);
  390. pinMode(Minus,INPUT_PULLUP);
  391. Serial.begin(115200);
  392. SPI.begin();
  393. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  394.  
  395. pinMode(RST,OUTPUT);
  396. pinMode(DC,OUTPUT);
  397. pinMode(CS0,OUTPUT);
  398. pinMode(CS1,OUTPUT);
  399. //pinMode(CK,OUTPUT);
  400. //pinMode(DA,OUTPUT);
  401. digitalWrite(CS0,HIGH);
  402. digitalWrite(CS1,HIGH);
  403. digitalWrite(RST,HIGH);
  404.  
  405. PHYS=EEPROM.read(0);
  406. if(PHYS>2){PHYS=0;EEPROM.update(0,PHYS);}
  407. brt=EEPROM.read(1);
  408. brightness(256-brt);
  409.  
  410. dispRST();
  411.  
  412. disp_init(Disp0);
  413. disp_init(Disp1);
  414. clr(Disp0);
  415. clr(Disp1);
  416. set_xy(30,3,Disp0);
  417. set_xy(30,3,Disp1);
  418. writeString("VU Meter",Disp0);
  419. writeString("Version 1.0",Disp1);
  420. set_xy(30,4,Disp0);
  421. set_xy(30,4,Disp1);
  422. writeString("sjm4306",Disp0);
  423. writeString("C 07/30/21",Disp1);
  424. delay(2000);
  425. }
  426.  
  427. void loop() {
  428. clr(Disp0);
  429. clr(Disp1);
  430. unsigned int cnt=0;
  431. unsigned int val0=0,val1=0;
  432. int pos0=0,pos1=0;
  433. int err_accum0=0,err_accum1=0;
  434. int err0=0,err1=0;
  435.  
  436. while(1){
  437. for(int i=0;i<1024;i++){ //transfer background stored in flash to ram display buffer
  438. dispbuff[i+2]=pgm_read_byte_near(Lbackground+i+2);
  439. }
  440.  
  441. val0=analogRead(A0)/8; //Grab Analog Input Measurement (scaled to 0-127)
  442. val1=analogRead(A1)/8;
  443.  
  444. if(PHYS){
  445. if(PHYS==1){p_gain=0.2;i_gain=0.8;} //Set gain values for current mode
  446. if(PHYS==2){p_gain=0.5;i_gain=0.1;}
  447.  
  448. err0=val0-pos0; //Calculate Error
  449. err1=val1-pos1;
  450.  
  451. err_accum0+=i_gain*err0; //Calculate PI
  452. pos0+=(int) (p_gain*err0+err_accum0);
  453.  
  454. err_accum1+=i_gain*err1;
  455. pos1+=(int) (p_gain*err1+err_accum1);
  456.  
  457. if(pos0>127){pos0=127;} //max min limiter
  458. if(pos0<0){pos0=0;}
  459. if(pos1>127){pos1=127;}
  460. if(pos1<0){pos1=0;}
  461. }else{pos0=val0;pos1=val1;}
  462.  
  463. drawline(71-(127-pos0)/8,63,pos0,20-(int)(((double)(pos0*(127-pos0)))/200),Disp0); //draw line over background
  464.  
  465. for(int i=0;i<1024;i++){ //transfer background stored in flash to ram display buffer
  466. dispbuff[i+2]=pgm_read_byte_near(Rbackground+i+2);
  467. }
  468. drawline(71-(127-pos1)/8,63,pos1,20-(int)(((double)(pos1*(127-pos1)))/200),Disp1);
  469.  
  470. if(digitalRead(BTN)==0){
  471. if(PHYS<2){PHYS++;}else{PHYS=0;}
  472. EEPROM.update(0,PHYS);
  473. set_xy(0,7,Disp1);
  474. if(PHYS==1){
  475. writeString("U",Disp1);
  476. }else if(PHYS==2){
  477. writeString("O",Disp1);
  478. }else{
  479. writeString("N",Disp1);
  480. }
  481. delay(1000);
  482. while(digitalRead(BTN)==0){;}
  483. }
  484.  
  485. if(digitalRead(Plus)==0){
  486. if(brt>1){brt--;EEPROM.update(1,brt);brightness(256-brt);}
  487. char buff[3];
  488. set_xy(0,7,Disp0);
  489. writeString(itoa(256-brt,buff,10),Disp0);
  490. delay(100);
  491. }
  492.  
  493. if(digitalRead(Minus)==0){
  494. if(brt<255){brt++;EEPROM.update(1,brt);brightness(256-brt);}
  495. char buff[3];
  496. set_xy(0,7,Disp0);
  497. writeString(itoa(256-brt,buff,10),Disp0);
  498. delay(100);
  499. }
  500.  
  501. delay(20);
  502. }
  503. }
  504.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement