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.

126 lines
3.6KB

  1. #include "arjo_modules.hpp"
  2. #include "dsp/digital.hpp"
  3. namespace rack_plugin_arjo_modules {
  4. struct Count : Module {
  5. enum ParamIds {
  6. COUNT_PARAM_1,
  7. COUNT_PARAM_2,
  8. COUNT_PARAM_3,
  9. COUNT_PARAM_4,
  10. NUM_PARAMS
  11. };
  12. enum InputIds {
  13. CLK_INPUT,
  14. RST_INPUT,
  15. NUM_INPUTS
  16. };
  17. enum OutputIds {
  18. OUTPUT_1,
  19. OUTPUT_2,
  20. OUTPUT_3,
  21. OUTPUT_4,
  22. NUM_OUTPUTS
  23. };
  24. enum LightIds {
  25. LIGHT_1,
  26. LIGHT_2,
  27. LIGHT_3,
  28. LIGHT_4,
  29. NUM_LIGHTS
  30. };
  31. SchmittTrigger clock_trigger;
  32. SchmittTrigger reset_trigger;
  33. PulseGenerator pulse_1;
  34. PulseGenerator pulse_2;
  35. PulseGenerator pulse_3;
  36. PulseGenerator pulse_4;
  37. int count_1 = 0;
  38. int count_2 = 0;
  39. int count_3 = 0;
  40. int count_4 = 0;
  41. int max_count_1 = 0;
  42. int max_count_2 = 0;
  43. int max_count_3 = 0;
  44. int max_count_4 = 0;
  45. Count() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  46. void step() override;
  47. };
  48. void Count::step() {
  49. max_count_1 = round(params[COUNT_PARAM_1].value);
  50. max_count_2 = round(params[COUNT_PARAM_2].value);
  51. max_count_3 = round(params[COUNT_PARAM_3].value);
  52. max_count_4 = round(params[COUNT_PARAM_4].value);
  53. if (reset_trigger.process(inputs[RST_INPUT].value)) {
  54. count_1 = max_count_1;
  55. count_2 = max_count_2;
  56. count_3 = max_count_3;
  57. count_4 = max_count_4;
  58. }
  59. if (clock_trigger.process(inputs[CLK_INPUT].value)) {
  60. count_1++;
  61. count_2++;
  62. count_3++;
  63. count_4++;
  64. }
  65. if (count_1 > max_count_1) {count_1 = 0; pulse_1.trigger(1e-3);}
  66. if (count_2 > max_count_2) {count_2 = 0; pulse_2.trigger(1e-3);}
  67. if (count_3 > max_count_3) {count_3 = 0; pulse_3.trigger(1e-3);}
  68. if (count_4 > max_count_4) {count_4 = 0; pulse_4.trigger(1e-3);}
  69. outputs[OUTPUT_1].value = pulse_1.process(1.0 / engineGetSampleRate()) ? 10.0f : 0.0f;
  70. outputs[OUTPUT_2].value = pulse_2.process(1.0 / engineGetSampleRate()) ? 10.0f : 0.0f;
  71. outputs[OUTPUT_3].value = pulse_3.process(1.0 / engineGetSampleRate()) ? 10.0f : 0.0f;
  72. outputs[OUTPUT_4].value = pulse_4.process(1.0 / engineGetSampleRate()) ? 10.0f : 0.0f;
  73. }
  74. struct CountWidget : ModuleWidget {
  75. CountWidget(Count *module) : ModuleWidget(module) {
  76. setPanel(SVG::load(assetPlugin(plugin, "res/count.svg")));
  77. // Screws
  78. addChild(Widget::create<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  79. addChild(Widget::create<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  80. // Inputs
  81. addInput(Port::create<small_port>(Vec(4.5, 50.5), Port::INPUT, module, Count::CLK_INPUT));
  82. addInput(Port::create<small_port>(Vec(23.5, 50.5), Port::INPUT, module, Count::RST_INPUT));
  83. // Knobs
  84. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(10.5, 79), module, Count::COUNT_PARAM_1, 0.0, 15.0, 0.0));
  85. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(10.5, 149), module, Count::COUNT_PARAM_2, 0.0, 15.0, 0.0));
  86. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(10.5, 219), module, Count::COUNT_PARAM_3, 0.0, 15.0, 0.0));
  87. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(10.5, 289), module, Count::COUNT_PARAM_4, 0.0, 15.0, 0.0));
  88. // Outputs
  89. addOutput(Port::create<PJ301MPort>(Vec(10.5, 111), Port::OUTPUT, module, Count::OUTPUT_1));
  90. addOutput(Port::create<PJ301MPort>(Vec(10.5, 180), Port::OUTPUT, module, Count::OUTPUT_2));
  91. addOutput(Port::create<PJ301MPort>(Vec(10.5, 251), Port::OUTPUT, module, Count::OUTPUT_3));
  92. addOutput(Port::create<PJ301MPort>(Vec(10.5, 321), Port::OUTPUT, module, Count::OUTPUT_4));
  93. }
  94. };
  95. } // namespace rack_plugin_arjo_modules
  96. using namespace rack_plugin_arjo_modules;
  97. RACK_PLUGIN_MODEL_INIT(arjo_modules, Count) {
  98. Model *modelCount = Model::create<Count, CountWidget>("arjo_modules", "count", "count", UTILITY_TAG);
  99. return modelCount;
  100. }