Skip to content

Abstracted Status NeoPixel to make DotStar usable #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@

# pylint: disable=no-name-in-module

import neopixel
import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_requests as requests

class ESPSPI_WiFiManager:
"""
A class to help manage the Wifi connection
"""
def __init__(self, esp, secrets, status_neopixel=None, attempts=2):
def __init__(self, esp, secrets, status_pixel, attempts=2):
"""
:param ESP_SPIcontrol esp: The ESP object we are using
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
:param status_pixel: (Optional) The pixel device - A NeoPixel or DotStar (default=None)
:type status_pixel: NeoPixel or DotStar
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
:param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None)
:type status_neopixel: Pin
"""
# Read the settings
self._esp = esp
Expand All @@ -54,11 +53,8 @@ def __init__(self, esp, secrets, status_neopixel=None, attempts=2):
self.password = secrets['password']
self.attempts = attempts
requests.set_interface(self._esp)
if status_neopixel:
self.neopix = neopixel.NeoPixel(status_neopixel, 1, brightness=0.2)
else:
self.neopix = None
self.neo_status(0)
self.statuspix = status_pixel
self.pixel_status(0)

def reset(self):
"""
Expand All @@ -84,10 +80,10 @@ def connect(self):
try:
if self.debug:
print("Connecting to AP...")
self.neo_status((100, 0, 0))
self.pixel_status((100, 0, 0))
self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
failure_count = 0
self.neo_status((0, 100, 0))
self.pixel_status((0, 100, 0))
except (ValueError, RuntimeError) as error:
print("Failed to connect, retrying\n", error)
failure_count += 1
Expand All @@ -110,9 +106,9 @@ def get(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((0, 0, 100))
self.pixel_status((0, 0, 100))
return_val = requests.get(url, **kw)
self.neo_status(0)
self.pixel_status(0)
return return_val

def post(self, url, **kw):
Expand All @@ -129,7 +125,7 @@ def post(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((0, 0, 100))
self.pixel_status((0, 0, 100))
return_val = requests.post(url, **kw)
return return_val

Expand All @@ -147,9 +143,9 @@ def put(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((0, 0, 100))
self.pixel_status((0, 0, 100))
return_val = requests.put(url, **kw)
self.neo_status(0)
self.pixel_status(0)
return return_val

def patch(self, url, **kw):
Expand All @@ -166,9 +162,9 @@ def patch(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((0, 0, 100))
self.pixel_status((0, 0, 100))
return_val = requests.patch(url, **kw)
self.neo_status(0)
self.pixel_status(0)
return return_val

def delete(self, url, **kw):
Expand All @@ -185,9 +181,9 @@ def delete(self, url, **kw):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((0, 0, 100))
self.pixel_status((0, 0, 100))
return_val = requests.delete(url, **kw)
self.neo_status(0)
self.pixel_status(0)
return return_val

def ping(self, host, ttl=250):
Expand All @@ -201,17 +197,17 @@ def ping(self, host, ttl=250):
"""
if not self._esp.is_connected:
self.connect()
self.neo_status((0, 0, 100))
self.pixel_status((0, 0, 100))
response_time = self._esp.ping(host, ttl=ttl)
self.neo_status(0)
self.pixel_status(0)
return response_time

def neo_status(self, value):
def pixel_status(self, value):
"""
Change Status NeoPixel if it was defined

:param value: The value to set the Board's Status NeoPixel to
:type value: int or 3-value tuple
"""
if self.neopix:
self.neopix.fill(value)
if self.statuspix:
self.statuspix.fill(value)
8 changes: 6 additions & 2 deletions examples/esp32spi_aio_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import board
import busio
from digitalio import DigitalInOut

import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager

Expand All @@ -20,7 +20,11 @@
esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

counter = 0

Expand Down
6 changes: 5 additions & 1 deletion examples/esp32spi_cheerlights.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

# neopixels
pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3)
Expand Down
7 changes: 6 additions & 1 deletion examples/esp32spi_localtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import rtc
Expand All @@ -22,7 +23,11 @@
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

the_rtc = rtc.RTC()

Expand Down