Bonjour,
Je débute en Python, il fallait bien qu'un jour je m'amuse avec ce langage. Je me suis débrouillé seul jusqu'à present, mais la je suis tombé sur un os, j'ai beau essayé de le ronger dans tous les sens, je n'arrive pas en extraire la moelle qui me permettra d'avancer et de passer à l'os suivant .
Le principe est : j'ai un vieux jukebox avec un systeme laser obsolète et surtout plus maintenu pas Pioneer (pour ne pas le dire). Donc j'ai eu l'idée d'utilisé un Raspberry Pi 3 avec un Raspbian Jessie. Au debut, j'ai voulu utilisé un Raspbian Stretch, mais il semblerait que Stretch ne gère plus simplement l'interface IR, donc downgrade vers Jessie.
J'ai installe MPD et MPC pour faire jouer les morceaux de musique, mais voulant conserver les interfaces IHM d'origine du jukebox (clavier, afficheurs 7 segments et télécommande) j'ai eu l'idée de faire un petit programme Python pour m'interfacer avec MPC.
Actuellement Le clavier matriciel fonctionne seul ou la télécommande IR fonctionne seul, mais si j'essaie de faire fonctionner la télécommande IR avec le clavier la réception IR me bloque le clavier car le système reste en attente d'une réception IR avant de passer au clavier.
Mon coté débutant en python me bloque sur ce point, c'est peut-être pas grand chose ou surement une erreur de ma part, mais je bloque. Alors si quelqu'un a la solution je veux bien un coup de main.
Merci.
Je vous met mon code (attention les yeux vont saigner, devant les erreurs, en lisant mon code )
Mon code principal
le code pour le clavier matriciel
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 import os import time import subprocess from subprocess import Popen,PIPE import envoiMPC from clavierCJV50 import keypad import pyirw #for disc in range(10,99): # d = int(disc/10) # d = disc-(d*10) # if d < 7 and d > 0: # os.system('mpc add '+str(disc)+'/01.mp3') # lecture du volume en cours kp = keypad() sockid = pyirw.init_irw() print ('--- IR pret ---') p = os.popen('mpc -f %file%') for cpt in range(0,5): out=p.readline() if 'volume:' in out: volume = int(out[7]+out[8]+out[9]) print ('volume = '+str(volume)) Cpt=0 toPlay='' print ('--- Clavier pret ---') # Boucle principale while True: digit = None #print('att digit') while digit == None: #print('att ir') key=None key = pyirw.next_key() if key: cle=key[0] if cle == b'KEY_VOLUMEUP': digit='+' if cle == b'KEY_VOLUMEDOWN': digit='-' if cle == b'KEY_MUTE': digit='M' if cle == b'KEY_CANCEL': digit='A' if Cpt==0: digit=None Cpt=1 else: Cpt=0 if digit==None: #print('att keyb') digit = kp.getKey() if digit == 'C': toPlay = '' elif digit == 'R': dummy = 0 elif digit == 'B': dummy = 0 elif digit == '+': if volume < 100: volume = volume + 5 os.popen('mpc volume '+str(volume)) elif digit == '-': if volume > 0: volume = volume - 5 os.popen('mpc volume '+str(volume)) elif digit == 'M': if volume > 0: oldvolume = volume volume=0 os.popen('mpc volume '+str(volume)) else: volume=oldvolume os.popen('mpc volume '+str(volume)) elif digit == 'A': os.popen('mpc next') #os.popen('mpc clear') else: toPlay = toPlay + str(digit) if (len(toPlay)>0 and int(toPlay[0]) < 1) or (len(toPlay)>1 and (int(toPlay[1]) < 1 or int(toPlay[1])) > 6): #beepbeep() print('beep beep') toPlay='' if len(toPlay) == 4: toPlay = toPlay[0]+toPlay[1]+'/'+toPlay[2]+toPlay[3] toPlay = 'mpc add '+toPlay+'.mp3' p = os.popen(toPlay) #out1 = p.readerror() #out2 = p.readline() #out3 = p.readline() #print(p.read()) #print(out2) #print(out3) if 'error' in p.read(): #beepbeep() print('beep beep') else: # beep print('beep') p = os.popen('mpc play') toPlay = '' print(toPlay) time.sleep(0.5)
le code pour la reception IR
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 # Gestion du clavier pour le Juke Box # Pionner CJ-V50 # Pionner CJ-V51 # La connection au GPIO doit se faire ainsi: # Key1 <-> GPIO4 [07] # Key2 <-> GPIO17 [11] # Key3 <-> GPIO27 [13] # Col1 <-> Non connecté # Col2 <-> GPIO22 [15] # Col3 <-> GPIO25 [22] # Col4 <-> GPIO24 [18] # Col5 <-> GPIO23 [16] # Col6 <-> GPIO26 [37] # coding=utf-8 import time import RPi.GPIO as GPIO GPIO.setwarnings(False) class keypad(): def __init__(self, columnCount = 3): GPIO.setmode(GPIO.BCM) # Définition des paramètres if columnCount is 3: self.KEYPAD = [ [1,2,3], [4,5,6], [7,8,9], [0,'C','+'], ['B','-','R'] ] # Affectation des broches self.ROW = [26,23,24,25,22] self.COLUMN = [4,17,27] else: return def getKey(self): # Définition des colonnes en sorties for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.OUT) GPIO.output(self.COLUMN[j],GPIO.LOW) # Définition des lignes en entrées for i in range(len(self.ROW)): GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) # Regarde si un bouton est poussé rowVal = -1 for i in range(len(self.ROW)): tmpRead = GPIO.input(self.ROW[i]) if tmpRead == 0: rowVal = i if rowVal < 0 or rowVal > 4: self.exit() return for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(self.ROW[rowVal],GPIO.OUT) GPIO.output(self.ROW[rowVal], GPIO.HIGH) colVal = -1 for j in range(len(self.COLUMN)): tmpRead = GPIO.input(self.COLUMN[j]) if tmpRead == 1: colVal = j if colVal < 0 or colVal > 2: self.exit() return # Retourne la position du bouton pressé self.exit() return self.KEYPAD[rowVal][colVal] def exit(self): # Retourne le nom du bouton pressé for i in range(len(self.ROW)): GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP) #if __name__ == '__main__': # # Initialisation du clavier # kp = keypad() # print ('--- Clavier prêt ---') # # Boucle principale # while True: # digit = None # while digit == None: # digit = kp.getKey() # # Affiche le nom de la touche enfoncée # print (digit) # time.sleep(0.5)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 #!/usr/bin/env python # Read lirc output, in order to sense key presses on an IR remote. # There are various Python packages that claim to do this but # they tend to require elaborate setup and I couldn't get any to work. # This approach requires a lircd.conf but does not require a lircrc. # If irw works, then in theory, this should too. # Based on irw.c, https://github.com/aldebaran/lirc/blob/master/tools/irw.c import socket SOCKPATH = "/var/run/lirc/lircd" sock = None def init_irw(): global sock sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) #print ('starting up on %s' % SOCKPATH) sock.connect(SOCKPATH) def next_key(): '''Get the next key pressed. Return keyname, updown. ''' print('ent ir') # while True: print('lit ir') data = sock.recv(128) # print("Data: " + data) data = data.strip() # if data: # break words = data.split() return words[2], words[1] #if __name__ == '__main__': # init_irw() # # while True: # keyname, updown = next_key() # print('%s (%s)' % (keyname, updown))
Partager