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.

92 lines
2.2KB

  1. #include "arjo_modules.hpp"
  2. #include "dsp/digital.hpp"
  3. namespace rack_plugin_arjo_modules {
  4. struct Switch : Module {
  5. enum ParamIds {
  6. NUM_PARAMS
  7. };
  8. enum InputIds {
  9. CLK_INPUT,
  10. RST_INPUT,
  11. ENUMS(VALUE_INPUT, 8),
  12. NUM_INPUTS
  13. };
  14. enum OutputIds {
  15. VALUE_OUTPUT,
  16. NUM_OUTPUTS
  17. };
  18. enum LightIds {
  19. ENUMS(LIGHTS, 8),
  20. NUM_LIGHTS
  21. };
  22. SchmittTrigger clock_trigger;
  23. SchmittTrigger reset_trigger;
  24. int current_input = 0;
  25. int max_input = 7;
  26. Switch() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  27. void step() override;
  28. };
  29. void Switch::step() {
  30. if (reset_trigger.process(inputs[RST_INPUT].value)) {
  31. current_input = 0;
  32. }
  33. if (clock_trigger.process(inputs[CLK_INPUT].value)) {
  34. current_input++;
  35. }
  36. if (current_input > max_input) {
  37. current_input = 0;
  38. }
  39. for (int i=0; i < 8; i++) {
  40. lights[LIGHTS + i].setBrightnessSmooth(0);
  41. }
  42. lights[LIGHTS + current_input].setBrightnessSmooth(1);
  43. outputs[VALUE_OUTPUT].value = inputs[VALUE_INPUT + current_input].value;
  44. }
  45. struct SwitchWidget : ModuleWidget {
  46. SwitchWidget(Switch *module) : ModuleWidget(module) {
  47. setPanel(SVG::load(assetPlugin(plugin, "res/switch.svg")));
  48. // Screws
  49. addChild(Widget::create<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  50. addChild(Widget::create<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  51. // Inputs
  52. addInput(Port::create<small_port>(Vec(4.5, 50.5), Port::INPUT, module, Switch::CLK_INPUT));
  53. addInput(Port::create<small_port>(Vec(23.5, 50.5), Port::INPUT, module, Switch::RST_INPUT));
  54. static const float portY[8] = {83-4, 113-4, 142-4, 172-4, 202-4, 232-4, 262-4, 292-4};
  55. for (int i = 0; i < 8; i++) {
  56. addChild(ModuleLightWidget::create<TinyLight<GreenLight>>(Vec(31.5, portY[i]), module, Switch::LIGHTS + i));
  57. addInput(Port::create<PJ301MPort>(Vec(10.5, portY[i]), Port::INPUT, module, Switch::VALUE_INPUT + i));
  58. }
  59. // Output
  60. addOutput(Port::create<PJ301MPort>(Vec(10.5, 321.5), Port::OUTPUT, module, Switch::VALUE_OUTPUT));
  61. }
  62. };
  63. } // namespace rack_plugin_arjo_modules
  64. using namespace rack_plugin_arjo_modules;
  65. RACK_PLUGIN_MODEL_INIT(arjo_modules, Switch) {
  66. Model *modelSwitch = Model::create<Switch, SwitchWidget>("arjo_modules", "switch", "switch", UTILITY_TAG);
  67. return modelSwitch;
  68. }