Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAneesh Dogra <lionaneesh@gmail.com>2012-12-30 06:27:36 (GMT)
committer Aneesh Dogra <lionaneesh@gmail.com>2012-12-30 06:27:53 (GMT)
commit753612d00ad1a548bfa723c11e1e73bb0588c443 (patch)
tree7f053a46e13917cb3904e865437cba4276244963
Initial commit
-rw-r--r--accelerometer_emulator.py133
1 files changed, 133 insertions, 0 deletions
diff --git a/accelerometer_emulator.py b/accelerometer_emulator.py
new file mode 100644
index 0000000..9c081f4
--- /dev/null
+++ b/accelerometer_emulator.py
@@ -0,0 +1,133 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2012 Aneesh Dogra <lionaneesh@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+from gi.repository import Gtk, Gdk
+
+def write_to_file(path, data):
+ fp = open(path, 'w')
+ fp.write(str(data))
+ fp.close()
+
+PATH = 'FILE' # add you file here
+# Also replace the ACCELEROMETER_DEVICE_PATH in your activity code
+# to this file for testing.
+
+class MyWindow(Gtk.Window):
+ def __init__(self):
+ Gtk.Window.__init__(self, title="Acceleromer Emulator")
+
+ self.x = 0
+ self.y = 0
+ self.z = -64
+
+ self.button_box = Gtk.VBox()
+ self.button_box_top = Gtk.HBox()
+ self.button_box_middle = Gtk.HBox()
+ self.button_box_bottom = Gtk.HBox()
+
+ # Up button
+ self.button_top = Gtk.Button(label="Up")
+ self.button_top.connect("clicked", self.up_button_clicked)
+ self.button_top.show()
+
+ # Left button
+ self.button_left = Gtk.Button(label="Left")
+ self.button_left.connect('clicked', self.left_button_clicked)
+ self.button_left.show()
+
+ # Reset button
+ self.button_reset = Gtk.Button(label="Reset")
+ self.button_reset.connect('clicked', self.reset_button_clicked)
+ self.button_reset.show()
+
+ # Right button
+ self.button_right = Gtk.Button(label="Right")
+ self.button_right.connect('clicked', self.right_button_clicked)
+ self.button_right.show()
+
+ # Down button
+ self.button_down = Gtk.Button(label="Down")
+ self.button_down.connect('clicked', self.down_button_clicked)
+ self.button_down.show()
+
+ self.button_box_top.pack_start(self.button_top, True, True, 0)
+ self.button_box_middle.pack_start(self.button_left, True, True, 0)
+ self.button_box_middle.pack_start(self.button_reset, True, True, 0)
+ self.button_box_middle.pack_start(self.button_right, True, True, 0)
+ self.button_box_bottom.pack_start(self.button_down, True, True, 0)
+
+ self.button_box.pack_start(self.button_box_top, True, True, 0)
+ self.button_box.pack_start(self.button_box_middle, True, True, 0)
+ self.button_box.pack_start(self.button_box_bottom, True, True, 0)
+ self.add(self.button_box)
+
+ self.connect('key-press-event', self.catch_keypress)
+
+ def catch_keypress(self, window, event):
+ Up = Gdk.keyval_from_name('Up')
+ Down = Gdk.keyval_from_name('Down')
+ Left = Gdk.keyval_from_name('Left')
+ Right = Gdk.keyval_from_name('Right')
+ Reset = Gdk.keyval_from_name('Escape')
+
+ keyval = event.keyval
+ if keyval == Up:
+ self.up_button_clicked(self.button_top)
+ if keyval == Down:
+ self.down_button_clicked(self.button_down)
+ if keyval == Left:
+ self.left_button_clicked(self.button_left)
+ if keyval == Right:
+ self.right_button_clicked(self.button_right)
+ if keyval == Reset:
+ self.reset_button_clicked(self.button_reset)
+
+ def up_button_clicked(self, widget):
+ print "Up"
+ self.y -= 18
+ if self.y < -64 * 18:
+ self.y = -64 * 18
+ write_to_file(PATH, (self.x, self.y, self.z))
+ def down_button_clicked(self, widget):
+ print "Down"
+ self.y += 18
+ if self.y > 64 * 18:
+ self.y = 64 * 18
+ write_to_file(PATH, (self.x, self.y, self.z))
+ def reset_button_clicked(self, widget):
+ print "Reset"
+ self.x = 0
+ self.y = 0
+ write_to_file(PATH, (self.x, self.y, self.z))
+ def left_button_clicked(self, widget):
+ print "Left"
+ self.x -= 18
+ if self.x < -64 * 18:
+ self.x = -64 * 18
+ write_to_file(PATH, (self.x, self.y, self.z))
+ def right_button_clicked(self, widget):
+ print "Right"
+ self.x += 18
+ if self.x > 64 * 18:
+ self.x = 64 * 18
+ write_to_file(PATH, (self.x, self.y, self.z))
+
+win = MyWindow()
+win.connect("delete-event", Gtk.main_quit)
+win.show_all()
+Gtk.main()