Browse Source

Moved python service file.

master
John Shaver 1 year ago
parent
commit
d1ffb76141
2 changed files with 12 additions and 85 deletions
  1. 12
    3
      main.py
  2. 0
    82
      service/service.py

+ 12
- 3
main.py View File

@@ -2,15 +2,21 @@
2 2
 # -*- coding: utf-8 -*-
3 3
 import RPi.GPIO as GPIO
4 4
 import time
5
+import socket
6
+
7
+sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8
+
9
+CONNECT_TO = ("10.0.0.4", 2000)
5 10
 
6 11
 
7 12
 def buttonEventHandler(value):
8
-    print "buton event!"
9 13
     if value is 0:
10 14
         GPIO.output(24, True)
15
+        sock.sendall("button_press")
11 16
         print('Button Pressed...')
12 17
     else:
13 18
         GPIO.output(24, False)
19
+        sock.sendall("button_release")
14 20
         print('Button Released...')
15 21
 
16 22
 def main():
@@ -23,12 +29,15 @@ def main():
23 29
     #GPIO.add_event_detect(23, GPIO.FALLING)
24 30
     #GPIO.add_event_callback(23, buttonEventHandler)
25 31
     try:
32
+        sock.connect(CONNECT_TO)
26 33
         while True:
27
-            GPIO.wait_for_edge(23, GPIO.BOTH)
34
+            GPIO.wait_for_edge(23, GPIO.BOTH, bouncetime=100)
28 35
             buttonEventHandler(GPIO.input(23))
29
-    except KeyboardInterrupt as e:
36
+    except:
30 37
         GPIO.cleanup()
38
+        sock.close()
31 39
         raise
40
+        
32 41
 
33 42
 if __name__== "__main__":
34 43
   main()    

+ 0
- 82
service/service.py View File

@@ -1,82 +0,0 @@
1
-#! /usr/bin/env python
2
-# -*- coding: utf-8 -*-
3
-
4
-from pulsectl import Pulse
5
-import SocketServer
6
-from threading import Thread
7
-
8
-LISTEN_ON = ("10.0.0.4", 2000)
9
-
10
-def main():
11
-    pulse = Pulse('mic-control')
12
-
13
-    sources = pulse.source_list()
14
-
15
-    sourceNumber = getSourceSelection(sources)
16
-    
17
-    server = SocketServer.TCPServer(LISTEN_ON, RequestHandler)
18
-
19
-    try:
20
-        server.serve_forever()
21
-    except KeyboardInterrupt:
22
-        server.shutdown()
23
-        server.server_close()
24
-
25
-
26
-def getSourceSelection(sources):
27
-    sourceCount = len(sources) - 1
28
-    error = False
29
-    while True:
30
-        message = error if error else "Please select a source: "
31
-        print(message)
32
-        for i, source in enumerate(sources):
33
-            print "%i: %s" % (i, source.description)  
34
-        selection = raw_input("Enter a number[0-%i]:" % sourceCount)
35
-        print("")
36
-        try :
37
-            return getInt(selection, sourceCount)
38
-        except InputError as e:
39
-            error = e.message
40
-
41
-def getInt(value, sourceCount):
42
-
43
-    number = 0
44
-    try:
45
-        number = int(value)
46
-    except ValueError as e:
47
-        raise InputError("Enter only a number...")
48
-
49
-    if number not in range(0, sourceCount):
50
-        raise InputError("Not a valid option...")
51
-
52
-    return number
53
-
54
-class RequestHandler(SocketServer.StreamRequestHandler):
55
-    def handle(self):
56
-
57
-        for line in self.rfile:
58
-            message = line.strip()
59
-            if message in messageTypes:
60
-                messageTypes[message]()
61
-            self.wfile.write(message)
62
-        print("Closed!")
63
-        self.request.close()
64
-
65
-
66
-def buttonPressed():
67
-    print("down!")
68
-
69
-def buttonReleased():
70
-    print("up!")
71
-
72
-messageTypes = {
73
-    'button_press': buttonPressed,
74
-    'button_release': buttonReleased,
75
-}
76
-
77
-class InputError(Exception):
78
-    def __init__(self, message):
79
-        self.message = message
80
-
81
-    
82
-main()

Loading…
Cancel
Save