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.

139 lines
3.7KB

  1. #include "ML_modules.hpp"
  2. #include "dsp/digital.hpp"
  3. namespace rack_plugin_ML_modules {
  4. struct TrigSwitch3 : Module {
  5. enum ParamIds {
  6. STEP_PARAM,
  7. NUM_PARAMS = STEP_PARAM+8
  8. };
  9. enum InputIds {
  10. TRIG_INPUT,
  11. CV_INPUT1 = TRIG_INPUT + 8,
  12. CV_INPUT2 = CV_INPUT1 + 8,
  13. CV_INPUT3 = CV_INPUT2 + 8,
  14. NUM_INPUTS = CV_INPUT3 + 8
  15. };
  16. enum OutputIds {
  17. OUT1_OUTPUT,
  18. OUT2_OUTPUT,
  19. OUT3_OUTPUT,
  20. NUM_OUTPUTS
  21. };
  22. enum LightIds {
  23. STEP_LIGHT,
  24. NUM_LIGHTS = STEP_LIGHT+8
  25. };
  26. TrigSwitch3() : Module( NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS ) { reset(); };
  27. void step() override;
  28. int position=0;
  29. const float in_min[4] = {0.0, 0.0, 0.0, -5.0};
  30. const float in_max[4] = {8.0, 6.0, 10.0, 5.0};
  31. SchmittTrigger stepTriggers[8];
  32. void reset() override {
  33. position = 0;
  34. for(int i=0; i<8; i++) lights[i].value = 0.0;
  35. };
  36. json_t *toJson() override {
  37. json_t *rootJ = json_object();
  38. json_object_set_new(rootJ, "position", json_integer(position));
  39. return rootJ;
  40. };
  41. void fromJson(json_t *rootJ) override {
  42. json_t *positionJ = json_object_get(rootJ, "position");
  43. if(positionJ) position = json_integer_value(positionJ);
  44. };
  45. };
  46. void TrigSwitch3::step() {
  47. for(int i=0; i<8; i++) {
  48. if( stepTriggers[i].process( inputs[TRIG_INPUT+i].normalize(0.0)) + params[STEP_PARAM+i].value ) position = i;
  49. lights[i].value = (i==position)?1.0:0.0;
  50. };
  51. outputs[OUT1_OUTPUT].value = inputs[CV_INPUT1+position].normalize(0.0);
  52. outputs[OUT2_OUTPUT].value = inputs[CV_INPUT2+position].normalize(0.0);
  53. outputs[OUT3_OUTPUT].value = inputs[CV_INPUT3+position].normalize(0.0);
  54. };
  55. struct TrigSwitch3Widget : ModuleWidget {
  56. TrigSwitch3Widget(TrigSwitch3 *module);
  57. };
  58. TrigSwitch3Widget::TrigSwitch3Widget(TrigSwitch3 *module) : ModuleWidget(module) {
  59. box.size = Vec(15*12, 380);
  60. {
  61. SVGPanel *panel = new SVGPanel();
  62. panel->box.size = box.size;
  63. panel->setBackground(SVG::load(assetPlugin(plugin,"res/TrigSwitch3.svg")));
  64. addChild(panel);
  65. }
  66. addChild(Widget::create<MLScrew>(Vec(15, 0)));
  67. addChild(Widget::create<MLScrew>(Vec(box.size.x-30, 0)));
  68. addChild(Widget::create<MLScrew>(Vec(15, 365)));
  69. addChild(Widget::create<MLScrew>(Vec(box.size.x-30, 365)));
  70. const float offset_y = 60, delta_y = 32, row1=15, row2 = row1+33, row3 = row2 + 25;
  71. for (int i=0; i<8; i++) {
  72. addInput(Port::create<MLPort>( Vec(row1, offset_y + i*delta_y), Port::INPUT, module, TrigSwitch3::TRIG_INPUT + i));
  73. addParam(ParamWidget::create<ML_MediumLEDButton>(Vec(row2 , offset_y + i*delta_y +3 ), module, TrigSwitch3::STEP_PARAM + i, 0.0, 1.0, 0.0));
  74. addChild(ModuleLightWidget::create<MLMediumLight<GreenLight>>( Vec(row2 + 4, offset_y + i*delta_y + 7), module, TrigSwitch3::STEP_LIGHT+i));
  75. addInput(Port::create<MLPort>( Vec(row3, offset_y + i*delta_y), Port::INPUT, module, TrigSwitch3::CV_INPUT1 + i));
  76. addInput(Port::create<MLPort>( Vec(row3+32, offset_y + i*delta_y), Port::INPUT, module, TrigSwitch3::CV_INPUT2 + i));
  77. addInput(Port::create<MLPort>( Vec(row3+64, offset_y + i*delta_y), Port::INPUT, module, TrigSwitch3::CV_INPUT3 + i));
  78. }
  79. addOutput(Port::create<MLPort>(Vec(row3, 320), Port::OUTPUT, module, TrigSwitch3::OUT1_OUTPUT));
  80. addOutput(Port::create<MLPort>(Vec(row3+32, 320), Port::OUTPUT, module, TrigSwitch3::OUT2_OUTPUT));
  81. addOutput(Port::create<MLPort>(Vec(row3+64, 320), Port::OUTPUT, module, TrigSwitch3::OUT3_OUTPUT));
  82. }
  83. } // namespace rack_plugin_ML_modules
  84. using namespace rack_plugin_ML_modules;
  85. RACK_PLUGIN_MODEL_INIT(ML_modules, TrigSwitch3) {
  86. Model *modelTrigSwitch3 = Model::create<TrigSwitch3, TrigSwitch3Widget>("ML modules", "TrigSwitch3", "TrigSwitch3 8->1", SWITCH_TAG, UTILITY_TAG );
  87. return modelTrigSwitch3;
  88. }