# This file includes callbacks for dynamical networks.

"""
    $TYPEDSIGNATURES

Returns a `PCMCallback`. `nbits` is the number of bits, `ti` is the initial time, `tbit` is the bit time, `high` is the high level of coupling and `low` is the low level of coupling.
"""
function PCMCallback(nbits::Int = 10, tbit::Real = 1.0;
    ti::Real = 0.0, high::Real = 1.0, low::Real = 0.0001, src::Int = 1, dst::Int = 2)
    tf = tbit * nbits
    tstops = ti:tbit:tf-tbit
    condition(u, t, integrator) = integrator.t ∈ tstops
    function action!(integrator)
        net, bits = integrator.sol.prob.p
        popfirst!(bits) ? add_edge!(net.graph, src, dst, high) : add_edge!(net.graph, src, dst, low)
        # integrator.u += rand(length(integrator.u))  # Perturbation to desynchronize 
    end
    DiscreteCallback(condition, action!)
end
