smartpi 0.1.22__py3-none-any.whl → 0.1.23__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
smartpi/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  __all__ = ["base_driver"]
2
2
 
3
- __version__ = "0.1.22"
3
+ __version__ = "0.1.23"
4
4
 
smartpi/base_driver.py CHANGED
@@ -641,7 +641,6 @@ def smartpi_init():
641
641
  motor.reset_motor_encoder(5)
642
642
  motor.reset_motor_encoder(6)
643
643
  time.sleep(0.1)
644
- gui.init()
645
644
  serial_lock.release()
646
645
  time.sleep(0.1)
647
646
 
smartpi/gui.py CHANGED
@@ -1,61 +1,87 @@
1
1
  import socket
2
2
  import json
3
- import sys
3
+ import time
4
4
 
5
- # 连接对象(模块级单例)
6
- _connection = None
5
+ class _gui_client:
6
+ def __init__(self, host="127.0.0.1", port=65167):
7
+ self.sock = socket.create_connection((host, port))
8
+ self.clear()
7
9
 
8
- def init(host="127.0.0.1", port=65167):
9
- """初始化GUI连接"""
10
- global _connection
11
- if _connection is None:
12
- _connection = socket.create_connection((host, port))
13
- _send({"type": "clear"})
10
+ def _send(self, cmd):
11
+ self.sock.sendall((json.dumps(cmd) + "\n").encode())
12
+ time.sleep(0.1)
14
13
 
15
- def _send(cmd):
16
- """发送命令到服务器"""
17
- if _connection is None and "pytest" not in sys.modules: # 允许测试环境不初始化
18
- raise ConnectionError("GUI not initialized. Call gui.init() first.")
19
- if _connection:
20
- _connection.sendall((json.dumps(cmd) + "\n").encode())
14
+ def show_text(self, x, y, text, color="black", size=16):
15
+ self._send({"type": "text", "x": x, "y": y, "text": text, "color": color, "size": size})
21
16
 
17
+ def print(self, text):
18
+ self._send({"type": "print", "text": text})
19
+
20
+ def println(self, text):
21
+ self._send({"type": "println", "text": text})
22
+
23
+ def show_image(self, x, y, path, width, height):
24
+ self._send({"type": "image", "x": x, "y": y, "path": path, "width": width, "height": height})
25
+
26
+ def draw_line(self, x1, y1, x2, y2, color="black", width=1):
27
+ self._send({"type": "line", "x1": x1, "y1": y1, "x2": x2, "y2": y2, "color": color, "width": width})
28
+
29
+ def fill_rect(self, x, y, w, h, color="black"):
30
+ self._send({"type": "fill_rect", "x": x, "y": y, "w": w, "h": h, "color": color})
31
+
32
+ def draw_rect(self, x, y, w, h, width, color="black"):
33
+ self._send({"type": "draw_rect", "x": x, "y": y, "w": w, "h": h, "width": width, "color": color})
34
+
35
+ def fill_circle(self, cx, cy, r, color="black"):
36
+ self._send({"type": "fill_circle", "cx": cx, "cy": cy, "r": r, "color": color})
37
+
38
+ def draw_circle(self, cx, cy, r, width, color="black"):
39
+ self._send({"type": "draw_circle", "cx": cx, "cy": cy, "r": r, "width": width, "color": color})
40
+
41
+ def clear(self):
42
+ self._send({"type": "clear"})
43
+
44
+ def finish(self):
45
+ self.sock.close()
46
+
47
+ # 创建全局实例
48
+ _client_instance = _gui_client()
49
+
50
+ # 将类方法提升为模块级别的函数
22
51
  def show_text(x, y, text, color="black", size=16):
23
- _send({"type": "text", "x": x, "y": y, "text": text, "color": color, "size": size})
52
+ _client_instance.show_text(x, y, text, color, size)
24
53
 
25
- def print(text): # 使用print作为函数名,因为调用时使用gui.print()
26
- _send({"type": "print", "text": text})
54
+ def print(text):
55
+ _client_instance.print(text)
27
56
 
28
57
  def println(text):
29
- _send({"type": "println", "text": text})
58
+ _client_instance.println(text)
30
59
 
31
60
  def show_image(x, y, path, width, height):
32
- _send({"type": "image", "x": x, "y": y, "path": path, "width": width, "height": height})
61
+ _client_instance.show_image(x, y, path, width, height)
33
62
 
34
63
  def draw_line(x1, y1, x2, y2, color="black", width=1):
35
- _send({"type": "line", "x1": x1, "y1": y1, "x2": x2, "y2": y2, "color": color, "width": width})
64
+ _client_instance.draw_line(x1, y1, x2, y2, color, width)
36
65
 
37
66
  def fill_rect(x, y, w, h, color="black"):
38
- _send({"type": "fill_rect", "x": x, "y": y, "w": w, "h": h, "color": color})
67
+ _client_instance.fill_rect(x, y, w, h, color)
39
68
 
40
69
  def draw_rect(x, y, w, h, width, color="black"):
41
- _send({"type": "draw_rect", "x": x, "y": y, "w": w, "h": h, "width": width, "color": color})
70
+ _client_instance.draw_rect(x, y, w, h, width, color)
42
71
 
43
72
  def fill_circle(cx, cy, r, color="black"):
44
- _send({"type": "fill_circle", "cx": cx, "cy": cy, "r": r, "color": color})
73
+ _client_instance.fill_circle(cx, cy, r, color)
45
74
 
46
75
  def draw_circle(cx, cy, r, width, color="black"):
47
- _send({"type": "draw_circle", "cx": cx, "cy": cy, "r": r, "width": width, "color": color})
76
+ _client_instance.draw_circle(cx, cy, r, width, color)
48
77
 
49
78
  def clear():
50
- _send({"type": "clear"})
51
-
52
- def close():
53
- """关闭GUI连接"""
54
- global _connection
55
- if _connection:
56
- _connection.close()
57
- _connection = None
58
-
59
- # 注册程序退出时自动关闭连接
60
- import atexit
61
- atexit.register(close)
79
+ _client_instance.clear()
80
+
81
+ def finish():
82
+ _client_instance.finish()
83
+
84
+ # 可选:如果希望仍然可以使用类创建新实例
85
+ def create_client(host="127.0.0.1", port=65167):
86
+ return _gui_client(host, port)
87
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: smartpi
3
- Version: 0.1.22
3
+ Version: 0.1.23
4
4
  Summary: A library use for H2-RCU
5
5
  Author: ZMROBO
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,9 +1,9 @@
1
- smartpi/__init__.py,sha256=dbeSgtxUXUnzBRBUqAF0gP2AbNr9dwL8WvaeDNMTegY,55
2
- smartpi/base_driver.py,sha256=dhdmWK0_ff2GduYtGXiZ3W4ICRMf-gIROYgRhXYjZok,24204
1
+ smartpi/__init__.py,sha256=vHEaSuG4o0m17zvDoMBhn-lttaKYGct1k73F5y1orhk,55
2
+ smartpi/base_driver.py,sha256=jP8cuQ0LRfjMuVQ2I6tY7fxp3JsjcUH8fdWKooIogME,24188
3
3
  smartpi/color_sensor.py,sha256=YXJjknYjp7teTZsHYZRAWgi73CH0MhBp1go9y0Inxyo,498
4
4
  smartpi/cw2015.py,sha256=1lAF-pi_ye_ya1AZQS1sjbgsDf7MThO5IAskKsNGBzA,5695
5
5
  smartpi/flash.py,sha256=-pUqg6FSVoBiLFKqrG9B4dFqn8lICnQsSPJr_MtZLIU,4132
6
- smartpi/gui.py,sha256=E98_soyWbEf_dwYhXZgMSXrgY5QuYoDTuFCPK63flIQ,2102
6
+ smartpi/gui.py,sha256=HaupXWg5cWw_n86ge8IxARrvL3gpcDCNRwATK9irwH8,2960
7
7
  smartpi/humidity.py,sha256=nZwiBtMWKNipVM83ymajZACPHxkC2vUJjDMmCOPN9lw,499
8
8
  smartpi/led.py,sha256=flvN7EJfP6VDTSiC93w1uR8pRxcZD9w2vLXA1GbJTo8,538
9
9
  smartpi/light_sensor.py,sha256=MayyVikWcXQfjeZrtYRnwYgHBDzu2g-mfJLpdd29EG8,1852
@@ -14,7 +14,7 @@ smartpi/temperature.py,sha256=VT79CYA41q1d_4AM-Y0eIMeIw7AtCkSXjWVws6Yx5yE,462
14
14
  smartpi/touch_sensor.py,sha256=P57RRQlqY0KexpMi-ydqwF5albOKCBOGb0Rb6zeVTqk,440
15
15
  smartpi/trace.py,sha256=tut7BMbq87ShaR5eNuv7PZtAEz9DS5_BDf0_muIZ-tQ,4577
16
16
  smartpi/ultrasonic.py,sha256=kmVpUfvE1oHoqgv92ZU6Fi-sO6DSwm10ssKsImNeOkY,624
17
- smartpi-0.1.22.dist-info/METADATA,sha256=znbfoZEXR-TzNdaUl9258HXSkWdCQAlj_aOfY2EZ4C0,311
18
- smartpi-0.1.22.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
19
- smartpi-0.1.22.dist-info/top_level.txt,sha256=PoLhUCmWAiQUg5UeN2fS-Y1iQyBbF2rdUlizXtpHGRQ,8
20
- smartpi-0.1.22.dist-info/RECORD,,
17
+ smartpi-0.1.23.dist-info/METADATA,sha256=VFDRXlgYEsVNBbHmPcqcwQnuu5VUeoFE0G1VtTfe3Vw,311
18
+ smartpi-0.1.23.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
19
+ smartpi-0.1.23.dist-info/top_level.txt,sha256=PoLhUCmWAiQUg5UeN2fS-Y1iQyBbF2rdUlizXtpHGRQ,8
20
+ smartpi-0.1.23.dist-info/RECORD,,