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.

155 lines
4.6KB

  1. #include "Computerscare.hpp"
  2. #include "dsp/digital.hpp"
  3. #include "dsp/filter.hpp"
  4. #include <string>
  5. #include <sstream>
  6. #include <iomanip>
  7. namespace rack_plugin_computerscare {
  8. #define NUM_LINES 16
  9. struct ComputerscareDebug : Module {
  10. enum ParamIds {
  11. PITCH_PARAM,
  12. MANUAL_TRIGGER,
  13. MANUAL_CLEAR_TRIGGER,
  14. NUM_PARAMS
  15. };
  16. enum InputIds {
  17. VAL_INPUT,
  18. TRG_INPUT,
  19. CLR_INPUT,
  20. NUM_INPUTS
  21. };
  22. enum OutputIds {
  23. SINE_OUTPUT,
  24. NUM_OUTPUTS
  25. };
  26. enum LightIds {
  27. BLINK_LIGHT,
  28. NUM_LIGHTS
  29. };
  30. std::string defaultStrValue = "0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n";
  31. std::string strValue = "0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n0.000000\n";
  32. float logLines[NUM_LINES] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  33. int lineCounter = 0;
  34. SchmittTrigger clockTrigger;
  35. SchmittTrigger clearTrigger;
  36. SchmittTrigger manualClockTrigger;
  37. SchmittTrigger manualClearTrigger;
  38. ComputerscareDebug() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  39. void step() override;
  40. // For more advanced Module features, read Rack's engine.hpp header file
  41. // - toJson, fromJson: serialization of internal data
  42. // - onSampleRateChange: event triggered by a change of sample rate
  43. // - onReset, onRandomize, onCreate, onDelete: implements special behavior when user clicks these from the context menu
  44. };
  45. void ComputerscareDebug::step() {
  46. std::string thisVal;
  47. if (clockTrigger.process(inputs[TRG_INPUT].value / 2.f) || manualClockTrigger.process(params[MANUAL_TRIGGER].value)) {
  48. for( unsigned int a = NUM_LINES-1; a > 0; a = a - 1 )
  49. {
  50. logLines[a] = logLines[a-1];
  51. }
  52. logLines[0] = inputs[VAL_INPUT].value;
  53. thisVal = std::to_string(logLines[0]).substr(0,10);
  54. for( unsigned int a = 1; a < NUM_LINES; a = a + 1 )
  55. {
  56. thisVal = thisVal + "\n" + std::to_string(logLines[a]).substr(0,10);
  57. }
  58. strValue = thisVal;
  59. }
  60. if(clearTrigger.process(inputs[CLR_INPUT].value / 2.f) || manualClearTrigger.process(params[MANUAL_CLEAR_TRIGGER].value)) {
  61. for( unsigned int a = 0; a < NUM_LINES; a++ )
  62. {
  63. logLines[a] = 0;
  64. }
  65. strValue = defaultStrValue;
  66. }
  67. }
  68. ////////////////////////////////////
  69. struct StringDisplayWidget3 : TransparentWidget {
  70. std::string *value;
  71. std::shared_ptr<Font> font;
  72. StringDisplayWidget3() {
  73. font = Font::load(assetPlugin(plugin, "res/Oswald-Regular.ttf"));
  74. };
  75. void draw(NVGcontext *vg) override
  76. {
  77. // Background
  78. NVGcolor backgroundColor = nvgRGB(0x10, 0x00, 0x10);
  79. NVGcolor StrokeColor = nvgRGB(0xC0, 0xC7, 0xDE);
  80. nvgBeginPath(vg);
  81. nvgRoundedRect(vg, -1.0, -1.0, box.size.x+2, box.size.y+2, 4.0);
  82. nvgFillColor(vg, StrokeColor);
  83. nvgFill(vg);
  84. nvgBeginPath(vg);
  85. nvgRoundedRect(vg, 0.0, 0.0, box.size.x, box.size.y, 4.0);
  86. nvgFillColor(vg, backgroundColor);
  87. nvgFill(vg);
  88. // text
  89. nvgFontSize(vg, 15);
  90. nvgFontFaceId(vg, font->handle);
  91. nvgTextLetterSpacing(vg, 2.5);
  92. std::stringstream to_display;
  93. to_display << std::setw(8) << *value;
  94. Vec textPos = Vec(6.0f, 12.0f);
  95. NVGcolor textColor = nvgRGB(0xC0, 0xE7, 0xDE);
  96. nvgFillColor(vg, textColor);
  97. nvgTextBox(vg, textPos.x, textPos.y,80,to_display.str().c_str(), NULL);
  98. }
  99. };
  100. struct ComputerscareDebugWidget : ModuleWidget {
  101. ComputerscareDebugWidget(ComputerscareDebug *module) : ModuleWidget(module) {
  102. setPanel(SVG::load(assetPlugin(plugin, "res/ComputerscareDebugPanel.svg")));
  103. addInput(Port::create<InPort>(Vec(3, 330), Port::INPUT, module, ComputerscareDebug::TRG_INPUT));
  104. addInput(Port::create<InPort>(Vec(33, 330), Port::INPUT, module, ComputerscareDebug::VAL_INPUT));
  105. addInput(Port::create<InPort>(Vec(63, 330), Port::INPUT, module, ComputerscareDebug::CLR_INPUT));
  106. addParam(ParamWidget::create<LEDButton>(Vec(6, 290), module, ComputerscareDebug::MANUAL_TRIGGER, 0.0, 1.0, 0.0));
  107. addParam(ParamWidget::create<LEDButton>(Vec(66, 290), module, ComputerscareDebug::MANUAL_CLEAR_TRIGGER, 0.0, 1.0, 0.0));
  108. StringDisplayWidget3 *display = new StringDisplayWidget3();
  109. display->box.pos = Vec(1,24);
  110. display->box.size = Vec(88, 250);
  111. display->value = &module->strValue;
  112. addChild(display);
  113. }
  114. };
  115. } // namespace rack_plugin_computerscare
  116. using namespace rack_plugin_computerscare;
  117. RACK_PLUGIN_MODEL_INIT(computerscare, ComputerscareDebug) {
  118. Model *modelComputerscareDebug = Model::create<ComputerscareDebug, ComputerscareDebugWidget>("computerscare", "computerscare-debug", "Debug", UTILITY_TAG);
  119. return modelComputerscareDebug;
  120. }