annotate py/lcd.py @ 598:d4fbfb5c46ff rust

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