#!/usr/bin/python3
# This file was automatically generated from drawio2cbd with the command:
#   /home/red/git/DrawioConvert/__main__.py StateEventLocation.xml -av -F CBD -e Experiment -t 100 -E delta=0.1

from CBD.Core import *
from CBD.lib.std import *
from CBD.lib.plotting import SignalPlotBlock

DELTA_T = 0.1

class Experiment(CBD):
    def __init__(self, block_name, axis1=(None), axis2=(None)):
        super().__init__(block_name, input_ports=[], output_ports=[])

        # Create the Blocks
        self.addBlock(StateEventLocationBlock("sel"))
        self.addBlock(SignalPlotBlock("eventplot", axes=(axis1), type=("plot"), xlim=(SignalPlotBlock.full_domain), ylim=(SignalPlotBlock.full_domain)))
        self.addBlock(SignalPlotBlock("valueplot", axes=(axis2), type=("plot"), xlim=(SignalPlotBlock.full_domain), ylim=(SignalPlotBlock.full_domain)))
        self.addBlock(AdderBlock("sum"))
        self.addBlock(ConstantBlock("threshold", value=(-50)))
        self.addBlock(TimeBlock("time"))

        # Create the Connections
        self.addConnection("sel", "eventplot", output_port_name='OUT1', input_port_name='IN1')
        self.addConnection("sum", "valueplot", output_port_name='OUT1', input_port_name='IN1')
        self.addConnection("sum", "sel", output_port_name='OUT1', input_port_name='IN1')
        self.addConnection("threshold", "sum", output_port_name='OUT1', input_port_name='IN1')
        self.addConnection("time", "sum", output_port_name='OUT1', input_port_name='IN2')


class StateEventLocationBlock(CBD):
    """
    Outputs '1' if the IN1 signal passes through zero at the current point in time (or in between the previous and the current point in time), otherwise it outputs '0'.
    """
    def __init__(self, block_name):
        super().__init__(block_name, input_ports=['IN1'], output_ports=['OUT1'])

        # Create the Blocks
        self.addBlock(ConstantBlock("zero", value=(0)))
        self.addBlock(LessThanBlock("lt"))
        self.addBlock(LessThanOrEqualsBlock("lte"))
        self.addBlock(DelayBlock("delay"))
        self.addBlock(AndBlock("and", numberOfInputs=(2)))

        # Create the Connections
        self.addConnection("IN1", "lte", input_port_name='IN2')
        self.addConnection("IN1", "delay", input_port_name='IN1')
        self.addConnection("IN1", "delay", input_port_name='IC')
        self.addConnection("zero", "lt", output_port_name='OUT1', input_port_name='IN2')
        self.addConnection("zero", "lte", output_port_name='OUT1', input_port_name='IN1')
        self.addConnection("delay", "lt", output_port_name='OUT1', input_port_name='IN1')
        self.addConnection("lt", "and", output_port_name='OUT1', input_port_name='IN1')
        self.addConnection("lte", "and", output_port_name='OUT1', input_port_name='IN2')
        self.addConnection("and", "OUT1", output_port_name='OUT1')


