What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Python help

Pages: (2): « First [ 1 ] 2 » Last »
Python help
Author: Message:
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
O.P. Python help
I'm coding a program that sends some data via serial when a key is pressed:

Python code:
from Tkinter import *
from serial import Serial

Arduino = Serial(3)
Arduino.baudrate = 115200

root = Tk()

def Acelerate (event):
    Arduino.write ("Ax")
def Reverse (event):
    Arduino.write ("Rx")
def Right (event):
    Arduino.write ("xD")
def Left (event):
    Arduino.write ("xE")

root.bind ('<Up>', Acelerate)
root.bind ('<Down>', Reverse)
root.bind ('<Right>', Right)
root.bind ('<Left>', Left)
root.mainloop()

It's working pretty fine. You press Up in you keyboard, it sends data which makes an RC car move forward. You press Down, it moves backward. When you press Right or Left, it turns its wheels to that side.

Now I need to do this: when I press, for example, Up and Right at the same time, I need to send "AD" (stupid math, "Ax + xD"), and analog for other combinations.
How can I do this multi-key binding?

I saw some examples with modifier keys, like "root.bind ('<Control-A>', asd)", but this combination <'Key1-Key2'> seems to work only with these modifiers (Control, Alt, Shift, etc).

So, do you think it's possible?

This post was edited on 09-26-2009 at 10:43 PM by Chancer.
09-14-2009 01:47 AM
Profile E-Mail PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
RE: Python help
Why are you trying to do this with python?

I see that your are working with arduino, try processing.

It's the language that the arduino language is based upon and it has a similar editor and has some arduino libraries to make it easier for you to create applications that can work together with your arduino.
[Image: 1-0.png]
             
09-14-2009 05:23 PM
Profile PM Web Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: Python help
It's a college projetc. We must use Python, however they don't teach us it...
09-14-2009 08:55 PM
Profile E-Mail PM Find Quote Report
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
Joined: Sep 2006
RE: Python help
I <3 python I can help you with this and I know some ppl who might be more help than me, do you have to use Tkinter?
something like pyglet might be more viable

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














09-15-2009 08:11 AM
Profile E-Mail PM Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: Python help
No, Tkinter is not mandatory. It's the only solution I found so far.
What do you suggest?
09-15-2009 08:45 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Python help
quote:
Originally posted by Chancer
What do you suggest?
quote:
Originally posted by Jarrod
something like pyglet might be more viable
I wonder what he suggests... lol sorry I had to!
09-16-2009 12:36 PM
Profile E-Mail PM Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: Python help
I know, I know... I mean, how can I use it?
I read it's documentation, but I can't make it detect multiple keys at once. All I could do was as functional as my code in first post.

I was expecting a piece of code, an example...
09-16-2009 05:55 PM
Profile E-Mail PM Find Quote Report
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
Joined: Sep 2006
RE: Python help, Solution :)
python code:
import pyglet as pyg
import random
from pyglet.window import key
window=pyg.window.Window()

#map- when arrow keys are pressed in pyglet, the symbol that represents them is a number
#he numbers are:
#65361 = left = 0
#65362 = up = 1
#65363 = right = 2
#65364 = down = 3
#we now have 4 numbers 1-4, each representing an arrow key
#
keys=[False]*4
#so now we have a list of 4 False values, which will represent each arrow,
#python indexes an array/list starting from 0

#above we mention 4 numbers, 65361,65362,65363,65364
#instead of mapping each to a dictionary if we get the last digit and subtract 1 we end up with a set like
#0,1,2,3
#so now if we let this number index keys like this
#keys[((int(str(symbol)[-1]))-1)]
#it is the same as saying:
#
#def on_key_press(symbol,modifiers,keys=keys):
#    if symbol == 65361:
#        keys[0]=True    #keys[0] is left
#    if symbol == 65362:
#        keys[1] == True
# #ect,ect

#keys are mapped ((int(str(symbol)[-1]))-1)

@window.event
def on_key_press(symbol,modifiers,keys=keys):
    #print symbol,((int(str(symbol)[-1]))-1)
    keys[((int(str(symbol)[-1]))-1)] = True
@window.event

def on_key_release(symbol,modifiers,keys=keys):
    keys[((int(str(symbol)[-1]))-1)] = False

def update(dt,keys=keys):
    #by using this statement we determine more complex events first (left + up) so it fulfils these instead fulfilling a less complex (left)
    if keys[0] and keys[1]:
        print 'left'+'up'
    elif keys[1] and keys[2]:
        print 'right'+'up'
    elif keys[0] and keys[3]:
        print 'down'+'left'
    elif keys[2] and keys[3]:
        print "down"+'right'
    elif keys[0]:
        print "left"    #due to this, if two conflicting keys are help the upper most one will occur
    elif keys[1]:
        print "up"        #up occurs if 'down' and 'up' are down, left occurs if 'left' and 'right' are down
    elif keys[2]:
        print 'right'
    elif keys[3]:
        print "down"
   

pyg.clock.schedule_interval(update, 0.1)
pyg.app.run()

try that,
sorry for the delay been a touch busy

This post was edited on 09-20-2009 at 09:30 PM by Jarrod.

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














09-20-2009 01:33 PM
Profile E-Mail PM Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: Python help
Great, man. It works just fine! Thanks!
I'll take a few time trying to decipher and understanding the code (I'm learning by myself... it's been kinda hard), and fine tuning the update interval.

One more question: using pyglet to do this will force me to create the whole interface with pyglet?
09-20-2009 03:26 PM
Profile E-Mail PM Find Quote Report
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
Joined: Sep 2006
RE: Python help
I added a few comments so you can understand it better (edited into the code above),
I'm going to yes, using pyglet will force you to do the whole interface in pyglet, technically you could create a Tkinter window as well but it would be cumbersome, eg the pyglet window would need to be focused to take input.
writing the interface in pyglet would not be a particularly bad idea and should be quite workable

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














09-20-2009 09:40 PM
Profile E-Mail PM Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On