You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.4KB

  1. #include "ML_modules.hpp"
  2. #include "dsp/digital.hpp"
  3. #include <sstream>
  4. #include <iomanip>
  5. namespace rack_plugin_ML_modules {
  6. struct VoltMeter : Module {
  7. enum ParamIds {
  8. NUM_PARAMS
  9. };
  10. enum InputIds {
  11. IN1_INPUT,
  12. IN2_INPUT,
  13. IN3_INPUT,
  14. IN4_INPUT,
  15. NUM_INPUTS
  16. };
  17. enum OutputIds {
  18. NUM_OUTPUTS
  19. };
  20. enum LightIds {
  21. NUM_LIGHTS
  22. };
  23. VoltMeter() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) { for(int i=0; i<4; i++) {volts[i] = 0.0f; active[i] = false;}};
  24. void step() override;
  25. float volts[4];
  26. bool active[4];
  27. };
  28. void VoltMeter::step() {
  29. for(int i=0; i<4; i++) {
  30. active[i] = inputs[IN1_INPUT+i].active;
  31. volts[i] = 0.9 * volts[i] + 0.1 * inputs[IN1_INPUT+i].normalize(0.0);
  32. };
  33. };
  34. struct VoltDisplayWidget : TransparentWidget {
  35. float *value;
  36. bool *on;
  37. std::shared_ptr<Font> font;
  38. VoltDisplayWidget() {
  39. font = Font::load(assetPlugin(plugin, "res/Segment7Standard.ttf"));
  40. };
  41. void draw(NVGcontext *vg) {
  42. // Background
  43. // NVGcolor backgroundColor = nvgRGB(0x44, 0x44, 0x44);
  44. NVGcolor backgroundColor = nvgRGB(0x20, 0x20, 0x20);
  45. NVGcolor borderColor = nvgRGB(0x10, 0x10, 0x10);
  46. nvgBeginPath(vg);
  47. nvgRoundedRect(vg, 0.0, 0.0, box.size.x, box.size.y, 4.0);
  48. nvgFillColor(vg, backgroundColor);
  49. nvgFill(vg);
  50. nvgStrokeWidth(vg, 1.0);
  51. nvgStrokeColor(vg, borderColor);
  52. nvgStroke(vg);
  53. nvgFontSize(vg, 18);
  54. nvgFontFaceId(vg, font->handle);
  55. nvgTextLetterSpacing(vg, 2.5);
  56. char display_string[10];
  57. sprintf(display_string,"%6.2f",*value);
  58. Vec textPos = Vec(6.0f, 17.0f);
  59. NVGcolor textColor = nvgRGB(0xdf, 0xd2, 0x2c);
  60. nvgFillColor(vg, nvgTransRGBA(textColor, 16));
  61. nvgText(vg, textPos.x, textPos.y, "~~~~~~", NULL);
  62. textColor = nvgRGB(0xda, 0xe9, 0x29);
  63. nvgFillColor(vg, nvgTransRGBA(textColor, 16));
  64. nvgText(vg, textPos.x, textPos.y, "\\\\\\\\\\\\", NULL);
  65. if(*on) {
  66. textColor = nvgRGB(0xf0, 0x00, 0x00);
  67. nvgFillColor(vg, textColor);
  68. nvgText(vg, textPos.x, textPos.y, display_string, NULL);
  69. };
  70. }
  71. };
  72. struct VoltMeterWidget : ModuleWidget {
  73. VoltMeterWidget(VoltMeter *module);
  74. TextField ** label;
  75. };
  76. VoltMeterWidget::VoltMeterWidget(VoltMeter *module) : ModuleWidget(module) {
  77. box.size = Vec(15*8, 380);
  78. // label = new TextField*[4];
  79. {
  80. SVGPanel *panel = new SVGPanel();
  81. panel->box.size = box.size;
  82. panel->setBackground(SVG::load(assetPlugin(plugin,"res/VoltMeter.svg")));
  83. addChild(panel);
  84. }
  85. const float delta_y = 70;
  86. addChild(Widget::create<MLScrew>(Vec(15, 0)));
  87. addChild(Widget::create<MLScrew>(Vec(15, 365)));
  88. addChild(Widget::create<MLScrew>(Vec(box.size.x-30, 0)));
  89. addChild(Widget::create<MLScrew>(Vec(box.size.x-30, 365)));
  90. for(int i=0; i<4; i++) {
  91. addInput(Port::create<MLPort>(Vec(12, 60+i*delta_y), Port::INPUT, module, VoltMeter::IN1_INPUT+i));
  92. VoltDisplayWidget *display = new VoltDisplayWidget();
  93. display->box.pos = Vec(10,90+i*delta_y);
  94. display->box.size = Vec(100, 20);
  95. display->value = &module->volts[i];
  96. display->on = &module->active[i];
  97. addChild(display);
  98. // label[i] = new TextField();
  99. // label[i]->box.pos = Vec(50,60+i*65);
  100. // label[i]->box.size = Vec(60,30);
  101. // addChild(label[i]);
  102. };
  103. }
  104. } // namespace rack_plugin_ML_modules
  105. using namespace rack_plugin_ML_modules;
  106. RACK_PLUGIN_MODEL_INIT(ML_modules, VoltMeter) {
  107. Model *modelVoltMeter = Model::create<VoltMeter, VoltMeterWidget>("ML modules", "VoltMeter", "Volt Meter", VISUAL_TAG, UTILITY_TAG);
  108. return modelVoltMeter;
  109. }