more backups
[rb-clock.git] / python / lcd-test.py
1 import RPi.GPIO as GPIO
2 from RPLCD.gpio import CharLCD
3 import socket
4 import fcntl
5 import struct
6 import time
7 from signal import signal, SIGINT
8 from sys import exit
9
10 lcd = CharLCD(cols=20, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23], numbering_mode=GPIO.BOARD)
11 lcd.clear()
12
13 def handler(signal_received, frame):
14   lcd.close()
15   print('SIGINT or CTRL-C detected. Exiting gracefully')
16   exit(0)
17
18 def get_ip_address(ifname):
19   s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
20   return socket.inet_ntoa(fcntl.ioctl(
21     s.fileno(),
22     0x8915, 
23     struct.pack('256s', ifname[:15])
24   )[20:24])
25
26 if __name__ == '__main__':
27   signal(SIGINT, handler)
28   while True:
29     for i in range(0,6):
30       lcd.clear()
31       lcd.write_string("Time: %s" %time.strftime("%H:%M:%S"))
32       lcd.cursor_pos = (1, 0)
33       lcd.write_string("Date: %s" %time.strftime("%m/%d/%Y"))
34       time.sleep(1)
35     lcd.clear()
36     lcd.write_string("IP Address:") 
37     lcd.cursor_pos = (1, 0)
38     lcd.write_string(get_ip_address('eth0'))
39     time.sleep(5)
40
41 #lcd.clear()
42 #lcd.write_string(u'Hello world!')
43 #lcd.crlf()
44 #lcd.write_string(u'Hello world!')
45 lcd.close()