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.

191 lines
3.9KB

  1. #include "Southpole.hpp"
  2. struct Abr : Module
  3. {
  4. enum ParamIds
  5. {
  6. SWITCH1_PARAM,
  7. SWITCH2_PARAM,
  8. SWITCH3_PARAM,
  9. SWITCH4_PARAM,
  10. SWITCH5_PARAM,
  11. SWITCH6_PARAM,
  12. SWITCH7_PARAM,
  13. SWITCH8_PARAM,
  14. NUM_PARAMS
  15. };
  16. enum InputIds
  17. {
  18. INA1_INPUT,
  19. INA2_INPUT,
  20. INA3_INPUT,
  21. INA4_INPUT,
  22. INA5_INPUT,
  23. INA6_INPUT,
  24. INA7_INPUT,
  25. INA8_INPUT,
  26. INB1_INPUT,
  27. INB2_INPUT,
  28. INB3_INPUT,
  29. INB4_INPUT,
  30. INB5_INPUT,
  31. INB6_INPUT,
  32. INB7_INPUT,
  33. INB8_INPUT,
  34. NUM_INPUTS
  35. };
  36. enum OutputIds
  37. {
  38. OUT1_OUTPUT,
  39. OUT2_OUTPUT,
  40. OUT3_OUTPUT,
  41. OUT4_OUTPUT,
  42. OUT5_OUTPUT,
  43. OUT6_OUTPUT,
  44. OUT7_OUTPUT,
  45. OUT8_OUTPUT,
  46. SUMA_OUTPUT,
  47. SUMB_OUTPUT,
  48. SUM_OUTPUT,
  49. NUM_OUTPUTS
  50. };
  51. enum LightIds
  52. {
  53. NUM_LIGHTS
  54. };
  55. bool swState[8] = {};
  56. Abr() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS)
  57. {
  58. reset();
  59. }
  60. void step() override;
  61. void reset() override
  62. {
  63. for (int i = 0; i < 8; i++)
  64. {
  65. swState[i] = false;
  66. }
  67. }
  68. void randomize() override
  69. {
  70. for (int i = 0; i < 8; i++)
  71. {
  72. swState[i] = (randomf() < 0.5);
  73. }
  74. }
  75. json_t *toJson() override
  76. {
  77. json_t *rootJ = json_object();
  78. json_t *swStatesJ = json_array();
  79. for (int i = 0; i < 8; i++)
  80. {
  81. json_t *swStateJ = json_boolean(swState[i]);
  82. json_array_append_new(swStatesJ, swStateJ);
  83. }
  84. json_object_set_new(rootJ, "swStates", swStatesJ);
  85. return rootJ;
  86. }
  87. void fromJson(json_t *rootJ) override
  88. {
  89. json_t *swStatesJ = json_object_get(rootJ, "swStates");
  90. if (swStatesJ)
  91. {
  92. for (int i = 0; i < 8; i++)
  93. {
  94. json_t *stateJ = json_array_get(swStatesJ, i);
  95. if (stateJ) {
  96. swState[i] = json_boolean_value(stateJ);
  97. }
  98. }
  99. }
  100. }
  101. };
  102. void Abr::step()
  103. {
  104. float outa = 0.;
  105. float outb = 0.;
  106. float out = 0.;
  107. for(int i = 0; i < 8; i++)
  108. {
  109. swState[i] = params[SWITCH1_PARAM + i].value > 0.5;
  110. if ( !swState[i] ) {
  111. if(inputs[INA1_INPUT + i].active)
  112. {
  113. float ina = inputs[INA1_INPUT + i].value;
  114. outputs[OUT1_OUTPUT + i].value = ina;
  115. outa += ina;
  116. out += ina;
  117. }
  118. } else {
  119. if(inputs[INB1_INPUT + i].active)
  120. {
  121. float inb = inputs[INB1_INPUT + i].value;
  122. outputs[OUT1_OUTPUT + i].value = inb;
  123. outb += inb;
  124. out += inb;
  125. }
  126. }
  127. }
  128. outputs[SUMA_OUTPUT].value = outa;
  129. outputs[SUMB_OUTPUT].value = outb;
  130. outputs[SUM_OUTPUT].value = out;
  131. }
  132. AbrWidget::AbrWidget()
  133. {
  134. auto *module = new Abr();
  135. setModule(module);
  136. box.size = Vec(6 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  137. {
  138. auto *panel = new SVGPanel();
  139. panel->box.size = box.size;
  140. panel->setBackground(SVG::load(assetPlugin(plugin, "res/Abr.svg")));
  141. addChild(panel);
  142. }
  143. const float x1 = 3.;
  144. const float x2 = 4.+20.;
  145. const float x3 = 5.+43.;
  146. const float x4 = 5.+63.;
  147. float yPos = 18.;
  148. for(int i = 0; i < 8; i++)
  149. {
  150. yPos += 32.;
  151. addInput(createInput<sp_Port>(Vec(x1, yPos), module, Abr::INA1_INPUT + i));
  152. addParam(createParam<sp_Switch>(Vec(x2+1, 3 + yPos), module, Abr::SWITCH1_PARAM + i, 0.0, 1.0, 0.0));
  153. addInput(createInput<sp_Port>(Vec(x3, yPos), module, Abr::INB1_INPUT + i));
  154. addOutput(createOutput<sp_Port>(Vec(x4, yPos), module, Abr::OUT1_OUTPUT + i));
  155. }
  156. yPos += 48.;
  157. addOutput(createOutput<sp_Port>(Vec(x1, yPos), module, Abr::SUMA_OUTPUT));
  158. addOutput(createOutput<sp_Port>(Vec(x3, yPos), module, Abr::SUMB_OUTPUT));
  159. addOutput(createOutput<sp_Port>(Vec(x4, yPos), module, Abr::SUM_OUTPUT));
  160. }