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.

151 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. SchmittTrigger ledTrigger[100];
  27. LEDS() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  28. void step() override;
  29. json_t *toJson() override {
  30. json_t *rootJ = json_object();
  31. // leds
  32. json_t *ledsJ = json_array();
  33. for (int i = 0; i < 100; i++) {
  34. json_t *ledJ = json_integer((int) ledState[i]);
  35. json_array_append_new(ledsJ, ledJ);
  36. }
  37. json_object_set_new(rootJ, "leds", ledsJ);
  38. return rootJ;
  39. }
  40. void fromJson(json_t *rootJ) override {
  41. // leds
  42. json_t *ledsJ = json_object_get(rootJ, "leds");
  43. if (ledsJ) {
  44. for (int i = 0; i < 100; i++) {
  45. json_t *ledJ = json_array_get(ledsJ, i);
  46. if (ledJ)
  47. ledState[i] = !!json_integer_value(ledJ);
  48. }
  49. }
  50. }
  51. void reset() override {
  52. for (int i = 0; i < 100; i++) {
  53. ledState[i] = false;
  54. }
  55. }
  56. void randomize() override {
  57. for (int i = 0; i < 100; i++) {
  58. ledState[i] = (randomUniform() > 0.5);
  59. }
  60. }
  61. };
  62. void LEDS::step() {
  63. if (rndTrigger.process(inputs[RND_INPUT].value))
  64. {for (int i = 0; i < 100; i++)
  65. {ledState[i] = (randomUniform() > 0.5);}
  66. }
  67. if (upTrigger.process(inputs[UP_INPUT].value))
  68. {
  69. for (int i = 0; i < 5; i++)
  70. {tempState[i] = ledState[i];}
  71. for (int i = 0; i < 95; i++)
  72. {ledState[i] = ledState[i+5];}
  73. for (int i = 0; i < 5; i++)
  74. {ledState[i+95] = tempState[i];}
  75. }
  76. for (int i = 0; i < 100; i++) {
  77. if (ledTrigger[i].process(params[ON_PARAM +i].value)) {ledState[i]=!ledState[i];}
  78. lights[LED_LIGHT +i].value=ledState[i];
  79. }
  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. }