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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down
4 changes: 4 additions & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def request(self, method: str, url: str, data=None, params=None) -> urequests.Re
if query_string:
url = f"{url}?{query_string}"

print(f"Requesting {method} {url} {'with data' if data else ''}...")

if method == "GET":
if data:
raise ValueError("GET requests cannot have data, use params instead")
Expand All @@ -33,6 +35,8 @@ def request(self, method: str, url: str, data=None, params=None) -> urequests.Re
else:
resp = urequests.request(method, url, headers=self.__HEADERS, data=json.dumps(data))

print(f"Response ({resp.status_code}): {resp.text}")

return resp

def check_health(self) -> bool:
Expand Down
Empty file added src/lib/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions src/lib/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from machine import Pin


class Button(object):
rest_state = False
# pin = None
# pin_number = 0
RELEASED = 'released'
PRESSED = 'pressed'

def __init__(self, pin, rest_state=False, callback=None, internal_pullup=False, internal_pulldown=False):
self.pin_number = pin
self.rest_state = rest_state
if internal_pulldown:
self.internal_pull = Pin.PULL_DOWN
self.rest_state = False
elif internal_pullup:
self.internal_pull = Pin.PULL_UP
self.rest_state = True
else:
self.internal_pull = None

self.pin = Pin(pin, mode=Pin.IN, pull=self.internal_pull)

self.callback = callback
self.active = False

def update(self):
# print(self.pin.value())
if self.pin.value() == (not self.rest_state) and (not self.active):
self.active = True
if self.callback != None:
self.callback(self.pin_number, Button.PRESSED)
return
if self.pin.value() == self.rest_state and self.active:
self.active = False
if self.callback != None:
self.callback(self.pin_number, Button.RELEASED)
return
64 changes: 64 additions & 0 deletions src/lib/buzzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Сreated by Immersive-programs:
# github: https://github.com/Immersive-programs/micropython-buzzer
from machine import PWM, Timer


class Buzzer:
"""Tone playback"""

def __init__(self, pin):
self.buzzer = PWM(pin)
self.beeptimer = Timer()
self.beeps = []
self.isenabled = True
self.isbeeping = False

def __beep(self):
"""Воспроизводит последовательность звуковых сигналов"""
if self.isenabled:
if len(self.beeps) > 0:
self.isbeeping = True
self.beeptimer.init(mode=Timer.ONE_SHOT, period=self.beeps[0][1], callback=lambda x: self.__beep())
if self.beeps[0][0] <= 20:
self.buzzer.duty_u16(1)
else:
self.buzzer.duty_u16(32768)
self.buzzer.freq(self.beeps[0][0])
del self.beeps[0]
else:
self.buzzer.freq(20)
self.buzzer.duty_u16(0)
self.beeptimer.deinit()
self.isbeeping = False

def beep(self, beeps):
"""Добавляет и запускает последовательность звуковых сигналов"""
self.beeps += beeps
if not self.isbeeping:
self.__beep()

def deinit(self):
self.buzzer.deinit()
self.beeptimer.deinit()
87 changes: 87 additions & 0 deletions src/lib/i2c_lcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import utime
import gc

from lcd_api import LcdApi
from machine import I2C

# PCF8574 pin definitions
MASK_RS = 0x01 # P0
MASK_RW = 0x02 # P1
MASK_E = 0x04 # P2

SHIFT_BACKLIGHT = 3 # P3
SHIFT_DATA = 4 # P4-P7


class I2cLcd(LcdApi):

# Implements a HD44780 character LCD connected via PCF8574 on I2C

def __init__(self, i2c, i2c_addr, num_lines, num_columns):
self.i2c = i2c
self.i2c_addr = i2c_addr
self.i2c.writeto(self.i2c_addr, bytes([0]))
utime.sleep_ms(20) # Allow LCD time to powerup
# Send reset 3 times
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
utime.sleep_ms(5) # Need to delay at least 4.1 msec
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
utime.sleep_ms(1)
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
utime.sleep_ms(1)
# Put LCD into 4-bit mode
self.hal_write_init_nibble(self.LCD_FUNCTION)
utime.sleep_ms(1)
LcdApi.__init__(self, num_lines, num_columns)
cmd = self.LCD_FUNCTION
if num_lines > 1:
cmd |= self.LCD_FUNCTION_2LINES
self.hal_write_command(cmd)
gc.collect()

def hal_write_init_nibble(self, nibble):
# Writes an initialization nibble to the LCD.
# This particular function is only used during initialization.
byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
gc.collect()

def hal_backlight_on(self):
# Allows the hal layer to turn the backlight on
self.i2c.writeto(self.i2c_addr, bytes([1 << SHIFT_BACKLIGHT]))
gc.collect()

def hal_backlight_off(self):
# Allows the hal layer to turn the backlight off
self.i2c.writeto(self.i2c_addr, bytes([0]))
gc.collect()

def hal_write_command(self, cmd):
# Write a command to the LCD. Data is latched on the falling edge of E.
byte = ((self.backlight << SHIFT_BACKLIGHT) |
(((cmd >> 4) & 0x0f) << SHIFT_DATA))
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
byte = ((self.backlight << SHIFT_BACKLIGHT) |
((cmd & 0x0f) << SHIFT_DATA))
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
if cmd <= 3:
# The home and clear commands require a worst case delay of 4.1 msec
utime.sleep_ms(5)
gc.collect()

def hal_write_data(self, data):
# Write data to the LCD. Data is latched on the falling edge of E.
byte = (MASK_RS |
(self.backlight << SHIFT_BACKLIGHT) |
(((data >> 4) & 0x0f) << SHIFT_DATA))
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
byte = (MASK_RS |
(self.backlight << SHIFT_BACKLIGHT) |
((data & 0x0f) << SHIFT_DATA))
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
gc.collect()
Loading