-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringer.py
50 lines (39 loc) · 1.35 KB
/
ringer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /usr/bin/python3
# Script to manage ringing
import RPi.GPIO as GPIO
from signal import pause
from config import RINGING
from time import sleep
from random import uniform
from config import RING_MIN
from config import RING_MAX
from config import RING_ON
from config import RING_OFF
from config import RINGER_PIN
class Ringer(object):
"""
Ringer class
"""
def __init__(self, ring_on=RING_ON, ring_off=RING_OFF, ringer_pin=RINGER_PIN,
ring_min=RING_MIN, ring_max=RING_MAX):
self.ring_on = ring_on
self.ring_off = ring_off
self.ring_min = ring_min
self.ring_max = ring_max
self.ringer_pin = ringer_pin
# Set ringer pin as output
GPIO.setup(self.ringer_pin, GPIO.OUT)
GPIO.output(self.ringer_pin, GPIO.LOW)
def play_ringer(self):
# Wait a random amount
wait = uniform(self.ring_min, self.ring_max)
print("Playing ringer in " + str(wait) + " minutes")
sleep(wait*60) # convert minutes to seconds
self.state = RINGING
while self.state == RINGING:
print("Ring ring...")
GPIO.output(self.ringer_pin, GPIO.HIGH)
sleep(self.ring_on)
GPIO.output(self.ringer_pin, GPIO.LOW)
print("Next ring in " + str(self.ring_off/60.0) + " minutes...")
sleep(self.ring_off)