-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_ctrl_led.py
67 lines (49 loc) · 1.51 KB
/
_ctrl_led.py
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
import sys
import time
import socket
import board
import neopixel
import RPi.GPIO as GPIO
from concurrent.futures import ThreadPoolExecutor
class CtrlLed:
_instance = None
@classmethod
def _getInstance(cls):
return cls._instance
@classmethod
def instance(cls, *args, **kargs):
cls._instance = cls(*args, **kargs)
cls._instance = cls._getInstance
return cls._instance
def __init__(self):
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, True)
self.pixels = neopixel.NeoPixel(board.D12, 1)
self._jupyter_flag = False
self._unstable_flag = False
self.flag_pool = ThreadPoolExecutor(max_workers=1)
def run(self):
try:
while 1:
self.flag_pool.submit(self.__monitoring_kernel)
if self._jupyter_flag:
self.pixels[0] = (0, 255, 0)
else:
self.pixels[0] = (255, 0, 0)
except KeyboardInterrupt:
self.exit()
sys.exit(0)
def __monitoring_kernel(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
res = sock.connect_ex(('127.0.0.1', 8888))
if res == 0:
self._jupyter_flag = True
else:
self._jupyter_flag = False
def exit(self):
self._jupyter_flag = False
self.pixels[0] = (0, 0, 0)
if __name__ == "__main__":
led = CtrlLed()
led.run()