# 계획
# Sample, cantilever, vision settting, defocus(범위, 단계)
# Sample: 측정할 시료 (가까우면 샘플의 색이 배경에 포함됨) (샘플로 부터 2000um)
# - Air:허공, Rubber: 어두운 색 비반사, Wafer: 밝은 반사
# - Cantilever: group 1 중 RAT 제공
# - vistion: Auto Exposure ON, Contrast 10%, LED [-20, 0, 20] (Cantilever Texture 가 보이는 지점을 0으로)
# - defocus: -200~200, 10um (라벨링은 -100~100)

import numpy as np
from SmartRemote import SmartRemote as SR 
import os
import afm2
from tqdm import tqdm



PATH = 'data//0327'  # data 폴더
cantliever = 'PPP-NCHAu'  # cantilever 이름 -> /를 파일경로로 인식해서 NSC36/Cr-Au 같은 경우 NSC36_Cr-Au로 표현, Scout 350 RAI 같은 경우 Scout350RAI
location = 'air'  # location 이름

target_path = os.path.join(os.getcwd(), PATH, f'{cantliever}_{location}')  # data 폴더 내에 해당 cantilever 이름, location에 따라 폴더 생성
print(target_path)
os.makedirs(target_path, exist_ok=True)


on_focus_f = afm2.get_focus_pos()  # 현재 Onfocus 상황의 Focus 값이 들어감
delta_light_list = [-20, 0, 20]  # Defalut 값을 기준으로 -20, +20
default_light = 40  # Light 값의 Defalut


# 캡처 반복 수행
for delta_f in tqdm(np.linspace(-200, 200, 41).astype(int)):  # -200um부터 200um까지 10씩 증가 
    afm2.move_to_1d_motion('f', on_focus_f + delta_f)  # Onfocus 상황의 Focus 값에 -200um부터 시작해서 10씩 더해준 값으로 이동

    for delta_light in delta_light_list:  # light 리스트를 반복해서 수행
        afm2.set_vision_config(light=default_light + delta_light, exposure=0, contrast=10, autoExposure=True, autoContrast=False, autoWb=True)  # Vision Setting # light=default_light + delta_light, exposure=0, contrast=10, autoExposure=True, autoContrast=False, autoWb=True
        afm2.sleep(1000)  # Vision Setting 설정이 적용되는 시간 고려
        path_name = f"{cantliever}_defocus_{on_focus_f + delta_f}_led_{default_light + delta_light}_location_{location}.jpg"  # cantilever 이름, location을 이용하여 이미지 이름 생성
        path_name = os.path.join(target_path, path_name)  # 폴더 위치 + 이미지 이름
        afm2.capture_image(path_name)  # 이미지 캡처 수행

# 원래 포커스 위치로 이동
afm2.move_to_1d_motion('f', on_focus_f)