smartpi 0.1.1__py3-none-any.whl → 0.1.2__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/GUI.py +0 -0
- smartpi/__init__.py +2 -2
- smartpi/base_driver.py +501 -0
- smartpi/color_sensor.py +6 -0
- smartpi/humidity.py +20 -0
- smartpi/led.py +17 -0
- smartpi/light_sensor.py +27 -0
- smartpi/motor.py +60 -0
- smartpi/move.py +0 -0
- smartpi/photosensitive.py +0 -0
- smartpi/servo.py +25 -0
- smartpi/temperature.py +18 -0
- smartpi/touch_sensor.py +15 -0
- smartpi/trace.py +0 -0
- smartpi/ultrasonic.py +27 -0
- {smartpi-0.1.1.dist-info → smartpi-0.1.2.dist-info}/METADATA +1 -3
- smartpi-0.1.2.dist-info/RECORD +19 -0
- smartpi/app_module.py +0 -119
- smartpi/module.py +0 -503
- smartpi/mpu6500.py +0 -130
- smartpi-0.1.1.dist-info/RECORD +0 -8
- {smartpi-0.1.1.dist-info → smartpi-0.1.2.dist-info}/WHEEL +0 -0
- {smartpi-0.1.1.dist-info → smartpi-0.1.2.dist-info}/top_level.txt +0 -0
smartpi/GUI.py
ADDED
|
File without changes
|
smartpi/__init__.py
CHANGED
smartpi/base_driver.py
ADDED
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
import serial,time,struct
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from collections import deque
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# 命令常量
|
|
8
|
+
BOOT_UPDATE_H = 0XFF
|
|
9
|
+
BOOT_UPDATE_L = 0XFF
|
|
10
|
+
READ_MODEL_H = 0XFF
|
|
11
|
+
READ_MODEL_L = 0X01
|
|
12
|
+
READ_VERSION_H = 0XFF
|
|
13
|
+
READ_VERSION_L = 0X02
|
|
14
|
+
READ_FACTORY_H = 0XFF
|
|
15
|
+
READ_FACTORY_L = 0X03
|
|
16
|
+
READ_HW_ID_H = 0XFF
|
|
17
|
+
READ_HW_ID_L = 0X04
|
|
18
|
+
READ_NAME_H = 0XFF
|
|
19
|
+
READ_NAME_L = 0X05
|
|
20
|
+
WRITE_NAME_H = 0XFF
|
|
21
|
+
WRITE_NAME_L = 0X06
|
|
22
|
+
READ_CONNECT_H = 0XFF
|
|
23
|
+
READ_CONNECT_L = 0X07
|
|
24
|
+
READ_BAT_H = 0XFF
|
|
25
|
+
READ_BAT_L = 0X0C
|
|
26
|
+
|
|
27
|
+
UPDATE_REQUEST_H = 0XFF
|
|
28
|
+
UPDATE_REQUEST_L = 0X10
|
|
29
|
+
MAX_COM_LEN_H = 0XFF
|
|
30
|
+
MAX_COM_LEN_L = 0X11
|
|
31
|
+
DL_MESSAGE_H = 0XFF
|
|
32
|
+
DL_MESSAGE_L = 0X12
|
|
33
|
+
READ_STATUS_H = 0XFF
|
|
34
|
+
READ_STATUS_L = 0X13
|
|
35
|
+
PAGE_CHECK_H = 0XFF
|
|
36
|
+
PAGE_CHECK_L = 0X14
|
|
37
|
+
PAGE_SEND_H = 0XFF
|
|
38
|
+
PAGE_SEND_L = 0X15
|
|
39
|
+
|
|
40
|
+
READ_PERIPH_H = 0X01
|
|
41
|
+
READ_PERIPH_L = 0X01
|
|
42
|
+
SINGLE_OP_H = 0X01
|
|
43
|
+
SINGLE_OP_L = 0X02
|
|
44
|
+
MODE_CHANGE_H = 0X01
|
|
45
|
+
MODE_CHANGE_L = 0X03
|
|
46
|
+
SEND_CYCLE_H = 0X01
|
|
47
|
+
SEND_CYCLE_L = 0X04
|
|
48
|
+
|
|
49
|
+
frames_queue = deque()
|
|
50
|
+
buffer = bytearray()
|
|
51
|
+
HEADER = bytes.fromhex('86 AB') # 帧头
|
|
52
|
+
FOOTER = bytes.fromhex('CF') # 帧尾
|
|
53
|
+
MIN_FRAME_LEN = 9 # 最小帧长度
|
|
54
|
+
|
|
55
|
+
# 串口配置参数
|
|
56
|
+
SERIAL_PORT = "/dev/ttyS3" # 串口设备路径
|
|
57
|
+
BAUD_RATE = 115200 # 波特率
|
|
58
|
+
TIMEOUT = 0.1 # 读取超时时间(秒)
|
|
59
|
+
|
|
60
|
+
ser = serial.Serial(
|
|
61
|
+
port=SERIAL_PORT,
|
|
62
|
+
baudrate=BAUD_RATE,
|
|
63
|
+
bytesize=serial.EIGHTBITS, # 8位数据位
|
|
64
|
+
parity=serial.PARITY_NONE, # 无校验位
|
|
65
|
+
stopbits=serial.STOPBITS_ONE, # 1位停止位
|
|
66
|
+
timeout=TIMEOUT,
|
|
67
|
+
xonxoff=False, # 关闭软件流控
|
|
68
|
+
rtscts=False, # 关闭硬件流控
|
|
69
|
+
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def uart3_init() -> Optional[serial.Serial]:
|
|
74
|
+
"""初始化串口"""
|
|
75
|
+
try:
|
|
76
|
+
if ser.is_open:
|
|
77
|
+
print("UART3初始化成功")
|
|
78
|
+
return ser
|
|
79
|
+
else:
|
|
80
|
+
print("Error opening UART3")
|
|
81
|
+
return None
|
|
82
|
+
except Exception as e:
|
|
83
|
+
print(f"Error opening UART3: {e}")
|
|
84
|
+
return None
|
|
85
|
+
|
|
86
|
+
def calculate_pro_check(command_h: int, command_l: int, data: List[bytes] = None) -> int:
|
|
87
|
+
if data:
|
|
88
|
+
len_data = len(data)
|
|
89
|
+
base_sum = 0x86 + 0xAB + 0x00 + 0x09 + len_data + command_h + command_l + 0x01 + 0xCF
|
|
90
|
+
base_sum += sum(data)
|
|
91
|
+
else:
|
|
92
|
+
base_sum = 0x86 + 0xAB + 0x00 + 0x09 + command_h + command_l + 0x01 + 0xCF
|
|
93
|
+
|
|
94
|
+
return base_sum % 256 # 确保结果为单字节(原C代码未取模,需根据实际协议调整)
|
|
95
|
+
|
|
96
|
+
def write_data(command_h: int, command_l: int, send_data: bytes= None) -> Optional[bytes]:
|
|
97
|
+
buffer = bytearray()
|
|
98
|
+
HEADER = bytes.fromhex('86 AB') # 帧头
|
|
99
|
+
FOOTER = bytes.fromhex('CF') # 帧尾
|
|
100
|
+
MIN_FRAME_LEN = 9 # 最小帧长度
|
|
101
|
+
if send_data:
|
|
102
|
+
pro_check = calculate_pro_check(command_h, command_l, list(send_data))
|
|
103
|
+
send_packet = [0x86, 0xAB, (0x09+len(send_data))//256, (0x09+len(send_data))%256, command_h, command_l, *send_data, 0x01, pro_check, 0xCF]
|
|
104
|
+
else:
|
|
105
|
+
pro_check = calculate_pro_check(command_h, command_l)
|
|
106
|
+
send_packet = [0x86, 0xAB, 0x00, 0x09, command_h, command_l, 0x01, pro_check, 0xCF]
|
|
107
|
+
send_bytes = bytes(send_packet)
|
|
108
|
+
|
|
109
|
+
ser.write(send_bytes)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def check_frame(frame: bytes) -> bool:
|
|
113
|
+
# 简化版校验,实际应根据协议实现
|
|
114
|
+
if len(frame) < 9:
|
|
115
|
+
return False
|
|
116
|
+
|
|
117
|
+
# 计算校验和
|
|
118
|
+
calculated_checksum = (sum(frame[:-2])+frame[-1])%256
|
|
119
|
+
frame_checksum = frame[-2] # 倒数第二个字节是校验和
|
|
120
|
+
|
|
121
|
+
return calculated_checksum == frame_checksum
|
|
122
|
+
|
|
123
|
+
def process_received_data():
|
|
124
|
+
global buffer
|
|
125
|
+
# 读取所有可用数据
|
|
126
|
+
data = ser.read(ser.in_waiting or 1)
|
|
127
|
+
if data:
|
|
128
|
+
buffer.extend(data)
|
|
129
|
+
while len(buffer)>=2:
|
|
130
|
+
# for x in buffer:
|
|
131
|
+
# print(f"{x:02X}", end=' ')
|
|
132
|
+
# print("\n")
|
|
133
|
+
# 1. 查找帧头
|
|
134
|
+
start_idx = buffer.find(HEADER)
|
|
135
|
+
if start_idx == -1:
|
|
136
|
+
# 没有找到帧头,清空无效数据(保留最后可能的部分帧头)
|
|
137
|
+
#print("Header no found")
|
|
138
|
+
if len(buffer) > len(HEADER) - 1:
|
|
139
|
+
buffer = buffer[-len(HEADER) + 1:]
|
|
140
|
+
return
|
|
141
|
+
# 2. 检查帧头后的长度字段是否足够
|
|
142
|
+
if start_idx + 4 > len(buffer):
|
|
143
|
+
# 长度字段不完整,等待更多数据
|
|
144
|
+
return
|
|
145
|
+
# 3. 解析帧长度
|
|
146
|
+
frame_length = (buffer[start_idx + 2] << 8) + buffer[start_idx + 3]
|
|
147
|
+
# 4. 检查完整帧是否已到达
|
|
148
|
+
end_idx = start_idx + frame_length - 1
|
|
149
|
+
if end_idx >= len(buffer):
|
|
150
|
+
# 完整帧尚未完全到达
|
|
151
|
+
return
|
|
152
|
+
# 5. 检查帧尾
|
|
153
|
+
if buffer[end_idx] != ord(FOOTER):
|
|
154
|
+
# 跳过当前帧头,继续查找
|
|
155
|
+
print("End no found")
|
|
156
|
+
buffer = buffer[start_idx + 1:]
|
|
157
|
+
continue
|
|
158
|
+
# 6. 提取完整帧
|
|
159
|
+
frame = buffer[start_idx:end_idx + 1]
|
|
160
|
+
frames_queue.append(frame)
|
|
161
|
+
# 7. 从缓冲区移除已处理帧
|
|
162
|
+
buffer = buffer[end_idx + 1:]
|
|
163
|
+
# 处理所有完整帧
|
|
164
|
+
if len(frames_queue) > 0:
|
|
165
|
+
frame = frames_queue.popleft()
|
|
166
|
+
if check_frame(frame):
|
|
167
|
+
return frame
|
|
168
|
+
else:
|
|
169
|
+
print("Check byte error")
|
|
170
|
+
|
|
171
|
+
# 功能函数类
|
|
172
|
+
##############################################################################读取设备信息
|
|
173
|
+
"""固件升级模式"""
|
|
174
|
+
def boot_update() -> None:
|
|
175
|
+
write_data(BOOT_UPDATE_H, BOOT_UPDATE_L)
|
|
176
|
+
while True:
|
|
177
|
+
response =process_received_data()
|
|
178
|
+
if response:
|
|
179
|
+
display_data = response[6:-3]
|
|
180
|
+
if display_data[0]==0X03:
|
|
181
|
+
print("固件更新启动\n")
|
|
182
|
+
elif display_data[0]==0X01:
|
|
183
|
+
print("擦除中......",f"{10*display_data[1]}","%\n")
|
|
184
|
+
elif display_data[0]==0X02:
|
|
185
|
+
print("升级中......",f"{10*display_data[1]}","%\n")
|
|
186
|
+
elif display_data[0]==0X05:
|
|
187
|
+
print("更新成功!\n")
|
|
188
|
+
elif display_data[0]==0X06:
|
|
189
|
+
print("更新失败!\n")
|
|
190
|
+
else:
|
|
191
|
+
for x in display_data:
|
|
192
|
+
print(f"{x:02X}", end=' ')
|
|
193
|
+
print("\n")
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
"""读取设备型号"""
|
|
197
|
+
def read_device_model() -> Optional[bytes]:
|
|
198
|
+
write_data(READ_MODEL_H, READ_MODEL_L)
|
|
199
|
+
start_time = time.time()
|
|
200
|
+
while True:
|
|
201
|
+
response =process_received_data()
|
|
202
|
+
if response:
|
|
203
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
204
|
+
print(f"设备型号: {display_data}")
|
|
205
|
+
return display_data
|
|
206
|
+
else:
|
|
207
|
+
if time.time() - start_time > 3:
|
|
208
|
+
print("读取超时")
|
|
209
|
+
buffer.clear()
|
|
210
|
+
return -1
|
|
211
|
+
|
|
212
|
+
"""读取版本号"""
|
|
213
|
+
def read_version() -> Optional[bytes]:
|
|
214
|
+
write_data(READ_VERSION_H, READ_VERSION_L)
|
|
215
|
+
start_time = time.time()
|
|
216
|
+
while True:
|
|
217
|
+
response =process_received_data()
|
|
218
|
+
if response:
|
|
219
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
220
|
+
print(f"版本号: {display_data}")
|
|
221
|
+
return display_data
|
|
222
|
+
else:
|
|
223
|
+
if time.time() - start_time > 3:
|
|
224
|
+
print("读取超时")
|
|
225
|
+
buffer.clear()
|
|
226
|
+
return -1
|
|
227
|
+
|
|
228
|
+
"""读取工厂信息"""
|
|
229
|
+
def read_factory_data() -> Optional[bytes]:
|
|
230
|
+
write_data(READ_FACTORY_H, READ_FACTORY_L)
|
|
231
|
+
start_time = time.time()
|
|
232
|
+
while True:
|
|
233
|
+
response =process_received_data()
|
|
234
|
+
if response:
|
|
235
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
236
|
+
print(f"厂家信息: {display_data}")
|
|
237
|
+
return display_data
|
|
238
|
+
else:
|
|
239
|
+
if time.time() - start_time > 3:
|
|
240
|
+
print("读取超时")
|
|
241
|
+
buffer.clear()
|
|
242
|
+
return -1
|
|
243
|
+
|
|
244
|
+
"""读取硬件ID"""
|
|
245
|
+
def read_hardware_ID() -> Optional[bytes]:
|
|
246
|
+
write_data(READ_HW_ID_H, READ_HW_ID_L)
|
|
247
|
+
start_time = time.time()
|
|
248
|
+
while True:
|
|
249
|
+
response =process_received_data()
|
|
250
|
+
if response:
|
|
251
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
252
|
+
print(f"硬件ID: {display_data}")
|
|
253
|
+
return display_data
|
|
254
|
+
else:
|
|
255
|
+
if time.time() - start_time > 3:
|
|
256
|
+
print("读取超时")
|
|
257
|
+
buffer.clear()
|
|
258
|
+
return -1
|
|
259
|
+
|
|
260
|
+
"""读取设备名称"""
|
|
261
|
+
def read_device_name() -> Optional[bytes]:
|
|
262
|
+
write_data(READ_NAME_H, READ_NAME_L)
|
|
263
|
+
start_time = time.time()
|
|
264
|
+
while True:
|
|
265
|
+
response =process_received_data()
|
|
266
|
+
if response:
|
|
267
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
268
|
+
print(f"设备名称: {display_data}")
|
|
269
|
+
return display_data
|
|
270
|
+
else:
|
|
271
|
+
if time.time() - start_time > 3:
|
|
272
|
+
print("读取超时")
|
|
273
|
+
buffer.clear()
|
|
274
|
+
return -1
|
|
275
|
+
|
|
276
|
+
"""设置设备名称"""
|
|
277
|
+
def write_device_name(send_data: str) -> Optional[bytes]:
|
|
278
|
+
data_bytes = send_data.encode('utf-8')
|
|
279
|
+
write_data(WRITE_NAME_H, WRITE_NAME_L, data_bytes)
|
|
280
|
+
start_time = time.time()
|
|
281
|
+
while True:
|
|
282
|
+
response =process_received_data()
|
|
283
|
+
if response:
|
|
284
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
285
|
+
print(f"设置状态: {display_data}")
|
|
286
|
+
return display_data
|
|
287
|
+
else:
|
|
288
|
+
if time.time() - start_time > 3:
|
|
289
|
+
print("读取超时")
|
|
290
|
+
buffer.clear()
|
|
291
|
+
return -1
|
|
292
|
+
|
|
293
|
+
"""读取连接方式"""#长度位少了1
|
|
294
|
+
def read_connected() -> Optional[bytes]:
|
|
295
|
+
write_data(READ_CONNECT_H, READ_CONNECT_L)
|
|
296
|
+
start_time = time.time()
|
|
297
|
+
while True:
|
|
298
|
+
response =process_received_data()
|
|
299
|
+
if response:
|
|
300
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
301
|
+
print(f"连接方式: {display_data}")
|
|
302
|
+
return display_data
|
|
303
|
+
else:
|
|
304
|
+
if time.time() - start_time > 3:
|
|
305
|
+
print("读取超时")
|
|
306
|
+
buffer.clear()
|
|
307
|
+
return -1
|
|
308
|
+
|
|
309
|
+
"""读取电池电量"""
|
|
310
|
+
def read_battery() -> Optional[bytes]:
|
|
311
|
+
write_data(READ_BAT_H, READ_BAT_L)
|
|
312
|
+
start_time = time.time()
|
|
313
|
+
while True:
|
|
314
|
+
response =process_received_data()
|
|
315
|
+
if response:
|
|
316
|
+
display_data = response[6:-3]
|
|
317
|
+
# for x in display_data:
|
|
318
|
+
# print(f"{x:02X}", end=' ')
|
|
319
|
+
print(f"电池电量: {((display_data[0]*256+display_data[1])/1000):.2f}V")
|
|
320
|
+
return display_data
|
|
321
|
+
else:
|
|
322
|
+
if time.time() - start_time > 3:
|
|
323
|
+
print("读取超时")
|
|
324
|
+
buffer.clear()
|
|
325
|
+
return -1
|
|
326
|
+
|
|
327
|
+
###############################################################################固件升级
|
|
328
|
+
|
|
329
|
+
"""下载更新请求"""
|
|
330
|
+
def update_request() -> Optional[bytes]:
|
|
331
|
+
write_data(UPDATE_REQUEST_H, UPDATE_REQUEST_L)
|
|
332
|
+
start_time = time.time()
|
|
333
|
+
while True:
|
|
334
|
+
response =process_received_data()
|
|
335
|
+
if response:
|
|
336
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
337
|
+
print(f"从机响应: {display_data}")
|
|
338
|
+
return display_data
|
|
339
|
+
else:
|
|
340
|
+
if time.time() - start_time > 3:
|
|
341
|
+
print("读取超时")
|
|
342
|
+
buffer.clear()
|
|
343
|
+
return -1
|
|
344
|
+
|
|
345
|
+
"""查询最大通讯长度"""
|
|
346
|
+
def read_max_com_len() -> Optional[bytes]:
|
|
347
|
+
write_data(MAX_COM_LEN_H, MAX_COM_LEN_L)
|
|
348
|
+
start_time = time.time()
|
|
349
|
+
while True:
|
|
350
|
+
response =process_received_data()
|
|
351
|
+
if response:
|
|
352
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
353
|
+
print(f"从机响应: {display_data}")
|
|
354
|
+
return display_data
|
|
355
|
+
else:
|
|
356
|
+
if time.time() - start_time > 3:
|
|
357
|
+
print("读取超时")
|
|
358
|
+
buffer.clear()
|
|
359
|
+
return -1
|
|
360
|
+
|
|
361
|
+
"""下载文件的信息"""
|
|
362
|
+
def download_massage() -> Optional[bytes]:
|
|
363
|
+
write_data(DL_MESSAGE_H, DL_MESSAGE_L, file_data)#文件信息来源从哪获取?
|
|
364
|
+
start_time = time.time()
|
|
365
|
+
while True:
|
|
366
|
+
response =process_received_data()
|
|
367
|
+
if response:
|
|
368
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
369
|
+
print(f"从机响应: {display_data}")
|
|
370
|
+
return display_data
|
|
371
|
+
else:
|
|
372
|
+
if time.time() - start_time > 3:
|
|
373
|
+
print("读取超时")
|
|
374
|
+
buffer.clear()
|
|
375
|
+
return -1
|
|
376
|
+
|
|
377
|
+
"""查询设备状态"""
|
|
378
|
+
def read_device_status() -> Optional[bytes]:
|
|
379
|
+
write_data(READ_STATUS_H, READ_STATUS_L)
|
|
380
|
+
start_time = time.time()
|
|
381
|
+
while True:
|
|
382
|
+
response =process_received_data()
|
|
383
|
+
if response:
|
|
384
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
385
|
+
print(f"从机响应: {display_data}")
|
|
386
|
+
return display_data
|
|
387
|
+
else:
|
|
388
|
+
if time.time() - start_time > 3:
|
|
389
|
+
print("读取超时")
|
|
390
|
+
buffer.clear()
|
|
391
|
+
return -1
|
|
392
|
+
|
|
393
|
+
"""发送页校验码"""
|
|
394
|
+
def write_page_check() -> Optional[bytes]:
|
|
395
|
+
write_data(PAGE_CHECK_H, PAGE_CHECK_L, page_check_data)
|
|
396
|
+
start_time = time.time()
|
|
397
|
+
while True:
|
|
398
|
+
response =process_received_data()
|
|
399
|
+
if response:
|
|
400
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
401
|
+
print(f"从机响应: {display_data}")
|
|
402
|
+
return display_data
|
|
403
|
+
else:
|
|
404
|
+
if time.time() - start_time > 3:
|
|
405
|
+
print("读取超时")
|
|
406
|
+
buffer.clear()
|
|
407
|
+
return -1
|
|
408
|
+
|
|
409
|
+
"""发送页数据"""
|
|
410
|
+
def write_page_check() -> Optional[bytes]:
|
|
411
|
+
write_data(PAGE_SEND_H, PAGE_SEND_L, page_send_data)
|
|
412
|
+
start_time = time.time()
|
|
413
|
+
while True:
|
|
414
|
+
response =process_received_data()
|
|
415
|
+
if response:
|
|
416
|
+
display_data = response[6:-3].decode(errors="ignore")
|
|
417
|
+
print(f"从机响应: {display_data}")
|
|
418
|
+
return display_data
|
|
419
|
+
else:
|
|
420
|
+
if time.time() - start_time > 3:
|
|
421
|
+
print("读取超时")
|
|
422
|
+
buffer.clear()
|
|
423
|
+
return -1
|
|
424
|
+
|
|
425
|
+
###############################################################################读取传感器信息
|
|
426
|
+
|
|
427
|
+
"""读取外设连接情况"""
|
|
428
|
+
def read_peripheral() -> Optional[bytes]:
|
|
429
|
+
write_data(READ_PERIPH_H, READ_PERIPH_L)
|
|
430
|
+
start_time = time.time()
|
|
431
|
+
while True:
|
|
432
|
+
response =process_received_data()
|
|
433
|
+
if response:
|
|
434
|
+
display_data = response[6:-3]
|
|
435
|
+
for x in display_data:
|
|
436
|
+
print(f"{x:02X}", end=' ')
|
|
437
|
+
print("\n")
|
|
438
|
+
return display_data
|
|
439
|
+
else:
|
|
440
|
+
if time.time() - start_time > 3:
|
|
441
|
+
print("读取超时")
|
|
442
|
+
buffer.clear()
|
|
443
|
+
return -1
|
|
444
|
+
|
|
445
|
+
"""单次操作外设"""
|
|
446
|
+
def single_operate_sensor(op_struct: bytes) -> Optional[bytes]:
|
|
447
|
+
write_data(SINGLE_OP_H, SINGLE_OP_L, op_struct)
|
|
448
|
+
start_time = time.time()
|
|
449
|
+
while True:
|
|
450
|
+
response =process_received_data()
|
|
451
|
+
if response:
|
|
452
|
+
display_data = response[6:-3]
|
|
453
|
+
for x in display_data:
|
|
454
|
+
print(f"{x:02X}", end=' ')
|
|
455
|
+
print("\n")
|
|
456
|
+
return display_data
|
|
457
|
+
else:
|
|
458
|
+
if time.time() - start_time > 3:
|
|
459
|
+
print("读取超时")
|
|
460
|
+
buffer.clear()
|
|
461
|
+
return -1
|
|
462
|
+
|
|
463
|
+
"""从机模式转换"""
|
|
464
|
+
def mode_change(send_data: str) -> Optional[bytes]:
|
|
465
|
+
write_data(MODE_CHANGE_H, MODE_CHANGE_L, send_data)
|
|
466
|
+
start_time = time.time()
|
|
467
|
+
while True:
|
|
468
|
+
response =process_received_data()
|
|
469
|
+
if response:
|
|
470
|
+
display_data = response[6:-3]
|
|
471
|
+
for x in display_data:
|
|
472
|
+
print(f"{x:02X}", end=' ')
|
|
473
|
+
print("\n")
|
|
474
|
+
return display_data
|
|
475
|
+
else:
|
|
476
|
+
if time.time() - start_time > 3:
|
|
477
|
+
print("读取超时")
|
|
478
|
+
buffer.clear()
|
|
479
|
+
return -1
|
|
480
|
+
|
|
481
|
+
"""智能模式发送周期"""
|
|
482
|
+
def mode_change(send_data: str) -> Optional[bytes]:
|
|
483
|
+
write_data(SEND_CYCLE_H, SEND_CYCLE_L, send_data)
|
|
484
|
+
start_time = time.time()
|
|
485
|
+
while True:
|
|
486
|
+
response =process_received_data()
|
|
487
|
+
if response:
|
|
488
|
+
display_data = response[6:-3]
|
|
489
|
+
for x in display_data:
|
|
490
|
+
print(f"{x:02X}", end=' ')
|
|
491
|
+
print("\n")
|
|
492
|
+
return display_data
|
|
493
|
+
else:
|
|
494
|
+
if time.time() - start_time > 3:
|
|
495
|
+
print("读取超时")
|
|
496
|
+
buffer.clear()
|
|
497
|
+
return -1
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
smartpi/color_sensor.py
ADDED
smartpi/humidity.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
import time
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from smartpi import base_driver
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
#湿度读取 port:连接P端口;
|
|
8
|
+
def get_value(port:bytes) -> Optional[bytes]:
|
|
9
|
+
humiture_str=[0xA0, 0x0C, 0x01, 0x71, 0x00, 0xBE]
|
|
10
|
+
humiture_str[0]=0XA0+port
|
|
11
|
+
humiture_str[2]=0
|
|
12
|
+
response = base_driver.single_operate_sensor(humiture_str)
|
|
13
|
+
if response:
|
|
14
|
+
return 0
|
|
15
|
+
else:
|
|
16
|
+
return -1
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
smartpi/led.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
import time
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from smartpi import base_driver
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
#�ʵƿ��� port:����P�˿ڣ�command:0:�صƣ�1:�죻2:�̣�3:����4:�ƣ�5:�ϣ�6:�ࣻ7:��
|
|
8
|
+
def set_color(port:bytes,command:bytes) -> Optional[bytes]:
|
|
9
|
+
color_lamp_str=[0xA0, 0x05, 0x00, 0xBE]
|
|
10
|
+
color_lamp_str[0]=0XA0+port
|
|
11
|
+
color_lamp_str[2]=command
|
|
12
|
+
response = base_driver.single_operate_sensor(color_lamp_str)
|
|
13
|
+
if response:
|
|
14
|
+
return 0
|
|
15
|
+
else:
|
|
16
|
+
return -1
|
|
17
|
+
|
smartpi/light_sensor.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
import time
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from smartpi import base_driver
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
#����ȡ port:����P�˿ڣ�command:1:��ȡ��2:���ƣ�3:�صƣ�
|
|
9
|
+
#def get_value(port:bytes,command:bytes) -> Optional[bytes]:
|
|
10
|
+
# light_str=[0xA0, 0x02, 0x00, 0xBE]
|
|
11
|
+
# light_str[0]=0XA0+port
|
|
12
|
+
# light_str[2]=command
|
|
13
|
+
# response = base_driver.single_operate_sensor(light_str)
|
|
14
|
+
# if response:
|
|
15
|
+
# return 0
|
|
16
|
+
# else:
|
|
17
|
+
# return -1
|
|
18
|
+
|
|
19
|
+
def get_value(port:bytes) -> Optional[bytes]:
|
|
20
|
+
light_str=[0xA0, 0x02, 0x00, 0xBE]
|
|
21
|
+
light_str[0]=0XA0+port
|
|
22
|
+
light_str[2]=1
|
|
23
|
+
response = base_driver.single_operate_sensor(light_str)
|
|
24
|
+
if response:
|
|
25
|
+
return 0
|
|
26
|
+
else:
|
|
27
|
+
return -1
|
smartpi/motor.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
import time
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from smartpi import base_driver
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
##############################################################
|
|
8
|
+
# motor.write_motor_dir(1,0)
|
|
9
|
+
# motor.write_motor_speed(1,100)
|
|
10
|
+
# time.sleep(0.5)
|
|
11
|
+
# motor.write_motor_dir(1,1)
|
|
12
|
+
# motor.write_motor_speed(1,50)
|
|
13
|
+
# time.sleep(0.5)
|
|
14
|
+
##############################################################
|
|
15
|
+
|
|
16
|
+
#��������ȡ port:����M�˿ڣ�
|
|
17
|
+
def read_motor_code(port:bytes) -> Optional[bytes]:
|
|
18
|
+
motor_str=[0xA0, 0x01, 0x01, 0xBE]
|
|
19
|
+
motor_str[0]=0XA0+port
|
|
20
|
+
response = base_driver.single_operate_sensor(motor_str)
|
|
21
|
+
if response:
|
|
22
|
+
return 0
|
|
23
|
+
else:
|
|
24
|
+
return -1
|
|
25
|
+
|
|
26
|
+
#�����ٶȿ��� port:����M�˿ڣ�speed:0~100
|
|
27
|
+
def write_motor_speed(port:bytes,speed:bytes) -> Optional[bytes]:
|
|
28
|
+
motor_str=[0xA0, 0x01, 0x02, 0x71, 0x00, 0xBE]
|
|
29
|
+
motor_str[0]=0XA0+port
|
|
30
|
+
motor_str[4]=speed
|
|
31
|
+
response = base_driver.single_operate_sensor(motor_str)
|
|
32
|
+
if response:
|
|
33
|
+
return 0
|
|
34
|
+
else:
|
|
35
|
+
return -1
|
|
36
|
+
|
|
37
|
+
#�����ٶȱ������ port:����M�˿ڣ�speed:0~100��code:0~65535
|
|
38
|
+
def motor_servoctl(port:bytes,speed:bytes,code:int) -> Optional[bytes]:
|
|
39
|
+
motor_str=[0xA0, 0x01, 0x04, 0x81, 0x00, 0x81, 0x00, 0x00, 0xBE]
|
|
40
|
+
motor_str[0]=0XA0+port
|
|
41
|
+
motor_str[4]=speed
|
|
42
|
+
motor_str[6]=code//256
|
|
43
|
+
motor_str[7]=code%256
|
|
44
|
+
response = base_driver.single_operate_sensor(motor_str)
|
|
45
|
+
if response:
|
|
46
|
+
return 0
|
|
47
|
+
else:
|
|
48
|
+
return -1
|
|
49
|
+
|
|
50
|
+
#���﷽����� port:����M�˿ڣ�dir:
|
|
51
|
+
def write_motor_dir(port:bytes,direc:bytes) -> Optional[bytes]:
|
|
52
|
+
motor_str=[0xA0, 0x01, 0x06, 0x71, 0x00, 0xBE]
|
|
53
|
+
motor_str[0]=0XA0+port
|
|
54
|
+
motor_str[4]=direc
|
|
55
|
+
response = base_driver.single_operate_sensor(motor_str)
|
|
56
|
+
if response:
|
|
57
|
+
return 0
|
|
58
|
+
else:
|
|
59
|
+
return -1
|
|
60
|
+
|
smartpi/move.py
ADDED
|
File without changes
|
|
File without changes
|
smartpi/servo.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
import time
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from smartpi import base_driver
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
###############################################
|
|
8
|
+
# servo.servo_operate(1,1,0)
|
|
9
|
+
# time.sleep(0.5)
|
|
10
|
+
# servo.servo_operate(1,1,180)
|
|
11
|
+
# time.sleep(0.5)
|
|
12
|
+
###############################################
|
|
13
|
+
|
|
14
|
+
#������� port:����P�˿ڣ�command:1:���� angle:�Ƕ�
|
|
15
|
+
def servo_operate(port:bytes,command:bytes,angle:bytes) -> Optional[bytes]:
|
|
16
|
+
servo_str=[0xA0, 0x0E, 0x01, 0x71, 0x00, 0xBE]
|
|
17
|
+
servo_str[0]=0XA0+port
|
|
18
|
+
servo_str[2]=command
|
|
19
|
+
servo_str[4]=angle
|
|
20
|
+
response = base_driver.single_operate_sensor(servo_str)
|
|
21
|
+
if response:
|
|
22
|
+
return 0
|
|
23
|
+
else:
|
|
24
|
+
return -1
|
|
25
|
+
|
smartpi/temperature.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
import time
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from smartpi import base_driver
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
#�¶ȶ�ȡ port:����P�˿ڣ�
|
|
9
|
+
def get_value(port:bytes) -> Optional[bytes]:
|
|
10
|
+
humiture_str=[0xA0, 0x0C, 0x01, 0x71, 0x00, 0xBE]
|
|
11
|
+
humiture_str[0]=0XA0+port
|
|
12
|
+
humiture_str[2]=1
|
|
13
|
+
response = base_driver.single_operate_sensor(humiture_str)
|
|
14
|
+
if response:
|
|
15
|
+
return 0
|
|
16
|
+
else:
|
|
17
|
+
return -1
|
|
18
|
+
|