Skip to content

Commit 822bafd

Browse files
committed
feat: add libs
1 parent 5a03eb7 commit 822bafd

File tree

6 files changed

+765
-0
lines changed

6 files changed

+765
-0
lines changed

src/lib/__init__.py

Whitespace-only changes.

src/lib/button.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from machine import Pin
2+
3+
4+
class Button(object):
5+
rest_state = False
6+
# pin = None
7+
# pin_number = 0
8+
RELEASED = 'released'
9+
PRESSED = 'pressed'
10+
11+
def __init__(self, pin, rest_state=False, callback=None, internal_pullup=False, internal_pulldown=False):
12+
self.pin_number = pin
13+
self.rest_state = rest_state
14+
if internal_pulldown:
15+
self.internal_pull = Pin.PULL_DOWN
16+
self.rest_state = False
17+
elif internal_pullup:
18+
self.internal_pull = Pin.PULL_UP
19+
self.rest_state = True
20+
else:
21+
self.internal_pull = None
22+
23+
self.pin = Pin(pin, mode=Pin.IN, pull=self.internal_pull)
24+
25+
self.callback = callback
26+
self.active = False
27+
28+
def update(self):
29+
# print(self.pin.value())
30+
if self.pin.value() == (not self.rest_state) and (not self.active):
31+
self.active = True
32+
if self.callback != None:
33+
self.callback(self.pin_number, Button.PRESSED)
34+
return
35+
if self.pin.value() == self.rest_state and self.active:
36+
self.active = False
37+
if self.callback != None:
38+
self.callback(self.pin_number, Button.RELEASED)
39+
return

src/lib/buzzer.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#
2+
# MIT License
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining
5+
# a copy of this software and associated documentation files (the
6+
# "Software"), to deal in the Software without restriction, including
7+
# without limitation the rights to use, copy, modify, merge, publish,
8+
# distribute, sublicense, and/or sell copies of the Software, and to
9+
# permit persons to whom the Software is furnished to do so, subject to
10+
# the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be
13+
# included in all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
23+
# Сreated by Immersive-programs:
24+
# github: https://github.com/Immersive-programs/micropython-buzzer
25+
from machine import PWM, Timer
26+
27+
28+
class Buzzer:
29+
"""Tone playback"""
30+
31+
def __init__(self, pin):
32+
self.buzzer = PWM(pin)
33+
self.beeptimer = Timer()
34+
self.beeps = []
35+
self.isenabled = True
36+
self.isbeeping = False
37+
38+
def __beep(self):
39+
"""Воспроизводит последовательность звуковых сигналов"""
40+
if self.isenabled:
41+
if len(self.beeps) > 0:
42+
self.isbeeping = True
43+
self.beeptimer.init(mode=Timer.ONE_SHOT, period=self.beeps[0][1], callback=lambda x: self.__beep())
44+
if self.beeps[0][0] <= 20:
45+
self.buzzer.duty_u16(1)
46+
else:
47+
self.buzzer.duty_u16(32768)
48+
self.buzzer.freq(self.beeps[0][0])
49+
del self.beeps[0]
50+
else:
51+
self.buzzer.freq(20)
52+
self.buzzer.duty_u16(0)
53+
self.beeptimer.deinit()
54+
self.isbeeping = False
55+
56+
def beep(self, beeps):
57+
"""Добавляет и запускает последовательность звуковых сигналов"""
58+
self.beeps += beeps
59+
if not self.isbeeping:
60+
self.__beep()
61+
62+
def deinit(self):
63+
self.buzzer.deinit()
64+
self.beeptimer.deinit()

src/lib/i2c_lcd.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import utime
2+
import gc
3+
4+
from lcd_api import LcdApi
5+
from machine import I2C
6+
7+
# PCF8574 pin definitions
8+
MASK_RS = 0x01 # P0
9+
MASK_RW = 0x02 # P1
10+
MASK_E = 0x04 # P2
11+
12+
SHIFT_BACKLIGHT = 3 # P3
13+
SHIFT_DATA = 4 # P4-P7
14+
15+
16+
class I2cLcd(LcdApi):
17+
18+
# Implements a HD44780 character LCD connected via PCF8574 on I2C
19+
20+
def __init__(self, i2c, i2c_addr, num_lines, num_columns):
21+
self.i2c = i2c
22+
self.i2c_addr = i2c_addr
23+
self.i2c.writeto(self.i2c_addr, bytes([0]))
24+
utime.sleep_ms(20) # Allow LCD time to powerup
25+
# Send reset 3 times
26+
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
27+
utime.sleep_ms(5) # Need to delay at least 4.1 msec
28+
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
29+
utime.sleep_ms(1)
30+
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
31+
utime.sleep_ms(1)
32+
# Put LCD into 4-bit mode
33+
self.hal_write_init_nibble(self.LCD_FUNCTION)
34+
utime.sleep_ms(1)
35+
LcdApi.__init__(self, num_lines, num_columns)
36+
cmd = self.LCD_FUNCTION
37+
if num_lines > 1:
38+
cmd |= self.LCD_FUNCTION_2LINES
39+
self.hal_write_command(cmd)
40+
gc.collect()
41+
42+
def hal_write_init_nibble(self, nibble):
43+
# Writes an initialization nibble to the LCD.
44+
# This particular function is only used during initialization.
45+
byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA
46+
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
47+
self.i2c.writeto(self.i2c_addr, bytes([byte]))
48+
gc.collect()
49+
50+
def hal_backlight_on(self):
51+
# Allows the hal layer to turn the backlight on
52+
self.i2c.writeto(self.i2c_addr, bytes([1 << SHIFT_BACKLIGHT]))
53+
gc.collect()
54+
55+
def hal_backlight_off(self):
56+
# Allows the hal layer to turn the backlight off
57+
self.i2c.writeto(self.i2c_addr, bytes([0]))
58+
gc.collect()
59+
60+
def hal_write_command(self, cmd):
61+
# Write a command to the LCD. Data is latched on the falling edge of E.
62+
byte = ((self.backlight << SHIFT_BACKLIGHT) |
63+
(((cmd >> 4) & 0x0f) << SHIFT_DATA))
64+
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
65+
self.i2c.writeto(self.i2c_addr, bytes([byte]))
66+
byte = ((self.backlight << SHIFT_BACKLIGHT) |
67+
((cmd & 0x0f) << SHIFT_DATA))
68+
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
69+
self.i2c.writeto(self.i2c_addr, bytes([byte]))
70+
if cmd <= 3:
71+
# The home and clear commands require a worst case delay of 4.1 msec
72+
utime.sleep_ms(5)
73+
gc.collect()
74+
75+
def hal_write_data(self, data):
76+
# Write data to the LCD. Data is latched on the falling edge of E.
77+
byte = (MASK_RS |
78+
(self.backlight << SHIFT_BACKLIGHT) |
79+
(((data >> 4) & 0x0f) << SHIFT_DATA))
80+
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
81+
self.i2c.writeto(self.i2c_addr, bytes([byte]))
82+
byte = (MASK_RS |
83+
(self.backlight << SHIFT_BACKLIGHT) |
84+
((data & 0x0f) << SHIFT_DATA))
85+
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
86+
self.i2c.writeto(self.i2c_addr, bytes([byte]))
87+
gc.collect()

0 commit comments

Comments
 (0)