import smbus
import sys
import threading
import datetime
import webiopi
import subprocess
import shlex
import RPi.GPIO as GPIO_
from time import sleep
GPIO = webiopi.GPIO
ALARM = 17
ALARMMODE = 27
SHUTDOWN = 22
GPIO_.setmode(GPIO_.BCM)
GPIO_.setup(ALARM, GPIO_.OUT, initial=GPIO_.LOW)
GPIO_.setup(ALARMMODE, GPIO_.IN, pull_up_down=GPIO_.PUD_DOWN)
GPIO_.setup(SHUTDOWN, GPIO_.IN, pull_up_down=GPIO_.PUD_DOWN)
ALARM_HOUR = 99
ALARM_MIN = 99
ALARM_LEN = 99
PREMIN = 99
ALARMLENCOUNT = 0
ALARMOUT = False
SHUTDOWNCOUNT = 0
SHUTDOWNMSG = False
#-------------------- LCD制御関数 -------------------
def setup_st7032():
trials = 5
for i in range(trials):
try:
c_lower = (contrast & 0xf)
c_upper = (contrast & 0x30)>>4
bus.write_i2c_block_data(address_st7032, register_setting, [0x38, 0x39, 0x14, 0x70|c_lower, 0x54|c_upper, 0x6c])
sleep(0.2)
bus.write_i2c_block_data(address_st7032, register_setting, [0x38, 0x0d, 0x01])
sleep(0.001)
break
except IOError:
if i==trials-1:
sys.exit()
def clear():
global position
global line
position = 0
line = 0
bus.write_byte_data(address_st7032, register_setting, 0x01)
sleep(0.001)
def newline():
global position
global line
if line == display_lines-1:
clear()
else:
line += 1
position = chars_per_line*line
bus.write_byte_data(address_st7032, register_setting, 0xc0)
sleep(0.001)
def write_string(s):
for c in list(s):
write_char(ord(c))
def write_char(c):
global position
byte_data = check_writable(c)
if position == display_chars:
clear()
elif position == chars_per_line*(line+1):
newline()
bus.write_byte_data(address_st7032, register_display, byte_data)
position += 1
def check_writable(c):
if c >= 0x06 and c <= 0xff :
return c
else:
return 0x20 #
bus = smbus.SMBus(1)
address_st7032 = 0x3e
register_setting = 0x00
register_display = 0x40
contrast = 36 # contrast:0-63(normal:30-40)
chars_per_line = 16 # LCD ROW 16
display_lines = 2 # LCD COLUMN 2
display_chars = chars_per_line*display_lines
position = 0
line = 0
setup_st7032()
#---------------------------------------
#LCD表示処理
def thread_lcd():
t1=threading.Timer(0.2,thread_lcd)
t1.start()
weekdays = ['Mon.','Tue.','Wed.','Thu.','Fri.','Sat.','Sun.']
now = datetime.datetime.today()
weekday = weekdays[now.weekday()]
date = '{:%Y/%m/%d}'.format(now)[2:]
if (GPIO.digitalRead(ALARMMODE) == GPIO.LOW):
out_str1 = date + ' ' + weekday + ' '
out_str2 = '{:%H:%M:%S}'.format(now) + ' '
else:
if (ALARM_HOUR < 10):
alhour_str = '0' + str(ALARM_HOUR)
else:
alhour_str = str(ALARM_HOUR)
if (ALARM_MIN < 10):
almin_str = '0' + str(ALARM_MIN)
else:
almin_str = str(ALARM_MIN)
out_str1 = date + ' ' + weekday + ' AL'
out_str2 = '{:%H:%M:%S}'.format(now) + ' ' + alhour_str + ':' + almin_str
if(SHUTDOWNMSG == False):
out_str = out_str1 + out_str2
write_string(out_str)
else:
write_string(' Shutdown ! ')
#アラーム処理
def thread_alarm():
t2=threading.Timer(0.5,thread_alarm)
t2.start()
global PREMIN
global ALARMLENCOUNT
global ALARMOUT
now = datetime.datetime.now()
if(now.minute != PREMIN):
PREMIN = now.minute
if(now.hour == ALARM_HOUR):
if(now.minute == ALARM_MIN):
ALARMOUT = True
ALARMLENCOUNT = 0
if (GPIO.digitalRead(ALARMMODE) == GPIO.HIGH):
if(ALARMOUT == True):
if(ALARMLENCOUNT < (ALARM_LEN * 120)):
ALARMLENCOUNT = ALARMLENCOUNT + 1
if (GPIO.digitalRead(ALARM) == GPIO.LOW):
GPIO.digitalWrite(ALARM, GPIO.HIGH)
else:
GPIO.digitalWrite(ALARM, GPIO.LOW)
else:
ALARMOUT = False
ALARMLENCOUNT = 0
GPIO.digitalWrite(ALARM, GPIO.LOW)
#シャットダウン処理
def thread_shutdown():
t3=threading.Timer(0.2,thread_shutdown)
t3.start()
global SHUTDOWNCOUNT
global SHUTDOWNMSG
if (GPIO.digitalRead(SHUTDOWN) == GPIO.HIGH):
SHUTDOWNCOUNT = SHUTDOWNCOUNT + 1
else:
SHUTDOWNCOUNT = 0
if (SHUTDOWNCOUNT > 15):
SHUTDOWNMSG = True
subprocess.check_output(shlex.split('sudo shutdown -h now'))
else:
SHUTDOWNMSG = False
# WebIOPi 初期処理
def setup():
global ALARM_HOUR
global ALARM_MIN
global ALARM_LEN
try:
f = open('/usr/share/webiopi/htdocs/iotalarmclock/alarm.dat','r')
except:
pass
else:
aldat = f.read().split(',')
ALARM_HOUR = int(aldat[0])
ALARM_MIN = int(aldat[1])
ALARM_LEN = int(aldat[2])
f.close()
t1=threading.Timer(0.2,thread_lcd)
t1.start()
t2=threading.Timer(0.5,thread_alarm)
t2.start()
t3=threading.Timer(0.2,thread_shutdown)
t3.start()
# WebIOPi 繰り返し処理
def loop():
webiopi.sleep(1)
# WebIOPi 終了処理
#def destroy():
# マクロ
@webiopi.macro
def getalarm():
return '%d,%d,%d' % (ALARM_HOUR, ALARM_MIN, ALARM_LEN)
@webiopi.macro
def setalarm(alhour, almin, allen):
global ALARM_HOUR
global ALARM_MIN
global ALARM_LEN
if(int(alhour) < 0):
ALARM_HOUR = 0
elif(int(alhour) > 23):
ALARM_HOUR = 23
else:
ALARM_HOUR = int(alhour)
if(int(almin) < 0):
ALARM_MIN = 0
elif(int(almin) > 59):
ALARM_MIN = 59
else:
ALARM_MIN = int(almin)
if(int(allen) < 1):
ALARM_LEN = 1
elif(int(allen) > 99):
ALARM_LEN = 99
else:
ALARM_LEN = int(allen)
f = open('/usr/share/webiopi/htdocs/iotalarmclock/alarm.dat','w')
aldat = str(ALARM_HOUR) + ',' + str(ALARM_MIN) + ',' + str(ALARM_LEN)
f.write(aldat)
f.close()
return getalarm()