Priorisierung

Python auf Einplatinencomputer wie Raspberry Pi, Banana Pi / Python für Micro-Controller
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Zeige ich doch. Die Klasse erwartet einfach nur drei unterschiedliche Werte, ich habe mir dafuer Konstanten definiert. Du hast doch auch schon welche.
DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

__deets__ hat geschrieben: Donnerstag 30. August 2018, 16:45 Zeige ich doch. Die Klasse erwartet einfach nur drei unterschiedliche Werte, ich habe mir dafuer Konstanten definiert. Du hast doch auch schon welche.
Hallo,

ja aber es geht so nicht, habe es so probiert und aus "video1"... "VideoDetect_1"... probiert, aber egal wie ich stecke, es ist immer noch die
prio ;-(

glg Marco
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Ich weiss jetzt nicht, was du damit meinst. Dazu musst du schon zeigen, wie konkret du das eingebaut hast. Und du musst auch ein bisschen Logik drumrum haben, denn du musst ja nur was machen, wenn sich was geaendert hat. Das nimmt dir die Komponente nicht ab, die trifft nur die Entscheidung, was jetzt gerade aktiv ist.
DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

__deets__ hat geschrieben: Donnerstag 30. August 2018, 17:01 Ich weiss jetzt nicht, was du damit meinst. Dazu musst du schon zeigen, wie konkret du das eingebaut hast. Und du musst auch ein bisschen Logik drumrum haben, denn du musst ja nur was machen, wenn sich was geaendert hat. Das nimmt dir die Komponente nicht ab, die trifft nur die Entscheidung, was jetzt gerade aktiv ist.
Hallo,

ich hatte das direkt über die def des MAX7456 gepackt...
wenn ich den Skript starte zeigt er mir kurz video1...4 an und dann den max reset und dann läuft die while...

glg Marco
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Mit einer solchen Aussage kann man nix anfangen. Einfach nur einkopiert bewirkt der Code gar nix. Und wie genau du den verwendest wird aus einer solchen Beschreibung nicht ersichtlich. Also bitte poste deinem Code.
DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

__deets__ hat geschrieben: Donnerstag 30. August 2018, 17:49 Mit einer solchen Aussage kann man nix anfangen. Einfach nur einkopiert bewirkt der Code gar nix. Und wie genau du den verwendest wird aus einer solchen Beschreibung nicht ersichtlich. Also bitte poste deinem Code.

Code: Alles auswählen

# coding: utf-8
# HAMKit VMAC PiHat 2.4 - VideoDetect Video and Audio Matrix with OSD
# Video A/V In 1 and Audio A/V In 4 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 1 and Audio A/V In 4 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# Video A/V In 2 and Audio A/V In 5 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 2 and Audio A/V In 5 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# Video A/V In 3 and Audio A/V In 6 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 3 and Audio A/V In 6 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# No VideoDetect Video Signal Inputs 1, 2 and 3: Video A/V In Pi with Audio
# A/V In Pi to Video A/V Out 1 and Audio A/V Out 3 with OSD
# No VideoDetect Video Signal Inputs 1, 2 and 3: Video A/V In Pi with Audio
# A/V In Pi to Video A/V Out 2 and Audio A/V Out 4 with OSD
# Marco Dittmann DL9AM, August 2018
#!/usr/bin/python

import RPi.GPIO as GPIO
import smbus
import spidev

import time
import datetime
import os
from ctypes import *

#====== GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()

#Define GPIO Outputs
LEDStatus = 22
PTT = 27
OSD_RST = 25

#Setup Outputs
GPIO.setup(LEDStatus,GPIO.OUT)
GPIO.setup(PTT,GPIO.OUT)
GPIO.setup(OSD_RST,GPIO.OUT)

#Define GPIO Inputs
GPIO_Button = 4
GPIO_VideoDetect_1 = 18
GPIO_VideoDetect_2 = 23
GPIO_VideoDetect_3 = 24
GPIO_SquelchDetect_1 = 12
GPIO_SquelchDetect_2 = 16
GPIO_SquelchDetect_3 = 20
GPIO_SquelchDetect_4 = 21
GPIO_D0 = 26
GPIO_D1 = 19
GPIO_D2 = 13
GPIO_D3 = 6
GPIO_DINT = 5

#Setup Inputs with pull-ups enabled
GPIO.setup(GPIO_Button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D0, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_DINT, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Initiate LEDs
GPIO.output(LEDStatus,GPIO.HIGH)
GPIO.output(PTT,GPIO.HIGH)
time.sleep(.2)
GPIO.output(LEDStatus,GPIO.LOW)
GPIO.output(PTT,GPIO.LOW)
time.sleep(1)

#Define Variables
VideoDetect_1 = False
VideoDetect_2 = False
VideoDetect_3 = False
DValueStore = 0
dateString = '%H %M %S'

#Good old fashion subroutines
def flashleds (flashes):
    for x in range(flashes):
        GPIO.output(LEDStatus,GPIO.HIGH)
        GPIO.output(PTT,GPIO.HIGH)
        time.sleep(.2)
        GPIO.output(LEDStatus,GPIO.LOW)
        GPIO.output(PTT,GPIO.LOW)
        time.sleep(1)

print ("VideoDetect Inputs 1, 2, 3 and SquelchDetect 1, 2, 3 and 4")

#====== FMS6501 Matrix

#define values
DEVICE_BUS = 1
DEVICE_ADDR = 0x43 #0x43 or 0x03
DEVICE_BUS_RELAIS = 1
DEVICE_ADDR_RELAIS = 0x01

#setup i2c bus
i2cbus = smbus.SMBus(DEVICE_BUS)
i2cbus_relais = smbus.SMBus(DEVICE_BUS_RELAIS)

#OSD REset High
GPIO.output(OSD_RST,GPIO.HIGH)

#Set all outputs to mute.
i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x0) #Mute
time.sleep(.01)

#OSD MAX7456
class max7456():

    # Create a SPI
    spi = spidev.SpiDev()

    # MAX7456 opcodes
    VM0_reg  = 0x00
    VM1_reg  = 0x01
    HOS_reg  = 0x02
    VOS_reg  = 0x03
    DMM_reg  = 0x04
    DMAH     = 0x05
    DMAL     = 0x06
    DMDI     = 0x07
    OSDM     = 0x0C
    RB0      = 0x10
    HOS_reg  = 0x02
    STATUS   = 0xA0

    # PAL - VM0_reg commands
    ENABLE_display      = 0x48
    ENABLE_display_vert = 0x4c
    MAX7456_reset       = 0x42
    DISABLE_display     = 0x40

    # Read command
    READ = 0x80
    MAX_screen_rows = 16

    # White levels
    WHITE_level_80  = 0x03
    WHITE_level_90  = 0x02
    WHITE_level_100 = 0x01
    WHITE_level_120 = 0x00

    chars = {' ':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9,
        '0':10, 'A':11, 'B':12, 'C':13, 'D':14, 'E':15, 'F':16, 'G':17, 'H':18, 'I':19,
        'J':20, 'K':21, 'L':22, 'M':23, 'N':24, 'O':25, 'P':26, 'Q':27, 'R':28, 'S':29,
        'T':30, 'U':31, 'V':32, 'W':33, 'X':34, 'Y':35, 'Z':36, 'a':37, 'b':38, 'c':39,
        'd':40, 'e':41, 'f':42, 'g':43, 'h':44, 'i':45, 'j':46, 'k':47, 'l':48, 'm':49,
        'n':50, 'o':51, 'p':52, 'q':53, 'r':54, 's':55, 't':56, 'u':57, 'v':58, 'w':59,
        'x':60, 'y':61, 'z':62, '(':63, ')':64, '.':65, '?':66, ';':67, ':':68, ',':69,
        '´':70, '/':71, '"':72, '-':73, '<':74, '>':75, '@':76

    }

    def __init__(self):
        # Open a SPI port - max7456 connected on SPI0
        self.spi.open(0, 0)
        self.spi.max_speed_hz = 1000000
        self.spi.bits_per_word = 8
        self.spi.cshigh = False
        self.spi.lsbfirst = False
        self.spi.mode = 0

        # On init, reset max7456
        self.reset()

        # Set all rows at the same white level
        for x in range (0, self.MAX_screen_rows):
          self.spi.xfer2([(self.RB0 + x), self.WHITE_level_90])

        # Enable max7456
        self.spi.xfer2([self.VM0_reg, self.ENABLE_display]);

    def printStr(self, X, Y, string, enable = True):
        disp = []
        for char in string:
                 disp.append(self.chars[char])

        print (string)

        if enable == False:
            self.spi.xfer([self.VM0_reg, self.Disable_display])

        # Enable 8 bit mode:
        dmm = self.spi.xfer2([self.DMM_reg + self.READ, 0x00])
        dmm = self.setBit(dmm[1], 6)
        self.spi.xfer2([self.DMM_reg, dmm])

        start = X * 30 + Y

        # Clear position
        self.spi.xfer2([self.DMAH, 0x00])
        self.spi.xfer2([self.DMAL, 0x00])

        for char in disp:
            # Write char
            dmah = self.spi.xfer2([self.DMAH + self.READ, 0x00])
            dmah = self.clearBit(dmah[1], 1)
            self.spi.xfer2([self.DMAH, dmah])

            dmah_pos = ((start >> 8) & 0x01)
            dmal = (start & 0xff)
            dmah = dmah | dmah_pos
            start = start + 1

            # Select MSB
            self.spi.xfer2([self.DMAH, dmah])
            self.spi.xfer2([self.DMAL, dmal])

            self.spi.xfer2([self.DMDI, (char)])

    def reset(self):
        self.spi.xfer2([self.VM0_reg, self.MAX7456_reset])
        time.sleep(.1)
        while True:
            r = self.spi.xfer([self.STATUS, 0x00])
            stable = self.testBit(r[1], 1)
            if stable == 0:
                print ("Reset MAX7456 Ok...")
                break
            break

    def testBit(self, value, offset):
        mask = 1 << value
        return(value & mask)

    def setBit(self, value, offset):
        mask = 1 << offset
        return(value + mask)

    def clearBit(self, int_type, offset):
        mask = ~(1 << offset)
        return(int_type & mask)

try:
    max7456 = max7456()

except KeyboardInterrupt:
    spi.close()




class SourceStack(object):


    def __init__(self):
        self._stack = []


    def on(self, value):
        if value not in self._stack:
            self._stack.append(value)


    def off(self, value):
        if value in self._stack:
            self._stack.remove(value)


    @property
    def current(self):
        return self._stack[-1] if self._stack else None


VIDEO1, VIDEO2, VIDEO3 = "video1", "video2", "video3"

ss = SourceStack()

ss.on(VIDEO1)
print(ss.current)
ss.on(VIDEO1)
print(ss.current)
ss.off(VIDEO2)
print(ss.current)
ss.on(VIDEO2)
print(ss.current)
ss.off(VIDEO1)
print(ss.current)
ss.off(VIDEO2)
print(ss.current)


#Loop
while True:

    VideoDetect_1 = GPIO.input(GPIO_VideoDetect_1)
    VideoDetect_2 = GPIO.input(GPIO_VideoDetect_2)
    VideoDetect_3 = GPIO.input(GPIO_VideoDetect_3)
    SquelchDetect_1 = GPIO.input(GPIO_SquelchDetect_1)
    SquelchDetect_2 = GPIO.input(GPIO_SquelchDetect_2)
    SquelchDetect_3 = GPIO.input(GPIO_SquelchDetect_3)
    SquelchDetect_4 = GPIO.input(GPIO_SquelchDetect_4)


    Button = GPIO.input(GPIO_Button)
    if Button == False:
        max7456.printStr(1,1, "HAMKit VMAC PiHat", enable = True)
        max7456.printStr(2,1, "Reboot by Sysop", enable = True)
        flashleds (10)
        os.system("sudo reboot")

    DINT = GPIO.input(GPIO_DINT)
    if DINT == True:
        D0 = GPIO.input(GPIO_D0)
        D1 = GPIO.input(GPIO_D1)
        D2 = GPIO.input(GPIO_D2)
        D3 = GPIO.input(GPIO_D3)
        DValue = D0+(D1*2)+(D2*4)+(D3*8)
        if DValue == 1:
            max7456.printStr(1,1, "CQ de 10.420GHz", enable = True)
            time.sleep(10)
            max7456.printStr(1,1, "               ", enable = True)
        elif DValue == 2:
              max7456.printStr(1,1, "CQ de 1.251GHz ", enable = True)
              time.sleep(10)
              max7456.printStr(1,1, "               ", enable = True)
        elif DValue == 3:
              max7456.printStr(1,1, "CQ de Test     ", enable = True)
              time.sleep(10)
              max7456.printStr(1,1, "               ", enable = True)

    if VideoDetect_1 == True:
        print ("VideoDetect_1")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x81) #Input 1 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x81) #Input 1 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x87) #Input 4 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x87) #Input 4 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x81) #Input 1 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "CQ 10.420GHz           ", enable = True)
        flashleds (1)

    elif VideoDetect_2 == True:
        print ("VideoDetect_2")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x83) #Input 2 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x83) #Input 2 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x88) #Input 5 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x88) #Input 5 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x83) #Input 2 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "CQ 1.251GHz            ", enable = True)
        flashleds (2)

    elif VideoDetect_3 == True:
        print ("VideoDetect_3")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x85) #Input 3 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x85) #Input 3 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x89) #Input 6 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x89) #Input 6 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x85) #Input 3 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "Test                   ", enable = True)
        flashleds (3)

    elif SquelchDetect_1 == False:
        print ("SquelchDetect_1")
        print ("CQ 436MHz")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x03, 0x03) #Switch HDMI 3 to DVB-T
        flashleds (4)

    else:
        print ("Pi")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x01, 0x01) #Switch HDMI 1 to camera
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x8c) #Input Pi-V to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x8c) #Input Pi-V to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x8b) #Input Pi-A to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x8b) #Input Pi-A to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x8c) #Input Pi-V to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, datetime.datetime.now().strftime(dateString), enable = True)
        max7456.printStr(14,9, ", Pi Temp     C", enable = True)
        file = open("/home/pi/1.txt","r")
        max7456.printStr(14,19, file.readline().rstrip('\n'), enable = True)
        file.close()
        flashleds (0)

    #Heatbeat PTT LED as test
    GPIO.output(PTT,GPIO.HIGH)
    time.sleep(.2)
    GPIO.output(PTT,GPIO.LOW)
    time.sleep(1)

__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Die Verwendung des Codes einfach einkopiert bewirkt nichts. Du musst die Instanz “ss” in deiner while Schleife benutzen. Und solltest meine Beispielaufrufe natürlich entfernen. Beispielhaft:

Code: Alles auswählen

ss = ....
while True:
    if <video1>:
         ss.on(“video1”)
    else:
          ss.off(“video1”)
     # für alle anderen auch 
     ....
     if ss.current == “video1”:
          ...
     elif ss.current == “video2”:
         ...
     else:
         ... # aus
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Hab’s nochmal umgearbeitet, der off call fehlte.
DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

Hallo,

kannst Du noch mal drüber gucken. Funktioniert jetzt wie gewollt Dank Dir.
Wenn so weit ok, habe da noch das Problem mit dem DTMF, und Button. Beides läuft ja im "while" mit und reagiert nur wenn die Zeile aktiv wird
im Durchlauf...
Geht es irgendwie, das beide Funktionen parallel zum VideoDetect laufen, also gleich reagieren ?
Wenn ja, wie ???

Dann bleiben nur noch zwei Sachen:

1. Gibt es eine Möglichkeit einen einmaligen Befehl bei aktivem GPIO abzusenden ?
Also wenn GPIO 18,23 ,24 als einzeln/und/oder "True" :
i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
time.sleep(.5)
i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x03, 0x02) #Switch HDMI 2 to CVBS
(schaltet ein Relais, wartet .5 und schaltet wieder das relais aus) -> Dadurch soll ein HDMI-Schalter betätigt werden...
und dann sobald GPIO 18,23, 24 alle low soll er wieder einmalig ein Relais schalten:
i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x03) #Switch HDMI 2 to CVBS
time.sleep(.5)
i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x03, 0x03) #Switch HDMI 2 to CVBS

das selbe noch einmalmit einem anderen GPIO 12 als False...
dieser schaltet ebenfalls einen HDMI und wenn PIN wieder low soll er wieder über i2c einen HDMI schalten....



Code: Alles auswählen

# coding: utf-8
# HAMKit VMAC PiHat 2.4 - VideoDetect Video and Audio Matrix with OSD
# Video A/V In 1 and Audio A/V In 4 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 1 and Audio A/V In 4 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# Video A/V In 2 and Audio A/V In 5 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 2 and Audio A/V In 5 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# Video A/V In 3 and Audio A/V In 6 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 3 and Audio A/V In 6 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# No VideoDetect Video Signal Inputs 1, 2 and 3: Video A/V In Pi with Audio
# A/V In Pi to Video A/V Out 1 and Audio A/V Out 3 with OSD
# No VideoDetect Video Signal Inputs 1, 2 and 3: Video A/V In Pi with Audio
# A/V In Pi to Video A/V Out 2 and Audio A/V Out 4 with OSD
# Marco Dittmann DL9AM, August 2018
#!/usr/bin/python

import RPi.GPIO as GPIO
import smbus
import spidev

import time
import datetime
import os
from ctypes import *

#====== GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()

#Define GPIO Outputs
LEDStatus = 22
PTT = 27
OSD_RST = 25

#Setup Outputs
GPIO.setup(LEDStatus,GPIO.OUT)
GPIO.setup(PTT,GPIO.OUT)
GPIO.setup(OSD_RST,GPIO.OUT)

#Define GPIO Inputs
GPIO_Button = 4
GPIO_VideoDetect_1 = 18
GPIO_VideoDetect_2 = 23
GPIO_VideoDetect_3 = 24
GPIO_SquelchDetect_1 = 12
GPIO_SquelchDetect_2 = 16
GPIO_SquelchDetect_3 = 20
GPIO_SquelchDetect_4 = 21
GPIO_D0 = 26
GPIO_D1 = 19
GPIO_D2 = 13
GPIO_D3 = 6
GPIO_DINT = 5

#Setup Inputs with pull-ups enabled
GPIO.setup(GPIO_Button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D0, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_DINT, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Initiate LEDs
GPIO.output(LEDStatus,GPIO.HIGH)
GPIO.output(PTT,GPIO.HIGH)
time.sleep(.2)
GPIO.output(LEDStatus,GPIO.LOW)
GPIO.output(PTT,GPIO.LOW)
time.sleep(1)

#Define Variables
VideoDetect_1 = False
VideoDetect_2 = False
VideoDetect_3 = False
DValueStore = 0
dateString = '%H %M %S'

#Good old fashion subroutines
def flashleds (flashes):
    for x in range(flashes):
        GPIO.output(LEDStatus,GPIO.HIGH)
        GPIO.output(PTT,GPIO.HIGH)
        time.sleep(.2)
        GPIO.output(LEDStatus,GPIO.LOW)
        GPIO.output(PTT,GPIO.LOW)
        time.sleep(1)

print ("VideoDetect Inputs 1, 2, 3 and SquelchDetect 1, 2, 3 and 4")

#====== FMS6501 Matrix

#define values
DEVICE_BUS = 1
DEVICE_ADDR = 0x43 #0x43 or 0x03
DEVICE_BUS_RELAIS = 1
DEVICE_ADDR_RELAIS = 0x01

#setup i2c bus
i2cbus = smbus.SMBus(DEVICE_BUS)
i2cbus_relais = smbus.SMBus(DEVICE_BUS_RELAIS)

#OSD REset High
GPIO.output(OSD_RST,GPIO.HIGH)

#Set all outputs to mute.
i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x0) #Mute
time.sleep(.01)

#Matrix Priority
class SourceStack(object):


    def __init__(self):
        self._stack = []


    def on(self, value):
        if value not in self._stack:
            self._stack.append(value)


    def off(self, value):
        if value in self._stack:
            self._stack.remove(value)


    @property
    def current(self):
        return self._stack[-1] if self._stack else None


ss = SourceStack()

#OSD MAX7456
class max7456():

    # Create a SPI
    spi = spidev.SpiDev()

    # MAX7456 opcodes
    VM0_reg  = 0x00
    VM1_reg  = 0x01
    HOS_reg  = 0x02
    VOS_reg  = 0x03
    DMM_reg  = 0x04
    DMAH     = 0x05
    DMAL     = 0x06
    DMDI     = 0x07
    OSDM     = 0x0C
    RB0      = 0x10
    HOS_reg  = 0x02
    STATUS   = 0xA0

    # PAL - VM0_reg commands
    ENABLE_display      = 0x48
    ENABLE_display_vert = 0x4c
    MAX7456_reset       = 0x42
    DISABLE_display     = 0x40

    # Read command
    READ = 0x80
    MAX_screen_rows = 16

    # White levels
    WHITE_level_80  = 0x03
    WHITE_level_90  = 0x02
    WHITE_level_100 = 0x01
    WHITE_level_120 = 0x00

    chars = {' ':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9,
        '0':10, 'A':11, 'B':12, 'C':13, 'D':14, 'E':15, 'F':16, 'G':17, 'H':18, 'I':19,
        'J':20, 'K':21, 'L':22, 'M':23, 'N':24, 'O':25, 'P':26, 'Q':27, 'R':28, 'S':29,
        'T':30, 'U':31, 'V':32, 'W':33, 'X':34, 'Y':35, 'Z':36, 'a':37, 'b':38, 'c':39,
        'd':40, 'e':41, 'f':42, 'g':43, 'h':44, 'i':45, 'j':46, 'k':47, 'l':48, 'm':49,
        'n':50, 'o':51, 'p':52, 'q':53, 'r':54, 's':55, 't':56, 'u':57, 'v':58, 'w':59,
        'x':60, 'y':61, 'z':62, '(':63, ')':64, '.':65, '?':66, ';':67, ':':68, ',':69,
        '´':70, '/':71, '"':72, '-':73, '<':74, '>':75, '@':76

    }

    def __init__(self):
        # Open a SPI port - max7456 connected on SPI0
        self.spi.open(0, 0)
        self.spi.max_speed_hz = 1000000
        self.spi.bits_per_word = 8
        self.spi.cshigh = False
        self.spi.lsbfirst = False
        self.spi.mode = 0

        # On init, reset max7456
        self.reset()

        # Set all rows at the same white level
        for x in range (0, self.MAX_screen_rows):
          self.spi.xfer2([(self.RB0 + x), self.WHITE_level_90])

        # Enable max7456
        self.spi.xfer2([self.VM0_reg, self.ENABLE_display]);

    def printStr(self, X, Y, string, enable = True):
        disp = []
        for char in string:
                 disp.append(self.chars[char])

        print (string)

        if enable == False:
            self.spi.xfer([self.VM0_reg, self.Disable_display])

        # Enable 8 bit mode:
        dmm = self.spi.xfer2([self.DMM_reg + self.READ, 0x00])
        dmm = self.setBit(dmm[1], 6)
        self.spi.xfer2([self.DMM_reg, dmm])

        start = X * 30 + Y

        # Clear position
        self.spi.xfer2([self.DMAH, 0x00])
        self.spi.xfer2([self.DMAL, 0x00])

        for char in disp:
            # Write char
            dmah = self.spi.xfer2([self.DMAH + self.READ, 0x00])
            dmah = self.clearBit(dmah[1], 1)
            self.spi.xfer2([self.DMAH, dmah])

            dmah_pos = ((start >> 8) & 0x01)
            dmal = (start & 0xff)
            dmah = dmah | dmah_pos
            start = start + 1

            # Select MSB
            self.spi.xfer2([self.DMAH, dmah])
            self.spi.xfer2([self.DMAL, dmal])

            self.spi.xfer2([self.DMDI, (char)])

    def reset(self):
        self.spi.xfer2([self.VM0_reg, self.MAX7456_reset])
        time.sleep(.1)
        while True:
            r = self.spi.xfer([self.STATUS, 0x00])
            stable = self.testBit(r[1], 1)
            if stable == 0:
                print ("Reset MAX7456 Ok...")
                break
            break

    def testBit(self, value, offset):
        mask = 1 << value
        return(value & mask)

    def setBit(self, value, offset):
        mask = 1 << offset
        return(value + mask)

    def clearBit(self, int_type, offset):
        mask = ~(1 << offset)
        return(int_type & mask)

try:
    max7456 = max7456()

except KeyboardInterrupt:
    spi.close()

#Loop
while True:

    VideoDetect_1 = GPIO.input(GPIO_VideoDetect_1)
    VideoDetect_2 = GPIO.input(GPIO_VideoDetect_2)
    VideoDetect_3 = GPIO.input(GPIO_VideoDetect_3)
    SquelchDetect_1 = GPIO.input(GPIO_SquelchDetect_1)
    SquelchDetect_2 = GPIO.input(GPIO_SquelchDetect_2)
    SquelchDetect_3 = GPIO.input(GPIO_SquelchDetect_3)
    SquelchDetect_4 = GPIO.input(GPIO_SquelchDetect_4)


    Button = GPIO.input(GPIO_Button)
    if Button == False:
        max7456.printStr(1,1, "HAMKit VMAC PiHat", enable = True)
        max7456.printStr(2,1, "Reboot by Sysop", enable = True)
        flashleds (10)
        os.system("sudo reboot")

    DINT = GPIO.input(GPIO_DINT)
    if DINT == True:
        D0 = GPIO.input(GPIO_D0)
        D1 = GPIO.input(GPIO_D1)
        D2 = GPIO.input(GPIO_D2)
        D3 = GPIO.input(GPIO_D3)
        DValue = D0+(D1*2)+(D2*4)+(D3*8)
        if DValue == 1:
            max7456.printStr(1,1, "CQ de 10.420GHz", enable = True)
            time.sleep(10)
            max7456.printStr(1,1, "               ", enable = True)
        elif DValue == 2:
              max7456.printStr(1,1, "CQ de 1.251GHz ", enable = True)
              time.sleep(10)
              max7456.printStr(1,1, "               ", enable = True)
        elif DValue == 3:
              max7456.printStr(1,1, "CQ de Test     ", enable = True)
              time.sleep(10)
              max7456.printStr(1,1, "               ", enable = True)

    if VideoDetect_1 == True:
       ss.on("VD_1")
    else:
         ss.off("VD_1")
    if VideoDetect_2 == True:
       ss.on("VD_2")
    else:
         ss.off("VD_2")
    if VideoDetect_3 == True:
       ss.on("VD_3")
    else:
         ss.off("VD_3")
    if SquelchDetect_1 == False:
       ss.on("SD_1")
    else:
         ss.off("SD_1")

    if ss.current == "VD_1":
#    if VideoDetect_1 == True:
        print ("VideoDetect_1")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x81) #Input 1 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x81) #Input 1 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x87) #Input 4 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x87) #Input 4 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x81) #Input 1 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "CQ 10.420GHz           ", enable = True)
        flashleds (1)

    elif ss.current == "VD_2":
#    elif VideoDetect_2 == True:
        print ("VideoDetect_2")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x83) #Input 2 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x83) #Input 2 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x88) #Input 5 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x88) #Input 5 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x83) #Input 2 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "CQ 1.251GHz            ", enable = True)
        flashleds (2)

    elif ss.current == "VD_3":
#    elif VideoDetect_3 == True:
        print ("VideoDetect_3")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x85) #Input 3 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x85) #Input 3 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x89) #Input 6 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x89) #Input 6 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x85) #Input 3 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "Test                   ", enable = True)
        flashleds (3)

    elif ss.current == "SD_1":
#    elif SquelchDetect_1 == False:
        print ("SquelchDetect_1")
        print ("CQ 436MHz")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x03, 0x03) #Switch HDMI 3 to DVB-T
        flashleds (4)

    else:
        print ("Pi")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x01, 0x01) #Switch HDMI 1 to camera
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x8c) #Input Pi-V to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x8c) #Input Pi-V to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x8b) #Input Pi-A to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x8b) #Input Pi-A to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x8c) #Input Pi-V to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, datetime.datetime.now().strftime(dateString), enable = True)
        max7456.printStr(14,9, ", Pi Temp     C", enable = True)
        file = open("/home/pi/1.txt","r")
        max7456.printStr(14,19, file.readline().rstrip('\n'), enable = True)
        file.close()
        flashleds (0)

    #Heatbeat PTT LED as test
    GPIO.output(PTT,GPIO.HIGH)
    time.sleep(.2)
    GPIO.output(PTT,GPIO.LOW)
    time.sleep(1)

DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

Hallo,

so noch mal geändert wie empfohlen mit lock.

Ich würde mich riesig freuen wenn Du mir noch bei den letzten Funktionen helfen könntest wie im
vorherigen Post angefragt...

Geht es irgendwie im Skript zu erkennen wenn GPIO x, x, x als UND/ODER aktiv eine einmalige Funktion mit time.sleep(1) auszuführen
um wenn aktiv über i2c eine Relaisplatine anzusteuern...

Das bräuchte ich wie geschrieben für die VideoDetect_1...3 und einmal für den SquelchDetect_1...
dazu müsste eine Funktion noch sein, die den i2c wieder ein Relais steuert wenn VideoDetect_1...3 und/oder Squelch_Detect_1 GPIO
low ist...

Das Shema wäre:
HDMI-Schalter:
Port 1 - Turmkamera
Port 2 - CVBS
Port 3 - DVB-T RX
Port 4 - frei

Und umgeschalten wird per Relais, welche jeweils am Taster sind per i2c.

Sobald VideoDetect_1...3 aktiv soll ein Befehl an den i2c gehen, dann time.sleep(1) und wieder ein Befehl um das Relais nur zu tasten...
Wenn VideoDetect_1...3 inaktiv soll wieder ein Befehl ausgelöst werden, der wieder den HDMI-Schalter auf die Kamera schaltet..
das gleiche für den SquelchDetect_1..

Wichtig wäre nur, wenn zu VideoDetect_1...3 der SquelchDetect_1 aktiv wird und VideoDetect_1...3 als und/oder weg fällt nicht auf die Kamera
geschaltet wird, quasi so, wenn SquelchDetect_1 zukommt der Rückschaltbefehl unter VideoDetect_1...3 geblockt wird und das auch umgekehrt
wenn SquelchDetect_1 aktiv und nun VideoDetect_1...3 als und/oder dazu kommt der Rückschaltbefehl geblockt wird...

Wäre das möglich ? Ich würde mich riesig über weitere Hilfe oder besser den benötigen Code freuen...
Wenn der Skript fertig ist wird er sicher für dieses Projekt weit verbreitet... also es kann auch ruhig der Autor genannt werden wenn gemocht...

glg Marco

Code: Alles auswählen

# coding: utf-8
# HAMKit VMAC PiHat 2.4 - VideoDetect Video and Audio Matrix with OSD
# Video A/V In 1 and Audio A/V In 4 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 1 and Audio A/V In 4 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# Video A/V In 2 and Audio A/V In 5 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 2 and Audio A/V In 5 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# Video A/V In 3 and Audio A/V In 6 to Video A/V Out 1 and Audio A/V Out 3
# with OSD
# Video A/V In 3 and Audio A/V In 6 to Video A/V Out 2 and Audio A/V Out 4
# with OSD
# No VideoDetect Video Signal Inputs 1, 2 and 3: Video A/V In Pi with Audio
# A/V In Pi to Video A/V Out 1 and Audio A/V Out 3 with OSD
# No VideoDetect Video Signal Inputs 1, 2 and 3: Video A/V In Pi with Audio
# A/V In Pi to Video A/V Out 2 and Audio A/V Out 4 with OSD
# Marco Dittmann DL9AM, August 2018
#!/usr/bin/python

import RPi.GPIO as GPIO
import smbus
import spidev

import threading

import time
import datetime
import os

from ctypes import *

#====== GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()

#Define GPIO Outputs
LEDStatus = 22
PTT = 27
OSD_RST = 25

#Setup Outputs
GPIO.setup(LEDStatus,GPIO.OUT)
GPIO.setup(PTT,GPIO.OUT)
GPIO.setup(OSD_RST,GPIO.OUT)

#Define GPIO Inputs
GPIO_Button = 4
GPIO_VideoDetect_1 = 18
GPIO_VideoDetect_2 = 23
GPIO_VideoDetect_3 = 24
GPIO_SquelchDetect_1 = 12
GPIO_SquelchDetect_2 = 16
GPIO_SquelchDetect_3 = 20
GPIO_SquelchDetect_4 = 21
GPIO_D0 = 26
GPIO_D1 = 19
GPIO_D2 = 13
GPIO_D3 = 6
GPIO_DINT = 5

#Setup Inputs with pull-ups enabled
GPIO.setup(GPIO_Button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_VideoDetect_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_SquelchDetect_4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D0, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_D3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_DINT, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Initiate LEDs
GPIO.output(LEDStatus,GPIO.HIGH)
GPIO.output(PTT,GPIO.HIGH)
time.sleep(.2)
GPIO.output(LEDStatus,GPIO.LOW)
GPIO.output(PTT,GPIO.LOW)
time.sleep(1)

#Define Variables
VideoDetect_1 = False
VideoDetect_2 = False
VideoDetect_3 = False
SquelchDetect_1 = True
SquelchDetect_2 = True
SquelchDetect_3 = True
SquelchDetect_4 = True
DValueStore = 0
dateString = '%H %M %S'

#Good old fashion subroutines
def flashleds (flashes):
    for x in range(flashes):
        GPIO.output(LEDStatus,GPIO.HIGH)
        GPIO.output(PTT,GPIO.HIGH)
        time.sleep(.2)
        GPIO.output(LEDStatus,GPIO.LOW)
        GPIO.output(PTT,GPIO.LOW)
        time.sleep(1)

print ("VideoDetect Inputs 1, 2, 3 and SquelchDetect 1, 2, 3 and 4")

#====== FMS6501 Matrix

#define values
DEVICE_BUS = 1
DEVICE_ADDR = 0x43 #0x43 or 0x03
DEVICE_BUS_RELAIS = 1
DEVICE_ADDR_RELAIS = 0x01

#setup i2c bus
i2cbus = smbus.SMBus(DEVICE_BUS)
i2cbus_relais = smbus.SMBus(DEVICE_BUS_RELAIS)

#OSD REset High
GPIO.output(OSD_RST,GPIO.HIGH)

#Set all outputs to mute.
i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x0) #Mute
time.sleep(.01)
i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x0) #Mute
time.sleep(.01)

#Matrix Priority
class SourceStack(object):

    def __init__(self):
        self._stack = []
        self._lock = threading.Lock()


    def on(self, value):
        with self._lock:
            if value not in self._stack:
                self._stack.append(value)


    def off(self, value):
        with self._lock:
            if value in self._stack:
                self._stack.remove(value)


    @property
    def current(self):
        with self._lock:
            return self._stack[-1] if self._stack else None

ss = SourceStack()

#OSD MAX7456
class max7456():

    # Create a SPI
    spi = spidev.SpiDev()

    # MAX7456 opcodes
    VM0_reg  = 0x00
    VM1_reg  = 0x01
    HOS_reg  = 0x02
    VOS_reg  = 0x03
    DMM_reg  = 0x04
    DMAH     = 0x05
    DMAL     = 0x06
    DMDI     = 0x07
    OSDM     = 0x0C
    RB0      = 0x10
    HOS_reg  = 0x02
    STATUS   = 0xA0

    # PAL - VM0_reg commands
    ENABLE_display      = 0x48
    ENABLE_display_vert = 0x4c
    MAX7456_reset       = 0x42
    DISABLE_display     = 0x40

    # Read command
    READ = 0x80
    MAX_screen_rows = 16

    # White levels
    WHITE_level_80  = 0x03
    WHITE_level_90  = 0x02
    WHITE_level_100 = 0x01
    WHITE_level_120 = 0x00

    chars = {' ':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9,
        '0':10, 'A':11, 'B':12, 'C':13, 'D':14, 'E':15, 'F':16, 'G':17, 'H':18, 'I':19,
        'J':20, 'K':21, 'L':22, 'M':23, 'N':24, 'O':25, 'P':26, 'Q':27, 'R':28, 'S':29,
        'T':30, 'U':31, 'V':32, 'W':33, 'X':34, 'Y':35, 'Z':36, 'a':37, 'b':38, 'c':39,
        'd':40, 'e':41, 'f':42, 'g':43, 'h':44, 'i':45, 'j':46, 'k':47, 'l':48, 'm':49,
        'n':50, 'o':51, 'p':52, 'q':53, 'r':54, 's':55, 't':56, 'u':57, 'v':58, 'w':59,
        'x':60, 'y':61, 'z':62, '(':63, ')':64, '.':65, '?':66, ';':67, ':':68, ',':69,
        '´':70, '/':71, '"':72, '-':73, '<':74, '>':75, '@':76

    }

    def __init__(self):
        # Open a SPI port - max7456 connected on SPI0
        self.spi.open(0, 0)
        self.spi.max_speed_hz = 1000000
        self.spi.bits_per_word = 8
        self.spi.cshigh = False
        self.spi.lsbfirst = False
        self.spi.mode = 0

        # On init, reset max7456
        self.reset()

        # Set all rows at the same white level
        for x in range (0, self.MAX_screen_rows):
          self.spi.xfer2([(self.RB0 + x), self.WHITE_level_90])

        # Enable max7456
        self.spi.xfer2([self.VM0_reg, self.ENABLE_display]);

    def printStr(self, X, Y, string, enable = True):
        disp = []
        for char in string:
                 disp.append(self.chars[char])

        print (string)

        if enable == False:
            self.spi.xfer([self.VM0_reg, self.Disable_display])

        # Enable 8 bit mode:
        dmm = self.spi.xfer2([self.DMM_reg + self.READ, 0x00])
        dmm = self.setBit(dmm[1], 6)
        self.spi.xfer2([self.DMM_reg, dmm])

        start = X * 30 + Y

        # Clear position
        self.spi.xfer2([self.DMAH, 0x00])
        self.spi.xfer2([self.DMAL, 0x00])

        for char in disp:
            # Write char
            dmah = self.spi.xfer2([self.DMAH + self.READ, 0x00])
            dmah = self.clearBit(dmah[1], 1)
            self.spi.xfer2([self.DMAH, dmah])

            dmah_pos = ((start >> 8) & 0x01)
            dmal = (start & 0xff)
            dmah = dmah | dmah_pos
            start = start + 1

            # Select MSB
            self.spi.xfer2([self.DMAH, dmah])
            self.spi.xfer2([self.DMAL, dmal])

            self.spi.xfer2([self.DMDI, (char)])

    def reset(self):
        self.spi.xfer2([self.VM0_reg, self.MAX7456_reset])
        time.sleep(.1)
        while True:
            r = self.spi.xfer([self.STATUS, 0x00])
            stable = self.testBit(r[1], 1)
            if stable == 0:
                print ("Reset MAX7456 Ok...")
                break
            break

    def testBit(self, value, offset):
        mask = 1 << value
        return(value & mask)

    def setBit(self, value, offset):
        mask = 1 << offset
        return(value + mask)

    def clearBit(self, int_type, offset):
        mask = ~(1 << offset)
        return(int_type & mask)

try:
    max7456 = max7456()

except KeyboardInterrupt:
    spi.close()

#Loop
while True:

    VideoDetect_1 = GPIO.input(GPIO_VideoDetect_1)
    VideoDetect_2 = GPIO.input(GPIO_VideoDetect_2)
    VideoDetect_3 = GPIO.input(GPIO_VideoDetect_3)
    SquelchDetect_1 = GPIO.input(GPIO_SquelchDetect_1)
    SquelchDetect_2 = GPIO.input(GPIO_SquelchDetect_2)
    SquelchDetect_3 = GPIO.input(GPIO_SquelchDetect_3)
    SquelchDetect_4 = GPIO.input(GPIO_SquelchDetect_4)


    Button = GPIO.input(GPIO_Button)
    if Button == False:
        max7456.printStr(1,1, "HAMKit VMAC PiHat", enable = True)
        max7456.printStr(2,1, "Reboot by Sysop", enable = True)
        flashleds (10)
        os.system("sudo reboot")

    DINT = GPIO.input(GPIO_DINT)
    if DINT == True:
        D0 = GPIO.input(GPIO_D0)
        D1 = GPIO.input(GPIO_D1)
        D2 = GPIO.input(GPIO_D2)
        D3 = GPIO.input(GPIO_D3)
        DValue = D0+(D1*2)+(D2*4)+(D3*8)
        if DValue == 1:
            max7456.printStr(1,1, "CQ de 10.420GHz", enable = True)
            time.sleep(10)
            max7456.printStr(1,1, "               ", enable = True)
        elif DValue == 2:
              max7456.printStr(1,1, "CQ de 1.251GHz ", enable = True)
              time.sleep(10)
              max7456.printStr(1,1, "               ", enable = True)
        elif DValue == 3:
              max7456.printStr(1,1, "CQ de Test     ", enable = True)
              time.sleep(10)
              max7456.printStr(1,1, "               ", enable = True)

    if VideoDetect_1 == True:
       ss.on("VD_1")
    else:
         ss.off("VD_1")
    if VideoDetect_2 == True:
       ss.on("VD_2")
    else:
         ss.off("VD_2")
    if VideoDetect_3 == True:
       ss.on("VD_3")
    else:
         ss.off("VD_3")
    if SquelchDetect_1 == False:
       ss.on("SD_1")
    else:
         ss.off("SD_1")

    if ss.current == "VD_1":
#    if VideoDetect_1 == True:
        print ("VideoDetect_1")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x81) #Input 1 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x81) #Input 1 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x87) #Input 4 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x87) #Input 4 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x81) #Input 1 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "CQ 10.420GHz           ", enable = True)
        flashleds (1)

    elif ss.current == "VD_2":
#    elif VideoDetect_2 == True:
        print ("VideoDetect_2")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x83) #Input 2 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x83) #Input 2 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x88) #Input 5 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x88) #Input 5 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x83) #Input 2 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "CQ 1.251GHz            ", enable = True)
        flashleds (2)

    elif ss.current == "VD_3":
#    elif VideoDetect_3 == True:
        print ("VideoDetect_3")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x02, 0x02) #Switch HDMI 2 to CVBS
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x85) #Input 3 to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x85) #Input 3 to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x89) #Input 6 to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x89) #Input 6 to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x85) #Input 3 to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, "Test                   ", enable = True)
        flashleds (3)

    elif ss.current == "SD_1":
#    elif SquelchDetect_1 == False:
        print ("SquelchDetect_1")
        print ("CQ 436MHz")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x03, 0x03) #Switch HDMI 3 to DVB-T
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x8c) #Input Pi-V to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x8c) #Input Pi-V to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x8b) #Input Pi-A to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x8b) #Input Pi-A to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x8c) #Input Pi-V to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, datetime.datetime.now().strftime(dateString), enable = True)
        max7456.printStr(14,9, ", Pi Temp     C", enable = True)
        file = open("/home/pi/1.txt","r")
        max7456.printStr(14,19, file.readline().rstrip('\n'), enable = True)
        file.close()
        flashleds (4)

    else:
        print ("Pi")
#        i2cbus_relais.write_byte_data(DEVICE_ADDR_RELAIS, 0x01, 0x01) #Switch HDMI 1 to camera
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x8c) #Input Pi-V to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x8c) #Input Pi-V to Output 2
        i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x8b) #Input Pi-A to Output 3
        i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x8b) #Input Pi-A to Output 4
        i2cbus.write_byte_data(DEVICE_ADDR, 0x07, 0x8c) #Input Pi-V to OSD In
        i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x86) #Input OSD to Output 1
        i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x86) #Input OSD to Output 2
        max7456.printStr(14,1, datetime.datetime.now().strftime(dateString), enable = True)
        max7456.printStr(14,9, ", Pi Temp     C", enable = True)
        file = open("/home/pi/1.txt","r")
        max7456.printStr(14,19, file.readline().rstrip('\n'), enable = True)
        file.close()
        flashleds (0)

    #Heatbeat PTT LED as test
    GPIO.output(PTT,GPIO.HIGH)
    time.sleep(.2)
    GPIO.output(PTT,GPIO.LOW)
    time.sleep(1)

__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Diese Beschreibung hat mir leider keinerlei Klarheit gebracht, was da wie voneinander abhängen soll.
Benutzeravatar
pillmuncher
User
Beiträge: 1482
Registriert: Samstag 21. März 2009, 22:59
Wohnort: Pfaffenwinkel

DL9AM hat geschrieben: Samstag 1. September 2018, 14:06 Geht es irgendwie im Skript zu erkennen wenn GPIO x, x, x als UND/ODER aktiv eine einmalige Funktion mit time.sleep(1) auszuführen
um wenn aktiv über i2c eine Relaisplatine anzusteuern...
Das ist kein verständlicher Satz. Wenn du es auf Deutsch nicht klar formulieren kannst, kannst du das auch nicht programmieren. Und auch niemand anders kann das.
In specifications, Murphy's Law supersedes Ohm's.
DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

Hallo,

bitte auf meinen Skript bezogen. Ginge es parallel eine GPIO Abfrage laufen zu lassen,
die bei aktiven GPIO (high) als "und/oder" bezogen
auf mehrere GPIO's (high) drei Befehle ausführt und
wartet bis die GPIO's wieder inaktiv sind (low)
und dann wieder drei neue Befehle einmalig ausführt?

so in der Art

if VideoDetect_1 == VideoDetect_2 == VideoDetect_3 == True
Befehl 1
time.sleep(1)
Befehl 3

if VideoDetect_1 == VideoDetect_2 == VideoDetect_3 == False
Befehl 1
time.sleep(1)
Befehl 3

dazu noch

if SquelchDetect_1 == False
Befehl 1
time.sleep(1)
Befehl 3

if SquelchDetect_1 == True
Befehl 1
time.sleep(1)
Befehl 3

aber das muss so sein, das wenn VideoDetect
erkannt und SquelchDetect dazu kommt oder umgekehrt der if not verriegelt wird bis wirklich
kein VideoDetect oder SquelchDetect mehr erkannt ist und dann erst der 2. if not Befehl ausgeführt wird.

Ginge das ?

Ziel wäre:

HDMI Schalter
Port 1 Kamera
Port 2 CVBS
Port 3 dvbt RX

grundeinstellung soll sein Kamera

erst wenn VideoDetect_1...3 als und/oder
True wird soll ein i2c Befehl ein relais schalten
für 1 Sekunde (Time.sleep(1)), dann erneut befehl2 i2c Relais zurück schalten. Dadurch wird auf CVBS geschaltet. Nun soll solange VideoDetect_1...3 als und/oder True ist nichts weiter passieren. erst wenn VideoDetect_1...3 als und/oder low wird, also auch der letzte VideoDetect low ist wieder auf die Kamera geschaltet werden.

Das gleiche brauch ich auch für SquelchDetect_1,
welcher auf HDMI 3 dvbt RX schalten soll.
wenn Squelch_detect True wird soll wieder auf HDMI 1 geschaltet werden.

hier wäre nur wichtig das wenn VideoDetect_1..3
aktiv ist, jetzt noch SquelchDetect_1 kommt und bei VideoDetect_1...3 alles low wird nicht auf die Kamera zurück geschalten wird . das gleiche auch umgekehrt wenn SquelchDetect_1 False ist und nun VideoDetect_1 ...3 dazu kommt und nun SquelchDetect_1 True wird nicht auf die Kamera zurück geschalten wird.
es darf nur auf die Kamera zurück geschalten werden wenn weder VideoDetect_1...3, noch SquelchDetect_1 aktiv sind...

ich hoffe das war jetzt zu verstehen wie ich es meine ?

glg
DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

Hallo,

ich möchte die Frage erst mal einfacher stellen.

Gibt es eine Möglichkeit in meinem bisherigen Skript in der while-Schleife zu erkennen ob VideoDetect_1 "True" bzw GPIO 18 high und dann nur einmal
einen Befehl auszuführen ? Sobald VideoDetect_1 inaktiv wird "False" bzw. GPIO 18 low einen Befehl ebenfalls nur einmal auszuführen ?

Wie müsste das aussehen ?

GLG Marco
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Nein, es ist leider nicht verstaendlich. Das Problem ist, dass du tief in einem Projekt steckst, dessen Zweck und Domäne hier keiner kennt. Und Fragen stellst, die auf der einen Seite klar machen, dass du die Loesung - also das Programmieren - nicht verstehst (was ja nicht schlimm ist), aber auf der anderen Seite Details beschreibst, die hier wiederum genauso wenig jemand kennt, dieses Verstaendnis brauchen, damit sie wissen, was warum wie miteinander zusammenhaengt.

Es gibt einen Grund, warum der Prozess, Softwareprojekte durchzuziehen durchaus als langwierig und anstrengend empfunden wird. Weil es erfordert, dass man die Programmierer ausreichend abholt in ihrem Verstaendnis des Problems, und diese wiederum umgekehrt auf die Auftraggeber einwirken koennen, weil bestimmte Anforderungen nicht oder nur durch ungebuehrlichen Aufwand realisierbar sind. Und dann kommt auch noch die Dimension der Nutzerfreundlichkeit dazu, denn sowohl Programmierer als auch Auftraggeber haben durch ihre Brille gegebenenfalls einen verzerrten Blick darauf, was einem Nutzer abverlangt werden kann und was nicht.

Und Projekte so darzustellen und zu steuern ist auch zu allem Ueberfluss ein Skill der genauso wie programmieren oder der Entwurf von elektronischen Schaltungen gelernt sein will.

Du kannst probieren, dein Projekt nochmal anders darzustellen. Beschreib, welches Problem es loesen soll, welche Komponenten es enthaelt, wie die Benutzung aussieht, welche technischen Schnittstellen zwischen denen existieren, wie die verschaltet sind. Und dann kann man da ggf. was draus machen. Aber ganz ehrlich: das ist nicht trivial, und das, wofuer man ueblicherweise bezahlt wird, wenn man sich damit auskennt.

Mal ein paar Zeilen Code beisteuern, oder eine Fehlermeldung erklaeren, das kann hier geleistet werden. Ein offensichtlich komplexes Produkt mal so eben aus der Lameng raus durch ein paar Forenpostings zu entwickeln ist sehr unwahrscheinlich, dass das funktioniert.
DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

Hallo,

ich habe mal einen Code zusammen gebaut, der aber noch nicht so funktioniert wie ich es mir vorstelle.
Halbwegs geht er, denn wenn ich GPIO 18 high mache gibt er einmal den Flankentext, wird Pin 18 low kommt einmal
"Ende" angezeigt...

Das würde so gehen um einmal einen Befehl auszuführen...

So müsste es aber auch bei GPIO 23 und 24 gehen ...

und als Step 2 müsste bei aktiven GPIO 18, 23 oder 24 das "False" blockiert werden, erst wenn der letzte aktive GPIO low wird, darf
das "Ende" auftauchen..

Code: Alles auswählen

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import datetime

# Variablen initialisieren
Tic = 0    # Zaehler
stopp = 0  # Zeitpunkt steigende Flanke
start = 0  # Zeitpunkt fallende Flanke
delta = 0  # Zeitdifferenz zwischen start und stopp
# Variablen initialisieren
Tic2 = 0    # Zaehler
stopp2 = 0  # Zeitpunkt steigende Flanke
start2 = 0  # Zeitpunkt fallende Flanke
delta2 = 0  # Zeitdifferenz zwischen start und stopp
# Variablen initialisieren
Tic3 = 0    # Zaehler
stopp3 = 0  # Zeitpunkt steigende Flanke
start3 = 0  # Zeitpunkt fallende Flanke
delta3 = 0  # Zeitdifferenz zwischen start und stopp

# GPIO initialisieren
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN) # Pin 11
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)

# internen Pullup-Widerstand aktivieren.
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Callback-Funktion fuer beide Flanken
def measure(channel):
  global start
  global stopp
  global delta
  if GPIO.input(18) == 0:       # fallende Flanke, Startzeit speichern
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("delta = %1.2f" % delta)

  global start2
  global stopp2
  global delta2
  if GPIO.input(23) == 0:
    start2 = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp2 = time.time()
    delta2 = stopp - start       # Zeitdifferenz berechnen
    print("delta = %1.2f" % delta)

  global start3
  global stopp3
  global delta3
  if GPIO.input(24) == 0:
    start3 = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp3 = time.time()
    delta3 = stopp - start       # Zeitdifferenz berechnen
    print("delta = %1.2f" % delta)

  if GPIO.input(18) == False:
    print("Test1")
  if GPIO.input(23) == False:
    print("Test2")
  if GPIO.input(24) == False:
    print("Test3")

# Interrupt fuer beide Flanken aktivieren
GPIO.add_event_detect(18, GPIO.BOTH, callback=measure, bouncetime=200)
GPIO.add_event_detect(23, GPIO.BOTH, callback=measure, bouncetime=200)
GPIO.add_event_detect(24, GPIO.BOTH, callback=measure, bouncetime=200)

try:
  i = 3
  while i < 100:
    # nix Sinnvolles tun
    Tic = Tic + 1
    print "Tic %d" % Tic
    time.sleep(1)

# reset GPIO settings if user pressed Ctrl+C
except KeyboardInterrupt:
  GPIO.cleanup()
  print("\nBye!")

DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

Hallo,

jetzt läuft es, aber ein Problem besteht noch bevor ich den Code einbaue.

Wie kann ich es hinbekommen, das solange GPIO 18 UND/ODER 23 UND/ODER 24 UND/ODER 12 nicht :

xx = 18,23,24,12
if GPIO.input(xx) == False:
print("SquelchDetect_1 - GPIO low")

ausgeführt wird - also quasi übersprungen ?

Erst wenn keiner der 4 GPIO mehr high ist, darf der Befehl erfolgen...

glg

Code: Alles auswählen

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import datetime

# Variablen initialisieren
Tic = 0    # Zaehler
stopp = 0  # Zeitpunkt steigende Flanke
start = 0  # Zeitpunkt fallende Flanke
delta = 0  # Zeitdifferenz zwischen start und stopp

# GPIO initialisieren
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN) # VideoDetect_1
GPIO.setup(23, GPIO.IN) # VideoDetect_2
GPIO.setup(24, GPIO.IN) # VideoDetect_3
GPIO.setup(12, GPIO.IN) # SquelchDetect_1

# internen Pullup-Widerstand aktivieren.
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Callback-Funktion fuer beide Flanken
def measure1(channel):
  global start
  global stopp
  global delta
  if GPIO.input(18) == 0:       # fallende Flanke, Startzeit speichern
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("VideoDetect_1 - GPIO high")
  if GPIO.input(18) == False:
    print("VideoDetect_1 - GPIO low")

def measure2(channel):
  global start
  global stopp
  global delta
  if GPIO.input(23) == 0:
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("VideoDetect_2 - GPIO high")
  if GPIO.input(23) == False:
    print("VideoDetect_2 - GPIO low")

def measure3(channel):
  global start
  global stopp
  global delta
  if GPIO.input(24) == 0:
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("VideoDetect_3 - GPIO high")
  if GPIO.input(24) == False:
    print("VideoDetect_3 - GPIO low")

def measure4(channel):
  global start
  global stopp
  global delta
  if GPIO.input(12) == 0 == False:
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("SquelchDetect_1 - GPIO high")
  if GPIO.input(12) == False:
    print("SquelchDetect_1 - GPIO low")

# Interrupt fuer beide Flanken aktivieren
GPIO.add_event_detect(18, GPIO.BOTH, callback=measure1, bouncetime=200)
GPIO.add_event_detect(23, GPIO.BOTH, callback=measure2, bouncetime=200)
GPIO.add_event_detect(24, GPIO.BOTH, callback=measure3, bouncetime=200)
GPIO.add_event_detect(12, GPIO.BOTH, callback=measure4, bouncetime=200)

try:
  i = 3
  while i < 100:
    # nix Sinnvolles tun
    Tic = Tic + 1
    print "Tic %d" % Tic
    time.sleep(1)

# reset GPIO settings if user pressed Ctrl+C
except KeyboardInterrupt:
  GPIO.cleanup()
  print("\nBye!")

DL9AM
User
Beiträge: 27
Registriert: Mittwoch 29. August 2018, 14:28

Hallo,

so geht es :


Code: Alles auswählen

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import datetime

# Variablen initialisieren
Tic = 0    # Zaehler
stopp = 0  # Zeitpunkt steigende Flanke
start = 0  # Zeitpunkt fallende Flanke
delta = 0  # Zeitdifferenz zwischen start und stopp

# GPIO initialisieren
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN) # VideoDetect_1
GPIO.setup(23, GPIO.IN) # VideoDetect_2
GPIO.setup(24, GPIO.IN) # VideoDetect_3
GPIO.setup(12, GPIO.IN) # SquelchDetect_1

# internen Pullup-Widerstand aktivieren.
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

# Callback-Funktion fuer beide Flanken
def measure1(channel):
  global start
  global stopp
  global delta
  if GPIO.input(18) == 0:       # fallende Flanke, Startzeit speichern
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("VideoDetect_1 - GPIO high")
  if GPIO.input(18) ++ GPIO.input(23) ++ GPIO.input(24) ++ GPIO.input(12) == False:
    print("VideoDetect_1 - GPIO low")

def measure2(channel):
  global start
  global stopp
  global delta
  if GPIO.input(23) == 0:
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("VideoDetect_2 - GPIO high")
  if GPIO.input(18) ++ GPIO.input(23) ++ GPIO.input(24) ++ GPIO.input(12) == False:
    print("VideoDetect_2 - GPIO low")

def measure3(channel):
  global start
  global stopp
  global delta
  if GPIO.input(24) == 0:
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("VideoDetect_3 - GPIO high")
  if GPIO.input(18) ++ GPIO.input(23) ++ GPIO.input(24) ++ GPIO.input(12) == False:
    print("VideoDetect_3 - GPIO low")

def measure4(channel):
  global start
  global stopp
  global delta
  if GPIO.input(12) == 0 == False:
    start = time.time()
  else:                         # steigende Flanke, Endezeit speichern
    stopp = time.time()
    delta = stopp - start       # Zeitdifferenz berechnen
    print("SquelchDetect_1 - GPIO high")
  if GPIO.input(18) ++ GPIO.input(23) ++ GPIO.input(24) ++ GPIO.input(12) == False:
    print("SquelchDetect_1 - GPIO low")

# Interrupt fuer beide Flanken aktivieren
GPIO.add_event_detect(18, GPIO.BOTH, callback=measure1, bouncetime=200)
GPIO.add_event_detect(23, GPIO.BOTH, callback=measure2, bouncetime=200)
GPIO.add_event_detect(24, GPIO.BOTH, callback=measure3, bouncetime=200)
GPIO.add_event_detect(12, GPIO.BOTH, callback=measure4, bouncetime=200)

try:
  i = 3
  while i < 100:
    # nix Sinnvolles tun
    Tic = Tic + 1
    print "Tic %d" % Tic
    time.sleep(1)

# reset GPIO settings if user pressed Ctrl+C
except KeyboardInterrupt:
  GPIO.cleanup()
  print("\nBye!")

__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Ich weiss nicht genau was jetzt daran "geht" - aber konkret sind doch die ganzen deltas geschweige denn starts und stops voellig sinnfrei, denn sie werden ja gar nicht genutzt. Und WENN sie genutzt werden wuerden, dann wuerde das ganze explodieren, denn start, stopp und delta sind globale Variablen die fuer alle verschiedenen Events benutzt werden. Wenn also measure1 kommt, und start belegt, und dann kommt danach measure2, und belegt start2, dann rechnet das naechste measure1 mit fallender Flanke delta ja nicht korrekt aus.

Aber wenn dein Problem damit geloest ist, dann ist ja gut :)
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Achso, ++ gibt es in Python nicht. Woher auch immer du das hast. Das ist ein normales +, weil + genauso wie - einfach vorangestellt werden kann. Und was du mit GPIO.input(18) ++ GPIO.input(23) ++ GPIO.input(24) ++ GPIO.input(12) sagst ist im Grunde nur

GPIO.input(18) or GPIO.input(23) orGPIO.input(24) or GPIO.input(12)

oder noch ein bisschen kompakter

any(GPIO.input(pin) for pin in (18, 23, 24, 12))
Antworten