summaryrefslogtreecommitdiffstats
path: root/abs/core/powermate_myth/powermate-myth.py
blob: 85e516534d11d35ca4c70d562d99133d2fc61fd8 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/python2

import powermate
import time
import os

EVENT_BUTTON_PRESS = 1
EVENT_RELATIVE_MOTION = 2


button_pressed = False
single_tap = False
mode = "nav"

current_event=(0,0,time.time())
last_event=current_event
skipped_event = 0
pressed_time = 0
button_held = False
led_brightness = 255
#pm = powermate.PowerMate("/dev/powermate")

pm = powermate.PowerMate()
pm.SetLEDState(led_brightness,0,0,0,0)


def runcmd(cmd):
#    print cmd
    os.system(cmd)


def volume_action(action):
    global mode
    global led_brightness
    if action == "L" :
        print "Volume down"

    if action == "R" :
        print "Volume UP"

    if action == "tap" :
        print "toggle mute"

    if action == "double_tap" :
        mode = "nav"
        led_brightness = 500
        print "mode is now nav"

    return


def nav_action(action):
    global mode
    global led_brightness
    keycmd = '''xdotool search --name "Mythtv Frontend" key %s'''
    if action == "L" :
        #print "prev"
       cmd =  keycmd %"Up"
       runcmd(cmd)

    if action == "R" :
        #print "next"
        cmd =  keycmd %"Down"
        runcmd(cmd)

    if action == "LP" :
        #print "back"
        cmd =  keycmd %"Left"
        runcmd(cmd)

    if action == "RP" :
        #print "tab"
        cmd =  keycmd %"Right"
        runcmd(cmd)


    if action == "tap" :
        #print "select"
        cmd =  keycmd %"Return"
        runcmd(cmd)

    if action == "double_tap" :
        mode = "volume"
        led_brightness = 10
        print "mode is now vol"

    if action == "button_held" :
        cmd =  keycmd %"Escape"
        runcmd(cmd)



    return




def act_on_event(action):

    if mode == "volume":
        volume_action(action)

    elif mode == "nav":
        nav_action(action)




event_stack=[current_event]
while 1:
        #process_events = True
        reset_lastevent = False
	event = pm.WaitForEvent(2)
       # print event
        if event:
            #event that happens after every button push, can be thrown away.
            if event[2] == 0:
                continue


        elif single_tap:
            pressed_diff = time.time() - pressed_time
            #This takes care of the holding down the button for X amount of time
            #print event,  button_pressed, pressed_diff, current_event
            #if pressed_diff >= 2 and button_pressed and current_event[0] == EVENT_BUTTON_PRESS:
            if pressed_diff >= 2 and button_pressed :
                act_on_event("button_held")
                single_tap = False
                pressed_time = 0
                button_pressed = False
                last_event_time = time.time()
                last_event=(0,0,time.time())
                pm.SetLEDState(led_brightness,0,0,0,0)
                #print "resetting last event:",  last_event


           #This handles a single press of the button.
            elif not button_pressed :
                act_on_event("tap")
                single_tap = False
                last_event_time = time.time()
                last_event=(0,0,time.time())

        if event:
            #print "processing:" , event
            current_event = event[2],event[4],time.time()

            last_event_time = last_event[2]
            current_event_time = current_event[2]

            last_event_only = last_event[0], last_event[1]
            current_event_only = current_event[0], current_event[1]

            repeat_rate = 10
            repeat_time = .50

            if last_event_only == current_event_only and  current_event[0] == EVENT_RELATIVE_MOTION :
                #check for time diff
                event_time_diff = current_event_time - last_event_time

                if  skipped_event <= repeat_rate and event_time_diff <= repeat_time:
                    skipped_event = skipped_event + 1
                    #print "skipped a repeat event : %s" %skipped_event
                    continue
            #event occured, reset skip counter
            skipped_event = 0

            #rotate dial
            if current_event[0] == EVENT_RELATIVE_MOTION:
                reset_lastevent = True
                single_tap = False
                if current_event[1] >= 0 :
                    if button_pressed :
                        #print "dial turned Counter clockwise and button is pressed"
                        act_on_event("RP")
                    else:
                        #print "dial turned Counter clockwise"
                        act_on_event("R")

                if current_event[1]  <=0 :
                    if button_pressed :
                        #print "dial turned clockwise and button is pressed"
                        act_on_event("LP")
                    else:
                        #print "dial turned clockwise"
                        act_on_event("L")


            #button was pushed
            if current_event[0] == EVENT_BUTTON_PRESS :
                pm.SetLEDState(0,0,0,0,0)
                if current_event[1] == 1:
                    button_pressed = True
                    pressed_time = current_event_time
                    tap_diff = current_event_time - last_event_time
                    #double tap
                    if tap_diff < 0.2 and last_event[0] == EVENT_BUTTON_PRESS:
                        act_on_event("double_tap")
                        single_tap = False
                    else:
                        single_tap = True
                        reset_lastevent = True

                #button was released
                elif current_event[1] == 0:
                    button_pressed = False
                    pressed_time  = 0
                    pm.SetLEDState(led_brightness,0,0,0,0)

        if reset_lastevent:
            last_event = current_event