BogaudioModules

BogaudioModules for VCV Rack
Log | Files | Refs | README | LICENSE

FlipFlop.cpp (3036B)


      1 
      2 #include "FlipFlop.hpp"
      3 
      4 void FlipFlop::reset() {
      5 	for (int i = 0; i < maxChannels; ++i) {
      6 		_flipped1[i] = false;
      7 		_trigger1[i].reset();
      8 		_resetTrigger1[i].reset();
      9 
     10 		_flipped2[i] = false;
     11 		_trigger2[i].reset();
     12 		_resetTrigger2[i].reset();
     13 	}
     14 }
     15 
     16 void FlipFlop::processAll(const ProcessArgs& args) {
     17 	for (int i = 0, n = std::max(1, inputs[IN1_INPUT].getChannels()); i < n; ++i) {
     18 		channelStep(
     19 			i,
     20 			n,
     21 			inputs[IN1_INPUT],
     22 			inputs[RESET1_INPUT],
     23 			outputs[A1_OUTPUT],
     24 			outputs[B1_OUTPUT],
     25 			_trigger1,
     26 			_resetTrigger1,
     27 			_flipped1
     28 		);
     29 	}
     30 
     31 	for (int i = 0, n = std::max(1, inputs[IN2_INPUT].getChannels()); i < n; ++i) {
     32 		channelStep(
     33 			i,
     34 			n,
     35 			inputs[IN2_INPUT],
     36 			inputs[RESET2_INPUT],
     37 			outputs[A2_OUTPUT],
     38 			outputs[B2_OUTPUT],
     39 			_trigger2,
     40 			_resetTrigger2,
     41 			_flipped2
     42 		);
     43 	}
     44 }
     45 
     46 void FlipFlop::channelStep(
     47 	int c,
     48 	int channels,
     49 	Input& triggerInput,
     50 	Input& resetInput,
     51 	Output& aOutput,
     52 	Output& bOutput,
     53 	PositiveZeroCrossing* trigger,
     54 	Trigger* resetTrigger,
     55 	bool* flipped
     56 ) {
     57 	bool triggered = trigger[c].next(triggerInput.getVoltage(c));
     58 	resetTrigger[c].process(resetInput.getPolyVoltage(c));
     59 	if (resetTrigger[c].isHigh()) {
     60 		flipped[c] = false;
     61 	}
     62 	else if (triggered) {
     63 		flipped[c] = !flipped[c];
     64 	}
     65 
     66 	if (flipped[c]) {
     67 		aOutput.setChannels(channels);
     68 		aOutput.setVoltage(0.0f, c);
     69 		bOutput.setChannels(channels);
     70 		bOutput.setVoltage(5.0f, c);
     71 	}
     72 	else {
     73 		aOutput.setChannels(channels);
     74 		aOutput.setVoltage(5.0f, c);
     75 		bOutput.setChannels(channels);
     76 		bOutput.setVoltage(0.0f, c);
     77 	}
     78 }
     79 
     80 struct FlipFlopWidget : BGModuleWidget {
     81 	static constexpr int hp = 3;
     82 
     83 	FlipFlopWidget(FlipFlop* module) {
     84 		setModule(module);
     85 		box.size = Vec(RACK_GRID_WIDTH * hp, RACK_GRID_HEIGHT);
     86 		setPanel(box.size, "FlipFlop");
     87 		createScrews();
     88 
     89 		// generated by svg_widgets.rb
     90 		auto in1InputPosition = Vec(10.5, 21.0);
     91 		auto reset1InputPosition = Vec(10.5, 56.0);
     92 		auto in2InputPosition = Vec(10.5, 172.0);
     93 		auto reset2InputPosition = Vec(10.5, 207.0);
     94 
     95 		auto a1OutputPosition = Vec(10.5, 94.0);
     96 		auto b1OutputPosition = Vec(10.5, 129.0);
     97 		auto a2OutputPosition = Vec(10.5, 245.0);
     98 		auto b2OutputPosition = Vec(10.5, 280.0);
     99 		// end generated by svg_widgets.rb
    100 
    101 		addInput(createInput<Port24>(in1InputPosition, module, FlipFlop::IN1_INPUT));
    102 		addInput(createInput<Port24>(reset1InputPosition, module, FlipFlop::RESET1_INPUT));
    103 		addInput(createInput<Port24>(in2InputPosition, module, FlipFlop::IN2_INPUT));
    104 		addInput(createInput<Port24>(reset2InputPosition, module, FlipFlop::RESET2_INPUT));
    105 
    106 		addOutput(createOutput<Port24>(a1OutputPosition, module, FlipFlop::A1_OUTPUT));
    107 		addOutput(createOutput<Port24>(b1OutputPosition, module, FlipFlop::B1_OUTPUT));
    108 		addOutput(createOutput<Port24>(a2OutputPosition, module, FlipFlop::A2_OUTPUT));
    109 		addOutput(createOutput<Port24>(b2OutputPosition, module, FlipFlop::B2_OUTPUT));
    110 	}
    111 };
    112 
    113 Model* modelFlipFlop = bogaudio::createModel<FlipFlop, FlipFlopWidget>("Bogaudio-FlipFlop", "FLIPFLOP", "Dual flipflop", "Logic", "Dual", "Polyphonic");