File: voctopanel.py

package info (click to toggle)
voctomix 1.3-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,368 kB
  • sloc: python: 4,958; sh: 733; makefile: 23
file content (82 lines) | stat: -rwxr-xr-x 2,290 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
import atexit
import socket
import sys
import time
import serial

from lib.config import Config

server_address = Config.get("server", "address")
server_port = Config.get("server", "port")

panel_port = Config.get("panel", "port")
panel_speed = Config.get("panel", "speed")
panel_size = Config.get("panel", "size")

conn, ser = None, None

try:
    conn = socket.create_connection((server_address, server_port))
except (ConnectionRefusedError, KeyboardInterrupt):
    print("Could not connect to voctocore")
    sys.exit()

@atexit.register
def close_conn():
    global conn
    conn and conn.close()

try:
    ser = serial.Serial(panel_port, panel_speed, timeout=1)
except (ValueError, serial.SerialException):
    print("Could not connect to voctopanel")
    sys.exit()

@atexit.register
def close_pannel():
    global ser
    ser.close()

print("Entering main loop. Press Control-C to exit.")


try:
    # just wait for keyboard interrupt in main thread
    while True:
        if True: #ser.in_waiting > 0:
            try:
                btn = ser.readline().decode("utf-8").strip()
            except serial.serialutil.SerialException:
                print("probably missed one button press")

            #check if this is a button press and if it is remove the leading v
            if str(btn)[:1] == 'v':
                btn = btn[1:]
                try:
                    cm = Config.get("buttons",btn)
                except:
                    print("broken or not defined button received")
                print(cm) #debug

                print("Sending: '{}'".format(cm))
                try:
                    conn.sendall(cm.encode('ascii') + b"\n")
                except BrokenPipeError:
                    print("voctocore disconnected, trying to reconnect")
                    try:
                        conn = socket.create_connection((server_address, server_port))
                        print("Reconnected to voctocore")
                    except:
                        pass
                led = btn + 'o'
                ser.write(led.encode())

            else:
                pass
                #print(btn[:1])
        else:
            print('no input')
        #time.sleep(1)
except KeyboardInterrupt:
    print("")