FIRST Robot Simulator - ToggleObject
 @(#) $Header: /usr/local/scalawags_cvs/Scalawags/Simulator2006/ToggleObject.wc,v 1.5 2006/03/13 23:21:28 abrown Exp $ 


What Is Simulated

ToggleObject simulates a toggle switch. It drives one bit of digital output.


Where Used

ToggleObject will typically be used in either or both of TheOperatorInterface.tcl and TheRobot.tcl. ToggleObject.tcl will typically be sourced in one of those files. The declarations for ::Toggle::new will then be created in one or both of those files as needed. Warning: If you source it in TheOperatorInterface.tcl, you must not also source it also in TheRobot.tcl. Sourcing must happen only once.


GUI View

Horizontal  
 
Vertical
 
 
::Toggle::new plan1 .robot.sw.plan1 ::portbb \
  64 64 0   horizontal bit5 "#000000" "#7f8f7f"
	
 
 
::Toggle::new plan0 .robot.sw.plan0 ::portbb \
  32 32 0   vertical bit4 "#000000" "#7f8f7f"
	
When the baton is left, the digital output is 64 with mask 64. When the baton is right, the digital output is 0. In other words, this switch has been configured to only control bit 6. To invert the output of the switch you would replace "32 32 0" with "32 0 32".  
 
When the baton is up, the digital output is 32 with mask 32. When the baton is down, the digital output is 0. In other words, this switch has been configured to only control bit 5.

Programmers View

There are two proc[edures] in ToggleObject.tcl. We are only concerned with the first one. The first few lines of the proc lists the parameters required.

  proc new { {ToggleName} {NameOnCanv}
		      {variable} {mask} {upleft} {downright}
		      {shape} {text} {Fg} {Bg} } \
  {
    # ToggleName and NameOnCanv must be unique names.
    # "variable" will be controlled by the digital output.
    # "mask", "upleft", and "downright" are numbers that control
    #   how "variable" is controlled.  Only the binary bits of "mask"
    #   that are 1s will be changes.  Other bits of "variable" are
    #   untouched.  If the baton is in the up or left position, then
    #   variable takes on the value of "upleft".  If the baton is in
    #   down or right position, the variable takes on the value of
    #   "downright".
    # "shape" must be either "horizontal" or "vertical".
    # "text" is displayed above the toggle switch.
    # Fg and Bg = Foreground and Background colors.
  

The parameters ToggleName and NameOnCanv are constants.

It is helpful to consider the values of "mask", "upleft", and "downright" in binary. If we want to control bit 5:

00100000 binary = 32 decimal


Example

We can put four toggles side by side using code like this:

  source {ToggleObject.tcl}

  ::Toggle::new plan3 .robot.switches.plan3 ::porthb \
    1 1 0     vertical bit7 "#000000" "#7f8f7f"
  ::Toggle::new plan2 .robot.switches.plan2 ::portbb \
    128 128 0 vertical bit6 "#000000" "#7f8f7f"
  ::Toggle::new plan1 .robot.switches.plan1 ::portbb \
    64 64 0   vertical bit5 "#000000" "#7f8f7f"
  ::Toggle::new plan0 .robot.switches.plan0 ::portbb \
    32 32 0   vertical bit4 "#000000" "#7f8f7f"
  pack .robot.switches.plan3 .robot.switches.plan2 -side left
  pack .robot.switches.plan1 .robot.switches.plan0 -side left
  

The declaration above is in TheRobot.tcl. The double colons in front of each variable name makes them operate at the root namespace of the Tcl program.



Last modified 11 Dec 2006
http://brown.armoredpenguin.com/~abrown/contact.html
http://brown.armoredpenguin.com/~abrown/first/first2006/Scalawags/Simulator2006/ToggleObject.html