DIG2, BEEP, EPROM samples

This commit is contained in:
James Bowman 2019-02-21 07:34:28 -08:00
parent 760d343786
commit 09f1d800bf
4 changed files with 178 additions and 7 deletions

View File

@ -1,31 +1,48 @@
"""
Drivers for electricdollarstore I2C parts
"""
import struct
import time
class Dig2:
""" DIG2 is a 2-digit 7-segment LED display """
def __init__(self, i2, a = 0x14):
self.i2 = i2
self.a = a
def raw(self, b0, b1):
""" Set all 8 segments from the bytes b0 and b1 """
self.i2.regwr(self.a, 0, b0, b1)
def hex(self, b):
""" Display a hex number 0-0xff """
self.i2.regwr(self.a, 1, b)
def dec(self, b):
""" Display a decimal number 00-99 """
self.i2.regwr(self.a, 2, b)
def dp(self, p0, p1):
""" Set the state the decimal point indicators """
self.i2.regwr(self.a, 3, (p1 << 1) | p0)
def brightness(self, b):
""" Set the brightness from 0 (off) to 255 (maximum) """
self.i2.regwr(self.a, 4, b)
class LED:
""" LED is an RGB LED """
def __init__(self, i2, a = 0x08):
self.i2 = i2
self.a = a
def rgb(self, r, g, b, t = 0):
"""
Set the color to (r,g,b). Each is a byte 0-255.
If t is nonzero, the change happens over t/30 seconds.
For example if t is 15 the color fades over a half-second.
"""
if t == 0:
self.i2.start(self.a, 0)
self.i2.write(bytes((0, r, g, b)))
@ -36,55 +53,92 @@ class LED:
self.i2.stop()
def hex(self, hhh, t = 0):
"""
Set the color to hhh, a 24-bit RGB color.
If t is nonzero, the change happens over t/30 seconds.
For example if t is 15 the color fades over a half-second.
"""
r = (hhh >> 16) & 0xff
g = (hhh >> 8) & 0xff
b = hhh & 0xff
self.rgb(r, g, b, t)
class Pot:
""" POT is an analog knob potentiometer """
def __init__(self, i2, a = 0x28):
self.i2 = i2
self.a = a
def read(self):
self.i2.start(self.a, 1)
(r,) = struct.unpack("B", self.i2.read(1))
self.i2.stop()
return r
def raw(self):
"""
Return the current knob rotation as a 16-bit integer.
"""
return self.i2.regrd(self.a, 0, "H")
def rd(self, r):
"""
Return the current knob rotation, scaled to the range 0 .. r
inclusive. For example rd(100) returns a value in the range 0 to 100.
"""
return self.i2.regrd(self.a, r)
class Beep:
""" BEEP is a beeper """
def __init__(self, i2, a = 0x30):
self.i2 = i2
self.a = a
def beep(self, dur, note):
"""
Play a note.
dur is the duration in centi-seconds.
note is a MIDI note in the range 21-127 inclusive.
"""
self.i2.regwr(self.a, dur, note)
class Remote:
""" REMOTE is a NEC IR code receiver / decoder """
def __init__(self, i2, a = 0x60):
self.i2 = i2
self.a = a
def key(self):
"""
For the electricdollarstore IR transmitter.
If there is a code in the queue, return its character code.
The layout of the remote is
p c n
< > ' '
- + =
0 % &
1 2 3
4 5 6
7 8 9
If there is no IR code in the queue, return None.
"""
while True:
r = self.i2.regrd(self.a, 0)
if r != 0:
return chr(r)
def raw(self):
"""
If there is a code in the queue, return a tuple containing the four-byte code,
and a timestamp.
If there is no IR code in the queue, return None.
"""
r = self.i2.regrd(self.a, 1, "4BH")
if r[:4] != (0xff, 0xff, 0xff, 0xff):
return r
age_in_ms = r[4]
return (r[:4], time.time() - age_in_ms * .001)
else:
return None
class Temp:
""" TEMP is a LM75B temperature sesnor """
def __init__(self, i2, a = 0x48):
self.i2 = i2
self.a = a
@ -93,4 +147,31 @@ class Temp:
return self.i2.regrd(self.a, r, ">h")
def read(self):
""" Return the current temperature in Celsius """
return (self.reg(0) >> 5) * 0.125
class EPROM:
""" EPROM is a CAT24C512 512 Kbit (64 Kbyte) flash memory """
def __init__(self, i2, a = 0x50):
self.i2 = i2
self.a = a
def write(self, addr, data):
""" Write data to EPROM, starting at address addr """
for i in range(0, len(data), 128):
self.i2.start(self.a, 0)
self.i2.write(struct.pack(">H", addr + i))
self.i2.write(data[i:i + 128])
self.i2.stop()
while self.i2.start(self.a, 0) == False:
pass
def read(self, addr, n):
""" Read n bytes from the EPROM, starting at address addr """
self.i2.start(self.a, 0)
self.i2.write(struct.pack(">H", addr))
self.i2.start(self.a, 1)
r = self.i2.read(n)
self.i2.stop()
return r
self.i2.stop()

View File

@ -0,0 +1,16 @@
import sys
import serial
import time
import struct
import random
from i2cdriver import I2CDriver, EDS
if __name__ == '__main__':
i2 = I2CDriver(sys.argv[1], True)
d = EDS.Beep(i2)
for note in range(55, 127):
d.beep(100, note)
time.sleep(.100)

View File

@ -0,0 +1,13 @@
import sys
import serial
import time
from i2cdriver import I2CDriver, EDS
if __name__ == '__main__':
i2 = I2CDriver(sys.argv[1])
d = EDS.Dig2(i2)
for i in range(100):
d.dec(i)
time.sleep(.05)

View File

@ -0,0 +1,61 @@
import sys
import time
from i2cdriver import I2CDriver, EDS
text = b"""\
CHAPTER 1. Loomings.
Call me Ishmael. Some years ago-never mind how long precisely-having little or no money in my
purse, and nothing particular to interest me on shore, I thought I would sail about a little
and see the watery part of the world. It is a way I have of driving off the spleen and
regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is
a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin
warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos
get such an upper hand of me, that it requires a strong moral principle to prevent me from
deliberately stepping into the street, and methodically knocking people's hats off-then, I
account it high time to get to sea as soon as I can. This is my substitute for pistol and ball.
With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship.
There is nothing surprising in this. If they but knew it, almost all men in their degree, some
time or other, cherish very nearly the same feelings towards the ocean with me.
There now is your insular city of the Manhattoes, belted round by wharves as Indian isles by
coral reefs-commerce surrounds it with her surf. Right and left, the streets take you
waterward. Its extreme downtown is the battery, where that noble mole is washed by waves, and
cooled by breezes, which a few hours previous were out of sight of land. Look at the crowds of
water-gazers there.
Circumambulate the city of a dreamy Sabbath afternoon. Go from Corlears Hook to Coenties Slip,
and from thence, by Whitehall, northward. What do you see?-Posted like silent sentinels all
around the town, stand thousands upon thousands of mortal men fixed in ocean reveries. Some
leaning against the spiles; some seated upon the pier-heads; some looking over the bulwarks of
ships from China; some high aloft in the rigging, as if striving to get a still better seaward
peep. But these are all landsmen; of week days pent up in lath and plaster-tied to counters,
nailed to benches, clinched to desks. How then is this? Are the green fields gone? What do they
here?
But look! here come more crowds, pacing straight for the water, and seemingly bound for a dive.
Strange! Nothing will content them but the extremest limit of the land; loitering under the
shady lee of yonder warehouses will not suffice. No. They must get just as nigh the water as
they possibly can without falling in. And there they stand-miles of them-leagues. Inlanders
all, they come from lanes and alleys, streets and avenues-north, east, south, and west. Yet
here they all unite. Tell me, does the magnetic virtue of the needles of the compasses of all
those ships attract them thither?
Once more. Say you are in the country; in some high land of lakes. Take almost any path you
please, and ten to one it carries you down in a dale, and leaves you there by a pool in the
stream. There is magic in it. Let the most absent-minded of men be plunged in his deepest
reveries-stand that man on his legs, set his feet a-going, and he will infallibly lead you to
water, if water there be in all that region. Should you ever be athirst in the great American
desert, try this experiment, if your caravan happen to be supplied with a metaphysical
professor. Yes, as every one knows, meditation and water are wedded for ever.
"""
if __name__ == '__main__':
i2 = I2CDriver(sys.argv[1])
d = EDS.EPROM(i2)
d.write(0, text) # Write the block of text starting at address 0
n = len(text)
rd = d.read(0, n) # Read it back
print(rd.decode()) # Display it