annotate py/lcd.py @ 484:4a51e2bf689b

a few fixes
author Matt Johnston <matt@ucc.asn.au>
date Thu, 06 Feb 2014 20:35:43 +0800
parents 0af7b38a1847
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
450
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/home/matt/templog/venv/bin/python
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import smbus
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import time
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import random
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 import sys
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 class LCD(object):
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 COMMAND = 0x00
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 DATA = 0x40
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 def __init__(self):
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 self.bus = smbus.SMBus(1)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 #self.address = 0b0111110
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 self.address = 0x3e
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 def setup(self):
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 time.sleep(0.01)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 cmds = """0x38 function set
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 0x39 function set
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 0x17 freq
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 0x78 # was 0x74 contrast, 101000 = 8 contrast (32 below), total 40
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 0x57 # was 0x54 # power/icon/contrast, 0110 = bon, c5
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 0x6a # was 0x6f follower , follow onr, rab 010 = 1.5
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 0x0c on/off"""
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 for l in cmds.split('\n'):
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 c = eval(l.split()[0])
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 self.cmd(c)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 # self.cmd(0x38)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 # self.cmd(0x39)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 # self.cmd(0x14)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 # self.cmd(0x78) # contrast
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 # self.cmd(0x57) # power/icon/contrast
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 # self.cmd(0x6f) # follower
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 # self.cmd(0x0c)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 # self.cmd(0x01)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 # self.cmd(0x04)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 def put(self, pos, word):
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 addr = 0b10000000 + (pos % 16) + (pos>=16)*40
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 self.cmd(addr)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 for char in word:
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 self.data(ord(char))
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 def write(self, a, b):
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 self.bus.write_byte_data(self.address, a, b)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 time.sleep(0.0001)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 def cmd(self, cmd):
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 self.write(self.COMMAND, cmd)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 def data(self, data):
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 self.write(self.DATA, data)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 l = LCD()
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 l.setup()
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 l.put(0, 'a')
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 l.put(1, 'b')
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 l.put(4, 'b')
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
453
0af7b38a1847 back to random words on the lcd
Matt Johnston <matt@ucc.asn.au>
parents: 450
diff changeset
63 words = [word.strip() for word in file('/usr/share/dict/words', 'r')]
0af7b38a1847 back to random words on the lcd
Matt Johnston <matt@ucc.asn.au>
parents: 450
diff changeset
64 random.shuffle(words)
0af7b38a1847 back to random words on the lcd
Matt Johnston <matt@ucc.asn.au>
parents: 450
diff changeset
65 #words = file(sys.argv[1], 'r').read().split()
450
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 pos = 0
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 last = ''
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 for word in words:
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 word = (word + ' '*16)[:16]
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 #pos = (pos + 16) % 32
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 #word = random.sample(words, 1)[0][:16] + ' '*16
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 #char = chr(int(random.random() * 26) + ord('a'))
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 l.put(0, last)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 l.put(16, word)
cea8ec101c6b lcd works
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 last = word
453
0af7b38a1847 back to random words on the lcd
Matt Johnston <matt@ucc.asn.au>
parents: 450
diff changeset
77 time.sleep(0.3)