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.

127 lines
3.0KB

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