smartpi 0.1.37__py3-none-any.whl → 0.1.39__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
@@ -4,5 +4,5 @@ from .base_driver import P1, P2, P3, P4, P5, P6, M1, M2, M3, M4, M5, M6
4
4
  __all__ = ["base_driver","gui","ultrasonic","touch_sensor","temperature","humidity","light_sensor","color_sensor","motor","servo","led","flash",
5
5
  "P1", "P2", "P3", "P4", "P5", "P6", "M1", "M2", "M3", "M4", "M5", "M6"]
6
6
 
7
- __version__ = "0.1.37"
7
+ __version__ = "0.1.39"
8
8
 
smartpi/base_driver.py CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding=utf-8
2
- import serial,time,struct,threading,fcntl,os
2
+ import serial,time,struct,threading,fcntl,os,termios
3
3
  from typing import List, Optional
4
4
  from collections import deque
5
5
  from . import servo,motor,cw2015,led,light_sensor
@@ -116,8 +116,8 @@ def calculate_pro_check(command_h: int, command_l: int, data: List[bytes] = None
116
116
 
117
117
  return base_sum % 256 # 确保结果为单字节(原C代码未取模,需根据实际协议调整)
118
118
 
119
- def write_data(command_h: int, command_l: int, send_data: bytes= None) -> Optional[bytes]:
120
- #with serial_lock:
119
+ def write_data(command_h: int, command_l: int, send_data: bytes= None, lock: bytes= True) -> Optional[bytes]:
120
+ if lock == True:
121
121
  buffer = bytearray()
122
122
  HEADER = bytes.fromhex('86 AB') # 帧头
123
123
  FOOTER = bytes.fromhex('CF') # 帧尾
@@ -133,7 +133,29 @@ def write_data(command_h: int, command_l: int, send_data: bytes= None) -> Option
133
133
  # for x in send_bytes:
134
134
  # print(f"{x:02X}", end=' ')
135
135
  # print("\n")
136
- buffer.clear()
136
+ buf_clear()
137
+ serial_lock.acquire() #获取线程锁
138
+ fcntl.flock(ser.fileno(), fcntl.LOCK_EX) #进程锁,阻塞其他进程
139
+ ser.write(send_bytes)
140
+ serial_lock.release() #释放线程锁
141
+ fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
142
+ elif lock == False:
143
+ buffer = bytearray()
144
+ HEADER = bytes.fromhex('86 AB') # 帧头
145
+ FOOTER = bytes.fromhex('CF') # 帧尾
146
+ MIN_FRAME_LEN = 9 # 最小帧长度
147
+ if send_data:
148
+ pro_check = calculate_pro_check(command_h, command_l, list(send_data))
149
+ send_packet = [0x86, 0xAB, (0x09+len(send_data))//256, (0x09+len(send_data))%256, command_h, command_l, *send_data, 0x01, pro_check, 0xCF]
150
+ else:
151
+ pro_check = calculate_pro_check(command_h, command_l)
152
+ send_packet = [0x86, 0xAB, 0x00, 0x09, command_h, command_l, 0x01, pro_check, 0xCF]
153
+ send_bytes = bytes(send_packet)
154
+
155
+ # for x in send_bytes:
156
+ # print(f"{x:02X}", end=' ')
157
+ # print("\n")
158
+ buf_clear()
137
159
  ser.write(send_bytes)
138
160
 
139
161
 
@@ -217,7 +239,7 @@ def process_received_data():
217
239
  def read_device_model() -> Optional[bytes]:
218
240
  serial_lock.acquire() #获取线程锁
219
241
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
220
- write_data(READ_MODEL_H, READ_MODEL_L)
242
+ write_data(READ_MODEL_H, READ_MODEL_L, None, False)
221
243
  start_time = time.time()
222
244
  while True:
223
245
  response =process_received_data()
@@ -225,12 +247,13 @@ def read_device_model() -> Optional[bytes]:
225
247
  serial_lock.release() #释放线程锁
226
248
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
227
249
  display_data = response[6:-3].decode(errors="ignore")
250
+ buf_clear()
228
251
  # print(f"设备型号: {display_data}")
229
252
  return display_data
230
253
  else:
231
254
  if time.time() - start_time > 3:
232
255
  print("读取超时")
233
- buffer.clear()
256
+ buf_clear()
234
257
  serial_lock.release() #释放线程锁
235
258
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
236
259
  return None
@@ -239,7 +262,7 @@ def read_device_model() -> Optional[bytes]:
239
262
  def read_version() -> Optional[bytes]:
240
263
  serial_lock.acquire() #获取线程锁
241
264
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
242
- write_data(READ_VERSION_H, READ_VERSION_L)
265
+ write_data(READ_VERSION_H, READ_VERSION_L, None, False)
243
266
  start_time = time.time()
244
267
  while True:
245
268
  response =process_received_data()
@@ -247,12 +270,13 @@ def read_version() -> Optional[bytes]:
247
270
  serial_lock.release() #释放线程锁
248
271
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
249
272
  display_data = response[6:-3].decode(errors="ignore")
273
+ buf_clear()
250
274
  # print(f"版本号: {display_data}")
251
275
  return display_data
252
276
  else:
253
277
  if time.time() - start_time > 3:
254
278
  print("读取超时")
255
- buffer.clear()
279
+ buf_clear()
256
280
  serial_lock.release() #释放线程锁
257
281
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
258
282
  return None
@@ -261,7 +285,7 @@ def read_version() -> Optional[bytes]:
261
285
  def read_factory_data() -> Optional[bytes]:
262
286
  serial_lock.acquire() #获取线程锁
263
287
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
264
- write_data(READ_FACTORY_H, READ_FACTORY_L)
288
+ write_data(READ_FACTORY_H, READ_FACTORY_L, None, False)
265
289
  start_time = time.time()
266
290
  while True:
267
291
  response =process_received_data()
@@ -269,12 +293,13 @@ def read_factory_data() -> Optional[bytes]:
269
293
  serial_lock.release() #释放线程锁
270
294
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
271
295
  display_data = response[6:-3].decode(errors="ignore")
296
+ buf_clear()
272
297
  # print(f"厂家信息: {display_data}")
273
298
  return display_data
274
299
  else:
275
300
  if time.time() - start_time > 3:
276
301
  print("读取超时")
277
- buffer.clear()
302
+ buf_clear()
278
303
  serial_lock.release() #释放线程锁
279
304
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
280
305
  return None
@@ -290,7 +315,7 @@ def read_hardware_ID() -> Optional[bytes]:
290
315
  def read_device_name() -> Optional[bytes]:
291
316
  serial_lock.acquire() #获取线程锁
292
317
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
293
- write_data(READ_NAME_H, READ_NAME_L)
318
+ write_data(READ_NAME_H, READ_NAME_L, None, False)
294
319
  start_time = time.time()
295
320
  while True:
296
321
  response =process_received_data()
@@ -298,12 +323,13 @@ def read_device_name() -> Optional[bytes]:
298
323
  serial_lock.release() #释放线程锁
299
324
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
300
325
  display_data = response[6:-3].decode(errors="ignore")
326
+ buf_clear()
301
327
  # print(f"设备名称: {display_data}")
302
328
  return display_data
303
329
  else:
304
330
  if time.time() - start_time > 3:
305
331
  print("读取超时")
306
- buffer.clear()
332
+ buf_clear()
307
333
  serial_lock.release() #释放线程锁
308
334
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
309
335
  return None
@@ -313,7 +339,7 @@ def write_device_name(send_data: str) -> Optional[bytes]:
313
339
  serial_lock.acquire() #获取线程锁
314
340
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
315
341
  data_bytes = send_data.encode('utf-8')
316
- write_data(WRITE_NAME_H, WRITE_NAME_L, data_bytes)
342
+ write_data(WRITE_NAME_H, WRITE_NAME_L, data_bytes, False)
317
343
  start_time = time.time()
318
344
  while True:
319
345
  response =process_received_data()
@@ -321,12 +347,13 @@ def write_device_name(send_data: str) -> Optional[bytes]:
321
347
  serial_lock.release() #释放线程锁
322
348
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
323
349
  display_data = response[6:-3].decode(errors="ignore")
350
+ buf_clear()
324
351
  # print(f"设置状态: {display_data}")
325
352
  return 0
326
353
  else:
327
354
  if time.time() - start_time > 3:
328
355
  print("读取超时")
329
- buffer.clear()
356
+ buf_clear()
330
357
  serial_lock.release() #释放线程锁
331
358
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
332
359
  return None
@@ -335,7 +362,7 @@ def write_device_name(send_data: str) -> Optional[bytes]:
335
362
  def read_connected() -> Optional[bytes]:
336
363
  serial_lock.acquire() #获取线程锁
337
364
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
338
- write_data(READ_CONNECT_H, READ_CONNECT_L)
365
+ write_data(READ_CONNECT_H, READ_CONNECT_L, None, False)
339
366
  start_time = time.time()
340
367
  while True:
341
368
  response =process_received_data()
@@ -343,12 +370,13 @@ def read_connected() -> Optional[bytes]:
343
370
  serial_lock.release() #释放线程锁
344
371
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
345
372
  display_data = response[6:-3].decode(errors="ignore")
373
+ buf_clear()
346
374
  # print(f"连接方式: {display_data}")
347
375
  return display_data
348
376
  else:
349
377
  if time.time() - start_time > 3:
350
378
  print("读取超时")
351
- buffer.clear()
379
+ buf_clear()
352
380
  serial_lock.release() #释放线程锁
353
381
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
354
382
  return None
@@ -367,7 +395,7 @@ def read_battery() -> Optional[bytes]:
367
395
  def read_peripheral() -> Optional[bytes]:
368
396
  serial_lock.acquire() #获取线程锁
369
397
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
370
- write_data(READ_PERIPH_H, READ_PERIPH_L)
398
+ write_data(READ_PERIPH_H, READ_PERIPH_L, None, False)
371
399
  start_time = time.time()
372
400
  while True:
373
401
  response =process_received_data()
@@ -375,6 +403,7 @@ def read_peripheral() -> Optional[bytes]:
375
403
  serial_lock.release() #释放线程锁
376
404
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
377
405
  display_data = response[6:-3]
406
+ buf_clear()
378
407
  # for x in display_data:
379
408
  # print(f"{x:02X}", end=' ')
380
409
  # print("\n")
@@ -382,7 +411,7 @@ def read_peripheral() -> Optional[bytes]:
382
411
  else:
383
412
  if time.time() - start_time > 3:
384
413
  print("读取超时")
385
- buffer.clear()
414
+ buf_clear()
386
415
  serial_lock.release() #释放线程锁
387
416
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
388
417
  return None
@@ -390,8 +419,8 @@ def read_peripheral() -> Optional[bytes]:
390
419
  """单次操作外设"""
391
420
  def single_operate_sensor(op_struct: bytes, block_time: float) -> Optional[bytes]:
392
421
  serial_lock.acquire() #获取线程锁
393
- fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
394
- write_data(SINGLE_OP_H, SINGLE_OP_L, op_struct)
422
+ fcntl.flock(ser.fileno(), fcntl.LOCK_EX) #进程锁,阻塞其他进程
423
+ write_data(SINGLE_OP_H, SINGLE_OP_L, op_struct, False)
395
424
  start_time = time.time()
396
425
  while True:
397
426
  response =process_received_data()
@@ -399,6 +428,7 @@ def single_operate_sensor(op_struct: bytes, block_time: float) -> Optional[bytes
399
428
  serial_lock.release() #释放线程锁
400
429
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
401
430
  display_data = response[6:-3]
431
+ buf_clear()
402
432
  # for x in display_data:
403
433
  # print(f"{x:02X}", end=' ')
404
434
  # print("\n")
@@ -406,7 +436,7 @@ def single_operate_sensor(op_struct: bytes, block_time: float) -> Optional[bytes
406
436
  else:
407
437
  if time.time() - start_time > 2+block_time:
408
438
  print("读取超时")
409
- buffer.clear()
439
+ buf_clear()
410
440
  serial_lock.release() #释放线程锁
411
441
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
412
442
  return None
@@ -416,17 +446,18 @@ def P_port_init(port:bytes) -> Optional[bytes]:
416
446
  servo_str=[0xA0, 0x0F, 0x00, 0xBE]
417
447
  servo_str[0]=0XA0+port
418
448
  time.sleep(0.005)
419
- response = single_operate_sensor(servo_str,0)
420
- if response:
421
- return 0
422
- else:
423
- return None
449
+ # response = single_operate_sensor(servo_str,0)
450
+ write_data(0X01, 0X02, servo_str)
451
+ # if response == None:
452
+ # return None
453
+ # else:
454
+ return 0
424
455
 
425
456
  """从机模式转换"""
426
457
  def mode_change(send_data: str) -> Optional[bytes]:
427
458
  serial_lock.acquire() #获取线程锁
428
459
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
429
- write_data(MODE_CHANGE_H, MODE_CHANGE_L, send_data)
460
+ write_data(MODE_CHANGE_H, MODE_CHANGE_L, send_data, False)
430
461
  start_time = time.time()
431
462
  while True:
432
463
  response =process_received_data()
@@ -450,7 +481,7 @@ def mode_change(send_data: str) -> Optional[bytes]:
450
481
  def mode_change(send_data: str) -> Optional[bytes]:
451
482
  serial_lock.acquire() #获取线程锁
452
483
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
453
- write_data(SEND_CYCLE_H, SEND_CYCLE_L, send_data)
484
+ write_data(SEND_CYCLE_H, SEND_CYCLE_L, send_data, False)
454
485
  start_time = time.time()
455
486
  while True:
456
487
  response =process_received_data()
@@ -502,8 +533,22 @@ def power_button_detec() -> bytes:
502
533
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
503
534
  return 0
504
535
 
505
- def buf_clear():
536
+ def buf_clear():
537
+ #清空Python层缓冲区
506
538
  buffer.clear()
539
+ frames_queue.clear()
540
+ #清空pyserial缓冲区
541
+ try:
542
+ # 尝试先读取所有已到达的数据
543
+ while ser.in_waiting > 0:
544
+ ser.read(ser.in_waiting)
545
+ # 然后重置输入缓冲区
546
+ ser.reset_input_buffer()
547
+ fcntl.ioctl(ser.fileno(), termios.TCIOFLUSH)
548
+ except Exception as e:
549
+ print(f"清空串口缓冲区时出错: {e}")
550
+
551
+ return True
507
552
 
508
553
  """H2-RCU初始化"""
509
554
  def smartpi_init():
@@ -515,6 +560,7 @@ def smartpi_init():
515
560
  serial_lock.acquire() #获取线程锁
516
561
  fcntl.flock(ser.fileno(), fcntl.LOCK_EX) # 进程锁,阻塞其他进程
517
562
  uart3_init()
563
+ serial_lock.release()
518
564
  fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
519
565
 
520
566
  servo.set_init(1)
@@ -560,7 +606,7 @@ def smartpi_init():
560
606
  P_port_init(5)
561
607
  P_port_init(6)
562
608
 
563
- time.sleep(0.1)
564
- serial_lock.release()
565
- time.sleep(0.1)
609
+ if is_lock_locked(serial_lock):
610
+ serial_lock.release() #释放线程锁
611
+ fcntl.flock(ser.fileno(), fcntl.LOCK_UN) # 释放进程锁
566
612
 
Binary file
File without changes
@@ -0,0 +1,30 @@
1
+ {
2
+ "_name_or_path": "thenlper/gte-small-zh",
3
+ "architectures": [
4
+ "BertModel"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "classifier_dropout": null,
8
+ "directionality": "bidi",
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 512,
12
+ "initializer_range": 0.02,
13
+ "intermediate_size": 2048,
14
+ "layer_norm_eps": 1e-12,
15
+ "max_position_embeddings": 512,
16
+ "model_type": "bert",
17
+ "num_attention_heads": 8,
18
+ "num_hidden_layers": 6,
19
+ "pad_token_id": 0,
20
+ "pooler_fc_size": 768,
21
+ "pooler_num_attention_heads": 12,
22
+ "pooler_num_fc_layers": 3,
23
+ "pooler_size_per_head": 128,
24
+ "pooler_type": "first_token_transform",
25
+ "position_embedding_type": "absolute",
26
+ "transformers_version": "4.33.2",
27
+ "type_vocab_size": 2,
28
+ "use_cache": true,
29
+ "vocab_size": 21128
30
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "per_channel": true,
3
+ "reduce_range": true,
4
+ "per_model_config": {
5
+ "model": {
6
+ "op_types": [
7
+ "Erf",
8
+ "Softmax",
9
+ "Shape",
10
+ "Unsqueeze",
11
+ "ReduceMean",
12
+ "Sub",
13
+ "Transpose",
14
+ "Mul",
15
+ "Pow",
16
+ "Cast",
17
+ "Gather",
18
+ "Reshape",
19
+ "MatMul",
20
+ "Slice",
21
+ "Div",
22
+ "Sqrt",
23
+ "Constant",
24
+ "Add",
25
+ "Concat"
26
+ ],
27
+ "weight_type": "QInt8"
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }