You Are Here:

Community: Developer Discussion Boards

#1 Old Question Cant get threads to work. - 2006-11-20, 10:39

Join Date: Feb 2006
Posts: 168
Location: Helsinki, Finland
RICH?
Offline
Regular Contributor
Im trying to create an app without a main loop and with a thread that checks for keypreses.
I've tried something like the code below but cant get it to work.


Code:
import appuifw
import thread
import e32
from key_codes import *

app_lock=e32.Ao_lock()
running=1

#Some other stuff... (app body etc.)

def key_check():
    while(running):
        #check for key presses

thread.start_new_thread(key_check, ())


app_lock.wait() #Is the problem something to do with this?
Any help with getting it to work is apreciated.
Reply With Quote

#2 Old Re: Cant get threads to work. - 2006-11-20, 11:05

Join Date: Sep 2006
Posts: 51
big_pig's Avatar
big_pig
Offline
Regular Contributor
Hi,
I just started to write pys60 code (as a hobby) and I am not an expert.
I can still say something about threads.

I did a game and wanted a music to play in background.

Seems easy?
First I must start a (sub)thread not using appuifw at all to play,
(just experimenting to know this)!
Then, when this subthread is running (playing), if I exit
the main UI- thread the system crashes on the phone ( sis-file), boots phone!
I must take care the music (non main)thread to stop first,
not easy because feature pack 2 has not much tools for music.

Some Python codes work differently running
on Python interpreter on the phone or as sis- file,
not making testing more easy!

Threads are tricky, good luck!
Reply With Quote

#3 Old Re: Cant get threads to work. - 2006-11-20, 11:23

Join Date: May 2006
Posts: 622
Location: Oulu, Finland
lfd
Offline
Super Contributor
Hei!

You only miss some little details. Check this out

Code:
import appuifw
import thread
import e32
from key_codes import *

def __exit__():
    global running
    if running:     # you have to terminate you thread yourself before exiting
        running = 0
    app_lock.signal()
    print "Goodbye crual thread!"

def key_check():
    global running
    print "Hello thread"
    while(running):
        #check for key presses
        pass

if __name__ == "__main__":
    app_lock=e32.Ao_lock()
    appuifw.app.exit_key_handler = __exit__
    appuifw.app.title = u'Hello thread'
    running=1
    thread.start_new_thread(key_check, ())
    app_lock.wait() #Noup, only wait that you signal the lock here in __exit__() function
LFD


Devices:
Nokia E61 3rd Edition - pys60 1.4.0

Tips and modules:
http://www.lfdm.net/thesis
Reply With Quote

#4 Old Thumbs up Re: Cant get threads to work. - 2006-11-20, 13:30

Join Date: Feb 2006
Posts: 168
Location: Helsinki, Finland
RICH?
Offline
Regular Contributor
Thanks so much to both of you! It works like a charm now.
Ill be posting the app here as soon as i get it ready...

EDIT:
Works only partly.
Code:
if keyboard.pressed(EKey1):
    print 'hello' #This works
Code:
if keyboard.pressed(EKey1):
    send('hello') #This doesn't (my send function.) It just freezez python.
    #The function itself works but when called from here it doesnt.
Whats going on?
Last edited by RICH? : 2006-11-20 at 14:41.
Reply With Quote

#5 Old Re: Cant get threads to work. - 2006-11-21, 17:15

Join Date: Dec 2004
Posts: 646
jplauril's Avatar
jplauril
Offline
Forum Nokia Expert
rich, what is your send function?
Reply With Quote

#6 Old Re: Cant get threads to work. - 2006-11-21, 20:03

Join Date: Feb 2006
Posts: 168
Location: Helsinki, Finland
RICH?
Offline
Regular Contributor
It just sends data with bluetooth (RFCOMM).

Code:
def send(txt):
    bt.sock.send(u""+txt)
I dont need a thread after all. But now I have another problem .
I need to detect the release of a key (opposite of "pressed") with Jurgens example. I need to modify the keyboard class but I have no idea how I should do it.
Reply With Quote

#7 Old Re: Cant get threads to work. - 2006-11-22, 10:27

Join Date: May 2006
Posts: 622
Location: Oulu, Finland
lfd
Offline
Super Contributor
Hi Rich?

I put here the source (I renamed code to codee or it breaks the code tag)

Code:
class Keyboard(object):
    def __init__(self,onevent=lambda:None):
        self._keyboard_state={}
        self._downs={}
        self._onevent=onevent

    def handle_event(self,event):
        if event['type'] == appuifw.EEventKeyDown:
            code=event['scancode']
            if not self.is_down(codee):
                self._downs[codee]=self._downs.get(codee,0)+1
            self._keyboard_state[codee]=1
        elif event['type'] == appuifw.EEventKeyUp:        <--- here is the "release" key
            self._keyboard_state[event['scancode']]=0
        self._onevent()

    def is_down(self,scancode):
        return self._keyboard_state.get(scancode,0)

    def pressed(self,scancode):
        if self._downs.get(scancode,0):
            self._downs[scancode]-=1
            return True
        return False
So basically if you only want to detect the release button of a special key, you can simplify all of this like, for example:

Code:
import appuifw
from key_codes import EScancodeUpArrow, EScancodeDownArrow

def event_callback(self, event):
    if event['scancode'] == EScancodeUpArrow and event['type'] == appuifw.EEventKeyUp:
        #do something
    elif event['scancode'] == EScancodeDownArrow and event['type'] == appuifw.EEventKeyUp:
        # do something else
By giving this function as a callback it will only detect release of a special key and drop all the rest. In this example it would detect it for the up and down key of the joystick. That should work

LFD


Devices:
Nokia E61 3rd Edition - pys60 1.4.0

Tips and modules:
http://www.lfdm.net/thesis
Reply With Quote

#8 Old Re: Cant get threads to work. - 2006-11-22, 16:43

Join Date: Feb 2006
Posts: 168
Location: Helsinki, Finland
RICH?
Offline
Regular Contributor
Hello!

I think got it working!
This should work.

Code:
#Needed to replace code with codee again...
class Keyboard(object):
    def __init__(self,onevent=lambda:None):
        self._keyboard_state={}
        self._downs={}
        self._ups={}
        self._onevent=onevent
    def handle_event(self,event):
        code=event['scancode']
        if event['type'] == appuifw.EEventKeyDown:
            if not self.is_down(codee):
                self._downs[codee]=self._downs.get(code,0)+1
            self._keyboard_state[codee]=1
        elif event['type'] == appuifw.EEventKeyUp:
            self._ups[codee]=self._ups.get(code,0)+1
            self._keyboard_state[event['scancode']]=0
        self._onevent()
    def is_down(self,scancode):
        return self._keyboard_state.get(scancode,0)
    def pressed(self,scancode):
        if self._downs.get(scancode,0):
            self._downs[scancode]-=1
            return True
        return False
    def released(self,scancode):
        if self._ups.get(scancode,0):
            self._ups[scancode]-=1
            return True
        return False
So now I can detect the release of a key too!

EDIT: The class is now actually a bit buggy.
Last edited by RICH? : 2006-11-22 at 17:34.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare() works in emulator but doesn't work in phone yencruz General Symbian C++ 2 2004-04-27 11:36
CommConnection does not work with Series 60 SDK, but works in JWT jackiechan2001 Mobile Java Tools & SDKs 1 2004-02-04 10:07
Does SDK v2.0 work on Series60 V6.1? liying Symbian Tools & SDKs 3 2004-02-02 09:57
does irda function work with the emulator ? IGhost General Symbian C++ 0 2002-12-07 18:43
8310 Simulator doesn't work DickJones General Browsing 1 2002-11-11 13:55

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia