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.

183 lines
5.8KB

  1. //**************************************************************************************
  2. //Flow module for VCV Rack by Alfredo Santamaria - AS - https://github.com/AScustomWorks/AS
  3. //
  4. //**************************************************************************************
  5. #include "AS.hpp"
  6. #include "dsp/digital.hpp"
  7. struct Flow: Module {
  8. enum ParamIds {
  9. SWITCH_1,
  10. SWITCH_2,
  11. MODE_PARAM,
  12. NUM_PARAMS
  13. };
  14. enum InputIds {
  15. INPUT_1,
  16. INPUT_2,
  17. RESET_1,
  18. RESET_2,
  19. CV_TRIG_INPUT_1,
  20. CV_TRIG_INPUT_2,
  21. NUM_INPUTS
  22. };
  23. enum OutputIds {
  24. OUTPUT_1,
  25. OUTPUT_2,
  26. NUM_OUTPUTS
  27. };
  28. enum LightIds {
  29. TRIGGER_LED_1,
  30. TRIGGER_LED_2,
  31. NUM_LIGHTS
  32. };
  33. SchmittTrigger btnTrigger1;
  34. SchmittTrigger extTrigger1;
  35. SchmittTrigger extReset1;
  36. SchmittTrigger btnTrigger2;
  37. SchmittTrigger extTrigger2;
  38. SchmittTrigger extReset2;
  39. bool on_1 = false;
  40. bool on_2 = false;
  41. bool light_inverted = false;
  42. float mute_fade1 =0.0f;
  43. float mute_fade2 =0.0f;
  44. const float fade_speed = 0.001f;
  45. Flow() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  46. void step() override;
  47. json_t *toJson()override {
  48. json_t *rootJm = json_object();
  49. json_t *on_statesJ = json_array();
  50. json_t *on_stateJ1 = json_integer((int) on_1);
  51. json_t *on_stateJ2 = json_integer((int) on_2);
  52. json_array_append_new(on_statesJ, on_stateJ1);
  53. json_array_append_new(on_statesJ, on_stateJ2);
  54. json_object_set_new(rootJm, "as_FlowStates", on_statesJ);
  55. return rootJm;
  56. }
  57. void fromJson(json_t *rootJm)override {
  58. json_t *on_statesJ = json_object_get(rootJm, "as_FlowStates");
  59. json_t *on_stateJ1 = json_array_get(on_statesJ, 0);
  60. json_t *on_stateJ2 = json_array_get(on_statesJ, 1);
  61. on_1 = !!json_integer_value(on_stateJ1);
  62. on_2 = !!json_integer_value(on_stateJ2);
  63. }
  64. };
  65. void Flow::step() {
  66. if (params[MODE_PARAM].value){
  67. //switch lights turn on when the switch is enabled
  68. light_inverted = false;
  69. }else{
  70. //switch lights turn off when the switch is enabled
  71. light_inverted = true;
  72. }
  73. //TRIGGER 1
  74. if (btnTrigger1.process(params[SWITCH_1].value)||extTrigger1.process(inputs[CV_TRIG_INPUT_1].value)) {
  75. on_1 = !on_1;
  76. }
  77. if (extReset1.process(inputs[RESET_1].value)) {
  78. on_1 = false;
  79. }
  80. //SOFT MUTE/UNMUTE
  81. mute_fade1 -= on_1 ? fade_speed : -fade_speed;
  82. if ( mute_fade1 < 0.0f ) {
  83. mute_fade1 = 0.0f;
  84. } else if ( mute_fade1 > 1.0f ) {
  85. mute_fade1 = 1.0f;
  86. }
  87. outputs[OUTPUT_1].value = inputs[INPUT_1].value * mute_fade1;
  88. if(light_inverted){
  89. lights[TRIGGER_LED_1].value = on_1 ? 0.0f : 1.0f;
  90. }else{
  91. lights[TRIGGER_LED_1].value = on_1 ? 1.0f : 0.0f;
  92. }
  93. //TRIGGER 2
  94. if (btnTrigger2.process(params[SWITCH_2].value)||extTrigger2.process(inputs[CV_TRIG_INPUT_2].value)) {
  95. on_2 = !on_2;
  96. }
  97. if (extReset2.process(inputs[RESET_2].value)) {
  98. on_2 = false;
  99. }
  100. //SOFT MUTE/UNMUTE
  101. mute_fade2 -= on_2 ? fade_speed : -fade_speed;
  102. if ( mute_fade2 < 0.0f ) {
  103. mute_fade2 = 0.0f;
  104. } else if ( mute_fade2 > 1.0f ) {
  105. mute_fade2 = 1.0f;
  106. }
  107. outputs[OUTPUT_2].value = inputs[INPUT_2].value * mute_fade2;
  108. if(light_inverted){
  109. lights[TRIGGER_LED_2].value = on_2 ? 0.0f : 1.0f;
  110. }else{
  111. lights[TRIGGER_LED_2].value = on_2 ? 1.0f : 0.0f;
  112. }
  113. }
  114. ////////////////////////////////////
  115. struct FlowWidget : ModuleWidget
  116. {
  117. FlowWidget(Flow *module);
  118. };
  119. FlowWidget::FlowWidget(Flow *module) : ModuleWidget(module) {
  120. setPanel(SVG::load(assetPlugin(plugin, "res/Flow.svg")));
  121. //SCREWS
  122. addChild(Widget::create<as_HexScrew>(Vec(RACK_GRID_WIDTH, 0)));
  123. addChild(Widget::create<as_HexScrew>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  124. addChild(Widget::create<as_HexScrew>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  125. addChild(Widget::create<as_HexScrew>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  126. //OLD/NEW SWITCH FROM 40-250 TO 30-300
  127. addParam(ParamWidget::create<as_CKSS>(Vec(67, 23), module, Flow::MODE_PARAM, 0.0f, 1.0f, 1.0f));
  128. static const float led_offset = 3.3;
  129. static const float led_center = 15;
  130. static const float y_offset = 150;
  131. //TRIGGER 1
  132. //SWITCH
  133. addParam(ParamWidget::create<BigLEDBezel>(Vec(led_center, 50), module, Flow::SWITCH_1, 0.0, 1.0, 0.0));
  134. addChild(ModuleLightWidget::create<GiantLight<RedLight>>(Vec(led_center+led_offset, 50+led_offset), module, Flow::TRIGGER_LED_1));
  135. //PORTS
  136. addInput(Port::create<as_PJ301MPort>(Vec(10, 140), Port::INPUT, module, Flow::CV_TRIG_INPUT_1));
  137. addInput(Port::create<as_PJ301MPort>(Vec(55, 140), Port::INPUT, module, Flow::RESET_1));
  138. addInput(Port::create<as_PJ301MPort>(Vec(10, 174), Port::INPUT, module, Flow::INPUT_1));
  139. addOutput(Port::create<as_PJ301MPort>(Vec(55, 174), Port::OUTPUT, module, Flow::OUTPUT_1));
  140. //TRIGGER 2
  141. //SWITCH
  142. addParam(ParamWidget::create<BigLEDBezel>(Vec(led_center, 50+y_offset), module, Flow::SWITCH_2, 0.0, 1.0, 0.0));
  143. addChild(ModuleLightWidget::create<GiantLight<RedLight>>(Vec(led_center+led_offset, 50+led_offset+y_offset), module, Flow::TRIGGER_LED_2));
  144. //PORTS
  145. addInput(Port::create<as_PJ301MPort>(Vec(10, 140+y_offset), Port::INPUT, module, Flow::CV_TRIG_INPUT_2));
  146. addInput(Port::create<as_PJ301MPort>(Vec(55, 140+y_offset), Port::INPUT, module, Flow::RESET_2));
  147. addInput(Port::create<as_PJ301MPort>(Vec(10, 174+y_offset), Port::INPUT, module, Flow::INPUT_2));
  148. addOutput(Port::create<as_PJ301MPort>(Vec(55, 174+y_offset), Port::OUTPUT, module, Flow::OUTPUT_2));
  149. }
  150. RACK_PLUGIN_MODEL_INIT(AS, Flow) {
  151. Model *modelFlow = Model::create<Flow, FlowWidget>("AS", "Flow", "Flow", SWITCH_TAG, UTILITY_TAG);
  152. return modelFlow;
  153. }