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.

131 lines
3.6KB

  1. ///////////////////////////////////////////////////
  2. //
  3. // 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 MentalButtons : Module {
  12. enum ParamIds {
  13. MOMENT,
  14. BUTTON_PARAM = MOMENT + 7,
  15. NUM_PARAMS = BUTTON_PARAM + 7
  16. };
  17. enum InputIds {
  18. NUM_INPUTS
  19. };
  20. enum OutputIds {
  21. MOMENT_OUT,
  22. OUTPUT = MOMENT_OUT +7,
  23. NUM_OUTPUTS = OUTPUT + 7
  24. };
  25. enum LightIds {
  26. BUTTON_LEDS,
  27. MOMENT_LEDS = BUTTON_LEDS + 7,
  28. NUM_LIGHTS = MOMENT_LEDS + 7
  29. };
  30. SchmittTrigger button_triggers[7];
  31. bool button_states[7] = {0,0,0,0,0,0,0};
  32. MentalButtons() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  33. void step() override;
  34. json_t *toJson() override
  35. {
  36. json_t *rootJ = json_object();
  37. // button states
  38. json_t *button_statesJ = json_array();
  39. for (int i = 0; i < 7; i++)
  40. {
  41. json_t *button_stateJ = json_integer((int) button_states[i]);
  42. json_array_append_new(button_statesJ, button_stateJ);
  43. }
  44. json_object_set_new(rootJ, "buttons", button_statesJ);
  45. return rootJ;
  46. }
  47. void fromJson(json_t *rootJ) override
  48. {
  49. // button states
  50. json_t *button_statesJ = json_object_get(rootJ, "buttons");
  51. if (button_statesJ)
  52. {
  53. for (int i = 0; i < 7; i++)
  54. {
  55. json_t *button_stateJ = json_array_get(button_statesJ, i);
  56. if (button_stateJ)
  57. button_states[i] = !!json_integer_value(button_stateJ);
  58. }
  59. }
  60. }
  61. };
  62. void MentalButtons::step()
  63. {
  64. for (int i = 0 ; i < 7 ; i++)
  65. {
  66. if (button_triggers[i].process(params[BUTTON_PARAM + i].value))
  67. {
  68. button_states[i] = !button_states[i];
  69. }
  70. lights[BUTTON_LEDS + i ].value = (button_states[i]) ? 1.0 : 0.0;
  71. outputs[OUTPUT + i].value = button_states[i] * 10.0;
  72. if (params[MOMENT + i].value > 0.0)
  73. {
  74. lights[MOMENT_LEDS + i ].value = 1.0;
  75. outputs[MOMENT_OUT + i].value = 10.0;
  76. }
  77. else
  78. {
  79. lights[MOMENT_LEDS + i ].value = 0.0;
  80. outputs[MOMENT_OUT + i].value = 0.0;
  81. }
  82. }
  83. }
  84. /////////////////////////////////////////////////////////////////////////////////////////
  85. struct MentalButtonsWidget : ModuleWidget {
  86. MentalButtonsWidget(MentalButtons *module);
  87. };
  88. MentalButtonsWidget::MentalButtonsWidget(MentalButtons *module) : ModuleWidget(module)
  89. {
  90. setPanel(SVG::load(assetPlugin(plugin, "res/MentalButtons.svg")));
  91. int spacing = 25;
  92. int group_offset = 184;
  93. int top_space = 15;
  94. for (int i = 0; i < 7 ; i++)
  95. {
  96. addOutput(Port::create<GateOutPort>(Vec(33, top_space + spacing * i), Port::OUTPUT, module, MentalButtons::OUTPUT + i));
  97. addParam(ParamWidget::create<LEDButton>(Vec(5, top_space + 3 + spacing * i), module, MentalButtons::BUTTON_PARAM +i, 0.0, 1.0, 0.0));
  98. addChild(ModuleLightWidget::create<MedLight<BlueLED>>(Vec(10, top_space + 8 + spacing * i), module, MentalButtons::BUTTON_LEDS + i));
  99. /// momentarys
  100. addOutput(Port::create<GateOutPort>(Vec(33, 10 + group_offset + spacing * i), Port::OUTPUT, module, MentalButtons::MOMENT_OUT + i));
  101. addParam(ParamWidget::create<LEDButton>(Vec(5, 10 + 3 + group_offset + spacing * i), module, MentalButtons::MOMENT + i, 0.0, 1.0, 0.0));
  102. addChild(ModuleLightWidget::create<MedLight<BlueLED>>(Vec(10,10 + 8 + group_offset + spacing * i), module, MentalButtons::MOMENT_LEDS + i));
  103. }
  104. }
  105. } // namespace rack_plugin_mental
  106. using namespace rack_plugin_mental;
  107. RACK_PLUGIN_MODEL_INIT(mental, MentalButtons) {
  108. Model *modelMentalButtons = Model::create<MentalButtons, MentalButtonsWidget>("mental", "MentalButtons", "Buttons", UTILITY_TAG);
  109. return modelMentalButtons;
  110. }