amigo / amigo.py

#!/usr/bin/env python

import sys
try:
	import pygtk
	pygtk.require('2.0')
except:
	pass

try:
	import gtk
	import gtk.glade
except:
	sys.exit(1)


#from pysqlite2 import dbapi2 as sqlite
import time
import gobject
import os
import os.path



try:
	import sugar.env
	from sugar.activity import activity
	SUGAR=True
except:
	SUGAR=False


#if not SUGAR:
try:
	import nxt.locator
	from nxt.motor import *
	from nxt.sensor import *
	try:
		Device = nxt.locator.find_one_brick()
	except:
		Device = None
		print "No se Encuentra Dispositivo"
except:
	pass


TARGET_TYPE_TEXT = 80
TARGET_TYPE_PIXMAP = 81

target = [
	('text/plain', 0, TARGET_TYPE_TEXT),
	('image/x-xpixmap', 0, TARGET_TYPE_PIXMAP), 
]




class amiGO:
	
	PTool=0
	PagesData={}
	CurrentPage=1
	IDx=''
	
	def __init__(self):
		self.gladefile="Amigo.glade"
#		if not SUGAR:
		try:
			global Device
			if Device!=None:
				self.Robot=Device.connect()
		except:
			print "No Robot"
			self.Robot=None
		if not SUGAR:
			appdir = os.getcwd()
			self.wTree = gtk.glade.XML(os.path.join(os.path.join(appdir,'amigo/glade'),self.gladefile),"MainWindow")
			self.MainWdw=self.wTree.get_widget("MainWindow")
		else:
			appdir = os.path.join(activity.get_bundle_path(),'amigo')
			self.wTree = gtk.glade.XML(os.path.join(os.path.join(appdir,'glade'),self.gladefile), "MainContainer")
			self.MainWdw=self.wTree.get_widget("MainContainer")
		self.PageData=self.wTree.get_widget("DesignArea")
		dic = { "on_NewPrj_clicked" : self.on_NewPrj_clicked,
				"on_OpenPrj_clicked" : self.on_OpenPrj_clicked,
				"on_SavePrj_clicked" : self.on_SavePrj_clicked,
				"on_RunPrj_clicked" : self.on_RunPrj_clicked,
				"on_StopMov_clicked" : self.on_StopMov_toggled,
				"on_BackMov_clicked" : self.on_BackMov_toggled,
				"on_ForwardMov_clicked" : self.on_ForwardMov_toggled,
				"on_LeftMov_clicked" : self.on_LeftMov_toggled,
				"on_RightMov_clicked" : self.on_RightMov_toggled,
				"on_MakePage_clicked" : self.on_MakePage_clicked,
				"on_RemvPage_clicked" : self.on_RemvPage_clicked,
				"on_PrevPage_clicked" : self.on_PrevPage_clicked,
				"on_NextPage_clicked" : self.on_NextPage_clicked,
				"on_PlayPage_clicked" : self.on_PlayPage_clicked,
				"on_MainWindow_destroy": gtk.main_quit
			}
		self.wTree.signal_autoconnect(dic)


	def refresh_Prjt_Wdw(self, Data=[]):
		if len(Data)==0:
			if self.PagesData.has_key(self.CurrentPage):
				PageData=self.PagesData[self.CurrentPage]
		self.PageData.window.clear()
		print self.PagesData


	def add_PageContents(self,command,rotation,duration=0,power=0,grade=0):
		idx=str(int(time.time()))
		Objt=[command,rotation]
		if len(self.PagesData)==0:
			self.PagesData[self.CurrentPage]={}
		if not self.PagesData.has_key(self.CurrentPage):
			self.PagesData[self.CurrentPage]={}
		self.PagesData[self.CurrentPage][idx]=Objt
		self.refresh_Prjt_Wdw()



	def on_NewPrj_clicked(self, widget):
		pass


	def on_OpenPrj_clicked(self, widget):
		pass


	def on_SavePrj_clicked(self, widget):
		pass


	def on_RunPrj_clicked(self, widget):
		if len(self.PagesData)==0:
			return
		PageCnt=self.PagesData.keys()
		PageCnt.sort()
		for Page in PageCnt:
			PageInfo=self.PagesData[Page]
			Played=PageInfo.keys()
			Played.sort()
			for timx in Played:
				print PageInfo[timx]
				time.sleep(1)
				Mov=PageInfo[timx][0]
				Rot=PageInfo[timx][1]
				self.on_Movement(Mov,Rot)


	def on_StopMov_toggled(self, widget):
		pass


	def on_ForwardMov_toggled(self, widget):
#		print "FW"
		self.add_PageContents('FW',1)
#		self.on_Movement('FW',1)
		pass


	def on_BackMov_toggled(self, widget):
#		self.on_Movement('BW',1)
		self.add_PageContents('BW',1)
		pass


	def on_LeftMov_toggled(self, widget):
		self.add_PageContents('LT',1)
#		self.on_Movement('LT',1)
		pass


	def on_RightMov_toggled(self, widget):
		self.add_PageContents('RT',1)
#		self.on_Movement('RT',1)
		pass


	def on_MakePage_clicked(self,widget):
		print "make"
		PageT=len(self.PagesData)+1
		if self.PagesData.has_key(self.CurrentPage):
			self.PagesData[PageT]={}
			self.CurrentPage=PageT
		else:
			self.PagesData[self.CurrentPage]={}
		self.refresh_Prjt_Wdw()


	def on_RemvPage_clicked(self,widget):
		if len(self.PagesData[self.CurrentPage])>0:
			Question=QuestionDialog("Esta Seguro?")
			resp=Question.run()
		else:
			resp=''
		if resp=='OK':
			del self.PagesData[self.CurrentPage]
			PageN=self.CurrentPage-1
			self.CurrentPage=PageN
		self.refresh_Prjt_Wdw()


	def on_PrevPage_clicked(self,widget):
		PageN=self.CurrentPage-1
		if PageN<=1:
			PageN=1
		self.CurrentPage=PageN
		self.refresh_Prjt_Wdw()


	def on_NextPage_clicked(self,widget):
		PageT=len(self.PagesData)
		PageN=self.CurrentPage+1
		if PageN>=PageT:
			PageN=PageT
		self.CurrentPage=PageN
		self.refresh_Prjt_Wdw()



	def on_PlayPage_clicked(self, widget):
		if not self.PagesData.has_key(self.CurrentPage):
			return
		else:
			if len(self.PagesData[self.CurrentPage])==0:
				return
		PageInfo=self.PagesData[self.CurrentPage]
		Played=PageInfo.keys()
		Played.sort()
		for timx in Played:
#			print PageInfo[timx]
			time.sleep(1)
			Mov=PageInfo[timx][0]
			Rot=PageInfo[timx][1]
			self.on_Movement(Mov,Rot)




	def on_Close(self, event):
		Robot.close()
		gtk.main_quit
		




	def on_Movement(self, type, timex=1):
		mov=0
		sign=1
		if timex==0:
			timex=1
		if type=='FW':
			mov=1
			sign=1
		elif type=='BW':
			mov=1
			sign=-1
		elif type=='RT':
			mov=2
		elif type=='LT':
			mov=3
		if mov==2 or mov==1:
			m_right = Motor(self.Robot, PORT_C)
#			print m_right
			m_right.power = 70*sign
			m_right.mode = MODE_MOTOR_ON
			m_right.run_state = RUN_STATE_RUNNING
			m_right.tacho_limit = 360*timex
			m_right.set_output_state()
		if mov==3 or mov==1:
			m_left = Motor(self.Robot, PORT_B)
			m_left.power = 70*sign
			m_left.mode = MODE_MOTOR_ON
			m_left.run_state = RUN_STATE_RUNNING
			m_left.tacho_limit = 360*timex
			m_left.set_output_state()
		Alert=UltrasonicSensor(self.Robot, PORT_4).get_sample()
		print Alert
		if Alert<=30:
			print "Alerta"
			self.on_Movement('BW')
#		print 'Ultrasonic:', 



if __name__=="__main__":
	Glob=amiGO()
	gtk.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.