nomad13
Full Member level 3
timer python appuifw
I am a biginner in phyton and my interest is in using nokia to control things using bluetooth.
Upon surfing the net i found a code useful. This code let's you send text from your cell to a computer with bluetooth dongle. The text is recieved by the computer via serial port (i'm running a hyperterminal on the PC).
What i have in mind is to send keypad entry from the cell to the PC directly without typing messages. So while a key is pressed, for example "1" the pc continously recieve "1" on the hyperterminal. After releasing the key the cellphone will also stop sending "1".
Here are the two codes that i want to combine:
Here is the bluetooth code
Here is the keypad routine
I hope some of you guys can help me.
I am a biginner in phyton and my interest is in using nokia to control things using bluetooth.
Upon surfing the net i found a code useful. This code let's you send text from your cell to a computer with bluetooth dongle. The text is recieved by the computer via serial port (i'm running a hyperterminal on the PC).
What i have in mind is to send keypad entry from the cell to the PC directly without typing messages. So while a key is pressed, for example "1" the pc continously recieve "1" on the hyperterminal. After releasing the key the cellphone will also stop sending "1".
Here are the two codes that i want to combine:
Here is the bluetooth code
Code:
# Copyright (c) 2005 Jurgen Scheible
# script that connects to the serial port of the PC
# and lets you send characters to the PC
import appuifw
# import the module socket
import socket
import e32
# function that handles the bluetooth connection:
def bt_connect():
global sock
# create a bluetooth socket
sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
target=''# here you can give the bt address of the other mobile if you know it
if not target:
# scan for bluetooth devices
address,services=socket.bt_discover()
print "Discovered: %s, %s"%(address,services)
if len(services)>1:
choices=services.keys()
choices.sort()
# bring up a popup menu and show the available bt devices for selection
choice=appuifw.popup_menu([unicode(services[x])+": "+x
for x in choices],u'Choose port:')
target=(address,services[choices[choice]])
else:
target=(address,services.values()[0])
print "Connecting to "+str(target)
# connect to the serial port of the PC
sock.connect(target)
print "OK."
# call the text input field function
bt_typetext()
# define the textinput function
def bt_typetext():
global sock
# create the text input field
test = appuifw.query(u"Type words", "text", u"")
# if cancel has been pressed, then quit the application otherwise send the character over bluetooth
if test == None:
exit_key_handler()
else:
# send the typed in characters over bluetooth to the PC
sock.send(test)
# call again the textinput field function to show the text input again
bt_typetext()
def exit_key_handler():
script_lock.signal()
appuifw.app.set_exit()
appuifw.app.title = u"bt mob to PC"
script_lock = e32.Ao_lock()
appuifw.app.exit_key_handler = exit_key_handler()
# call the function that handles the bluetooth connection
bt_connect()
script_lock.wait()
Here is the keypad routine
Code:
# Copyright (c) 2005 Jurgen Scheible
# use keyboard keys Arrow up,down,lefet,right, 1 and * to trigger pop-up note
import appuifw
import e32
from key_codes import *
# you can use this class as a chunk as it is.
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(code):
self._downs[code]=self._downs.get(code,0)+1
self._keyboard_state[code]=1
elif event['type'] == appuifw.EEventKeyUp:
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
# set and instance of Keyboard (so you can use all the functions of
# that class later in the script by typing e.g. keyboard.pressed...)
keyboard=Keyboard()
# define the function that lets the application quit
def quit():
global running
running=0
appuifw.app.set_exit()
running=1
appuifw.app.screen='normal'
# use the appuifw.Canvas function and as "event_callback" put "
# keyboard.handle_event", a function which does the keyboard scan
canvas=appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback=None)
# set the application body to canvas
appuifw.app.body=canvas
appuifw.app.exit_key_handler=quit
# create a loop which the script runs all the time through to check whether a
# key has been pressed.
while running:
# check if the left arrow key has been pressed
if keyboard.pressed(EScancodeLeftArrow):
appuifw.note(u"Arrow left", "info")
# check if the right arrow key has been pressed
if keyboard.pressed(EScancodeRightArrow):
appuifw.note(u"Arrow right", "info")
if keyboard.pressed(EScancodeDownArrow):
appuifw.note(u"Arrow down", "info")
if keyboard.pressed(EScancodeUpArrow):
appuifw.note(u"Arrow up", "info")
if keyboard.pressed(EScancodeSelect):
appuifw.note(u"Select", "info")
if keyboard.pressed(EScancode1):
appuifw.note(u"1", "info")
if keyboard.pressed(EScancodeStar):
appuifw.note(u"*", "info")
e32.ao_yield()
I hope some of you guys can help me.