Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 KB | None | 0 0
  1. ///////////////////////////////////////////////////
  2. //
  3. //   Dual Counter - VCV Module
  4. //
  5. //   Strum 2017 - 2019
  6. //   Thanks to ML for the diaplay code
  7. //
  8. ///////////////////////////////////////////////////
  9.  
  10. #include "mental.hpp"
  11.  
  12. #include <sstream>
  13. #include <iomanip>
  14.  
  15. struct MentalCounters : Module {
  16.     enum ParamIds {
  17.     RST_BUTTON,
  18.     COUNT_NUM_PARAM,
  19.     RST_BUTTON_2,
  20.     COUNT_NUM_PARAM_2,
  21.         NUM_PARAMS
  22.     };  
  23.     enum InputIds {
  24.     CLK_IN,
  25.     RESET_IN,
  26.     CLK_IN_2,
  27.     RESET_IN_2,  
  28.         NUM_INPUTS
  29.     };
  30.     enum OutputIds {
  31.         OUTPUT,
  32.     OUTPUT_2,    
  33.         NUM_OUTPUTS
  34.     };
  35.  
  36.   dsp::SchmittTrigger clock_trigger;
  37.   dsp::SchmittTrigger reset_trigger;
  38.   int count_limit = 0;
  39.   int count = 0;
  40.   dsp::SchmittTrigger clock_trigger_2;
  41.   dsp::SchmittTrigger reset_trigger_2;
  42.   int count_limit_2 = 0;
  43.   int count_2 = 0;
  44.  
  45.     MentalCounters();
  46.  
  47.     void process(const ProcessArgs& args) override;
  48. };
  49.  
  50. MentalCounters::MentalCounters()
  51. {
  52.   params.resize(NUM_PARAMS);
  53.     inputs.resize(NUM_INPUTS);
  54.     outputs.resize(NUM_OUTPUTS);
  55.  
  56.   configParam(MentalCounters::COUNT_NUM_PARAM, 0.0, 32.0, 0.0, "");
  57.   configParam(MentalCounters::RST_BUTTON, 0.0, 1.0, 0.0, "");
  58.   configParam(MentalCounters::COUNT_NUM_PARAM_2, 0.0, 32.0, 0.0, "");
  59.   configParam(MentalCounters::RST_BUTTON_2, 0.0, 1.0, 0.0, "");
  60. }
  61.  
  62. void MentalCounters::process(const ProcessArgs& args)
  63. {
  64.   count_limit = round(params[COUNT_NUM_PARAM].getValue());
  65.   count_limit_2 = round(params[COUNT_NUM_PARAM_2].getValue());
  66.  
  67.   bool reset = false;
  68.   bool reset_2 = false;
  69.  
  70.   if (reset_trigger.process(params[RST_BUTTON].getValue())  || (reset_trigger.process(inputs[RESET_IN].getVoltage())))
  71.     {
  72.     reset = true;
  73.     count = 0;
  74.     outputs[OUTPUT].setVoltage(0);
  75.   }  
  76.   if (reset == false)
  77.     {
  78.         if (clock_trigger.process(inputs[CLK_IN].getVoltage()) && count <= count_limit)
  79.                     count++;   
  80.   }
  81.   if (count == count_limit) outputs[OUTPUT].setVoltage(10.0);
  82.   if (count > count_limit)
  83.   {
  84.     count = 0;
  85.     outputs[OUTPUT].setVoltage(0);
  86.   }
  87.   ///////////// counter 2
  88.   if (reset_trigger_2.process(params[RST_BUTTON_2].getValue())  || (reset_trigger_2.process(inputs[RESET_IN_2].getVoltage())))
  89.     {
  90.     reset_2 = true;
  91.     count_2 = 0;
  92.     outputs[OUTPUT_2].setVoltage(0);
  93.   }  
  94.   if (reset_2 == false)
  95.     {
  96.         if (clock_trigger_2.process(inputs[CLK_IN_2].getVoltage()) && count_2 <= count_limit_2)
  97.                     count_2++; 
  98.   }
  99.   if (count_2 == count_limit_2) outputs[OUTPUT_2].setVoltage(10.0);
  100.   if (count_2 > count_limit_2)
  101.   {
  102.     count_2 = 0;
  103.     outputs[OUTPUT_2].setVoltage(0);
  104.   }  
  105. }
  106.  
  107.  
  108. ////////////////////////////////////
  109. struct NumberDisplayWidget3 : TransparentWidget {
  110.  
  111.   int *value;
  112.   std::shared_ptr<Font> font;
  113.  
  114.   NumberDisplayWidget3() {
  115.     font = APP->window->loadFont(asset::plugin(pluginInstance, "res/Segment7Standard.ttf"));
  116.   };
  117.  
  118.   void draw(const DrawArgs& args) override
  119.   {
  120.     // Background
  121.     NVGcolor backgroundColor = nvgRGB(0x00, 0x00, 0x00);
  122.     NVGcolor StrokeColor = nvgRGB(0x00, 0x47, 0x7e);
  123.     nvgBeginPath(args.vg);
  124.     nvgRoundedRect(args.vg, -1.0, -1.0, box.size.x+2, box.size.y+2, 4.0);
  125.     nvgFillColor(args.vg, StrokeColor);
  126.     nvgFill(args.vg);
  127.     nvgBeginPath(args.vg);
  128.     nvgRoundedRect(args.vg, 0.0, 0.0, box.size.x, box.size.y, 4.0);
  129.     nvgFillColor(args.vg, backgroundColor);
  130.     nvgFill(args.vg);    
  131.    
  132.     // text
  133.     nvgFontSize(args.vg, 18);
  134.     nvgFontFaceId(args.vg, font->handle);
  135.     nvgTextLetterSpacing(args.vg, 2.5);
  136.  
  137.     std::stringstream to_display;  
  138.     to_display << std::setw(3) << *value;
  139.  
  140.     Vec textPos = Vec(6.0f, 17.0f);  
  141.     NVGcolor textColor = nvgRGB(0x00, 0x47, 0x7e);
  142.     nvgFillColor(args.vg, textColor);
  143.     nvgText(args.vg, textPos.x, textPos.y, to_display.str().c_str(), NULL);
  144.   }
  145. };
  146.  
  147. //////////////////////////////////
  148. struct MentalCountersWidget : ModuleWidget {
  149.   MentalCountersWidget(MentalCounters *module)
  150. {
  151.   setModule(module);
  152.  
  153.   setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/MentalCounters.svg")));
  154.  
  155.   int group_offset = 190;
  156.  
  157.   addParam(createParam<MedKnob>(Vec(2, 20), module, MentalCounters::COUNT_NUM_PARAM));
  158.   addInput(createInput<GateInPort>(Vec(3, 90), module, MentalCounters::CLK_IN));
  159.     addInput(createInput<GateInPort>(Vec(3, 120), module, MentalCounters::RESET_IN));
  160.  
  161.   addParam(createParam<LEDButton>(Vec(5, 145), module, MentalCounters::RST_BUTTON));
  162.  
  163.   addOutput(createOutput<GateOutPort>(Vec(33, 90), module, MentalCounters::OUTPUT));
  164.  
  165.   NumberDisplayWidget3 *display = new NumberDisplayWidget3();
  166.     display->box.pos = Vec(5,50);
  167.     display->box.size = Vec(50, 20);
  168.     display->value = &module->count_limit;
  169.     addChild(display);
  170.  
  171.   /////////// counter 2
  172.   addParam(createParam<MedKnob>(Vec(2, 20 + group_offset), module, MentalCounters::COUNT_NUM_PARAM_2));
  173.   addInput(createInput<GateInPort>(Vec(3, 90 + group_offset), module, MentalCounters::CLK_IN_2));
  174.     addInput(createInput<GateInPort>(Vec(3, 120 + group_offset), module, MentalCounters::RESET_IN_2));
  175.  
  176.   addParam(createParam<LEDButton>(Vec(5, 145 + group_offset), module, MentalCounters::RST_BUTTON_2));
  177.  
  178.   addOutput(createOutput<GateOutPort>(Vec(33, 90 + group_offset), module, MentalCounters::OUTPUT_2));
  179.  
  180.   NumberDisplayWidget3 *display_2 = new NumberDisplayWidget3();
  181.     display_2->box.pos = Vec(5,50 + group_offset);
  182.     display_2->box.size = Vec(50, 20);
  183.     display_2->value = &module->count_limit_2;
  184.     addChild(display_2);  
  185.      
  186. }
  187. };
  188.  
  189. Model *modelMentalCounters = createModel<MentalCounters, MentalCountersWidget>("MentalCounters");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement