From d62a4456b5b02372d51c75a839b8d659c5c77e07 Mon Sep 17 00:00:00 2001 From: Bill Sideris Date: Wed, 5 Jun 2024 23:51:20 +0300 Subject: [PATCH] adcv#GPIO in shell --- source/be.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/source/be.py b/source/be.py index a3c2d3f0..ed1b1890 100644 --- a/source/be.py +++ b/source/be.py @@ -697,6 +697,38 @@ def xarg(rinpt: str = None, fn: bool = False) -> dict: allwords.append(temp_s) s = False inpt[i] = inpt[i][inpt[i].find('"') + 1 :] + elif inpt[i].startswith("adcv#") and ( + inpt[i][5:] in be.devices["gpiochip"][0].pins + ): + if not s: + pin_name = inpt[i][5:] + if pin_name not in pv[0]["digitalio_store"]: + if be.devices["gpiochip"][0].is_free(pin_name): + tmp_gpio = be.devices["gpiochip"][0].adc(pin_name) + inpt[i] = str( + tmp_gpio.value + * (tmp_gpio.reference_voltage / 65535) + ) + tmp_gpio.deinit() + else: + term.write("Could not allocate GPIO " + pin_name) + else: + # Can read digitalio, ignore + inpt[i] = str(pv[0]["digitalio_store"][pin_name].value) + elif inpt[i].endswith('"'): + temp_s += be.api.adv_input(inpt[i][:-1]) + words.append(temp_s) + allwords.append(temp_s) + s = False + elif '"' not in inpt[i]: + temp_s += " " + be.api.adv_input(inpt[i][1:]) + continue + else: + temp_s += " " + be.api.adv_input(inpt[i][1 : inpt[i].find('"')]) + words.append(temp_s) + allwords.append(temp_s) + s = False + inpt[i] = inpt[i][inpt[i].find('"') + 1 :] elif (not s) and (not mw) and inpt[i].startswith('"$'): if inpt[i].endswith('"'): inpt[i] = be.api.adv_input(inpt[i][2:-1]) @@ -1507,6 +1539,7 @@ def var(inpt: str): # variables setter / editor or inpt[2].startswith("/") or inpt[2].startswith("gp#") or inpt[2].startswith("adc#") + or inpt[2].startswith("adcv#") or inpt[2] in vr("digitalio_store", pid=0) ): valid = False @@ -1530,13 +1563,20 @@ def var(inpt: str): # variables setter / editor tmp_gpio = be.devices["gpiochip"][0].input(pin_name) new_var += str(tmp_gpio.value) tmp_gpio.deinit() - elif inpt[2].startswith("adc#"): # gpio read + elif inpt[2].startswith("adc#"): # adc read pin_name = inpt[2][4:] if be.devices["gpiochip"][0].is_free(pin_name): tmp_gpio = be.devices["gpiochip"][0].adc(pin_name) if tmp_gpio is not None: # ADC2 may fail on ESP32 new_var += str(tmp_gpio.value) tmp_gpio.deinit() + elif inpt[2].startswith("adcv#"): # adc voltage read + pin_name = inpt[2][5:] + if be.devices["gpiochip"][0].is_free(pin_name): + tmp_gpio = be.devices["gpiochip"][0].adc(pin_name) + if tmp_gpio is not None: # ADC2 may fail on ESP32 + new_var += str(tmp_gpio.value * (3.3 / 65535)) + tmp_gpio.deinit() else: new_var += str(inpt[2]) if valid: # now do the actual set