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.

150 lines
3.2KB

  1. #include "cf.hpp"
  2. #include "dsp/digital.hpp"
  3. namespace rack_plugin_cf {
  4. struct LEDS : Module {
  5. enum ParamIds {
  6. ON_PARAM,
  7. NUM_PARAMS = ON_PARAM + 100
  8. };
  9. enum InputIds {
  10. RND_INPUT,
  11. UP_INPUT,
  12. NUM_INPUTS
  13. };
  14. enum OutputIds {
  15. NUM_OUTPUTS
  16. };
  17. enum LightIds {
  18. LED_LIGHT,
  19. NUM_LIGHTS = LED_LIGHT + 100
  20. };
  21. int wait = 0;
  22. bool ledState[100] = {};
  23. bool tempState[5] = {};
  24. SchmittTrigger rndTrigger;
  25. SchmittTrigger upTrigger;
  26. LEDS() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  27. void step() override;
  28. json_t *toJson() override {
  29. json_t *rootJ = json_object();
  30. // leds
  31. json_t *ledsJ = json_array();
  32. for (int i = 0; i < 100; i++) {
  33. json_t *ledJ = json_integer((int) ledState[i]);
  34. json_array_append_new(ledsJ, ledJ);
  35. }
  36. json_object_set_new(rootJ, "leds", ledsJ);
  37. return rootJ;
  38. }
  39. void fromJson(json_t *rootJ) override {
  40. // leds
  41. json_t *ledsJ = json_object_get(rootJ, "leds");
  42. if (ledsJ) {
  43. for (int i = 0; i < 100; i++) {
  44. json_t *ledJ = json_array_get(ledsJ, i);
  45. if (ledJ)
  46. ledState[i] = !!json_integer_value(ledJ);
  47. }
  48. }
  49. }
  50. void reset() override {
  51. for (int i = 0; i < 100; i++) {
  52. ledState[i] = false;
  53. }
  54. }
  55. void randomize() override {
  56. for (int i = 0; i < 100; i++) {
  57. ledState[i] = (randomUniform() > 0.5);
  58. }
  59. }
  60. };
  61. void LEDS::step() {
  62. if (rndTrigger.process(inputs[RND_INPUT].value))
  63. {for (int i = 0; i < 100; i++)
  64. {ledState[i] = (randomUniform() > 0.5);}
  65. }
  66. if (upTrigger.process(inputs[UP_INPUT].value))
  67. {
  68. for (int i = 0; i < 5; i++)
  69. {tempState[i] = ledState[i];}
  70. for (int i = 0; i < 95; i++)
  71. {ledState[i] = ledState[i+5];}
  72. for (int i = 0; i < 5; i++)
  73. {ledState[i+95] = tempState[i];}
  74. }
  75. if (wait == 0) {
  76. for (int i = 0; i < 100; i++) {
  77. if (params[ON_PARAM +i].value) {ledState[i]=!ledState[i]; wait = 20000;}
  78. lights[LED_LIGHT +i].value=ledState[i];
  79. }} else wait = wait-1;
  80. }
  81. struct LButton : SVGSwitch, MomentarySwitch {
  82. LButton() {
  83. addFrame(SVG::load(assetPlugin(plugin, "res/L.svg")));
  84. addFrame(SVG::load(assetPlugin(plugin, "res/Ldown.svg")));
  85. sw->wrap();
  86. box.size = sw->box.size;
  87. }
  88. };
  89. struct LEDSWidget : ModuleWidget {
  90. LEDSWidget(LEDS *module);
  91. };
  92. LEDSWidget::LEDSWidget(LEDS *module) : ModuleWidget(module) {
  93. setPanel(SVG::load(assetPlugin(plugin, "res/LEDS.svg")));
  94. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  95. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 0)));
  96. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  97. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 365)));
  98. for (int i = 0; i < 20; i++) {
  99. for (int j = 0; j < 5; j++) {
  100. addParam(ParamWidget::create<LButton>(Vec(j*15+10-0.8, i*15+35-0.8), module, LEDS::ON_PARAM + (i*5+j), 0.0, 1.0, 0.0));
  101. addChild(ModuleLightWidget::create<MediumLight<BlueLight>>(Vec(j*15+10, i*15+35), module, LEDS::LED_LIGHT + (i*5+j)));
  102. }}
  103. addInput(Port::create<PJ301MPort>(Vec(11, 340), Port::INPUT, module, LEDS::RND_INPUT));
  104. addInput(Port::create<PJ301MPort>(Vec(54, 340), Port::INPUT, module, LEDS::UP_INPUT));
  105. }
  106. } // namespace rack_plugin_cf
  107. using namespace rack_plugin_cf;
  108. RACK_PLUGIN_MODEL_INIT(cf, LEDS) {
  109. Model *modelLEDS = Model::create<LEDS, LEDSWidget>("cf", "LEDS", "Leds", VISUAL_TAG);
  110. return modelLEDS;
  111. }