#!/usr/bin/python
# coding=utf-8
import time; # 載入python 時間函數
#import the library
from pyA20.gpio import gpio
from pyA20.gpio import port
from time import sleep

######## 回復全部 LED 初始化時狀態 ###########
def led_down():
        gpio.output(port.PG8, gpio.HIGH)
        gpio.output(port.PG9, gpio.HIGH)
        gpio.output(port.PG6, gpio.HIGH)
        gpio.output(port.PG7, gpio.HIGH)
        gpio.output(port.PA1, gpio.LOW)
        gpio.output(port.PA0, gpio.LOW)
        gpio.output(port.PA3, gpio.LOW)
        gpio.output(port.PA11, gpio.LOW)
        gpio.output(port.PA12, gpio.LOW)
        gpio.output(port.PA6, gpio.LOW)
        gpio.output(port.PA18, gpio.LOW)

######## LED 發亮時間 ########### 

def sleetim():
	sleep(0.005)

######## 分折十位個位 二進制碼 ########

def check2(xdata):
    if xdata  > 9:
        data_h= (xdata / 10) % 10
	data_l= (xdata / 1) % 10
	bin_data_h="{0:b}".format(data_h)
	bin_data_h=bin_data_h.zfill(4)
	bin_data_l="{0:b}".format(data_l)
	bin_data_l=bin_data_l.zfill(4)

    else:
	bin_data_h="{0:b}".format(0)
	bin_data_h=bin_data_h.zfill(4)
	bin_data_l="{0:b}".format(xdata)
	bin_data_l=bin_data_l.zfill(4)
    return (bin_data_h,bin_data_l)

def tonled(portno,hhour,lhour,hmin,lmin,hsec,lsec,wday):
    gpio.output(portno, gpio.LOW)
    if hhour=="1":
       	gpio.output(port.PA12, gpio.HIGH)
    else:
	gpio.output(port.PA12, gpio.LOW)

    if lhour=="1":
	gpio.output(port.PA11, gpio.HIGH)
    else:
	gpio.output(port.PA11, gpio.LOW)

    if hmin=="1":
	gpio.output(port.PA6, gpio.HIGH)
    else:
	gpio.output(port.PA6, gpio.LOW)

    if lmin=="1":
	gpio.output(port.PA1, gpio.HIGH)
    else:
	gpio.output(port.PA1, gpio.LOW)

    if hsec=="1":
	gpio.output(port.PA0, gpio.HIGH)
    else:
	gpio.output(port.PA0, gpio.LOW)

    if lsec=="1":
	gpio.output(port.PA3, gpio.HIGH)
    else:
	gpio.output(port.PA3, gpio.LOW)

    if wday=="1":
	gpio.output(port.PA18, gpio.HIGH)
    else:
	gpio.output(port.PA18, gpio.LOW)

    sleetim()

    gpio.output(portno, gpio.HIGH)
    led_down()




#initialize the gpio module
gpio.init()

#setup the port (same as raspberry pi's gpio.setup() function)

#例為高電位(+)時同時行為低電位(-)時 , LED 導通發亮

gpio.setcfg(port.PA12, gpio.OUTPUT) # 列1  代表十位時
gpio.setcfg(port.PA11, gpio.OUTPUT) # 列2  代表個位時 
gpio.setcfg(port.PA6, gpio.OUTPUT) # 列3   代表十位分 
gpio.setcfg(port.PA1, gpio.OUTPUT) # 列4   代表個位分
gpio.setcfg(port.PA0, gpio.OUTPUT) # 列5   代表十位秒
gpio.setcfg(port.PA3, gpio.OUTPUT) # 列6   代表個位秒
gpio.setcfg(port.PA18, gpio.OUTPUT) # 列8  代表星期 

gpio.setcfg(port.PG8, gpio.OUTPUT) # 行4   代表二進制第4個位
gpio.setcfg(port.PG9, gpio.OUTPUT) # 行6   代表二進制第3個位 
gpio.setcfg(port.PG6, gpio.OUTPUT) # 行7   代表二進制第2個位 
gpio.setcfg(port.PG7, gpio.OUTPUT) # 行8   代表二進制第1個位


#now we do something (light up the LED) 初始化測試全部 LED 發光 2 秒
gpio.output(port.PG8, gpio.LOW)
gpio.output(port.PG9, gpio.LOW)
gpio.output(port.PG6, gpio.LOW)
gpio.output(port.PG7, gpio.LOW)

gpio.output(port.PA1, gpio.HIGH)
gpio.output(port.PA0, gpio.HIGH)
gpio.output(port.PA3, gpio.HIGH)
gpio.output(port.PA12, gpio.HIGH)
gpio.output(port.PA11, gpio.HIGH)
gpio.output(port.PA6, gpio.HIGH)
gpio.output(port.PA18, gpio.HIGH)

#turn off the LED after 2 seconds
sleep(2)

led_down()


# 進入主程序 , 讀取當前作業系統時間日期 
while True:
    localtime = time.localtime(time.time())
#	print "Local current time :", localtime

#### 分拆 時分秒 十位個位數 , 把分拆出來的數轉做二進制
    (bin_high_hour,bin_low_hour)=check2(localtime.tm_hour)
    (bin_high_min,bin_low_min)=check2(localtime.tm_min)
    (bin_high_sec,bin_low_sec)=check2(localtime.tm_sec)

    bin_wday="{0:b}".format(localtime.tm_wday+1)
    bin_wday=bin_wday.zfill(4)

#### 把二進制數對應高低電平, 例如 1001 , high low low high 
#### PG8 PG9 PG6 PG7 逐行點亮
    tonled(port.PG8,bin_high_hour[:1],bin_low_hour[:1],bin_high_min[:1],bin_low_min[:1],bin_high_sec[:1],bin_low_sec[:1],bin_wday[:1]) 
    tonled(port.PG9,bin_high_hour[1:2],bin_low_hour[1:2],bin_high_min[1:2],bin_low_min[1:2],bin_high_sec[1:2],bin_low_sec[1:2],bin_wday[1:2]) 
    tonled(port.PG6,bin_high_hour[2:3],bin_low_hour[2:3],bin_high_min[2:3],bin_low_min[2:3],bin_high_sec[2:3],bin_low_sec[2:3],bin_wday[2:3]) 
    tonled(port.PG7,bin_high_hour[3:4],bin_low_hour[3:4],bin_high_min[3:4],bin_low_min[3:4],bin_high_sec[3:4],bin_low_sec[3:4],bin_wday[3:4]) 
