smartpi 1.1.4__py3-none-any.whl → 1.1.5__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.
Files changed (125) hide show
  1. smartpi/__init__.py +8 -0
  2. smartpi/__init__.pyc +0 -0
  3. smartpi/_gui.py +66 -0
  4. smartpi/_gui.pyc +0 -0
  5. smartpi/ai_asr.py +1037 -0
  6. smartpi/ai_asr.pyc +0 -0
  7. smartpi/ai_llm.py +934 -0
  8. smartpi/ai_llm.pyc +0 -0
  9. smartpi/ai_tts.py +938 -0
  10. smartpi/ai_tts.pyc +0 -0
  11. smartpi/ai_vad.py +83 -0
  12. smartpi/ai_vad.pyc +0 -0
  13. smartpi/audio.py +125 -0
  14. smartpi/audio.pyc +0 -0
  15. smartpi/base_driver.py +618 -0
  16. smartpi/base_driver.pyc +0 -0
  17. smartpi/camera.py +84 -0
  18. smartpi/camera.pyc +0 -0
  19. smartpi/color_sensor.py +18 -0
  20. smartpi/color_sensor.pyc +0 -0
  21. smartpi/cw2015.py +179 -0
  22. smartpi/cw2015.pyc +0 -0
  23. smartpi/flash.py +130 -0
  24. smartpi/flash.pyc +0 -0
  25. smartpi/humidity.py +20 -0
  26. smartpi/humidity.pyc +0 -0
  27. smartpi/led.py +19 -0
  28. smartpi/led.pyc +0 -0
  29. smartpi/light_sensor.py +72 -0
  30. smartpi/light_sensor.pyc +0 -0
  31. smartpi/local_model.py +432 -0
  32. smartpi/local_model.pyc +0 -0
  33. smartpi/mcp_client.py +100 -0
  34. smartpi/mcp_client.pyc +0 -0
  35. smartpi/mcp_fastmcp.py +322 -0
  36. smartpi/mcp_fastmcp.pyc +0 -0
  37. smartpi/mcp_intent_recognizer.py +408 -0
  38. smartpi/mcp_intent_recognizer.pyc +0 -0
  39. smartpi/models/__init__.py +0 -0
  40. smartpi/models/__init__.pyc +0 -0
  41. smartpi/models/snakers4_silero-vad/__init__.py +0 -0
  42. smartpi/models/snakers4_silero-vad/__init__.pyc +0 -0
  43. smartpi/models/snakers4_silero-vad/hubconf.py +56 -0
  44. smartpi/models/snakers4_silero-vad/hubconf.pyc +0 -0
  45. smartpi/motor.py +177 -0
  46. smartpi/motor.pyc +0 -0
  47. smartpi/move.py +218 -0
  48. smartpi/move.pyc +0 -0
  49. smartpi/onnx_hand_workflow.py +201 -0
  50. smartpi/onnx_hand_workflow.pyc +0 -0
  51. smartpi/onnx_image_workflow.py +176 -0
  52. smartpi/onnx_image_workflow.pyc +0 -0
  53. smartpi/onnx_pose_workflow.py +482 -0
  54. smartpi/onnx_pose_workflow.pyc +0 -0
  55. smartpi/onnx_text_workflow.py +173 -0
  56. smartpi/onnx_text_workflow.pyc +0 -0
  57. smartpi/onnx_voice_workflow.py +437 -0
  58. smartpi/onnx_voice_workflow.pyc +0 -0
  59. smartpi/posemodel/__init__.py +0 -0
  60. smartpi/posemodel/__init__.pyc +0 -0
  61. smartpi/posenet_utils.py +222 -0
  62. smartpi/posenet_utils.pyc +0 -0
  63. smartpi/rknn_hand_workflow.py +245 -0
  64. smartpi/rknn_hand_workflow.pyc +0 -0
  65. smartpi/rknn_image_workflow.py +405 -0
  66. smartpi/rknn_image_workflow.pyc +0 -0
  67. smartpi/rknn_pose_workflow.py +592 -0
  68. smartpi/rknn_pose_workflow.pyc +0 -0
  69. smartpi/rknn_text_workflow.py +240 -0
  70. smartpi/rknn_text_workflow.pyc +0 -0
  71. smartpi/rknn_voice_workflow.py +394 -0
  72. smartpi/rknn_voice_workflow.pyc +0 -0
  73. smartpi/servo.py +178 -0
  74. smartpi/servo.pyc +0 -0
  75. smartpi/temperature.py +18 -0
  76. smartpi/temperature.pyc +0 -0
  77. smartpi/tencentcloud-speech-sdk-python/__init__.py +1 -0
  78. smartpi/tencentcloud-speech-sdk-python/__init__.pyc +0 -0
  79. smartpi/tencentcloud-speech-sdk-python/asr/__init__.py +0 -0
  80. smartpi/tencentcloud-speech-sdk-python/asr/__init__.pyc +0 -0
  81. smartpi/tencentcloud-speech-sdk-python/asr/flash_recognizer.py +178 -0
  82. smartpi/tencentcloud-speech-sdk-python/asr/flash_recognizer.pyc +0 -0
  83. smartpi/tencentcloud-speech-sdk-python/asr/speech_recognizer.py +311 -0
  84. smartpi/tencentcloud-speech-sdk-python/asr/speech_recognizer.pyc +0 -0
  85. smartpi/tencentcloud-speech-sdk-python/common/__init__.py +1 -0
  86. smartpi/tencentcloud-speech-sdk-python/common/__init__.pyc +0 -0
  87. smartpi/tencentcloud-speech-sdk-python/common/credential.py +6 -0
  88. smartpi/tencentcloud-speech-sdk-python/common/credential.pyc +0 -0
  89. smartpi/tencentcloud-speech-sdk-python/common/log.py +16 -0
  90. smartpi/tencentcloud-speech-sdk-python/common/log.pyc +0 -0
  91. smartpi/tencentcloud-speech-sdk-python/common/utils.py +7 -0
  92. smartpi/tencentcloud-speech-sdk-python/common/utils.pyc +0 -0
  93. smartpi/tencentcloud-speech-sdk-python/soe/__init__.py +0 -0
  94. smartpi/tencentcloud-speech-sdk-python/soe/__init__.pyc +0 -0
  95. smartpi/tencentcloud-speech-sdk-python/soe/speaking_assessment.py +276 -0
  96. smartpi/tencentcloud-speech-sdk-python/soe/speaking_assessment.pyc +0 -0
  97. smartpi/tencentcloud-speech-sdk-python/tts/__init__.py +0 -0
  98. smartpi/tencentcloud-speech-sdk-python/tts/__init__.pyc +0 -0
  99. smartpi/tencentcloud-speech-sdk-python/tts/flowing_speech_synthesizer.py +294 -0
  100. smartpi/tencentcloud-speech-sdk-python/tts/flowing_speech_synthesizer.pyc +0 -0
  101. smartpi/tencentcloud-speech-sdk-python/tts/speech_synthesizer.py +144 -0
  102. smartpi/tencentcloud-speech-sdk-python/tts/speech_synthesizer.pyc +0 -0
  103. smartpi/tencentcloud-speech-sdk-python/tts/speech_synthesizer_ws.py +234 -0
  104. smartpi/tencentcloud-speech-sdk-python/tts/speech_synthesizer_ws.pyc +0 -0
  105. smartpi/tencentcloud-speech-sdk-python/vc/__init__.py +0 -0
  106. smartpi/tencentcloud-speech-sdk-python/vc/__init__.pyc +0 -0
  107. smartpi/tencentcloud-speech-sdk-python/vc/speech_convertor_ws.py +237 -0
  108. smartpi/tencentcloud-speech-sdk-python/vc/speech_convertor_ws.pyc +0 -0
  109. smartpi/text_gte_model/__init__.py +0 -0
  110. smartpi/text_gte_model/__init__.pyc +0 -0
  111. smartpi/text_gte_model/config/__init__.py +0 -0
  112. smartpi/text_gte_model/config/__init__.pyc +0 -0
  113. smartpi/text_gte_model/gte/__init__.py +0 -0
  114. smartpi/text_gte_model/gte/__init__.pyc +0 -0
  115. smartpi/touch_sensor.py +16 -0
  116. smartpi/touch_sensor.pyc +0 -0
  117. smartpi/trace.py +120 -0
  118. smartpi/trace.pyc +0 -0
  119. smartpi/ultrasonic.py +20 -0
  120. smartpi/ultrasonic.pyc +0 -0
  121. {smartpi-1.1.4.dist-info → smartpi-1.1.5.dist-info}/METADATA +3 -2
  122. smartpi-1.1.5.dist-info/RECORD +137 -0
  123. smartpi-1.1.4.dist-info/RECORD +0 -77
  124. {smartpi-1.1.4.dist-info → smartpi-1.1.5.dist-info}/WHEEL +0 -0
  125. {smartpi-1.1.4.dist-info → smartpi-1.1.5.dist-info}/top_level.txt +0 -0
@@ -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
+ #颜色读取 正常返回值:1-红色;2-绿色;3-蓝色;4-黄色;5-黑色;6-白色; 读取错误:-1
7
+ def get_value(port:bytes) -> Optional[bytes]:
8
+ color_str=[0xA0, 0x04, 0x00, 0xBE]
9
+ color_str[0]=0XA0+port
10
+ color_str[2]=1
11
+ time.sleep(0.005)
12
+ response = base_driver.single_operate_sensor(color_str,0)
13
+ if response == None:
14
+ return None
15
+ else:
16
+ return response[4]
17
+
18
+
smartpi/color_sensor.pyc CHANGED
Binary file
smartpi/cw2015.py ADDED
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env python3
2
+ import os
3
+ import sys
4
+ import fcntl
5
+ import time
6
+
7
+ # I2C设备文件路径
8
+ I2C_DEV_PATH = "/dev/i2c-2" # 根据实际情况修改总线号
9
+
10
+ # I2C从设备地址
11
+ CW2015_ADDR = 0x62 # 7位地址
12
+
13
+ # I2C通信常量
14
+ I2C_SLAVE = 0x0703
15
+ I2C_SMBUS = 0x0720 # SMBus传输
16
+
17
+ # SMBus命令结构
18
+ class i2c_smbus_ioctl_data:
19
+ def __init__(self, read_write, command, size, data):
20
+ self.read_write = read_write # 0 = write, 1 = read
21
+ self.command = command # 寄存器地址
22
+ self.size = size # 数据大小
23
+ self.data = data # 数据指针
24
+
25
+ # 寄存器地址定义
26
+ VERSION = 0x00
27
+ VCELL_H = 0x02
28
+ VCELL_L = 0x03
29
+ SOC_B = 0x04
30
+ SOC = 0x05
31
+ RRT_H = 0x06
32
+ RRT_L = 0x07
33
+ CONFIG = 0x08
34
+ MOOD = 0x0A
35
+
36
+ class I2CDevice:
37
+ """通过设备文件直接访问I2C设备"""
38
+ def __init__(self, device_path, device_addr):
39
+ """
40
+ 初始化I2C设备
41
+ :param device_path: I2C设备文件路径 (例如 "/dev/i2c-2")
42
+ :param device_addr: I2C从设备地址 (7位地址)
43
+ """
44
+ self.device_path = device_path
45
+ self.device_addr = device_addr
46
+ self.fd = None
47
+
48
+ try:
49
+ # 打开设备文件
50
+ self.fd = os.open(device_path, os.O_RDWR)
51
+ # 设置从设备地址
52
+ fcntl.ioctl(self.fd, I2C_SLAVE, device_addr)
53
+ #print(f"成功打开 {device_path} 并设置地址 0x{device_addr:02X}")
54
+ except Exception as e:
55
+ print(f"打开I2C设备失败: {e}")
56
+ if self.fd:
57
+ os.close(self.fd)
58
+ sys.exit(1)
59
+
60
+ def __del__(self):
61
+ """关闭设备文件"""
62
+ if self.fd:
63
+ os.close(self.fd)
64
+
65
+ def write_byte(self, reg_addr, value):
66
+ """
67
+ 向寄存器写入一个字节
68
+ :param reg_addr: 寄存器地址
69
+ :param value: 要写入的值
70
+ :return: 成功返回True,失败返回False
71
+ """
72
+ try:
73
+ # 构造写入数据:寄存器地址 + 值
74
+ data = bytes([reg_addr, value])
75
+ os.write(self.fd, data)
76
+ return True
77
+ except Exception as e:
78
+ print(f"写入寄存器0x{reg_addr:02X}失败: {e}")
79
+ return False
80
+
81
+ def read_byte(self, reg_addr):
82
+ """
83
+ 从寄存器读取一个字节
84
+ :param reg_addr: 寄存器地址
85
+ :return: 读取到的字节值,失败返回0
86
+ """
87
+ try:
88
+ # 先写入寄存器地址
89
+ os.write(self.fd, bytes([reg_addr]))
90
+ # 然后读取一个字节
91
+ value = os.read(self.fd, 1)
92
+ return value[0] if value else 0
93
+ except Exception as e:
94
+ print(f"读取寄存器0x{reg_addr:02X}失败: {e}")
95
+ return 0
96
+
97
+ def read_word(self, reg_addr):
98
+ """
99
+ 从寄存器读取两个字节(先高字节后低字节)
100
+ :param reg_addr: 寄存器地址(高字节寄存器)
101
+ :return: (高字节 << 8) | 低字节
102
+ """
103
+ try:
104
+ # 先写入寄存器地址
105
+ os.write(self.fd, bytes([reg_addr]))
106
+ # 读取两个字节
107
+ data = os.read(self.fd, 2)
108
+ if len(data) == 2:
109
+ return (data[0] << 8) | data[1]
110
+ else:
111
+ return 0
112
+ except Exception as e:
113
+ print(f"读取寄存器0x{reg_addr:02X}失败: {e}")
114
+ return 0
115
+
116
+ class CW2015:
117
+ """CW2015电池监测芯片驱动"""
118
+ def __init__(self, i2c_dev_path=I2C_DEV_PATH, i2c_addr=CW2015_ADDR):
119
+ """
120
+ 初始化CW2015
121
+ :param i2c_dev_path: I2C设备路径
122
+ :param i2c_addr: I2C地址
123
+ """
124
+ self.device = I2CDevice(i2c_dev_path, i2c_addr)
125
+
126
+ def init(self):
127
+ """初始化芯片配置"""
128
+ # 写入配置
129
+ if not self.device.write_byte(CONFIG, 0x50):
130
+ print("配置寄存器写入失败")
131
+ return False
132
+ if not self.device.write_byte(MOOD, 0x00):
133
+ print("模式寄存器写入失败")
134
+ return False
135
+
136
+ time.sleep(0.05) # 50ms延时
137
+ #print("CW2015初始化完成")
138
+ return True
139
+
140
+ def get_id(self):
141
+ """获取芯片ID"""
142
+ return self.device.read_byte(VERSION)
143
+
144
+ def get_voltage(self):
145
+ """读取电池电压(mV)"""
146
+ # 读取电压高字节寄存器
147
+ vh = self.device.read_byte(VCELL_H)
148
+ # 读取电压低字节寄存器
149
+ vl = self.device.read_byte(VCELL_L)
150
+
151
+ # 组合14位ADC值
152
+ adc_value = ((vh & 0x3F) << 8) | vl
153
+ # 转换为电压值 (305μV/LSB)
154
+ voltage = adc_value * 305 / 1000
155
+ return int(voltage)
156
+
157
+ def get_soc(self, mode=0):
158
+ """读取电池剩余电量"""
159
+ if mode == 0:
160
+ return self.device.read_byte(SOC_B) # 百分比整数
161
+ else:
162
+ return self.device.read_byte(SOC) # 高精度值
163
+
164
+ def get_remaining_time(self):
165
+ """获取剩余工作时间(分钟)"""
166
+ # 读取时间高字节寄存器
167
+ rh = self.device.read_byte(RRT_H)
168
+ # 读取时间低字节寄存器
169
+ rl = self.device.read_byte(RRT_L)
170
+
171
+ # 提取剩余时间(13位)
172
+ remaining_time = ((rh & 0x1F) << 8) | rl
173
+ return remaining_time
174
+
175
+ def get_alert_status(self):
176
+ """获取告警状态"""
177
+ rh = self.device.read_byte(RRT_H)
178
+ return (rh >> 7) & 0x01
179
+
smartpi/cw2015.pyc CHANGED
Binary file
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 = 120 # 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/flash.pyc CHANGED
Binary file
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端口;正常返回:湿度数据; 读取错误:-1
8
+ def get_value(port:bytes) -> Optional[bytes]:
9
+ humi_str=[0XA0, 0X0C, 0X01, 0X71, 0X00, 0XBE]
10
+ humi_str[0]=0XA0+port
11
+ humi_str[4]=0X01
12
+ time.sleep(0.005)
13
+ response = base_driver.single_operate_sensor(humi_str,0)
14
+ if response == None:
15
+ return None
16
+ else:
17
+ return response[4]
18
+
19
+
20
+
smartpi/humidity.pyc CHANGED
Binary file
smartpi/led.py ADDED
@@ -0,0 +1,19 @@
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:白; 正常返回:0; 读取错误:-1
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,0)
13
+ time.sleep(0.005)
14
+ base_driver.write_data(0X01, 0X02, color_lamp_str)
15
+ # if response == None:
16
+ # return None
17
+ # else:
18
+ return 0
19
+
smartpi/led.pyc CHANGED
Binary file
@@ -0,0 +1,72 @@
1
+ # coding=utf-8
2
+ import time
3
+ from typing import List, Optional
4
+ from smartpi import base_driver
5
+
6
+ #光电光值读取 port:连接P端口; 正常返回:光值数据; 读取错误:-1
7
+ def turn_off(port:bytes) -> Optional[bytes]:
8
+ light_str=[0xA0, 0x02, 0x00, 0xBE]
9
+ light_str[0]=0XA0+port
10
+ light_str[2]=0x03
11
+ # response = base_driver.single_operate_sensor(light_str,0)
12
+ base_driver.write_data(0X01, 0X02, light_str)
13
+ # if response == None:
14
+ # return None
15
+ # else:
16
+ return 0
17
+
18
+ #光电光值读取 port:连接P端口; 正常返回:光值数据; 读取错误:-1
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]=0x01
23
+ time.sleep(0.005)
24
+ response = base_driver.single_operate_sensor(light_str,0)
25
+ if response == None:
26
+ return None
27
+ else:
28
+ light_data=response[4:-1]
29
+ light_num=int.from_bytes(light_data, byteorder='big', signed=True)
30
+ return light_num
31
+
32
+ #光电阈值设置 port:连接P端口; threshold:设置的阈值0~4000
33
+ def set_threshold(port:bytes,threshold:int) -> Optional[bytes]:
34
+ light_str=[0xA0, 0x02, 0x00, 0x81, 0x00, 0x00, 0xBE]
35
+ light_str[0]=0XA0+port
36
+ light_str[2]=0x04
37
+ light_str[4]=threshold//256
38
+ light_str[5]=threshold%256
39
+ time.sleep(0.005)
40
+ base_driver.write_data(0X01, 0X02, light_str)
41
+ # response = base_driver.single_operate_sensor(light_str,0)
42
+ # if response == None:
43
+ # return None
44
+ # else:
45
+ return 0
46
+
47
+ #光电阈值读取 port:连接P端口;
48
+ def get_threshold(port:bytes) -> Optional[bytes]:
49
+ light_str=[0xA0, 0x02, 0x00, 0xBE]
50
+ light_str[0]=0XA0+port
51
+ light_str[2]=0x05
52
+ time.sleep(0.005)
53
+ response = base_driver.single_operate_sensor(light_str,0)
54
+ if response == None:
55
+ return None
56
+ else:
57
+ light_data=response[4:-1]
58
+ light_num=int.from_bytes(light_data, byteorder='big', signed=True)
59
+ return light_num
60
+
61
+ #光电读取当前值和设定阈值比较后的bool值 port:连接P端口;
62
+ def get_bool_data(port:bytes) -> Optional[bytes]:
63
+ light_str=[0xA0, 0x02, 0x00, 0xBE]
64
+ light_str[0]=0XA0+port
65
+ light_str[2]=0x06
66
+ time.sleep(0.005)
67
+ response = base_driver.single_operate_sensor(light_str,0)
68
+ if response == None:
69
+ return None
70
+ else:
71
+ return response[4]
72
+
smartpi/light_sensor.pyc CHANGED
Binary file