test commit
This commit is contained in:
@@ -27,18 +27,27 @@ SCK_PIN = 6 # GPIO6 -> 31번 핀
|
||||
|
||||
# 설정 변수
|
||||
CALIBRATION_FACTOR = -411.17 # 캘리브레이션 팩터
|
||||
measure_delay = 0.1 # 측정 간격 (초)
|
||||
measure_delay = 0.1 # 측정 간격 (초) - 기본 100ms
|
||||
DELAY_STEP = 0.01 # 측정 간격 조절 단위 (10ms)
|
||||
density = 1.0 # 기본값: 물 (1.0 g/ml)
|
||||
|
||||
# 상태 변수
|
||||
is_paused = False
|
||||
is_calibrated = False
|
||||
start_time = 0
|
||||
offset_value = 0
|
||||
|
||||
def set_paused(value):
|
||||
global is_paused
|
||||
is_paused = value
|
||||
|
||||
def set_density(value):
|
||||
global density
|
||||
density = value
|
||||
|
||||
# HX711 인스턴스
|
||||
hx = None
|
||||
|
||||
|
||||
def init_hx711():
|
||||
"""HX711 초기화"""
|
||||
global hx
|
||||
@@ -55,10 +64,9 @@ def init_hx711():
|
||||
print("배선 확인 필요")
|
||||
return False
|
||||
|
||||
|
||||
def perform_calibration():
|
||||
"""캘리브레이션 수행"""
|
||||
global is_calibrated, offset_value
|
||||
global is_calibrated, offset_value, start_time
|
||||
|
||||
if is_calibrated:
|
||||
return
|
||||
@@ -85,7 +93,7 @@ def perform_calibration():
|
||||
print()
|
||||
|
||||
is_calibrated = True
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
def tare():
|
||||
"""영점 조정 함수"""
|
||||
@@ -103,16 +111,14 @@ def tare():
|
||||
offset_value = sum(readings) / len(readings)
|
||||
print(">> 영점 조정 완료!\n")
|
||||
|
||||
|
||||
def get_weight():
|
||||
"""무게 측정 함수"""
|
||||
raw_value = hx.read()
|
||||
weight_g = (raw_value - offset_value) / CALIBRATION_FACTOR
|
||||
|
||||
weight_g = max(0, weight_g)
|
||||
volume_ml = weight_g / density
|
||||
return weight_g, volume_ml
|
||||
|
||||
|
||||
def print_help():
|
||||
"""도움말 출력"""
|
||||
print()
|
||||
@@ -126,14 +132,12 @@ def print_help():
|
||||
print("-" * 50)
|
||||
print()
|
||||
|
||||
|
||||
def check_input():
|
||||
"""비동기 키보드 입력 확인"""
|
||||
if select.select([sys.stdin], [], [], 0)[0]:
|
||||
return sys.stdin.read(1)
|
||||
return None
|
||||
|
||||
|
||||
def process_command(cmd):
|
||||
"""명령어 처리"""
|
||||
global is_paused
|
||||
@@ -171,41 +175,44 @@ def process_command(cmd):
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
global start_time
|
||||
|
||||
if not init_hx711():
|
||||
return
|
||||
|
||||
|
||||
perform_calibration()
|
||||
print_help()
|
||||
|
||||
|
||||
old_settings = termios.tcgetattr(sys.stdin)
|
||||
|
||||
|
||||
try:
|
||||
tty.setcbreak(sys.stdin.fileno())
|
||||
print("명령어 테스트 (q: 종료)...\n")
|
||||
|
||||
print("측정 시작 (종료하려면 'q' 입력)...\n")
|
||||
|
||||
while True:
|
||||
cmd = check_input()
|
||||
if cmd:
|
||||
if not process_command(cmd):
|
||||
break
|
||||
|
||||
|
||||
if is_paused:
|
||||
time.sleep(0.1)
|
||||
continue
|
||||
|
||||
|
||||
weight_g, volume_ml = get_weight()
|
||||
print(f"측정: {weight_g:.1f}g / {volume_ml:.1f}ml")
|
||||
elapsed_time = time.time() - start_time
|
||||
|
||||
# JSON 형식으로 출력
|
||||
print(f'{{"volume_ml(mL)":{volume_ml:.1f},"density":{density},"time":{elapsed_time:.2f},"delay":{int(measure_delay*1000)}}}')
|
||||
time.sleep(measure_delay)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\n>> 프로그램 종료\n")
|
||||
|
||||
|
||||
finally:
|
||||
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
|
||||
print("프로그램 종료됨.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user