<!DOCTYPE aesl-source>
<network>


<!--list of global events-->


<!--list of constants-->


<!--show keywords state-->
<keywords flag="true"/>


<!--node thymio-II-->
<node nodeId="58594" name="thymio-II">
var recording=0
var playing=0
var file=0
var lock=0
var wait=0
var l[8]

var speed_left
var speed_right
var acting
var light


mic.threshold=50 #set to 0 to start w/o waiting 

#init
call sound.system(-1)    #mute all system sounds
call leds.top(32,32,32)
callsub display
timer.period[0]=0
timer.period[1]=0

#events
onevent buttons
  # record sound
  if button.forward==1 and recording==0 and playing==0 and lock==0 then
     recording=1
     wait=1
     acting=0
  	 call leds.top(32,16,0)
  end
  # playsound
  if button.backward==1 and playing==0 and recording==0 and lock==0 then
     playing=1
     acting=0
  	 call leds.top(0,32,0)
  	 call sound.replay(file)
  end
  # next file
  if button.right==1 and playing==0 and recording==0 and lock==0 then
      lock=1
      acting=0
      timer.period[0]=500
      callsub nextfile  	    
  end
  #previous file
  if button.left==1 and playing==0 and recording==0 and lock==0 then     
     lock=1   #lock button.left and button.right
     acting=0
     timer.period[1]=500
     callsub prevfile	
  end
  #unlock buttons
  if  button.right==0 and button.left==0 and lock==1  then
    timer.period[0]=0
    timer.period[1]=0
  	lock=0  
  end   
  # stop
  if button.center==1 then
    acting=0
  	if recording==1 then
  	  call sound.record(-1)
  	  recording=0
  	  wait=0
    elseif playing==1 then
  	  call sound.replay(-1)
  	  playing=0       	  
    end
    call leds.top(32,32,32)
  end

onevent mic
  if wait==1 then
  	call sound.record(file)
    call leds.top(32,0,0)
    wait=0
  end

onevent sound.finished
  if playing==1 then
     if acting==0 then
       call leds.top(32,32,32)
     end
     playing=0  	
  end
 
onevent acc
  # diplay sound intensity
  if acting==1 then
    light=mic.intensity/8
    if playing==1 then
       # speaking
       call leds.top(0,0,light)
       call leds.bottom.left(0,0,light)
       call leds.bottom.right(0,0,light)	
    else
       # listening
       if speed_left!=0 or speed_right!=0 then
         light=0
       end
       call leds.top(0,light,0)
       call leds.bottom.left(0,light,0)
       call leds.bottom.right(0,light,0)	       
    end
  end

onevent rc5
  acting=1
  # select file
  if 1&lt;=rc5.command and rc5.command&lt;=8  and recording==0 and lock==0 then
     playing=1
  	 call leds.top(0,32,0)
  	 file=rc5.command-1
  	 callsub display
  	 call sound.replay(file)
  end 
  # move forward 
  if rc5.command==80 then
    speed_left+=100
    speed_right+=100
  end
  # move backward
  if rc5.command==81 then
    speed_left-=100
    speed_right-=100
  end
  # move left
  if rc5.command==85 then
    speed_left-=100
    speed_right+=100
  end
  # move right
  if rc5.command==86 then
    speed_left+=100
    speed_right-=100
  end
  # stop moving
  if rc5.command==87 then
  	speed_left=0
  	speed_right=0
  end
  callsub move
 
onevent timer0
   callsub nextfile
   
onevent timer1
   callsub prevfile  
  
  
# procedures 
sub display
  call math.fill(l,0) # switch off all leds
  l[file]=32          # switch on led "file"
  call leds.circle(l[0],l[1],l[2],l[3],l[4],l[5],l[6],l[7])
 
sub nextfile
  file+=1
  if file>7 then
     file-=8
  end
  callsub display
   
sub prevfile
  file-=1
  if file&lt;0 then
    file+=8
  end
  callsub display
   


sub move
  if speed_left>500 then
  	speed_left=500
  end
  if speed_left&lt;-500 then
  	speed_left=-500
  end
  if speed_right>500 then
  	speed_right=500
  end
  if speed_right&lt;-500 then
  	speed_right=-500
  end
  motor.left.target=speed_left
  motor.right.target=speed_right
  
  
  
</node>


</network>
