smartpi 0.1.14__py3-none-any.whl → 0.1.15__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 +1 -1
- smartpi/base_driver.py +27 -9
- smartpi/flash.py +130 -0
- smartpi/light_sensor.py +39 -2
- smartpi/trace.py +31 -0
- {smartpi-0.1.14.dist-info → smartpi-0.1.15.dist-info}/METADATA +1 -1
- {smartpi-0.1.14.dist-info → smartpi-0.1.15.dist-info}/RECORD +9 -7
- {smartpi-0.1.14.dist-info → smartpi-0.1.15.dist-info}/WHEEL +0 -0
- {smartpi-0.1.14.dist-info → smartpi-0.1.15.dist-info}/top_level.txt +0 -0
smartpi/__init__.py
CHANGED
smartpi/base_driver.py
CHANGED
|
@@ -116,6 +116,10 @@ def write_data(command_h: int, command_l: int, send_data: bytes= None) -> Option
|
|
|
116
116
|
send_packet = [0x86, 0xAB, 0x00, 0x09, command_h, command_l, 0x01, pro_check, 0xCF]
|
|
117
117
|
send_bytes = bytes(send_packet)
|
|
118
118
|
|
|
119
|
+
# for x in send_bytes:
|
|
120
|
+
# print(f"{x:02X}", end=' ')
|
|
121
|
+
# print("\n")
|
|
122
|
+
|
|
119
123
|
ser.write(send_bytes)
|
|
120
124
|
|
|
121
125
|
|
|
@@ -135,12 +139,26 @@ def process_received_data():
|
|
|
135
139
|
global buffer
|
|
136
140
|
# 读取所有可用数据
|
|
137
141
|
data = ser.read(ser.in_waiting or 1)
|
|
142
|
+
|
|
143
|
+
# if data:
|
|
144
|
+
# buffer.extend(data)
|
|
145
|
+
#
|
|
146
|
+
# # 检查缓冲区中是否有0xCF
|
|
147
|
+
# if 0xCF in buffer:
|
|
148
|
+
# # 找到0xCF的位置
|
|
149
|
+
# cf_index = buffer.index(0xCF)
|
|
150
|
+
# # 提取从开始到0xCF的完整帧(包括0xCF)
|
|
151
|
+
# frame = buffer[:cf_index + 1]
|
|
152
|
+
# # 从缓冲区中移除已处理的数据
|
|
153
|
+
# buffer = buffer[cf_index + 1:]
|
|
154
|
+
# return bytes(frame)
|
|
155
|
+
|
|
138
156
|
if data:
|
|
139
157
|
buffer.extend(data)
|
|
140
158
|
while len(buffer)>=2:
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
159
|
+
# for x in buffer:
|
|
160
|
+
# print(f"{x:02X}", end=' ')
|
|
161
|
+
# print("\n")
|
|
144
162
|
# 1. 查找帧头
|
|
145
163
|
start_idx = buffer.find(HEADER)
|
|
146
164
|
if start_idx == -1:
|
|
@@ -485,9 +503,9 @@ def mode_change(send_data: str) -> Optional[bytes]:
|
|
|
485
503
|
if response:
|
|
486
504
|
serial_lock.release() #释放线程锁
|
|
487
505
|
display_data = response[6:-3]
|
|
488
|
-
for x in display_data:
|
|
489
|
-
print(f"{x:02X}", end=' ')
|
|
490
|
-
print("\n")
|
|
506
|
+
# for x in display_data:
|
|
507
|
+
# print(f"{x:02X}", end=' ')
|
|
508
|
+
# print("\n")
|
|
491
509
|
return display_data
|
|
492
510
|
else:
|
|
493
511
|
if time.time() - start_time > 3:
|
|
@@ -506,9 +524,9 @@ def mode_change(send_data: str) -> Optional[bytes]:
|
|
|
506
524
|
if response:
|
|
507
525
|
serial_lock.release() #释放线程锁
|
|
508
526
|
display_data = response[6:-3]
|
|
509
|
-
for x in display_data:
|
|
510
|
-
print(f"{x:02X}", end=' ')
|
|
511
|
-
print("\n")
|
|
527
|
+
# for x in display_data:
|
|
528
|
+
# print(f"{x:02X}", end=' ')
|
|
529
|
+
# print("\n")
|
|
512
530
|
return display_data
|
|
513
531
|
else:
|
|
514
532
|
if time.time() - start_time > 3:
|
smartpi/flash.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import struct
|
|
3
|
+
import fcntl
|
|
4
|
+
|
|
5
|
+
# 配置文件路径 (当前目录下的flash.bin)
|
|
6
|
+
FLASH_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/home/Interface/flash/flash.bin")
|
|
7
|
+
FLASH_FILE_2 = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/home/Interface/flash/flash_2.bin")
|
|
8
|
+
|
|
9
|
+
DATA_SIZE = 2 # 每个数据2字节
|
|
10
|
+
TOTAL_SLOTS = 100 # 100个数据槽
|
|
11
|
+
|
|
12
|
+
def _init_flash_file():
|
|
13
|
+
"""初始化存储文件"""
|
|
14
|
+
if not os.path.exists(FLASH_FILE):
|
|
15
|
+
with open(FLASH_FILE, "wb") as f:
|
|
16
|
+
f.write(b'\x00' * DATA_SIZE * TOTAL_SLOTS)
|
|
17
|
+
|
|
18
|
+
def write(address, data):
|
|
19
|
+
"""
|
|
20
|
+
写入数据到指定地址
|
|
21
|
+
:param address: 地址编号 (1-100)
|
|
22
|
+
:param data: 要写入的整数数据 (2字节范围)
|
|
23
|
+
"""
|
|
24
|
+
if not 1 <= address <= TOTAL_SLOTS:
|
|
25
|
+
raise ValueError(f"地址必须在1-{TOTAL_SLOTS}范围内")
|
|
26
|
+
|
|
27
|
+
# 2字节数据范围: 0-65535 (0xFFFF)
|
|
28
|
+
if not 0 <= data <= 0xFFFF:
|
|
29
|
+
raise ValueError("数据必须在0-65535范围内")
|
|
30
|
+
|
|
31
|
+
_init_flash_file()
|
|
32
|
+
|
|
33
|
+
# 计算文件偏移量 (每个地址2字节)
|
|
34
|
+
offset = (address - 1) * DATA_SIZE
|
|
35
|
+
|
|
36
|
+
with open(FLASH_FILE, "r+b") as f:
|
|
37
|
+
# 使用文件锁确保写入安全
|
|
38
|
+
fcntl.flock(f, fcntl.LOCK_EX)
|
|
39
|
+
f.seek(offset)
|
|
40
|
+
# 将整数打包为2字节小端格式
|
|
41
|
+
f.write(struct.pack('<H', data))
|
|
42
|
+
f.flush()
|
|
43
|
+
fcntl.flock(f, fcntl.LOCK_UN)
|
|
44
|
+
|
|
45
|
+
def read(address):
|
|
46
|
+
"""
|
|
47
|
+
从指定地址读取数据
|
|
48
|
+
:param address: 地址编号 (1-100)
|
|
49
|
+
:return: 读取到的整数数据
|
|
50
|
+
"""
|
|
51
|
+
if not 1 <= address <= TOTAL_SLOTS:
|
|
52
|
+
raise ValueError(f"地址必须在1-{TOTAL_SLOTS}范围内")
|
|
53
|
+
|
|
54
|
+
_init_flash_file()
|
|
55
|
+
|
|
56
|
+
offset = (address - 1) * DATA_SIZE
|
|
57
|
+
|
|
58
|
+
with open(FLASH_FILE, "rb") as f:
|
|
59
|
+
fcntl.flock(f, fcntl.LOCK_SH) # 共享锁
|
|
60
|
+
f.seek(offset)
|
|
61
|
+
# 读取2字节并解包为整数
|
|
62
|
+
data_bytes = f.read(DATA_SIZE)
|
|
63
|
+
fcntl.flock(f, fcntl.LOCK_UN)
|
|
64
|
+
|
|
65
|
+
if len(data_bytes) != DATA_SIZE:
|
|
66
|
+
return 0 # 返回默认值0
|
|
67
|
+
|
|
68
|
+
return struct.unpack('<H', data_bytes)[0]
|
|
69
|
+
|
|
70
|
+
# 可选扩展功能
|
|
71
|
+
def erase_all():
|
|
72
|
+
"""擦除所有数据(重置为0)"""
|
|
73
|
+
with open(FLASH_FILE, "wb") as f:
|
|
74
|
+
f.write(b'\x00' * DATA_SIZE * TOTAL_SLOTS)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _init_flash_file_2():
|
|
78
|
+
"""初始化存储文件"""
|
|
79
|
+
if not os.path.exists(FLASH_FILE_2):
|
|
80
|
+
with open(FLASH_FILE_2, "wb") as f:
|
|
81
|
+
f.write(b'\x00' * DATA_SIZE * TOTAL_SLOTS)
|
|
82
|
+
|
|
83
|
+
def write_2(address, data):
|
|
84
|
+
if not 1 <= address <= TOTAL_SLOTS:
|
|
85
|
+
raise ValueError(f"地址必须在1-{TOTAL_SLOTS}范围内")
|
|
86
|
+
|
|
87
|
+
# 2字节数据范围: 0-65535 (0xFFFF)
|
|
88
|
+
if not 0 <= data <= 0xFFFF:
|
|
89
|
+
raise ValueError("数据必须在0-65535范围内")
|
|
90
|
+
|
|
91
|
+
_init_flash_file_2()
|
|
92
|
+
|
|
93
|
+
# 计算文件偏移量 (每个地址2字节)
|
|
94
|
+
offset = (address - 1) * DATA_SIZE
|
|
95
|
+
|
|
96
|
+
with open(FLASH_FILE_2, "r+b") as f:
|
|
97
|
+
# 使用文件锁确保写入安全
|
|
98
|
+
fcntl.flock(f, fcntl.LOCK_EX)
|
|
99
|
+
f.seek(offset)
|
|
100
|
+
# 将整数打包为2字节小端格式
|
|
101
|
+
f.write(struct.pack('<H', data))
|
|
102
|
+
f.flush()
|
|
103
|
+
fcntl.flock(f, fcntl.LOCK_UN)
|
|
104
|
+
|
|
105
|
+
def read_2(address):
|
|
106
|
+
if not 1 <= address <= TOTAL_SLOTS:
|
|
107
|
+
raise ValueError(f"地址必须在1-{TOTAL_SLOTS}范围内")
|
|
108
|
+
|
|
109
|
+
_init_flash_file_2()
|
|
110
|
+
|
|
111
|
+
offset = (address - 1) * DATA_SIZE
|
|
112
|
+
|
|
113
|
+
with open(FLASH_FILE_2, "rb") as f:
|
|
114
|
+
fcntl.flock(f, fcntl.LOCK_SH) # 共享锁
|
|
115
|
+
f.seek(offset)
|
|
116
|
+
# 读取2字节并解包为整数
|
|
117
|
+
data_bytes = f.read(DATA_SIZE)
|
|
118
|
+
fcntl.flock(f, fcntl.LOCK_UN)
|
|
119
|
+
|
|
120
|
+
if len(data_bytes) != DATA_SIZE:
|
|
121
|
+
return 0 # 返回默认值0
|
|
122
|
+
|
|
123
|
+
return struct.unpack('<H', data_bytes)[0]
|
|
124
|
+
|
|
125
|
+
# 可选扩展功能
|
|
126
|
+
def erase_all_2():
|
|
127
|
+
"""擦除所有数据(重置为0)"""
|
|
128
|
+
with open(FLASH_FILE_2, "wb") as f:
|
|
129
|
+
f.write(b'\x00' * DATA_SIZE * TOTAL_SLOTS)
|
|
130
|
+
|
smartpi/light_sensor.py
CHANGED
|
@@ -3,11 +3,11 @@ import time
|
|
|
3
3
|
from typing import List, Optional
|
|
4
4
|
from smartpi import base_driver
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
#����ֵ��ȡ port:����P�˿ڣ� �������أ���ֵ����; ��ȡ����-1
|
|
7
7
|
def get_value(port:bytes) -> Optional[bytes]:
|
|
8
8
|
light_str=[0xA0, 0x02, 0x00, 0xBE]
|
|
9
9
|
light_str[0]=0XA0+port
|
|
10
|
-
light_str[2]=
|
|
10
|
+
light_str[2]=0x01
|
|
11
11
|
response = base_driver.single_operate_sensor(light_str)
|
|
12
12
|
if response == None:
|
|
13
13
|
return None
|
|
@@ -15,4 +15,41 @@ def get_value(port:bytes) -> Optional[bytes]:
|
|
|
15
15
|
light_data=response[4:-1]
|
|
16
16
|
light_num=int.from_bytes(light_data, byteorder='big', signed=True)
|
|
17
17
|
return light_num
|
|
18
|
+
|
|
19
|
+
#�����ֵ���� port:����P�˿ڣ� threshold�����õ���ֵ0~4000
|
|
20
|
+
def set_threshold(port:bytes,threshold:int) -> Optional[bytes]:
|
|
21
|
+
light_str=[0xA0, 0x02, 0x00, 0x81, 0x00, 0x00, 0xBE]
|
|
22
|
+
light_str[0]=0XA0+port
|
|
23
|
+
light_str[2]=0x04
|
|
24
|
+
light_str[4]=threshold//256
|
|
25
|
+
light_str[5]=threshold%256
|
|
26
|
+
response = base_driver.single_operate_sensor(light_str)
|
|
27
|
+
if response == None:
|
|
28
|
+
return None
|
|
29
|
+
else:
|
|
30
|
+
return 0
|
|
31
|
+
|
|
32
|
+
#�����ֵ��ȡ port:����P�˿ڣ�
|
|
33
|
+
def get_threshold(port:bytes) -> Optional[bytes]:
|
|
34
|
+
light_str=[0xA0, 0x02, 0x00, 0xBE]
|
|
35
|
+
light_str[0]=0XA0+port
|
|
36
|
+
light_str[2]=0x05
|
|
37
|
+
response = base_driver.single_operate_sensor(light_str)
|
|
38
|
+
if response == None:
|
|
39
|
+
return None
|
|
40
|
+
else:
|
|
41
|
+
light_data=response[4:-1]
|
|
42
|
+
light_num=int.from_bytes(light_data, byteorder='big', signed=True)
|
|
43
|
+
return light_num
|
|
44
|
+
|
|
45
|
+
#����ȡ��ǰֵ���趨��ֵ�ȽϺ��boolֵ port:����P�˿ڣ�
|
|
46
|
+
def get_bool_data(port:bytes) -> Optional[bytes]:
|
|
47
|
+
light_str=[0xA0, 0x02, 0x00, 0xBE]
|
|
48
|
+
light_str[0]=0XA0+port
|
|
49
|
+
light_str[2]=0x06
|
|
50
|
+
response = base_driver.single_operate_sensor(light_str)
|
|
51
|
+
if response == None:
|
|
52
|
+
return None
|
|
53
|
+
else:
|
|
54
|
+
return response[4]
|
|
18
55
|
|
smartpi/trace.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
import time
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from smartpi import base_driver
|
|
5
|
+
|
|
6
|
+
#循迹卡单通道光值读取 port:连接P端口;正常返回:通道光值数据; 读取错误:None
|
|
7
|
+
def get_analog(port:bytes, chn:bytes) -> Optional[bytes]:
|
|
8
|
+
trace_str=[0xA0, 0x21, 0x01, 0x71, 0x00, 0xBE]
|
|
9
|
+
trace_str[0]=0XA0+port
|
|
10
|
+
trace_str[4]=0x20+chn
|
|
11
|
+
response = base_driver.single_operate_sensor(trace_str)
|
|
12
|
+
if response == None:
|
|
13
|
+
return None
|
|
14
|
+
else:
|
|
15
|
+
trace_data=response[4:-1]
|
|
16
|
+
trace_num=int.from_bytes(trace_data, byteorder='big', signed=True)
|
|
17
|
+
return trace_num
|
|
18
|
+
|
|
19
|
+
#循迹卡设置全部颜色 port:连接P端口;正常返回:通道光值数据; 读取错误:None
|
|
20
|
+
def set_color(port:bytes, color:bytes) -> Optional[bytes]:
|
|
21
|
+
trace_str=[0xA0, 0x20, 0x01, 0x71, 0x00, 0xBE]
|
|
22
|
+
trace_str[0]=0XA0+port
|
|
23
|
+
trace_str[4]=color
|
|
24
|
+
response = base_driver.single_operate_sensor(trace_str)
|
|
25
|
+
if response == None:
|
|
26
|
+
return None
|
|
27
|
+
else:
|
|
28
|
+
return response[4]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
smartpi/__init__.py,sha256=
|
|
2
|
-
smartpi/base_driver.py,sha256=
|
|
1
|
+
smartpi/__init__.py,sha256=dSznPiZfuuDogA_EVuESFxs3sJut0lzCo-ys_AJaccE,55
|
|
2
|
+
smartpi/base_driver.py,sha256=3_dsEM1Sbx43HVICOFZmByHGAjIRbba902GEWsThDSU,20202
|
|
3
3
|
smartpi/color_sensor.py,sha256=sTqD3jApjmc6qHMrDyEy2UjaRt8vhJZNR88vzgUiLKs,496
|
|
4
4
|
smartpi/cw2015.py,sha256=1lAF-pi_ye_ya1AZQS1sjbgsDf7MThO5IAskKsNGBzA,5695
|
|
5
|
+
smartpi/flash.py,sha256=Luz0TjinQSkx31uVknqfSWkiAiVrqIE2Iba7lk3AOzM,4132
|
|
5
6
|
smartpi/gui.py,sha256=E98_soyWbEf_dwYhXZgMSXrgY5QuYoDTuFCPK63flIQ,2102
|
|
6
7
|
smartpi/humidity.py,sha256=xtALQ_IlcwR2RCYvopCSmeNajB45kQU_ckk6FZ0rqko,497
|
|
7
8
|
smartpi/led.py,sha256=n3_k1jGcQptfGXhezDLaYzH6UptgluP4Ze6qP_Y4WmU,536
|
|
8
|
-
smartpi/light_sensor.py,sha256=
|
|
9
|
+
smartpi/light_sensor.py,sha256=glDa4O0wW0kamb-tI3qf509qM7zA8UUjVXbA9si3TXM,1844
|
|
9
10
|
smartpi/motor.py,sha256=uvuAwt2j5LjdLaMfNisXqaGh1ro3fZDvHU8IXd2fn9Q,4527
|
|
10
11
|
smartpi/move.py,sha256=3qzrJCGA-qbsLXBpklY2DErtw0jlzMELzozjhEvRzKs,6028
|
|
11
12
|
smartpi/servo.py,sha256=B6X3yCoEz82qqpUIE5MSO0Eg9YZJ5zDzJEcRpioZpUo,4625
|
|
12
13
|
smartpi/temperature.py,sha256=px2YeqgG63nPkyhJA1wDg3dwYx_oOCYuhMjtsVm_YO0,460
|
|
13
14
|
smartpi/touch_sensor.py,sha256=F6IIQGewNRhC9U1RbHpVzuGYqb8H41lpeQ1Ejwsc_T8,438
|
|
15
|
+
smartpi/trace.py,sha256=pa85IKqNzqAPGtRAoGGYwYmTh3RXOtuKnLrv9_z9qmM,1095
|
|
14
16
|
smartpi/ultrasonic.py,sha256=0meczFKXFLUt92kLxipeEc37vb5duvJjPs4kgtlpO8M,622
|
|
15
|
-
smartpi-0.1.
|
|
16
|
-
smartpi-0.1.
|
|
17
|
-
smartpi-0.1.
|
|
18
|
-
smartpi-0.1.
|
|
17
|
+
smartpi-0.1.15.dist-info/METADATA,sha256=OVWU7rX_t-hjb8WDnOQKBCfmW7Sh17APIuXUs7A9cnM,311
|
|
18
|
+
smartpi-0.1.15.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
19
|
+
smartpi-0.1.15.dist-info/top_level.txt,sha256=PoLhUCmWAiQUg5UeN2fS-Y1iQyBbF2rdUlizXtpHGRQ,8
|
|
20
|
+
smartpi-0.1.15.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|