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.

160 lines
5.0KB

  1. ///////////////////////////////////////////////////
  2. //
  3. // Radio Buttons VCV Module
  4. //
  5. // Strum 2017
  6. //
  7. ///////////////////////////////////////////////////
  8. #include "mental.hpp"
  9. #include "dsp/digital.hpp"
  10. namespace rack_plugin_mental {
  11. struct MentalRadioButtons : Module {
  12. enum ParamIds {
  13. BUTTON2,
  14. BUTTON_PARAM = BUTTON2 + 7,
  15. NUM_PARAMS = BUTTON_PARAM + 7
  16. };
  17. enum InputIds {
  18. INS1,
  19. INS2 = INS1 + 7,
  20. NUM_INPUTS = INS2 + 7
  21. };
  22. enum OutputIds {
  23. BUTTON2_OUT,
  24. OUTPUT = BUTTON2_OUT +7,
  25. NUM_OUTPUTS = OUTPUT + 7
  26. };
  27. enum LightIds {
  28. BUTTON_LEDS,
  29. BUTTON2_LEDS = BUTTON_LEDS + 7,
  30. NUM_LIGHTS = BUTTON2_LEDS + 7
  31. };
  32. SchmittTrigger button_triggers[7];
  33. SchmittTrigger button2_triggers[7];
  34. bool button_states[7] = {1,0,0,0,0,0,0};
  35. bool button2_states[7] = {1,0,0,0,0,0,0};
  36. MentalRadioButtons() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  37. void step() override;
  38. json_t *toJson() override
  39. {
  40. json_t *rootJ = json_object();
  41. // button states
  42. json_t *button_statesJ = json_array();
  43. for (int i = 0; i < 7; i++)
  44. {
  45. json_t *button_stateJ = json_integer((int) button_states[i]);
  46. json_array_append_new(button_statesJ, button_stateJ);
  47. }
  48. json_object_set_new(rootJ, "buttons", button_statesJ);
  49. json_t *button2_statesJ = json_array();
  50. for (int i = 0; i < 7; i++)
  51. {
  52. json_t *button2_stateJ = json_integer((int) button2_states[i]);
  53. json_array_append_new(button2_statesJ, button2_stateJ);
  54. }
  55. json_object_set_new(rootJ, "buttons2", button2_statesJ);
  56. return rootJ;
  57. }
  58. void fromJson(json_t *rootJ) override
  59. {
  60. // button states
  61. json_t *button_statesJ = json_object_get(rootJ, "buttons");
  62. if (button_statesJ)
  63. {
  64. for (int i = 0; i < 7; i++)
  65. {
  66. json_t *button_stateJ = json_array_get(button_statesJ, i);
  67. if (button_stateJ)
  68. button_states[i] = !!json_integer_value(button_stateJ);
  69. }
  70. }
  71. json_t *button2_statesJ = json_object_get(rootJ, "buttons2");
  72. if (button2_statesJ)
  73. {
  74. for (int i = 0; i < 7; i++)
  75. {
  76. json_t *button2_stateJ = json_array_get(button2_statesJ, i);
  77. if (button2_stateJ)
  78. button2_states[i] = !!json_integer_value(button2_stateJ);
  79. }
  80. }
  81. }
  82. };
  83. void MentalRadioButtons::step()
  84. {
  85. for (int i = 0 ; i < 7 ; i++)
  86. {
  87. if (button_triggers[i].process(params[BUTTON_PARAM + i].value) || button_triggers[i].process(inputs[INS1 + i].value))
  88. {
  89. for (int j = 0 ; j < 7 ; j ++)
  90. {
  91. button_states[j] = false ;
  92. }
  93. button_states[i] = !button_states[i];
  94. }
  95. lights[BUTTON_LEDS + i ].value = (button_states[i]) ? 1.0 : 0.0;
  96. outputs[OUTPUT + i].value = button_states[i] * 10.0;
  97. if (button2_triggers[i].process(params[BUTTON2 + i].value) || button_triggers[i].process(inputs[INS2 + i].value))
  98. {
  99. for (int j = 0 ; j < 7 ; j ++)
  100. {
  101. button2_states[j] = false ;
  102. }
  103. button2_states[i] = !button2_states[i];
  104. }
  105. lights[BUTTON2_LEDS + i ].value = (button2_states[i]) ? 1.0 : 0.0;
  106. outputs[BUTTON2_OUT + i].value = button2_states[i] * 10.0;
  107. }
  108. }
  109. /////////////////////////////////////////////////////////////////////////
  110. struct MentalRadioButtonsWidget : ModuleWidget {
  111. MentalRadioButtonsWidget(MentalRadioButtons *module);
  112. };
  113. MentalRadioButtonsWidget::MentalRadioButtonsWidget(MentalRadioButtons *module) : ModuleWidget(module)
  114. {
  115. setPanel(SVG::load(assetPlugin(plugin, "res/MentalRadioButtons.svg")));
  116. int spacing = 25;
  117. int group_offset = 184;
  118. int top_space = 15;
  119. for (int i = 0; i < 7 ; i++)
  120. {
  121. addInput(Port::create<GateInPort>(Vec(3, top_space + spacing * i), Port::INPUT, module, MentalRadioButtons::INS1 + i));
  122. addOutput(Port::create<GateOutPort>(Vec(63, top_space + spacing * i), Port::OUTPUT, module, MentalRadioButtons::OUTPUT + i));
  123. addParam(ParamWidget::create<LEDButton>(Vec(35, top_space + 3 + spacing * i), module, MentalRadioButtons::BUTTON_PARAM +i, 0.0, 1.0, 0.0));
  124. addChild(ModuleLightWidget::create<MedLight<BlueLED>>(Vec(40, top_space + 8 + spacing * i), module, MentalRadioButtons::BUTTON_LEDS + i));
  125. /// group 2
  126. addInput(Port::create<GateInPort>(Vec(3, 10 + group_offset + spacing * i), Port::INPUT, module, MentalRadioButtons::INS2 + i));
  127. addOutput(Port::create<GateOutPort>(Vec(63, 10 + group_offset + spacing * i), Port::OUTPUT, module, MentalRadioButtons::BUTTON2_OUT + i));
  128. addParam(ParamWidget::create<LEDButton>(Vec(35, 10 + 3 + group_offset + spacing * i), module, MentalRadioButtons::BUTTON2 + i, 0.0, 1.0, 0.0));
  129. addChild(ModuleLightWidget::create<MedLight<BlueLED>>(Vec(40,10 + 8 + group_offset + spacing * i), module, MentalRadioButtons::BUTTON2_LEDS + i));
  130. }
  131. }
  132. } // namespace rack_plugin_mental
  133. using namespace rack_plugin_mental;
  134. RACK_PLUGIN_MODEL_INIT(mental, MentalRadioButtons) {
  135. Model *modelMentalRadioButtons = Model::create<MentalRadioButtons, MentalRadioButtonsWidget>("mental", "MentalRadioButtons", "Radio Buttons", CONTROLLER_TAG, SWITCH_TAG, UTILITY_TAG);
  136. return modelMentalRadioButtons;
  137. }