Closed-loop optogenetic control of normal and pathological network dynamics