Skip to content

Commit

Permalink
I give a shot (#51)
Browse files Browse the repository at this point in the history
* I give a shot

* I make it work

* I make it work v2

* I make it work v3

* I make it work v4
  • Loading branch information
joostlek committed Oct 17, 2023
1 parent 2d4bf31 commit 6147da8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ jobs:
poetry install --no-interaction
- name: 🚀 Run pytest
run: poetry run pytest --cov app tests
env:
DB_NAME: stair-challenge
DB_PASSWORD: ""
DB_PORT: 3306
DB_ROOT_PASSWORD: ""
DB_USER: ""
FLASK_APP: run.py
FLASK_ENV: development
FLASK_RUN_HOST: "127.0.0.1"
MQTT_BROKER_PORT: 1883
MQTT_BROKER_URL: ""
MQTT_KEEPALIVE: 60
SECRET_KEY: boop
- name: ⬆️ Upload coverage artifact
uses: actions/upload-artifact@v3.1.3
with:
Expand Down
46 changes: 24 additions & 22 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@

sandglass_thread: threading.Thread = None


# ----------------------------------------------------------------------------#
# LED strip configuration.
# ----------------------------------------------------------------------------#
LED_COUNT = 104 # Number of LED pixels.
LED_PIN = 10 # GPIO pin connected to the pixels (18 uses PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 125 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # ON to invert the signal (Using level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53

led_controller = LEDController(
LED_COUNT,
LED_PIN,
LED_FREQ_HZ,
LED_DMA,
LED_BRIGHTNESS,
LED_INVERT,
LED_CHANNEL,
)

# -----------------------------------
# Create Application Factory Function
# -----------------------------------
Expand All @@ -72,6 +94,8 @@ def create_app() -> Flask:
# Initialize the socketio instance
socketio.init_app(app)
socketio.async_mode = app.config["SOCKETIO_ASYNC_MODE"]
led_controller.connect()
led_controller.turn_off()

initialize_extensions(app)
register_blueprints(app)
Expand Down Expand Up @@ -139,28 +163,6 @@ def make_session_permanent() -> None:
from app.blueprints.auth.models import User
from app.blueprints.backend.models import Sensor, Workout

# ----------------------------------------------------------------------------#
# LED strip configuration.
# ----------------------------------------------------------------------------#
LED_COUNT = 104 # Number of LED pixels.
LED_PIN = 10 # GPIO pin connected to the pixels (18 uses PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 125 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # ON to invert the signal (Using level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53

led_controller = LEDController(
LED_COUNT,
LED_PIN,
LED_FREQ_HZ,
LED_DMA,
LED_BRIGHTNESS,
LED_INVERT,
LED_CHANNEL,
)
led_controller.turn_off()


def register_blueprints(app: Flask) -> None:
"""Register the blueprints.
Expand Down
13 changes: 12 additions & 1 deletion app/led_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def random_color(self) -> Color:
class LEDController:
"""LED strip configuration."""

strip: PixelStrip

def __init__(
self,
count: int,
Expand All @@ -139,7 +141,16 @@ def __init__(
led_invert (bool): True to invert the signal (using level shift)
led_channel (int): set to '1' for GPIOs 13, 19, 41, 45 or 53
"""
self.strip = PixelStrip(count, pin, freq_hz, dma, invert, brightness, channel)
self.count = count
self.pin = pin
self.freq_hz = freq_hz
self.dma = dma
self.brightness = brightness
self.invert = invert
self.channel = channel

def connect(self) -> None:
self.strip = PixelStrip(self.count, self.pin, self.freq_hz, self.dma, self.invert, self.brightness, self.channel)
try:
self.strip.begin()
# self.set_color(Color(0, 0, 0))
Expand Down
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
# Fixtures
# --------

@pytest.fixture(scope="module", autouse=True)
def mock_strip() -> None:
mock = MagicMock()
with patch("app.led_controller.PixelStrip.begin"),\
patch("app.led_controller.PixelStrip", mock),\
patch("app.led_controller.PixelStrip.setPixelColor"),\
patch("app.led_controller.PixelStrip.numPixels"),\
patch("app.led_controller.PixelStrip.show"):
yield

@pytest.fixture(scope="module", autouse=True)
def mock_mqtt() -> None:
with patch("app.mqtt.connect"):
yield


@pytest.fixture(scope="module")
def new_user() -> pytest.fixture:
Expand Down

0 comments on commit 6147da8

Please sign in to comment.