FGInt Rotary Encoder

From FlightGear wiki
Jump to navigation Jump to search

RotaryEncoder class will be used to manage Rotary Encoder . Based on the MCP2307.

Methods explain below show how to read the encoder, but to be managed, it need a worker that will allow to read

Displays Configuration File

Displays configuration file : /opt/fgint/Config/Pedestal/rotenc.cfg

This file describes the configuration of the 2 rotary encoder 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=ROTARY_ENC
library=RotaryEncoder
module=RotaryEncoder
properylist=device,encodername,port,in1,in2,swin,initvalue,minvalue,maxvalue,increment
createmethod=CreateRotEncoders

[PROPERTIES]
prop01=device
prop02=encodername
prop03=port
prop04=in1
prop05=in2
prop06=swin
prop07=initvalue
prop08=minvalue
prop09=maxvalue
prop10=increment

[RMP0INNER]
device=IOPACK2
encodername=rmp0inner
port=A
in1=3
in2=4
swin=0
initvalue=0
minvalue=0
maxvalue=360
increment=1

[RMP0OUTER]
device=IOPACK2
encodername=rmp0outer
port=A
in1=1
in2=2
swin=0
initvalue=0
minvalue=0
maxvalue=360
increment=1

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.
  • propertylist : List of parameter that are needed to create a display object.
  • createmethod : Method from FG interface Class that will be used to create display objects.

propertylist and createmethod are deprecated and will be removed in the next release.

PROPERTIES Section

Properties section is defined to declare and order each parameter that is needed to create the elements (Super Class FGInt RotaryEncoder in that case, take some parameters to create a display object, as like Number of digit, Common of the fist digit ... etc). This is how the interface will be able to understand and use the displays configurations to create them.

In Segment Display, 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      # Display Name
prop03=port      # MCP23017 Pins port
prop04=in1       # MCP23017 Rotary Encoder input1 pin
prop05=in2       # MCP23017 Rotary Encoder input2 pin
prop06=swin      # MCP23017 Rotary Encoder Switch input pin (if any, set 0 if none or not used)
prop07=initvalue # Rotary Encoder Initial value 
prop08=minvalue  # Rotary Encoder minimal value
prop09=maxvalue  # Rotary Encoder maxvalue
prop10=increment # Rotary Encoder increment

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 Rotary Encoder

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

class RotaryEncoder:
    # Just the encoder, no switch, no LEDs

    def __init__(self, device, encodername, port, in1, in2, swin, initvalue, minvalue, maxvalue, increment, debug=0):

here how Segment display should be instantiated: To create a 7 segment display with this class you have to call :

RotaryEncoder(device, encodername, port, in1, in2, swin, initvalue, minvalue, maxvalue, increment, debug=0)

"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)

Rotary Encoder Methods

getName()

Returns the rotary encoder name according to you configuration.

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.createElements()
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0INNER = FGINT1.getElement('RMP0INNER')
>>> RMP0INNER.getName()
'rmp0inner'
>>>

get_delta()

Return delta information when moving the rotary encoder. That allow to detect if rotary have been turn clockwise or counter-clockwiser

Turning clockwise

fgint@fgintovhd:~ $ python3
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.createElements()
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0INNER = FGINT1.getElement('RMP0INNER')
>>> RMP0INNER.get_delta()
0
>>> RMP0INNER.get_delta()
1
>>>

Turning counter-clockwise

>>> RMP0INNER.get_delta()
0
>>> RMP0INNER.get_delta()
-1
>>>

get_switchstate()

Return the switch pin state defined for the rotary encoder (if any) according to your configuration.

fgint@fgintovhd:~ $ python3
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.createElements()
>>> FGINT1.createDevices()
>>> FGINT1.createElements()
>>> RMP0INNER = FGINT1.getElement('RMP0INNER')
>>> RMP0INNER.get_switchstate()
0
>>>

Encoder button pushed

>>> RMP0INNER.get_switchstate()
1
>>>

Rotary Encoder Worker

So as explain before, rotary encoder are not directly manage throught the RotaryEncoder Class but with a worker that will create a thread dedicate to the rotary encoder which will read regulary the rotary encoder state regarding the main loop state.

Rotary Encoder Worker is created in the main script and we will see how later, but for now here all methods that you will need to manage rotary encoder

Rotary Encoder Worker Methods

run()

start the rotary encoder worker (mandatory before reading the rotary encoder)

get_delta()

Return delta information when moving the rotary encoder. That allow to detect if rotary have been turn clockwise or counter-clockwiser

getValue()

Return the rotary encoder value

setValue()

Set the rotary encoder value (can be used if needed to réinitialize the encoder value)

getMinValue()

Return the minimum value

getMaxValue()

Return the maximal value

getIncrement()

Return increment value