Skip to content

Commit f4481be

Browse files
committed
cleanup and fix comments, cleanup examples
1 parent e433ef6 commit f4481be

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -408,35 +408,41 @@ def scan_networks(self):
408408
return APs
409409
return None
410410

411-
def set_ip_config(self, new_ip, new_gw, new_mask="255.255.255.0"):
412-
"""Tells the ESP32 to set ip, gateway and network mask b"\xFF" """
411+
def set_ip_config(self, ip_address, gateway, mask="255.255.255.0"):
412+
"""Tells the ESP32 to set ip, gateway and network mask b"\xFF"
413+
414+
:param str ip_address: IP address (as a string).
415+
:param str gateway: Gateway (as a string).
416+
:param str mask: Mask, defaults to 255.255.255.0 (as a string).
417+
"""
413418
resp = self._send_command_get_response(
414419
_SET_IP_CONFIG,
415420
params=[
416421
b"\x00",
417-
self.unpretty_ip(new_ip),
418-
self.unpretty_ip(new_gw),
419-
self.unpretty_ip(new_mask),
422+
self.unpretty_ip(ip_address),
423+
self.unpretty_ip(gateway),
424+
self.unpretty_ip(mask),
420425
],
421426
sent_param_len_16=False,
422427
)
423428
return resp
424429

425-
def set_dns_config(self, dns1, dns2="8.8.8.8"):
426-
"""Tells the ESP32 to set DNS, with dns2 default to google dns=8.8.8.8"""
430+
def set_dns_config(self, dns1, dns2):
431+
"""Tells the ESP32 to set DNS
432+
433+
:param str dns1: DNS server 1 IP as a string.
434+
:param str dns2: DNS server 2 IP as a string.
435+
"""
427436
resp = self._send_command_get_response(
428437
_SET_DNS_CONFIG, [b"\x00", self.unpretty_ip(dns1), self.unpretty_ip(dns2)]
429438
)
430439
if resp[0][0] != 1:
431440
raise RuntimeError("Failed to set dns with esp32")
432441

433442
def set_hostname(self, hostname):
434-
"""
435-
Tells the ESP32 to set hostname
443+
"""Tells the ESP32 to set hostname for DHCP.
436444
437-
:params str hostname: Set the host name, used by DHCP to associate a local
438-
domain name like hostname.home for example, depending
439-
on the DHCP server setup.
445+
:param str hostname: The new host name.
440446
"""
441447
resp = self._send_command_get_response(_SET_HOSTNAME, [hostname.encode()])
442448
if resp[0][0] != 1:
@@ -555,8 +561,7 @@ def connect(self, secrets):
555561
self.connect_AP(secrets["ssid"], secrets["password"])
556562

557563
def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-name
558-
"""
559-
Connect to an access point with given name and password.
564+
"""Connect to an access point with given name and password.
560565
Will wait until specified timeout seconds and return on success
561566
or raise an exception on failure.
562567
@@ -589,8 +594,7 @@ def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-n
589594
def create_AP(
590595
self, ssid, password, channel=1, timeout=10
591596
): # pylint: disable=invalid-name
592-
"""
593-
Create an access point with the given name, password, and channel.
597+
"""Create an access point with the given name, password, and channel.
594598
Will wait until specified timeout seconds and return on success
595599
or raise an exception on failure.
596600
@@ -851,8 +855,7 @@ def set_esp_debug(self, enabled):
851855
raise RuntimeError("Failed to set debug mode")
852856

853857
def set_pin_mode(self, pin, mode):
854-
"""
855-
Set the io mode for a GPIO pin.
858+
"""Set the io mode for a GPIO pin.
856859
857860
:param int pin: ESP32 GPIO pin to set.
858861
:param value: direction for pin, digitalio.Direction or integer (0=input, 1=output).
@@ -868,8 +871,7 @@ def set_pin_mode(self, pin, mode):
868871
raise RuntimeError("Failed to set pin mode")
869872

870873
def set_digital_write(self, pin, value):
871-
"""
872-
Set the digital output value of pin.
874+
"""Set the digital output value of pin.
873875
874876
:param int pin: ESP32 GPIO pin to write to.
875877
:param bool value: Value for the pin.
@@ -881,8 +883,7 @@ def set_digital_write(self, pin, value):
881883
raise RuntimeError("Failed to write to pin")
882884

883885
def set_analog_write(self, pin, analog_value):
884-
"""
885-
Set the analog output value of pin, using PWM.
886+
"""Set the analog output value of pin, using PWM.
886887
887888
:param int pin: ESP32 GPIO pin to write to.
888889
:param float value: 0=off 1.0=full on
@@ -895,8 +896,7 @@ def set_analog_write(self, pin, analog_value):
895896
raise RuntimeError("Failed to write to pin")
896897

897898
def set_digital_read(self, pin):
898-
"""
899-
Get the digital input value of pin. Returns the boolean value of the pin.
899+
"""Get the digital input value of pin. Returns the boolean value of the pin.
900900
901901
:param int pin: ESP32 GPIO pin to read from.
902902
"""
@@ -914,8 +914,7 @@ def set_digital_read(self, pin):
914914
)
915915

916916
def set_analog_read(self, pin, atten=ADC_ATTEN_DB_11):
917-
"""
918-
Get the analog input value of pin. Returns an int between 0 and 65536.
917+
"""Get the analog input value of pin. Returns an int between 0 and 65536.
919918
920919
:param int pin: ESP32 GPIO pin to read from.
921920
:param int atten: attenuation constant
@@ -951,6 +950,7 @@ def get_time(self):
951950
def set_certificate(self, client_certificate):
952951
"""Sets client certificate. Must be called
953952
BEFORE a network connection is established.
953+
954954
:param str client_certificate: User-provided .PEM certificate up to 1300 bytes.
955955
"""
956956
if self._debug:
@@ -973,6 +973,7 @@ def set_certificate(self, client_certificate):
973973
def set_private_key(self, private_key):
974974
"""Sets private key. Must be called
975975
BEFORE a network connection is established.
976+
976977
:param str private_key: User-provided .PEM file up to 1700 bytes.
977978
"""
978979
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(

examples/esp32spi_ipconfig.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
14
import time
25
import board
36
import busio
47
from digitalio import DigitalInOut
5-
import adafruit_requests as requests
68
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
79
from adafruit_esp32spi import adafruit_esp32spi
8-
import neopixel
9-
from rainbowio import colorwheel
10-
import gc
11-
from secrets import secrets
1210

13-
IP_ADDRESS = "192.168.2.2"
14-
GATEWAY_ADDRESS = "192.168.2.1"
11+
# Get wifi details and more from a secrets.py file
12+
try:
13+
from secrets import secrets
14+
except ImportError:
15+
print("WiFi secrets are kept in secrets.py, please add them there!")
16+
raise
17+
18+
IP_ADDRESS = "192.168.1.111"
19+
GATEWAY_ADDRESS = "192.168.1.1"
1520
SUBNET_MASK = "255.255.255.0"
1621

17-
UDP_IN_ADDR = "192.168.2.2"
22+
UDP_IN_ADDR = "192.168.1.1"
1823
UDP_IN_PORT = 5500
1924

2025
UDP_TIMEOUT = 20
@@ -49,7 +54,7 @@
4954
ip1 = esp.ip_address
5055

5156
print("set ip dns")
52-
esp.set_dns_config("192.168.2.1", "8.8.8.8")
57+
esp.set_dns_config("192.168.1.1", "8.8.8.8")
5358

5459
print("set ip config")
5560
esp.set_ip_config(IP_ADDRESS, GATEWAY_ADDRESS, SUBNET_MASK)

0 commit comments

Comments
 (0)