Back to index

gcompris  8.2.2
Public Member Functions | Public Attributes
electric.Gcompris_electric Class Reference

List of all members.

Public Member Functions

def __init__
def start
def end
def ok
def repeat
def key_press
def pause
def set_level
def increment_level
def cleanup_game
def display_game
def get_current_tools
def tool_item_event
def assign_tool
def create_components
def run_simulation
def call_gnucap
def convert_gnucap_value

Public Attributes

 gcomprisBoard
 tools
 board_paused
 gamewon
 components
 gnucap_timer
 gnucap_timer_interval
 gnucap_binary
 rootitem
 select_tool
 select_tool_number
 current_tool
 old_tool_item

Detailed Description

Tux hide a number, you must guess it

Definition at line 38 of file electric.py.


Constructor & Destructor Documentation

def electric.Gcompris_electric.__init__ (   self,
  gcomprisBoard 
)

Definition at line 41 of file electric.py.

00041 
00042   def __init__(self, gcomprisBoard):
00043 
00044     self.gcomprisBoard = gcomprisBoard
00045 
00046     self.gcomprisBoard.disable_im_context = True
00047 
00048     # Part of UI : tools buttons
00049     # TOOL SELECTION
00050     self.tools = [
00051       ["DEL",    "draw/tool-del.png",     "draw/tool-del_on.png",     gcompris.CURSOR_DEL],
00052       ["SELECT", "draw/tool-select.png",  "draw/tool-select_on.png",  gcompris.CURSOR_SELECT]
00053       ]
00054 
00055     # These are used to let us restart only after the bonus is displayed.
00056     # When the bonus is displayed, it call us first with pause(1) and then
00057     # with pause(0)
00058     self.board_paused  = 0
00059     self.gamewon       = 0
00060 
00061     # The list of placed components
00062     self.components = []
00063     self.gnucap_timer = 0
00064     self.gnucap_timer_interval = 500
00065 
00066     self.gnucap_binary = None


Member Function Documentation

def electric.Gcompris_electric.assign_tool (   self,
  newtool 
)

Definition at line 252 of file electric.py.

00252 
00253   def assign_tool(self, newtool):
00254     # Deactivate old button
00255     item = self.tools[self.current_tool][4]
00256     item.set(pixbuf = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin(self.tools[self.current_tool][1])))
00257 
00258     # Activate new button
00259     self.current_tool = newtool
00260     item = self.tools[newtool][4]
00261     item.set(pixbuf = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin(self.tools[self.current_tool][2])))
00262     gcompris.set_cursor(self.tools[self.current_tool][3]);
00263 

Here is the caller graph for this function:

Definition at line 315 of file electric.py.

00315 
00316   def call_gnucap(self):
00317     if not self.components:
00318       if debug: print "call_gnucap: No component"
00319       self.gnucap_timer = 0
00320       return
00321 
00322     connected = False
00323     for component in self.components:
00324       if component.is_connected():
00325         connected = True
00326 
00327     if debug: print "call_gnucap create the tempfile"
00328 
00329     fd, filename = tempfile.mkstemp(".gnucap", "gcompris_electric", None, True)
00330     f = os.fdopen(fd, "w+t")
00331 
00332     gnucap = "Title GCompris\n"
00333 
00334     # Ugly hack: connect a 0 ohm (1 fempto) resistor between net 0
00335     # and first net found
00336     gnucap += "R999999999 0 "
00337     for component in self.components:
00338       if component.is_connected():
00339         gnucap += str(component.get_nodes()[0].get_wires()[0].get_wire_id())
00340         break
00341     gnucap += " 1f\n"
00342 
00343     gnucap_print = ""
00344     for component in self.components:
00345       if component.is_connected():
00346         thisgnucap = component.to_gnucap("")
00347         gnucap += thisgnucap[0]
00348         gnucap_print += thisgnucap[1]
00349 
00350     gnucap += gnucap_print
00351     gnucap += ".dc\n"
00352     gnucap += ".end\n"
00353     if debug: print gnucap
00354     f.writelines(gnucap)
00355     f.close()
00356 
00357     #
00358     # Run gnucap with the temporary datafile we created.
00359     #
00360     if debug: print "calling gnucap: %s -b %s" % (self.gnucap_binary, filename)
00361     output = os.popen("%s -b %s" % (self.gnucap_binary, filename))
00362 
00363     #
00364     # Read and analyse gnucap result
00365     #
00366     if debug: print "---------------- GNUCAP OUTPUT PARSING ---------------------"
00367     line = ""
00368     for line in output.readlines():
00369       if debug: print "==="
00370       if debug: print line
00371       if(line.split() == " 0."):
00372         break
00373     if debug: print "------------------------------------------------------------"
00374 
00375     if debug: print "===>"
00376     if debug: print line
00377     if debug: print "===>"
00378 
00379     # Close it to check errors
00380     results = output.close()
00381     if results:
00382       print('Failed to run gnugap with error ', results)
00383       self.gnucap_timer = 0
00384       return
00385 
00386     values = []
00387     if(line.split()[0] == "0."):
00388       if debug: print "FOUND 0."
00389       values = line.split()
00390       del values[0]
00391 
00392     if debug: print values
00393     i = 0
00394 
00395     # Set all component values
00396     for component in self.components:
00397       if not component.is_connected():
00398         done = component.set_voltage_intensity(False, 0.0, 0.0)
00399       else:
00400         # Each Component class may have several gnucap component to retrieve
00401         # data from
00402         done = False
00403         while not done:
00404           if debug: print "Processing component %d" %(i,)
00405           try:
00406             if debug: print values[i]
00407             if debug: print values[i+1]
00408             dumy = values[i]
00409             dumy = values[i+1]
00410           except:
00411             if debug: print "Warning: gnucap parsing mismatch"
00412             done = True
00413             continue
00414 
00415           try:
00416             volt = self.convert_gnucap_value(values[i])
00417             amp = self.convert_gnucap_value(values[i+1])
00418             done = component.set_voltage_intensity(True, volt, amp)
00419             if debug: print "Converted U=%sV I=%sA" %(volt, amp)
00420           except:
00421             if debug: print "Failed to convert V=%sV I=%sA" %(values[i], values[i+1])
00422             done = component.set_voltage_intensity(False, 0.0, 0.0)
00423 
00424           i += 2
00425 
00426 
00427     if not debug: os.remove(filename)
00428     self.gnucap_timer = 0

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 176 of file electric.py.

00176 
00177   def cleanup_game(self):
00178     self.gamewon = False
00179 
00180     if self.gnucap_timer :
00181       gtk.timeout_remove(self.gnucap_timer)
00182       self.gnucap_timer = 0
00183 
00184     # remove the appended items from our tools
00185     for i in range(0,len(self.tools)):
00186       self.tools[i].pop()
00187 
00188     # No more component in the simulation set
00189     self.components = []
00190 
00191     # Remove the root item removes all the others inside it
00192     self.rootitem.destroy()

Here is the caller graph for this function:

Definition at line 432 of file electric.py.

00432 
00433   def convert_gnucap_value(self, value):
00434     unit = 1
00435 
00436     if value.endswith("T"):
00437       unit = e12
00438       value = value.replace("T", "")
00439 
00440     if value.endswith("G"):
00441       unit = 1e9
00442       value = value.replace("G", "")
00443 
00444     if value.endswith("Meg"):
00445       unit = 1e6
00446       value = value.replace("Meg", "")
00447 
00448     if value.endswith("K"):
00449       unit = 1e3
00450       value = value.replace("K", "")
00451 
00452     if value.endswith("u"):
00453       unit = 1e-6
00454       value = value.replace("u", "")
00455 
00456     if value.endswith("n"):
00457       unit = 1e-9
00458       value = value.replace("n", "")
00459 
00460     if value.endswith("p"):
00461       unit = 1e-12
00462       value = value.replace("p", "")
00463 
00464     if value.endswith("f"):
00465       unit = 1e-15
00466       value = value.replace("f", "")
00467 
00468     # return absolue value
00469     sign = 1
00470     if float(value) < 0:
00471       sign = -1
00472 
00473     return (float(sign)*float(value)*unit)
00474 
00475 
00476 
00477 
00478 # ----------------------------------------------------------------------
00479 # ----------------------------------------------------------------------
00480 # ----------------------------------------------------------------------
00481 # ----------------------------------------------------------------------
00482 # ----------------------------------------------------------------------
00483 
00484 
# A wire between 2 Nodes

Here is the caller graph for this function:

def electric.Gcompris_electric.create_components (   self,
  level 
)

Definition at line 267 of file electric.py.

00267 
00268   def create_components(self, level):
00269 
00270     if(level == 1):
00271       # A list of couple (component class, it's value)
00272       component_set = ((Battery, 10),
00273                        (Bulb, 0.11),
00274                        (Switch, None),
00275                        )
00276     elif(level == 2):
00277       # A list of couple (component class, it's value)
00278       component_set = ((Battery, 10),
00279                        (Bulb, 0.11),
00280                        (Rheostat, 1000),
00281                        (Switch, None),
00282                        (Switch2, None),
00283                        (Connection, None),
00284                        )
00285     elif(level == 3):
00286       # A list of couple (component class, it's value)
00287       component_set = ((Battery, 10),
00288                        (Bulb, 0.11),
00289                        (Rheostat, 1000),
00290                        (Resistor, 1000),
00291                        (Switch, None),
00292                        (Connection, None),
00293                        (Diode, None),
00294                        )
00295 
00296     Selector(self, component_set)
00297 
00298 
00299 
00300 
00301 # ----------------------------------------------------------------------
00302 # ----------------------------------------------------------------------
00303 # ----------------------------------------------------------------------
00304 # ----------------------------------------------------------------------

Definition at line 194 of file electric.py.

00194 
00195   def display_game(self):
00196 
00197       # Create our rootitem. We put each canvas item in it so at the end we
00198       # only have to kill it. The canvas deletes all the items it contains
00199       # automaticaly.
00200       self.rootitem = self.gcomprisBoard.canvas.root().add(
00201           gnomecanvas.CanvasGroup,
00202           x=0.0,
00203           y=0.0
00204           )
00205 
00206       self.create_components(self.gcomprisBoard.level)
00207 
00208       # Display the tools
00209       x = 12
00210       y = 10
00211       for i in range(0,len(self.tools)):
00212 
00213         item = self.rootitem.add(
00214           gnomecanvas.CanvasPixbuf,
00215           pixbuf = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin(self.tools[i][1])),
00216           x=x,
00217           y=y
00218           )
00219         x += 45
00220         item.connect("event", self.tool_item_event, i)
00221 
00222         if(self.tools[i][0]=="SELECT"):
00223           self.select_tool = item
00224           self.select_tool_number = i
00225           # Always select the SELECT item by default
00226           self.current_tool = i
00227           self.old_tool_item = item
00228           self.old_tool_item.set(pixbuf = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin(self.tools[i][2])))
00229           gcompris.set_cursor(self.tools[i][3]);
00230 
00231         # Add the item in self.tools for later use
00232         self.tools[i].append(item)
00233 

Here is the caller graph for this function:

Definition at line 98 of file electric.py.

00098 
00099   def end(self):
00100 
00101     gcompris.reset_locale()
00102 
00103     gcompris.set_cursor(gcompris.CURSOR_DEFAULT);
00104 
00105     # Remove the root item removes all the others inside it
00106     self.cleanup_game()

Here is the call graph for this function:

Definition at line 236 of file electric.py.

00236 
00237   def get_current_tools(self):
00238       return(self.tools[self.current_tool][0])
00239 

Definition at line 159 of file electric.py.

00159 
00160   def increment_level(self):
00161     self.gcomprisBoard.sublevel += 1
00162 
00163     if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
00164       # Try the next level
00165       self.gcomprisBoard.sublevel=1
00166       self.gcomprisBoard.level += 1
00167       gcompris.bar_set_level(self.gcomprisBoard)
00168 
00169       if(self.gcomprisBoard.level>self.gcomprisBoard.maxlevel):
00170         # the current board is finished : bail out
00171         gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
00172         return 0
00173 
00174     return 1

Here is the caller graph for this function:

def electric.Gcompris_electric.key_press (   self,
  keyval,
  commit_str,
  preedit_str 
)

Definition at line 114 of file electric.py.

00114 
00115   def key_press(self, keyval, commit_str, preedit_str):
00116     # Return  True  if you did process a key
00117     # Return  False if you did not processed a key
00118     #         (gtk need to send it to next widget)
00119     return False

Definition at line 107 of file electric.py.

00107 
00108   def ok(self):
00109     pass

def electric.Gcompris_electric.pause (   self,
  pause 
)

Definition at line 120 of file electric.py.

00120 
00121   def pause(self, pause):
00122     self.board_paused = pause
00123 
00124     # When the bonus is displayed, it call us first with pause(1) and then
00125     # with pause(0)
00126     # the game is won
00127     if(pause == 0 and self.gamewon):
00128       self.increment_level()
00129       self.gamewon = 0
00130 
00131       self.cleanup_game()
00132       self.display_game()
00133 
00134     return
00135 

Here is the call graph for this function:

Definition at line 110 of file electric.py.

00110 
00111   def repeat(self):
00112     if debug: print("Gcompris_electric repeat.")
00113 

Here is the caller graph for this function:

Definition at line 305 of file electric.py.

00305 
00306   def run_simulation(self):
00307     if debug: print "run_simulation %s" %(self.gnucap_binary,)
00308     if not self.gnucap_binary:
00309       return
00310 
00311     if debug: print "self.gnucap_timer = %d" %(self.gnucap_timer,)
00312     if not self.gnucap_timer:
00313       if debug: print "run_simulation timeout_add"
00314       self.gnucap_timer = gobject.timeout_add(self.gnucap_timer_interval, self.call_gnucap)

Here is the call graph for this function:

def electric.Gcompris_electric.set_level (   self,
  level 
)

Definition at line 136 of file electric.py.

00136 
00137   def set_level(self, level):
00138     self.gcomprisBoard.level=level;
00139     self.gcomprisBoard.sublevel=1;
00140 
00141     # Set the level in the control bar
00142     gcompris.bar_set_level(self.gcomprisBoard);
00143 
00144     self.cleanup_game()
00145     self.display_game()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 67 of file electric.py.

00067 
00068   def start(self):
00069 
00070     self.gcomprisBoard.level=1
00071     self.gcomprisBoard.maxlevel=3
00072     self.gcomprisBoard.sublevel=1
00073     self.gcomprisBoard.number_of_sublevel=1
00074 
00075     gcompris.bar_set(gcompris.BAR_LEVEL)
00076 
00077     gcompris.bar_set_level(self.gcomprisBoard)
00078 
00079     gcompris.set_background(self.gcomprisBoard.canvas.root(),
00080                             gcompris.skin.image_to_skin("gcompris-bg.jpg"))
00081 
00082     self.display_game()
00083 
00084     #
00085     # Check gnucap is installed and save it's path in self.gnucap_binary
00086     #
00087     for binary in ("/usr/bin/gnucap",
00088                    "/usr/local/bin/gnucap",
00089                    "gnucap.exe"):
00090       if(os.path.exists(binary)):
00091         self.gnucap_binary = binary
00092         break
00093 
00094     if not self.gnucap_binary:
00095       gcompris.utils.dialog(_("Cannot find the 'gnucap' electric simulator.\nYou can download and install it from:\n<http://geda.seul.org/tools/gnucap/>\nTo be detected, it must be installed in\n/usr/bin/gnucap or /usr/local/bin/gnucap.\nYou can still use this activity to draw schematics without computer simulation."),
00096                             None)
00097 

Here is the call graph for this function:

def electric.Gcompris_electric.tool_item_event (   self,
  item,
  event,
  tool 
)

Definition at line 242 of file electric.py.

00242 
00243   def tool_item_event(self, item, event, tool):
00244 
00245     if event.type == gtk.gdk.BUTTON_PRESS:
00246       if event.button == 1:
00247         self.assign_tool(tool)
00248         return True
00249 
00250     return False
00251 

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 57 of file electric.py.

Definition at line 61 of file electric.py.

Definition at line 225 of file electric.py.

Definition at line 58 of file electric.py.

Definition at line 43 of file electric.py.

Definition at line 65 of file electric.py.

Definition at line 62 of file electric.py.

Definition at line 63 of file electric.py.

Definition at line 226 of file electric.py.

Definition at line 199 of file electric.py.

Definition at line 222 of file electric.py.

Definition at line 223 of file electric.py.

Definition at line 49 of file electric.py.


The documentation for this class was generated from the following file: