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.

166 lines
4.2KB

  1. ///////////////////////////////////////////////////////////////////
  2. //
  3. // Patch Matrix Module for VCV
  4. //
  5. // Strum 2017
  6. // strum@softhome.net
  7. //
  8. ///////////////////////////////////////////////////////////////////
  9. #include "mental.hpp"
  10. #include "dsp/digital.hpp"
  11. namespace rack_plugin_mental {
  12. struct MentalPatchMatrix : Module {
  13. enum ParamIds {
  14. SWITCHES,
  15. NUM_PARAMS = SWITCHES + 100
  16. };
  17. enum InputIds {
  18. INPUTS,
  19. NUM_INPUTS = INPUTS + 10
  20. };
  21. enum OutputIds {
  22. OUTPUTS,
  23. NUM_OUTPUTS = OUTPUTS + 10
  24. };
  25. enum LightIds {
  26. SWITCH_LIGHTS,
  27. NUM_LIGHTS = SWITCH_LIGHTS + 100
  28. };
  29. SchmittTrigger switch_triggers[10][10];
  30. bool switch_states[10][10] =
  31. {{0,0,0,0,0,0,0,0,0,0},
  32. {0,0,0,0,0,0,0,0,0,0},
  33. {0,0,0,0,0,0,0,0,0,0},
  34. {0,0,0,0,0,0,0,0,0,0},
  35. {0,0,0,0,0,0,0,0,0,0},
  36. {0,0,0,0,0,0,0,0,0,0},
  37. {0,0,0,0,0,0,0,0,0,0},
  38. {0,0,0,0,0,0,0,0,0,0},
  39. {0,0,0,0,0,0,0,0,0,0},
  40. {0,0,0,0,0,0,0,0,0,0}};
  41. float input_values[10] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
  42. float sums[10] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
  43. MentalPatchMatrix() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  44. void step() override;
  45. json_t *toJson() override
  46. {
  47. json_t *rootJ = json_object();
  48. // button states
  49. json_t *button_statesJ = json_array();
  50. for (int i = 0; i < 10; i++)
  51. {
  52. for (int j = 0; j < 10; j++)
  53. {
  54. json_t *button_stateJ = json_integer((int) switch_states[i][j]);
  55. json_array_append_new(button_statesJ, button_stateJ);
  56. }
  57. }
  58. json_object_set_new(rootJ, "buttons", button_statesJ);
  59. return rootJ;
  60. }
  61. void fromJson(json_t *rootJ) override
  62. {
  63. // button states
  64. json_t *button_statesJ = json_object_get(rootJ, "buttons");
  65. if (button_statesJ)
  66. {
  67. for (int i = 0; i < 10; i++)
  68. {
  69. for (int j = 0; j < 10; j++)
  70. {
  71. json_t *button_stateJ = json_array_get(button_statesJ, i*10 + j);
  72. if (button_stateJ)
  73. switch_states[i][j] = !!json_integer_value(button_stateJ);
  74. }
  75. }
  76. }
  77. }
  78. };
  79. ////// Step function
  80. void MentalPatchMatrix::step() {
  81. for ( int i = 0 ; i < 10 ; i++)
  82. {
  83. sums[i] = 0.0;
  84. }
  85. // deal with buttons
  86. for (int i = 0 ; i < 10 ; i++)
  87. {
  88. for (int j = 0 ; j < 10 ; j++)
  89. {
  90. if (switch_triggers[i][j].process(params[SWITCHES+j*10 + i].value))
  91. {
  92. switch_states[i][j] = !switch_states[i][j];
  93. }
  94. lights[SWITCH_LIGHTS + i + j * 10].value = (switch_states[i][j]) ? 1.0 : 0.0;
  95. }
  96. }
  97. // get inputs
  98. for (int i = 0 ; i < 10 ; i++)
  99. {
  100. input_values[i] = inputs[INPUTS + i].value;
  101. }
  102. // add inputs
  103. for (int i = 0 ; i < 10 ; i++)
  104. {
  105. for (int j = 0 ; j < 10 ; j++)
  106. {
  107. if (switch_states[j][i]) sums[i] += input_values[j];
  108. }
  109. }
  110. /// outputs
  111. for (int i = 0 ; i < 10 ; i++)
  112. {
  113. outputs[OUTPUTS + i].value = sums[i];
  114. }
  115. }
  116. ///// Gui
  117. struct MentalPatchMatrixWidget : ModuleWidget {
  118. MentalPatchMatrixWidget(MentalPatchMatrix *module);
  119. };
  120. MentalPatchMatrixWidget::MentalPatchMatrixWidget(MentalPatchMatrix *module) : ModuleWidget(module)
  121. {
  122. setPanel(SVG::load(assetPlugin(plugin, "res/MentalPatchMatrix.svg")));
  123. int top_row = 75;
  124. int row_spacing = 25;
  125. int column_spacing = 25;
  126. for (int i = 0 ; i < 10 ; i++)
  127. {
  128. addInput(Port::create<InPort>(Vec(3, i * row_spacing + top_row), Port::INPUT, module, MentalPatchMatrix::INPUTS + i));
  129. addOutput(Port::create<OutPort>(Vec(33 + i * column_spacing , top_row + 10 * row_spacing), Port::OUTPUT, module, MentalPatchMatrix::OUTPUTS + i));
  130. for(int j = 0 ; j < 10 ; j++ )
  131. {
  132. addParam(ParamWidget::create<LEDButton>(Vec(35 + column_spacing * j, top_row + row_spacing * i), module, MentalPatchMatrix::SWITCHES + i + j * 10, 0.0, 1.0, 0.0));
  133. addChild(ModuleLightWidget::create<MedLight<BlueLED>>(Vec(35 + column_spacing * j + 5, top_row + row_spacing * i + 5), module, MentalPatchMatrix::SWITCH_LIGHTS + i + j * 10));
  134. }
  135. }
  136. }
  137. } // namespace rack_plugin_mental
  138. using namespace rack_plugin_mental;
  139. RACK_PLUGIN_MODEL_INIT(mental, MentalPatchMatrix) {
  140. Model *modelMentalPatchMatrix = Model::create<MentalPatchMatrix, MentalPatchMatrixWidget>("mental", "MentalPatchMatrix", "Patch Matrix", UTILITY_TAG);
  141. return modelMentalPatchMatrix;
  142. }