add lock for external resources

This commit is contained in:
Brendan Haines 2020-03-02 16:24:01 -07:00
parent e5e1fb54a3
commit 34abba0129

View File

@ -29,14 +29,20 @@ adc_mux = AnalogIn(adc, ADS.P0)
white_cal = [0]*8 white_cal = [0]*8
black_cal = [5]*8 black_cal = [5]*8
adc_lock = threading.Lock()
def get_reflectivity(chan): def get_reflectivity(chan):
chan = int(chan) chan = int(chan)
global mux_io global mux_io
global adc_mux global adc_mux
global adc_lock
mux = 1-np.array(list(f"{chan:04b}"), dtype=int) mux = 1-np.array(list(f"{chan:04b}"), dtype=int)
adc_lock.acquire()
for ii, io in enumerate(mux_io): for ii, io in enumerate(mux_io):
io.value = mux[ii] io.value = mux[ii]
return adc_mux.voltage voltage = adc_mux.voltage
adc_lock.release()
return voltage
def get_normalized_reflectivity(chan): def get_normalized_reflectivity(chan):
global white_cal global white_cal
@ -60,16 +66,16 @@ def update_plot(attrname=None, old=None, new=None):
brightness = [get_normalized_reflectivity(c) for c in range(8)] brightness = [get_normalized_reflectivity(c) for c in range(8)]
global plt_data global plt_data
plt_data = dict(x=brightness_idx, y=brightness) plt_data = dict(x=brightness_idx, y=brightness)
plt_source.data = plt_data # plt_source.data = plt_data
def cal_white(attrname=None, old=None, new=None): def cal_white(attrname=None, old=None, new=None):
global white_cal global white_cal
white_cal = brightness.copy() white_cal = [get_reflectivity(c) for c in range(8)]
update_plot() update_plot()
def cal_black(attrname=None, old=None, new=None): def cal_black(attrname=None, old=None, new=None):
global black_cal global black_cal
black_cal = brightness.copy() black_cal = [get_reflectivity(c) for c in range(8)]
update_plot() update_plot()
cal_white_button = Button(label="Cal White") cal_white_button = Button(label="Cal White")