Skip to content

Commit 7bf9f3c

Browse files
authored
Merge branch 'adafruit:main' into patch-1
2 parents 88d03e9 + 35cebc9 commit 7bf9f3c

File tree

6 files changed

+203
-26
lines changed

6 files changed

+203
-26
lines changed

.gitignore

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
1-
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2022 Kattni Rembor, written for Adafruit Industries
22
#
3-
# SPDX-License-Identifier: Unlicense
3+
# SPDX-License-Identifier: MIT
44

5+
# Do not include files and directories created by your personal work environment, such as the IDE
6+
# you use, except for those already listed here. Pull requests including changes to this file will
7+
# not be accepted.
8+
9+
# This .gitignore file contains rules for files generated by working with CircuitPython libraries,
10+
# including building Sphinx, testing with pip, and creating a virual environment, as well as the
11+
# MacOS and IDE-specific files generated by using MacOS in general, or the PyCharm or VSCode IDEs.
12+
13+
# If you find that there are files being generated on your machine that should not be included in
14+
# your git commit, you should create a .gitignore_global file on your computer to include the
15+
# files created by your personal setup. To do so, follow the two steps below.
16+
17+
# First, create a file called .gitignore_global somewhere convenient for you, and add rules for
18+
# the files you want to exclude from git commits.
19+
20+
# Second, configure Git to use the exclude file for all Git repositories by running the
21+
# following via commandline, replacing "path/to/your/" with the actual path to your newly created
22+
# .gitignore_global file:
23+
# git config --global core.excludesfile path/to/your/.gitignore_global
24+
25+
# CircuitPython-specific files
526
*.mpy
6-
.idea
27+
28+
# Python-specific files
729
__pycache__
8-
_build
930
*.pyc
31+
32+
# Sphinx build-specific files
33+
_build
34+
35+
# This file results from running `pip -e install .` in a local repository
36+
*.egg-info
37+
38+
# Virtual environment-specific files
1039
.env
11-
bundles
40+
41+
# MacOS-specific files
1242
*.DS_Store
13-
.eggs
14-
dist
15-
**/*.egg-info
43+
44+
# IDE-specific files
45+
.idea
46+
.vscode
47+
*~

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
repos:
66
- repo: https://github.com/python/black
7-
rev: 20.8b1
7+
rev: 22.3.0
88
hooks:
99
- id: black
1010
- repo: https://github.com/fsfe/reuse-tool

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Introduction
55
:target: https://docs.circuitpython.org/projects/esp32spi/en/latest/
66
:alt: Documentation Status
77

8-
.. image:: https://img.shields.io/discord/327254708534116352.svg
8+
.. image:: https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_Bundle/main/badges/adafruit_discord.svg
99
:target: https://adafru.it/discord
1010
:alt: Discord
1111

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@
2929
import struct
3030
import time
3131
from micropython import const
32-
from digitalio import Direction
3332
from adafruit_bus_device.spi_device import SPIDevice
33+
from digitalio import Direction
3434

3535
__version__ = "0.0.0-auto.0"
3636
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI.git"
3737

3838
_SET_NET_CMD = const(0x10)
3939
_SET_PASSPHRASE_CMD = const(0x11)
40+
_SET_IP_CONFIG = const(0x14)
41+
_SET_DNS_CONFIG = const(0x15)
42+
_SET_HOSTNAME = const(0x16)
4043
_SET_AP_NET_CMD = const(0x18)
4144
_SET_AP_PASSPHRASE_CMD = const(0x19)
4245
_SET_DEBUG_CMD = const(0x1A)
@@ -67,6 +70,7 @@
6770
_START_SCAN_NETWORKS = const(0x36)
6871
_GET_FW_VERSION_CMD = const(0x37)
6972
_SEND_UDP_DATA_CMD = const(0x39)
73+
_GET_REMOTE_DATA_CMD = const(0x3A)
7074
_GET_TIME = const(0x3B)
7175
_GET_IDX_BSSID_CMD = const(0x3C)
7276
_GET_IDX_CHAN_CMD = const(0x3D)
@@ -124,6 +128,8 @@
124128
ADC_ATTEN_DB_6 = const(2)
125129
ADC_ATTEN_DB_11 = const(3)
126130

131+
# pylint: disable=too-many-lines
132+
127133

128134
class ESP_SPIcontrol: # pylint: disable=too-many-public-methods, too-many-instance-attributes
129135
"""A class that will talk to an ESP32 module programmed with special firmware
@@ -405,6 +411,46 @@ def scan_networks(self):
405411
return APs
406412
return None
407413

414+
def set_ip_config(self, ip_address, gateway, mask="255.255.255.0"):
415+
"""Tells the ESP32 to set ip, gateway and network mask b"\xFF"
416+
417+
:param str ip_address: IP address (as a string).
418+
:param str gateway: Gateway (as a string).
419+
:param str mask: Mask, defaults to 255.255.255.0 (as a string).
420+
"""
421+
resp = self._send_command_get_response(
422+
_SET_IP_CONFIG,
423+
params=[
424+
b"\x00",
425+
self.unpretty_ip(ip_address),
426+
self.unpretty_ip(gateway),
427+
self.unpretty_ip(mask),
428+
],
429+
sent_param_len_16=False,
430+
)
431+
return resp
432+
433+
def set_dns_config(self, dns1, dns2):
434+
"""Tells the ESP32 to set DNS
435+
436+
:param str dns1: DNS server 1 IP as a string.
437+
:param str dns2: DNS server 2 IP as a string.
438+
"""
439+
resp = self._send_command_get_response(
440+
_SET_DNS_CONFIG, [b"\x00", self.unpretty_ip(dns1), self.unpretty_ip(dns2)]
441+
)
442+
if resp[0][0] != 1:
443+
raise RuntimeError("Failed to set dns with esp32")
444+
445+
def set_hostname(self, hostname):
446+
"""Tells the ESP32 to set hostname for DHCP.
447+
448+
:param str hostname: The new host name.
449+
"""
450+
resp = self._send_command_get_response(_SET_HOSTNAME, [hostname.encode()])
451+
if resp[0][0] != 1:
452+
raise RuntimeError("Failed to set hostname with esp32")
453+
408454
def wifi_set_network(self, ssid):
409455
"""Tells the ESP32 to set the access point to the given ssid"""
410456
resp = self._send_command_get_response(_SET_NET_CMD, [ssid])
@@ -518,8 +564,7 @@ def connect(self, secrets):
518564
self.connect_AP(secrets["ssid"], secrets["password"])
519565

520566
def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-name
521-
"""
522-
Connect to an access point with given name and password.
567+
"""Connect to an access point with given name and password.
523568
Will wait until specified timeout seconds and return on success
524569
or raise an exception on failure.
525570
@@ -552,8 +597,7 @@ def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-n
552597
def create_AP(
553598
self, ssid, password, channel=1, timeout=10
554599
): # pylint: disable=invalid-name
555-
"""
556-
Create an access point with the given name, password, and channel.
600+
"""Create an access point with the given name, password, and channel.
557601
Will wait until specified timeout seconds and return on success
558602
or raise an exception on failure.
559603
@@ -806,6 +850,14 @@ def server_state(self, socket_num):
806850
resp = self._send_command_get_response(_GET_STATE_TCP_CMD, self._socknum_ll)
807851
return resp[0][0]
808852

853+
def get_remote_data(self, socket_num):
854+
"""Get the IP address and port of the remote host"""
855+
self._socknum_ll[0][0] = socket_num
856+
resp = self._send_command_get_response(
857+
_GET_REMOTE_DATA_CMD, self._socknum_ll, reply_params=2
858+
)
859+
return {"ip_addr": resp[0], "port": struct.unpack("<H", resp[1])[0]}
860+
809861
def set_esp_debug(self, enabled):
810862
"""Enable/disable debug mode on the ESP32. Debug messages will be
811863
written to the ESP32's UART."""
@@ -814,8 +866,7 @@ def set_esp_debug(self, enabled):
814866
raise RuntimeError("Failed to set debug mode")
815867

816868
def set_pin_mode(self, pin, mode):
817-
"""
818-
Set the io mode for a GPIO pin.
869+
"""Set the io mode for a GPIO pin.
819870
820871
:param int pin: ESP32 GPIO pin to set.
821872
:param value: direction for pin, digitalio.Direction or integer (0=input, 1=output).
@@ -831,8 +882,7 @@ def set_pin_mode(self, pin, mode):
831882
raise RuntimeError("Failed to set pin mode")
832883

833884
def set_digital_write(self, pin, value):
834-
"""
835-
Set the digital output value of pin.
885+
"""Set the digital output value of pin.
836886
837887
:param int pin: ESP32 GPIO pin to write to.
838888
:param bool value: Value for the pin.
@@ -844,8 +894,7 @@ def set_digital_write(self, pin, value):
844894
raise RuntimeError("Failed to write to pin")
845895

846896
def set_analog_write(self, pin, analog_value):
847-
"""
848-
Set the analog output value of pin, using PWM.
897+
"""Set the analog output value of pin, using PWM.
849898
850899
:param int pin: ESP32 GPIO pin to write to.
851900
:param float value: 0=off 1.0=full on
@@ -858,8 +907,7 @@ def set_analog_write(self, pin, analog_value):
858907
raise RuntimeError("Failed to write to pin")
859908

860909
def set_digital_read(self, pin):
861-
"""
862-
Get the digital input value of pin. Returns the boolean value of the pin.
910+
"""Get the digital input value of pin. Returns the boolean value of the pin.
863911
864912
:param int pin: ESP32 GPIO pin to read from.
865913
"""
@@ -877,8 +925,7 @@ def set_digital_read(self, pin):
877925
)
878926

879927
def set_analog_read(self, pin, atten=ADC_ATTEN_DB_11):
880-
"""
881-
Get the analog input value of pin. Returns an int between 0 and 65536.
928+
"""Get the analog input value of pin. Returns an int between 0 and 65536.
882929
883930
:param int pin: ESP32 GPIO pin to read from.
884931
:param int atten: attenuation constant
@@ -914,6 +961,7 @@ def get_time(self):
914961
def set_certificate(self, client_certificate):
915962
"""Sets client certificate. Must be called
916963
BEFORE a network connection is established.
964+
917965
:param str client_certificate: User-provided .PEM certificate up to 1300 bytes.
918966
"""
919967
if self._debug:
@@ -936,6 +984,7 @@ def set_certificate(self, client_certificate):
936984
def set_private_key(self, private_key):
937985
"""Sets private key. Must be called
938986
BEFORE a network connection is established.
987+
939988
:param str private_key: User-provided .PEM file up to 1700 bytes.
940989
"""
941990
if self._debug:

adafruit_esp32spi/digitalio.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def __init__(self, esp_pin, esp):
5050

5151
def init(self, mode=IN):
5252
"""Initalizes a pre-defined pin.
53+
5354
:param mode: Pin mode (IN, OUT, LOW, HIGH). Defaults to IN.
5455
"""
5556
if mode is not None:
@@ -64,6 +65,7 @@ def init(self, mode=IN):
6465

6566
def value(self, val=None):
6667
"""Sets ESP32 Pin GPIO output mode.
68+
6769
:param val: Pin output level (LOW, HIGH)
6870
"""
6971
if val is not None:
@@ -133,6 +135,7 @@ def deinit(self):
133135

134136
def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
135137
"""Set the drive mode and value and then switch to writing out digital values.
138+
136139
:param bool value: Default mode to set upon switching.
137140
:param DriveMode drive_mode: Drive mode for the output.
138141
"""
@@ -142,6 +145,7 @@ def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
142145

143146
def switch_to_input(self, pull=None):
144147
"""Sets the pull and then switch to read in digital values.
148+
145149
:param Pull pull: Pull configuration for the input.
146150
"""
147151
raise NotImplementedError(
@@ -156,6 +160,7 @@ def direction(self):
156160
@direction.setter
157161
def direction(self, pin_dir):
158162
"""Sets the direction of the pin.
163+
159164
:param Direction dir: Pin direction (Direction.OUTPUT or Direction.INPUT)
160165
"""
161166
self.__direction = pin_dir
@@ -176,6 +181,7 @@ def value(self):
176181
@value.setter
177182
def value(self, val):
178183
"""Sets the digital logic level of the pin.
184+
179185
:param type value: Pin logic level.
180186
:param int value: Pin logic level. 1 is logic high, 0 is logic low.
181187
:param bool value: Pin logic level. True is logic high, False is logic low.
@@ -195,8 +201,9 @@ def drive_mode(self):
195201
@drive_mode.setter
196202
def drive_mode(self, mode):
197203
"""Sets the pin drive mode.
204+
198205
:param DriveMode mode: Defines the drive mode when outputting digital values.
199-
Either PUSH_PULL or OPEN_DRAIN
206+
Either PUSH_PULL or OPEN_DRAIN
200207
"""
201208
if mode is DriveMode.OPEN_DRAIN:
202209
raise NotImplementedError(

0 commit comments

Comments
 (0)