#================================================================================
#
#  Simple example for analog communication setup between 
#  analog input neuron and analog recorder.
#
#  Authors: Dejan Pecevski, dejan@igi.tugraz.at
#           Thomas Natschlaeger, thomas.natschlaeger@scch.at
#
#  Date: November 2006
#
#================================================================================


import sys

# add path to the pypcsim.so file = $PCSIM_HOME/lib
sys.path.append("../_build/lib")

from pypcsim import *
import random
from datetime import datetime
from math import *

random.seed( datetime.today().microsecond )
random.seed( 134987 )
tstart=datetime.today()

###################################################################
# Create an empty network
###################################################################
#net = SingleThreadNetwork( SimParameter( dt=Time.sec( DTsim ) , minDelay = Time.ms(1), constructionRNGSeed = 134987 ) )
#net = MultiThreadNetwork( 2, SimParameter( dt=Time.sec( DTsim ) , minDelay = Time.ms(1), constructionRNGSeed = 134987 ) )
net = SingleThreadNetwork( SimParameter( dt=Time.ms( 0.1 ) , minDelay = Time.ms(0.1), constructionRNGSeed = 134987 ) )
#net = DistributedMultiThreadNetwork( 4, SimParameter( dt=Time.sec( DTsim ) , minDelay = Time.ms(0.1), constructionRNGSeed = 134987 ) )

# specify the input values for the analog input neurons
input_values = [ 1, 2, 3, 4 ]

# create a model / template for the recorders that are to be created
rec_model = AnalogRecorder()

# create a model / template for the analog input neurons that are to be created
inp_neuron_model = AnalogInputNeuron()

# add 5 recorders in the network based on the model
rec = net.add(rec_model, 5)

# add 5 input neurons in the network based on the model
input_nrns = net.add(inp_neuron_model, 5)

# setup the analog input curves generated by the input neurons
for nrn in input_nrns:
    net.object(nrn).setAnalogValues(input_values)

# connect the input neurons to the recorders with connection delay = i * 0.1 ms
for i in range( len(input_nrns) ):
    net.connect(input_nrns[i], rec[i], Time.ms(0.1*i))

# simulate the network
net.advance( int( Time.ms(0.5).in_sec() / Time.ms(0.1).in_sec() ) )

# at the end print the recorded values by the recorders
for i in range(5):
    print "recorder " , i , " values:" , list(net.object(rec[i]).getRecordedValues())

