|
100 | 100 | _SET_PIN_MODE_CMD = const(0x50)
|
101 | 101 | _SET_DIGITAL_WRITE_CMD = const(0x51)
|
102 | 102 | _SET_ANALOG_WRITE_CMD = const(0x52)
|
| 103 | +_SET_DIGITAL_READ_CMD = const(0x53) |
| 104 | +_SET_ANALOG_READ_CMD = const(0x54) |
103 | 105 |
|
104 | 106 | _START_CMD = const(0xE0)
|
105 | 107 | _END_CMD = const(0xEE)
|
|
131 | 133 | WL_AP_LISTENING = const(7)
|
132 | 134 | WL_AP_CONNECTED = const(8)
|
133 | 135 | WL_AP_FAILED = const(9)
|
| 136 | + |
| 137 | +ADC_ATTEN_DB_0 = const(0) |
| 138 | +ADC_ATTEN_DB_2_5 = const(1) |
| 139 | +ADC_ATTEN_DB_6 = const(2) |
| 140 | +ADC_ATTEN_DB_11 = const(3) |
134 | 141 | # pylint: enable=bad-whitespace
|
135 | 142 |
|
136 | 143 | class ESP_SPIcontrol: # pylint: disable=too-many-public-methods, too-many-instance-attributes
|
@@ -779,6 +786,45 @@ def set_analog_write(self, pin, analog_value):
|
779 | 786 | if resp[0][0] != 1:
|
780 | 787 | raise RuntimeError("Failed to write to pin")
|
781 | 788 |
|
| 789 | + def set_digital_read(self, pin): |
| 790 | + """ |
| 791 | + Get the digital input value of pin. Returns the boolean value of the pin. |
| 792 | +
|
| 793 | + :param int pin: ESP32 GPIO pin to read from. |
| 794 | + """ |
| 795 | + # Verify nina-fw => 1.5.0 |
| 796 | + fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2] |
| 797 | + assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above." |
| 798 | + |
| 799 | + resp = self._send_command_get_response(_SET_DIGITAL_READ_CMD, |
| 800 | + ((pin,),))[0] |
| 801 | + if resp[0] == 0: |
| 802 | + return False |
| 803 | + elif resp[0] == 1: |
| 804 | + return True |
| 805 | + else: |
| 806 | + raise ValueError("_SET_DIGITAL_READ response error: response is not boolean", resp[0]) |
| 807 | + |
| 808 | + def set_analog_read(self, pin, atten=ADC_ATTEN_DB_11): |
| 809 | + """ |
| 810 | + Get the analog input value of pin. Returns an int between 0 and 65536. |
| 811 | +
|
| 812 | + :param int pin: ESP32 GPIO pin to read from. |
| 813 | + :param int atten: attenuation constant |
| 814 | + """ |
| 815 | + # Verify nina-fw => 1.5.0 |
| 816 | + fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2] |
| 817 | + assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above." |
| 818 | + |
| 819 | + resp = self._send_command_get_response(_SET_ANALOG_READ_CMD, |
| 820 | + ((pin,), (atten,))) |
| 821 | + resp_analog = struct.unpack('<i', resp[0]) |
| 822 | + if resp_analog[0] < 0: |
| 823 | + raise ValueError("_SET_ANALOG_READ parameter error: invalid pin", resp_analog[0]) |
| 824 | + if self._debug: |
| 825 | + print(resp, resp_analog, resp_analog[0], 16 * resp_analog[0]) |
| 826 | + return 16 * resp_analog[0] |
| 827 | + |
782 | 828 | def get_time(self):
|
783 | 829 | """The current unix timestamp"""
|
784 | 830 | if self.status == WL_CONNECTED:
|
|
0 commit comments