meta data for this page
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
maemo:shared_code [2011/08/18 17:42] japskua [Creating Buttons with Images] |
maemo:shared_code [2011/08/18 23:19] (current) grp4 |
||
---|---|---|---|
Line 5: | Line 5: | ||
So, add the codes underneath. You can copy the template underneath and just add to the line: | So, add the codes underneath. You can copy the template underneath and just add to the line: | ||
- | ===== Template | + | ===== Tutorial for PyGtk ===== |
+ | http:// | ||
- | * Group: Group name | + | though not every function in pygtk library |
- | * Description: | + | |
- | <code python | + | some examples |
- | # This is just a print hello world! example | + | <code python |
- | print " | + | def main(self): |
+ | # place where fin info about pygtk http:// | ||
+ | win = hildon.StackableWindow() | ||
+ | |||
+ | # Create and pack labels | ||
+ | vbox = gtk.VBox(False, | ||
+ | hbox = gtk.HBox(False, | ||
+ | |||
+ | # | ||
+ | labelHeader = gtk.Label(" | ||
+ | labelSubHeader = gtk.Label(" | ||
+ | |||
+ | # buttonGTK1 = gtk.Button(" | ||
+ | # buttonGTK1.connect(" | ||
+ | |||
+ | buttonGTK = gtk.ToggleButton(" | ||
+ | buttonGTK.connect(" | ||
+ | |||
+ | | ||
+ | # | ||
+ | |||
+ | #b= gtk.ToggleButton(" | ||
+ | # | ||
+ | |||
+ | #c = gtk.RadioButton(None, | ||
+ | # | ||
+ | |||
+ | #adjus = gtk.Adjustment(value=60, | ||
+ | #d = gtk.SpinButton(adjustment= adjus, climb_rate=0.1, | ||
+ | # | ||
+ | #hscrollbar = gtk.HSscrollbar(adjustment) | ||
+ | |||
+ | # | ||
+ | combo = gtk.Combo() | ||
+ | combo.entry.set_text(" | ||
+ | slist = [ " | ||
+ | combo.set_popdown_strings(slist) | ||
+ | hbox.pack_start(combo, | ||
+ | |||
+ | hbox.pack_start(buttonGTK, | ||
+ | |||
+ | vbox.pack_start(labelHeader, | ||
+ | vbox.pack_start(labelSubHeader, | ||
+ | vbox.pack_start(hbox, | ||
+ | |||
+ | |||
+ | |||
+ | # Add label' | ||
+ | win.add(vbox) | ||
+ | |||
+ | win.connect(" | ||
+ | |||
+ | win.show_all() | ||
+ | |||
+ | gtk.main() | ||
+ | </ | ||
+ | |||
+ | ===== Software playtrought ===== | ||
+ | |||
+ | * Group: Esa-Petri and Hilmi | ||
+ | * Description: | ||
+ | |||
+ | |||
+ | <code python MeHear.py> | ||
+ | from optparse import OptionParser | ||
+ | from time import sleep | ||
+ | |||
+ | import sys, os | ||
+ | import pygtk, gtk, gobject | ||
+ | import dbus | ||
+ | import gst | ||
+ | import pygst | ||
+ | import sys | ||
+ | import time | ||
+ | import gtk | ||
+ | import hildon | ||
+ | |||
+ | |||
+ | class Record(): | ||
+ | mic = None | ||
+ | |||
+ | def __init__(self, | ||
+ | """ | ||
+ | @summary: The Constructor class, that searches for the microphone on initialization | ||
+ | """ | ||
+ | bus = dbus.SystemBus() | ||
+ | hal_manager = bus.get_object(" | ||
+ | "/ | ||
+ | hal_manager = dbus.Interface(hal_manager, | ||
+ | |||
+ | | ||
+ | devices = hal_manager.FindDeviceStringMatch(" | ||
+ | |||
+ | identifiers = [] | ||
+ | |||
+ | for dev in devices: | ||
+ | device = bus.get_object(" | ||
+ | |||
+ | card = device.GetAllProperties(dbus_interface=" | ||
+ | if card[" | ||
+ | print "%d. %s" % (card[" | ||
+ | identifiers.append(card[" | ||
+ | |||
+ | self.mic = identifiers[0] | ||
+ | |||
+ | """ | ||
+ | @attention: this is important it initializes softfare playtrought (audioconvert is not nesesity) | ||
+ | @summary: takes stream from mic to autosink | ||
+ | """ | ||
+ | self.pipeline = gst.parse_launch(""" | ||
+ | |||
+ | |||
+ | def rec(self): | ||
+ | """ | ||
+ | @summary: sets recording on | ||
+ | """ | ||
+ | self.pipeline.set_state(gst.STATE_PLAYING) | ||
+ | print "" | ||
+ | print " | ||
+ | |||
+ | |||
+ | def stoprec(self): | ||
+ | """ | ||
+ | @summary: sets recording off | ||
+ | """ | ||
+ | self.pipeline.set_state(gst.STATE_NULL) | ||
+ | print "" | ||
+ | print " | ||
+ | |||
+ | |||
+ | class Prosesor(): | ||
+ | """ | ||
+ | @summary: Takes care of prossesing the sound | ||
+ | """ | ||
+ | |||
+ | |||
+ | def __init__(self): | ||
+ | volume=0 | ||
+ | gain =0 | ||
+ | CutThereshold=0 | ||
+ | |||
+ | |||
+ | def proses(self, | ||
+ | a=1 | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | class player(): | ||
+ | def __init__(self): | ||
+ | """ | ||
+ | @summary: player part | ||
+ | """ | ||
+ | self.player = gst.element_factory_make(" | ||
+ | fakesink = gst.element_factory_make(" | ||
+ | self.player.set_property(" | ||
+ | |||
+ | audiosink = gst.element_factory_make(" | ||
+ | self.recorder=Record(audiosink) | ||
+ | """ | ||
+ | @attention: next links raw data from mic to player mekanism of gstreamer | ||
+ | """ | ||
+ | self.player.set_property(" | ||
+ | self.playmode = True | ||
+ | |||
+ | bus = self.player.get_bus() | ||
+ | bus.add_signal_watch() | ||
+ | bus.connect(" | ||
+ | |||
+ | |||
+ | |||
+ | def on_message(self, | ||
+ | t = message.type | ||
+ | if t == gst.MESSAGE_EOS: | ||
+ | self.player.set_state(gst.STATE_NULL) | ||
+ | self.playmode = False | ||
+ | elif t == gst.MESSAGE_ERROR: | ||
+ | self.player.set_state(gst.STATE_NULL) | ||
+ | err, debug = message.parse_error() | ||
+ | print " | ||
+ | self.playmode = False | ||
+ | |||
+ | def play(self): | ||
+ | self.recorder.rec() | ||
+ | self.player.set_state(gst.STATE_PLAYING) | ||
+ | print " | ||
+ | |||
+ | |||
+ | def stop(self): | ||
+ | # | ||
+ | self.player.set_state(gst.STATE_NULL) | ||
+ | self.recorder.stoprec() | ||
+ | print " | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | class MeHear(hildon.Program): | ||
+ | def __init__(self): | ||
+ | self.pl= player() | ||
+ | |||
+ | |||
+ | def amplify_button_clicked(self, | ||
+ | buttontext = button.get_label() | ||
+ | text = buttontext | ||
+ | if text == " | ||
+ | label.set_text("" | ||
+ | button.set_label(" | ||
+ | self.pl.play() | ||
+ | print "play button pressed" | ||
+ | |||
+ | if text == " | ||
+ | label.set_text(" | ||
+ | button.set_label(" | ||
+ | self.pl.stop() | ||
+ | print "stop button pressed" | ||
+ | |||
+ | |||
+ | |||
+ | def main(self): | ||
+ | win = hildon.StackableWindow() | ||
+ | |||
+ | # Create and pack labels | ||
+ | vbox = gtk.VBox(False, | ||
+ | hbox = gtk.HBox(False, | ||
+ | |||
+ | # | ||
+ | labelHeader = gtk.Label(" | ||
+ | labelSubHeader = gtk.Label(" | ||
+ | |||
+ | # buttonGTK1 = gtk.Button(" | ||
+ | # buttonGTK1.connect(" | ||
+ | |||
+ | buttonGTK = gtk.Button(" | ||
+ | buttonGTK.connect(" | ||
+ | |||
+ | # hbox.pack_start(buttonGTK1, | ||
+ | hbox.pack_start(buttonGTK, | ||
+ | |||
+ | vbox.pack_start(labelHeader, | ||
+ | vbox.pack_start(labelSubHeader, | ||
+ | vbox.pack_start(hbox, | ||
+ | |||
+ | |||
+ | |||
+ | # Add label' | ||
+ | win.add(vbox) | ||
+ | |||
+ | win.connect(" | ||
+ | |||
+ | win.show_all() | ||
+ | |||
+ | gtk.main() | ||
+ | |||
+ | |||
+ | if __name__ == " | ||
+ | |||
+ | app = MeHear() | ||
+ | app.main() | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Sending SMS ===== | ||
+ | |||
+ | * Group: Japskua | ||
+ | * Description: | ||
+ | |||
+ | <code python sms.py> | ||
+ | import dbus | ||
+ | |||
+ | ############################################################################################ | ||
+ | """ | ||
+ | |||
+ | def octify(str): | ||
+ | ''' | ||
+ | Returns a list of octet bytes representing | ||
+ | each char of the input str. | ||
+ | ''' | ||
+ | |||
+ | bytes = map(ord, str) | ||
+ | bitsconsumed = 0 | ||
+ | referencebit = 7 | ||
+ | octets = [] | ||
+ | |||
+ | while len(bytes): | ||
+ | byte = bytes.pop(0) | ||
+ | byte = byte >> bitsconsumed | ||
+ | |||
+ | try: | ||
+ | nextbyte = bytes[0] | ||
+ | bitstocopy = (nextbyte & (0xff >> referencebit)) << referencebit | ||
+ | octet = (byte | bitstocopy) | ||
+ | |||
+ | except: | ||
+ | octet = (byte | 0x00) | ||
+ | |||
+ | if bitsconsumed != 7: | ||
+ | octets.append(byte | bitstocopy) | ||
+ | bitsconsumed += 1 | ||
+ | referencebit -= 1 | ||
+ | else: | ||
+ | bitsconsumed = 0 | ||
+ | referencebit = 7 | ||
+ | |||
+ | return octets | ||
+ | |||
+ | def semi_octify(str): | ||
+ | ''' | ||
+ | Expects a string containing two digits. | ||
+ | Returns an octet - | ||
+ | first nibble in the octect is the first | ||
+ | digit and the second nibble represents | ||
+ | the second digit. | ||
+ | ''' | ||
+ | try: | ||
+ | digit_1 = int(str[0]) | ||
+ | digit_2 = int(str[1]) | ||
+ | octet = (digit_2 << 4) | digit_1 | ||
+ | except: | ||
+ | octet = (1 << 4) | digit_1 | ||
+ | |||
+ | return octet | ||
+ | |||
+ | |||
+ | def deoctify(arr): | ||
+ | |||
+ | referencebit = 1 | ||
+ | doctect = [] | ||
+ | bnext = 0x00 | ||
+ | |||
+ | for i in arr: | ||
+ | |||
+ | bcurr = ((i & (0xff >> referencebit)) << referencebit) >> 1 | ||
+ | bcurr = bcurr | bnext | ||
+ | |||
+ | if referencebit != 7: | ||
+ | doctect.append( bcurr ) | ||
+ | bnext = (i & (0xff << (8 - referencebit)) ) >> 8 - referencebit | ||
+ | referencebit += 1 | ||
+ | else: | ||
+ | doctect.append( bcurr ) | ||
+ | bnext = (i & (0xff << (8 - referencebit)) ) >> 8 - referencebit | ||
+ | doctect.append( bnext ) | ||
+ | bnext = 0x00 | ||
+ | referencebit = 1 | ||
+ | |||
+ | return '' | ||
+ | |||
+ | |||
+ | def createPDUmessage(number, | ||
+ | ''' | ||
+ | Returns a list of bytes to represent a valid PDU message | ||
+ | ''' | ||
+ | numlength = len(number) | ||
+ | if (numlength % 2) == 0: | ||
+ | rangelength = numlength | ||
+ | else: | ||
+ | number = number + ' | ||
+ | rangelength = len(number) | ||
+ | |||
+ | octifiednumber = [ semi_octify(number[i: | ||
+ | octifiedmsg = octify(msg) | ||
+ | HEADER = 1 | ||
+ | FIRSTOCTETOFSMSDELIVERMSG = 10 | ||
+ | ADDR_TYPE = 129 #unknown format | ||
+ | number_length = len(number) | ||
+ | msg_length = len(msg) | ||
+ | pdu_message = [HEADER, FIRSTOCTETOFSMSDELIVERMSG, | ||
+ | pdu_message.extend(octifiednumber) | ||
+ | pdu_message.append(0) | ||
+ | pdu_message.append(0) | ||
+ | pdu_message.append(msg_length) | ||
+ | pdu_message.extend(octifiedmsg) | ||
+ | return pdu_message | ||
+ | |||
+ | |||
+ | ################################################### | ||
+ | """ | ||
+ | |||
+ | |||
+ | |||
+ | class Messager(object): | ||
+ | """ | ||
+ | @summary: This is the Messager class, that handles sending SMS messages | ||
+ | """ | ||
+ | |||
+ | def __init__(self): | ||
+ | """ | ||
+ | @summary: The Constructor, | ||
+ | """ | ||
+ | self.bus = dbus.SystemBus() | ||
+ | self.smsobject = self.bus.get_object(' | ||
+ | self.smsiface = dbus.Interface(self.smsobject, | ||
+ | |||
+ | |||
+ | def Send(self, message, number): | ||
+ | """ | ||
+ | @summary: The Send function, that actually sends the given message | ||
+ | @param message: The message to be sent | ||
+ | @type message: String | ||
+ | @param number: The number to send the message to (can be with country code also) | ||
+ | @type number: String | ||
+ | """ | ||
+ | arr = dbus.Array(createPDUmessage(number.replace(' | ||
+ | msg = dbus.Array([arr]) | ||
+ | self.smsiface.Send(msg, | ||
+ | |||
+ | if __name__ == " | ||
+ | messager = Messager() | ||
+ | messager.Send(" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Making Phone Calls ===== | ||
+ | |||
+ | * Group: Japskua | ||
+ | * Description: | ||
+ | |||
+ | <code python call.py> | ||
+ | ''' | ||
+ | Created on Aug 18, 2011 | ||
+ | |||
+ | @author: Japskua | ||
+ | ''' | ||
+ | |||
+ | import dbus | ||
+ | |||
+ | class Caller(object): | ||
+ | """ | ||
+ | @summary: | ||
+ | """ | ||
+ | |||
+ | def __init__(self): | ||
+ | """ | ||
+ | @summary: The Constructor | ||
+ | """ | ||
+ | # Do nothing | ||
+ | self.bus = dbus.SystemBus() | ||
+ | self.csd_call = dbus.Interface(self.bus.get_object(" | ||
+ | "/ | ||
+ | " | ||
+ | |||
+ | def Call(self, number): | ||
+ | """ | ||
+ | @summary: The function to Call to a given number | ||
+ | @param number: The number to call to | ||
+ | @type number: String | ||
+ | """ | ||
+ | self.csd_call.CreateWith(str(number), | ||
+ | |||
+ | |||
+ | if __name__ == ' | ||
+ | caller = Caller() | ||
+ | caller.Call(" | ||
</ | </ | ||