1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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()
|