Skip to content
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
161 changes: 161 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
urequests.py
47 changes: 47 additions & 0 deletions src/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json

import urequests


class ApiInterface:
__HEADERS = {
"accept": "application/json"
}

__BASE_URL = "http://185.185.71.49:8000"
__HEALTH_URL = __BASE_URL + "/health"
__DEVICE_URL = __BASE_URL + "/device"
__NEXT_DEVICE_URL = __DEVICE_URL + "/next"
__PREVIOUS_DEVICE_URL = __DEVICE_URL + "/previous"
__PLAY_URL = __BASE_URL + "/play"

def request(self, method: str, url: str, data=None) -> urequests.Response:
if data is None:
data = {}

if method == "GET":
resp = urequests.request(method, url, headers=self.__HEADERS)
else:
resp = urequests.request(method, url, headers=self.__HEADERS, data=json.dumps(data))

print(resp.status_code)
print(resp.text)
return resp

def check_health(self) -> bool:
try:
return self.request("GET", self.__HEALTH_URL).status_code == 200
except:
return False

def get_current_device(self) -> dict:
return self.request("GET", self.__DEVICE_URL).json()

def next_device(self) -> dict:
return self.request("POST", self.__NEXT_DEVICE_URL).json()

def previous_device(self) -> dict:
return self.request("POST", self.__PREVIOUS_DEVICE_URL).json()

def play(self, track: str) -> dict:
return self.request("POST", self.__PLAY_URL, {"id": track}).json()
17 changes: 17 additions & 0 deletions src/buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from lib.button import Button


class ButtonsInterface:
def __init__(self, left_button_pin: int, right_button_pin: int):
self.__left_button = Button(left_button_pin, internal_pullup=True)
self.__right_button = Button(right_button_pin, internal_pullup=True)

def update(self) -> None:
self.__left_button.update()
self.__right_button.update()

def is_left_button_pressed(self) -> bool:
return self.__left_button.active

def is_right_button_pressed(self) -> bool:
return self.__right_button.active
14 changes: 14 additions & 0 deletions src/buzzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from machine import Pin

from lib.buzzer import Buzzer


class BuzzerInterface:
def __init__(self, pin: int):
self.__buzzer = Buzzer(Pin(pin))

def play_success(self):
self.__buzzer.beep([[500, 50], [900, 150]])

def play_failure(self):
self.__buzzer.beep([[392, 200], [330, 200], [261, 400]])
69 changes: 69 additions & 0 deletions src/display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import machine
from lib.i2c_lcd import I2cLcd


class Symbol:
def __init__(self, index: int, data: bytearray):
self.index = index
self.data = data


class Symbols:
TICK = Symbol(0, bytearray([0x00, 0x00, 0x01, 0x02, 0x14, 0x08, 0x00, 0x00]))
CROSS = Symbol(1, bytearray([0x00, 0x00, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x00]))


class DisplayInterface:
def __init__(self, sda_pin: int, scl_pin: int):
self.__i2c = machine.I2C(0, sda=machine.Pin(sda_pin), scl=machine.Pin(scl_pin), freq=400000)
self.__lcd = I2cLcd(self.__i2c, 0x27, 2, 16)
self.__init_symbols()

def print(self, text: str, line: int = 1, position: int = 1, clear_line: bool = False, clear_full: bool = False):
self.__validate_inputs(line, position)
text = self.__trim_text(text)

if clear_full:
self.clear()

if clear_line:
self.clear(line)

self.__lcd.move_to(position - 1, line - 1)
self.__lcd.putstr(text)

def print_centered(self, text: str, line: int = 1):
self.__validate_inputs(line)
text = self.__trim_text(text)

self.__lcd.move_to(0, line - 1)
self.__lcd.putstr(text.center(16))

def clear(self, line: int = None):
self.__validate_inputs(line)

if line:
self.__lcd.move_to(0, line - 1)
self.__lcd.putstr(" " * 16)
else:
self.__lcd.clear()

def __init_symbols(self):
for symbol in Symbols.__dict__.values():
if isinstance(symbol, Symbol):
self.__lcd.custom_char(symbol.index, symbol.data)

@staticmethod
def __validate_inputs(line: int = None, position: int = None):
if line and line not in [1, 2]:
raise ValueError("Line must be 1 or 2")

if position and position not in range(1, 16):
raise ValueError("Position must be between 1 and 16")

@staticmethod
def __trim_text(text: str):
if len(text) > 16:
text = text[:16]

return text
Loading