# Дружинин
import time
import rp2
from machine import I2C, Pin
from ds1307 import DS1307
from rp2 import PIO
time.sleep(0.1) # Wait for USB to become ready
I2C_ADDR = 0x68
H_MORN_START, H_MORN_END = 7, 8
M_MORN_START, M_MORN_END = 0, 20
H_EVEN_START, H_EVEN_END = 20, 21
M_EVEN_START, M_EVEN_END = 0, 20
BREAK, BREAK_REPEAT = 5, 3
i2c = I2C(1, scl=Pin(19), sda=Pin(26), freq=400_000)
clock = DS1307(addr=I2C_ADDR, i2c=i2c)
led = Pin(5, Pin.OUT)
button = Pin(10, Pin.IN, Pin.PULL_UP)
@rp2.asm_pio(out_init=[PIO.OUT_LOW])
def echo():
wrap_target()
mov(pins, isr)
mov(isr, invert(isr))
pull(noblock)
mov(x, osr)
mov(y, x)
label("loop")
jmp(y_dec, "loop")
wrap()
sm = rp2.StateMachine(0, echo, freq=1_000_000, out_base=Pin(6))
sm.active(1)
def play_signal(count):
print(f'Проигрывается сигнал № {count}')
for _ in range(count):
led.on()
sm.put(1_000_000 // 500)
time.sleep(1)
sm.put(1_000_000 // 1000)
time.sleep(1)
sm.put(0)
led.off()
def is_time_in_period(h_start, h_end, m_start, m_end, h_cur, m_cur):
result = False
if h_start <= h_cur < h_end:
result = True
elif h_start == h_cur and m_start <= m_cur:
result = True
elif h_end == h_cur and m_cur < m_end:
result = True
return result
def check_time():
weekday = clock.datetime[-2]
hour, minute = clock.hour, clock.minute
result = True
if weekday in [1, 7]:
result = False
if (not is_time_in_period(H_MORN_START, H_MORN_END, M_MORN_START, M_MORN_END, hour, minute) and
not is_time_in_period(H_EVEN_START, H_EVEN_END, M_EVEN_START, M_EVEN_END, hour, minute)):
result = False
return result
while True:
print('Запуск сначала')
count = 1
if check_time():
time.sleep(BREAK)
play_signal(count)
if button.value() == 0:
print('Кнопка нажата')
continue
while button.value() == 1:
time.sleep(BREAK_REPEAT)
count += 1
play_signal(count)
print('Кнопка нажата')