Switch Class: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
m (→‎Switches Methods: Adding getName method)
m (Side Bar Update)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Sidebar
{{Sidebar
| name = GUI Navbar
| name = GUI Navbar
| title = FG Int Objects Modules
| title = FG Int Switches Classes
| contentstyle = text-align: left;
| contentstyle = text-align: left;
| content1 = '''Modules Classes'''
| content1 = * [[Switch Class]]
* [[FGInt Segment Display]]
* [[FGInt SwitchLight]]
* [[FGInt Switches]]
* [[FGInt Rotary Encoder]]
| content2 = '''Switches Class'''
* [[Switch Class]]
* [[DoubleSwitch Class]]
* [[DoubleSwitch Class]]
* [[RotarySwitch Class]]
* [[RotarySwitch Class]]
* [[ToogleSwitch Class]]
* [[ToogleSwitch Class]]
| content9 = '''Navigation'''
| content9 = '''Navigation'''
* [[FG Int Objects Modules]]
* [[FG interface]]
* [[FG interface]]
}}
}}
Segments switch Class '''''Switch''''' will be used to manage standard 2 position switches (only 1 input needed per switch) and is based on the MCP23017.
Switches Class '''''Switch''''' will be used to manage standard 2 position switches (only 1 input needed per switch) and is based on the MCP23017.


== Switches Configuration File ==
== Switches Configuration File ==
Displays configuration file : ''/opt/fgint/Config/RadioPanel/switches.cfg''<br />
Displays configuration file : ''/opt/fgint/Config/RadioPanel/switches.cfg''
This file describes the 2 positions switches configuration used on the radio panel.<br />
 
There are two separate parts in this file.<br />
Values to send to simulator for this switch are :
 
* 0 for '''off'''
* 1 for '''on'''
 
This file describes the 2 positions switches configuration used on the radio panel.<br />There are two separate parts in this file.


# a general configuration part, in the CONF and PROPERTIES sections.
# a general configuration part, in the CONF and PROPERTIES sections.
Line 81: Line 81:


== Creating Switches ==
== Creating Switches ==
The order of the parameters does not fall from the sky. If we look more closely at the Class "Switch" in the '''FGIntSwitch.py'''
The order of the parameters does not fall from the sky. If we look more closely at the Class "'''Switch'''" in the '''FGIntSwitch.py'''


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 110: Line 110:


== Switches Methods ==
== Switches Methods ==
=== getValueType() ===
=== getName() ===
Return the type of value that is used witch this switch. Can be bool (boolean), int (integer) or string (string) according with the configuration file.
Return the switch Name according the configuration
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Python 3.4.2 (default, Sep 26 2018, 07:16:01)
Python 3.4.2 (default, Sep 26 2018, 07:16:01)
Line 121: Line 121:
>>> FGINT1.createElements()
>>> FGINT1.createElements()
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW.getValueType()
>>> RMP0PSW.getName()
'int'
'rmp0psw'
>>>
>>>
</syntaxhighlight>
</syntaxhighlight>


=== getName() ===
=== getValueType() ===
Return the switch Name according the configuration
Return the type of value that is used witch this switch. Can be bool (boolean), int (integer) or string (string) according with the configuration file.
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Python 3.4.2 (default, Sep 26 2018, 07:16:01)
Python 3.4.2 (default, Sep 26 2018, 07:16:01)
Line 137: Line 137:
>>> FGINT1.createElements()
>>> FGINT1.createElements()
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW.getName()
>>> RMP0PSW.getValueType()
'rmp0psw'
'int'
>>>
>>>
</syntaxhighlight>
</syntaxhighlight>
Line 153: Line 153:
>>> FGINT1.createElements()
>>> FGINT1.createElements()
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW.getValueType()
>>> RMP0PSW.getNode()
'None'
'None'
>>>
>>>
Line 159: Line 159:


=== getTypedData() ===
=== getTypedData() ===
Return the typed value given as agument
Return the typed value given as agument according to what have been set in the '''''valuestype''''' property
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Python 3.4.2 (default, Sep 26 2018, 07:16:01)
Python 3.4.2 (default, Sep 26 2018, 07:16:01)
Line 179: Line 179:
Return the actual state of the switch by reading the I/O Device
Return the actual state of the switch by reading the I/O Device
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('overheadpanel.cfg')
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0PWSW = FGINT1.getElement('RMP0PWSW')
>>> RMP0PWSW.getSwitchState()
'0'
>>>
</syntaxhighlight>Set the switch 'On' on the panel<syntaxhighlight lang="python3">
>>> RMP0PWSW.getSwitchState()
'1'
>>>
</syntaxhighlight>Combined with the value typing function <syntaxhighlight lang="python3">
>>> RMP0PWSW.getTypedData(RMP0PWSW.getSwitchState())
1
>>> RMP0PWSW.getTypedData(RMP0PWSW.getSwitchState())
0
>>>
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 18:48, 2 September 2022

Switches Class Switch will be used to manage standard 2 position switches (only 1 input needed per switch) and is based on the MCP23017.

Switches Configuration File

Displays configuration file : /opt/fgint/Config/RadioPanel/switches.cfg

Values to send to simulator for this switch are :

  • 0 for off
  • 1 for on

This file describes the 2 positions switches configuration used on the radio panel.
There are two separate parts in this file.

  1. a general configuration part, in the CONF and PROPERTIES sections.
  2. the other sections each describe a display.
[CONF]
confname=SWITCHES
library=FGIntSwitch
module=Switch
properylist=device,name,port,pin,values,valuestype,invert,node

[PROPERTIES]
prop01=device
prop02=name
prop03=port
prop04=pin
prop05=values
prop06=valuestype
prop07=invert
prop08=node

[RMP0PSW]
name=rmp0psw
device=IOPACK1
port=A
pin=2
values=0,1
valuestype=int
node=None
invert=0

CONF Section

The CONF describe what type of object are describe here and how they will be built.

  • confname : Nothing special here, just a name to set about what we are talking about.
  • library : FG Interface python library from which the class will be loaded.
  • module : Module name (Class name) in the library file that will be loaded. Here we are using the Switch Class
  • propertylist : List of parameter that are needed to create a switch object.

PROPERTIES Section

Properties section is defined to declare and order each parameter that is needed to create the elements (Super Class FGInt Switches in that case, take some parameters to create a switch object, as the input where the switch is connected on the interface). This is how the interface will be able to understand and use the switch configurations to create them.

For Switches, to create one, you will need to provide for each required parameters. And the section gives the order in which the parameters will be called when creating the object.

[PROPERTIES]
prop01=device      # Device Logical Name
prop02=name        # Switch Name
prop03=port        # MCP23017 port where the switch is connected
prop04=pin         # MCP23017 input where the switch is connected
prop05=values      # Values that will be provided for the switch state
prop06=valuestype  # Typed Values that is used with this switch
prop07=invert      # Describe if the input need to be manage inverted [ activate = 0 ]
prop08=node        # FG Property that is manage by the switch

In PROPERTIES section, you have to provide parameters name and in which order. With this, FG interface just have to read parameters for each object and use them to create each object.

Creating Switches

The order of the parameters does not fall from the sky. If we look more closely at the Class "Switch" in the FGIntSwitch.py

class Switch:
    """
    """

    ###############
    # Properties
    ###############

    ###############
    # Constructor
    ###############
    def __init__(self, device, name, port, pin, values, valuestype, invert, node, debug=0):

here how switches should be instantiated:

To create a switch with this class you have to call :

Switch(device, name, port, pin, values, valuestype, invert, node, [debug])

"debug" is not mandatory. If not provide, il will be automaticly set to 0

But as already explained several times, you should not have to call this method directly. In normal operation, the objects will be created via the methods of the interface itself (with the createElement() and/or createElements() methods)

Switches Methods

getName()

Return the switch Name according the configuration

Python 3.4.2 (default, Sep 26 2018, 07:16:01)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW.getName()
'rmp0psw'
>>>

getValueType()

Return the type of value that is used witch this switch. Can be bool (boolean), int (integer) or string (string) according with the configuration file.

Python 3.4.2 (default, Sep 26 2018, 07:16:01)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW.getValueType()
'int'
>>>

getNode()

Return the node that is manage by the switch according the configuration file.

Python 3.4.2 (default, Sep 26 2018, 07:16:01)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW.getNode()
'None'
>>>

getTypedData()

Return the typed value given as agument according to what have been set in the valuestype property

Python 3.4.2 (default, Sep 26 2018, 07:16:01)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('radiopanel.cfg', 0)
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0PSW = FGINT1.getElement('RMP0PSW')
>>> RMP0PSW.getTypedData(0)
0
>>> RMP0PSW.getTypedData(1)
1
>>>

getSwitchState()

Return the actual state of the switch by reading the I/O Device

Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from FGInterface import FGInterface as FGINT
>>> FGINT1 = FGINT('overheadpanel.cfg')
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0PWSW = FGINT1.getElement('RMP0PWSW')
>>> RMP0PWSW.getSwitchState()
'0'
>>>

Set the switch 'On' on the panel

>>> RMP0PWSW.getSwitchState()
'1'
>>>

Combined with the value typing function

>>> RMP0PWSW.getTypedData(RMP0PWSW.getSwitchState())
1
>>> RMP0PWSW.getTypedData(RMP0PWSW.getSwitchState())
0
>>>