python i2c sample code 4 RPi 改定版です。
指定した 時分での電源制御機能を追加しました。
============================================
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smbus # sudo apt-get install python-smbus
import time
import os
import sys
import argparse
import re
class i2cLPC1114:
i2c = smbus.SMBus(1) # B, B+ 512MB ...2B,3B,Zero
# i2c = smbus.SMBus(0) # B 256MB
addr = 0x28
def __init__(self):
pass
def led_off(self):
self.i2c.write_i2c_block_data(self.addr, 0x20, [0x00, 0x00])
def led_on(self):
self.i2c.write_i2c_block_data(self.addr, 0x20, [0x00, 0x01])
def led_flash(self):
self.i2c.write_i2c_block_data(self.addr, 0x20, [0x00, 0x02])
def down(self, time):
st=(time-60)/60
os.system("/sbin/shutdown -h " "+" + str(st) )
lt=[ord(c) for c in str(time)]
lt.insert(0,0x00)
lt.insert(1,0x10)
lt.append(0x00)
self.i2c.write_i2c_block_data(self.addr, 0x11, lt)
def up(self, time):
lt=[0x00, 0x10]
lt.extend([ord(c) for c in str(time)])
lt.append(0x00)
self.i2c.write_i2c_block_data(self.addr, 0x10, lt)
def hm2s(hm):
from datetime import datetime
n=datetime.now()
try:
# t=hm.split(":")
m = re.match(r"^([1]?[0-9]|2[0-3]):([1-5]?[0-9])$", hm)
# print m.group(1)
# print m.group(2)
# print m.group(0)
t=[""]*2
if m.group(0):
t[0] = m.group(1)
t[1] = m.group(2)
# print t[0]
# print t[1]
else:
raise
t=datetime(datetime.now().year,datetime.now().month,datetime.now().day, int(t[0]),int(t[1]),0)
except:
print "require -ft hh:mm",
print " hh value is 0..23",
print " mm value is 0..59"
quit();
r = (t-n).total_seconds()
if r <0:
print " It is past time. Please specify the future time."
quit();
return int(r)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('cmd', choices=('loff','lon','lb','down','up', 'fdown','fup'))
parser.add_argument('-t', '--time', type=int, help="Specified units are minutes")
parser.add_argument('-ft', '--ftime', help="Please specify hour:minute")
parser.add_argument('--version', action='version', version='%(prog)s 0.3')
args = parser.parse_args()
# print(args)
# print(args.cmd)
# print(args.time)
if args.cmd=="down" or args.cmd=="up":
if not args.time:
print "require -t [time(min)]"
quit();
else:
mt=args.time * 60
if args.cmd=="down":
if args.time >=2:
pass
else:
print "Please specify more than 2 minutes"
quit();
if args.cmd=="fdown" or args.cmd=="fup":
if not args.ftime:
print "require -ft [hh:mm]"
quit();
m = i2cLPC1114()
if args.cmd=="loff":
m.led_off()
elif args.cmd=="lon":
m.led_on()
elif args.cmd=="lb":
m.led_flash()
elif args.cmd=="down":
m.down(int(mt))
elif args.cmd=="up":
m.up(int(mt))
elif args.cmd=="fdown":
tt=hm2s(args.ftime)
if tt >=2*60:
pass
else:
print "Please specify the time after 2 minutes"
# print tt
m.down(tt)
elif args.cmd=="fup":
tt=hm2s(args.ftime)
m.up(tt)
# print tt
else:
pass
=======================================
[使い方]
# ./RPi1114_set3.py -h
usage: RPi1114_set3.py [-h] [-t TIME] [-ft FTIME] [--version]
{loff,lon,lb,down,up,fdown,fup}
positional arguments:
{loff,lon,lb,down,up,fdown,fup}
optional arguments:
-h, --help show this help message and exit
-t TIME, --time TIME Specified units are minutes
-ft FTIME, --ftime FTIME
Please specify hour:minute
--version show program's version number and exit
[使用例]
2分後に電源断(指定1分前にshutdownを実行)
# ./RPi1114_set3.py down -t 2
10分後に電源投入(OS起動)
# ./RPi1114_set3.py up -t 10
15:30分に電源断(指定1分前にshutdownを実行)
# ./RPi1114_set3.py fdown -ft 15:30
9:00分に電源投入(OS起動)
# ./RPi1114_set3.py fup -ft 9:00
最近のコメント