Stap 4: Python code op PC
Op de PC zal ik python gebruiken om te ontvangen van de opdrachten van de arduino en vertalen naar sleutel-persen.
Op uw PC moet u Installeer python 2.7, de win32-bibliotheek en de pySerial bibliotheek.
Recieve.pyw
Ik ben met behulp van de .pyw-extensie, zodat het draait op de achtergrond.
import serial #The library for communicating with the arduino import serial.tools.list_ports #The library for finding the arduino on the PC import os import thread #The library multiple processes on the same time from time import sleep #The library for delays # Libraries for simulating key-presses and getting th status of functions from win32gui import * from win32api import * from win32con import * #The config file you'll make for your remote from config_ir import * #The name of the arduino you like to connect name = "Arduino Mega 2560" #Get the port the arduino is connected to ports = list(serial.tools.list_ports.comports()) for p in ports: if(p[1][:len(name)] == name): port = p[0] #Connect to the arduino ser = serial.Serial(port, 9600) #function to get the lock key's and send if they change def lock(): caps_old = 0 numl_old = 0 scrl_old = 0 sleep(1) while 1: caps = GetKeyState(VK_CAPITAL) & 0x0001 numl = GetKeyState(VK_NUMLOCK) & 0x0001 scrl = GetKeyState(VK_SCROLL) & 0x0001 if(caps != caps_old): ser.write(chr(1)) caps_old = caps if(numl != numl_old): ser.write(chr(2)) numl_old = numl if(scrl != scrl_old): ser.write(chr(3)) scrl_old = scrl sleep(0.1) #Function for reading the remote data and bind it to a key-press def read_serial(): while 1: data = 0 #get the data data = int(ser.readline()) #Determine which key it was and what it should do if(data == int(Mute)): keybd_event(VK_VOLUME_MUTE, 0,0,0) #Mute elif(data == int(Vol_up)): keybd_event(VK_VOLUME_UP, 0,0,0) #Vol + elif(data == int(Vol_down)): keybd_event(VK_VOLUME_DOWN, 0,0,0) #Vol - elif(data == int(Back)): keybd_event(VK_BROWSER_BACK, 0,0,0) #Back elif(data == int(Play)): keybd_event(VK_MEDIA_PLAY_PAUSE, 0,0,0) #Play/Pause elif(data == int(My_media)): os.system('start C:\Users\Laurens\Videos') #My Media elif(data == int(Up)): keybd_event(VK_UP, 0,0,0) #Up elif(data == int(Right)): keybd_event(VK_RIGHT, 0,0,0) #Right elif(data == int(Down)): keybd_event(VK_DOWN, 0,0,0) #Down elif(data == int(Left)): keybd_event(VK_LEFT, 0,0,0) #Left elif(data == int(OK)): keybd_event(VK_RETURN, 0,0,0) #OK else: pass #Main loop try: #Start multiple threads thread.start_new_thread(lock,()) thread.start_new_thread(read_serial,()) except: print "treading not working" #Do nothing while 1: pass
IR_codes.py
In dit bestand waarvan u de naam wilt wijzigen van de "button" array aan alle de gewenste knoppen. Nadat u dat hebt gedaan hoeft je alleen het uitvoeren van dit script.
! Let op!
Bij het indrukken van een toets die u moet drukken korte, anders itll ' voeren dezelfde opdracht voor de naam van de volgende sleutel.
import serial #Communication library import serial.tools.list_ports inp = [] print "Starting connection" #Device to connect to name = "Arduino Mega 2560" ports = list(serial.tools.list_ports.comports()) for p in ports: if(p[1][:len(name)] == name): port = p[0] ser = serial.Serial(port, 9600) #The buttons you like to map in the software. #Add or remove the key's you want, but THEY MAY NOT CONTAIN SPACES! button = ["Power", "Mute", "Vol_up", "Vol_down", "ch_up", "ch_down", "OK", "Up", "Down", "Left", "Right", "Back", "Play", "Home", "My_media", "Rewind", "Forward", "Next", "Previous", "Stop", "Exit", "Info", "TV_guide"] #Insert every button for i in range(0, len(button)): inp.append(0) print "Press the %(button)s button" % {"button": button[i]} inp[i] = ser.readline().replace('\n', '').replace('\r', '') print "" ser.close() config = open("config_ir.py", 'w') #write the buttons to the config file for i in range(0,len(button)): line = button[i] + "= '" + inp[i] + "'" print line config.write(line) config.write("\n") config.close() #wait for enter raw_input()
Als u vragen heeft voel vrij te vragen.