Saturday, June 23, 2007


Yea, ok, I'm a geek. I'm an old geek. I like analog clocks, but my favorite one kept loosing time on occasion. So I scrounged an unused analog clock with a quartz movement from around the house to experiment on, pulled out the tools and performed a bit of surgery. I disconnected the drive coil from the rest of the circuit and soldered 2 wires on to it, put it back together and called the surgery a success. Then added a trimpot resistor inline with one of the wires, and connected them up to the parallel port to D0 and D1 / pin 2 and pin 3. The trimpot is a 5k resistor, but is currently set to around 600 ohms.
I connected this up to my linux machines' parallel port. The following python code is
what I'm currently running with. It needs to be run under root to access the /dev/port device correctly.

Happy Hacking, Toby Lane.


When starting this program, the clock hand positions can be given, e.g.
> clock 4 45 33
which tells the program that the clock hour hand is at 4, the minute is at 45, and the second is pointing to 33. The program will speed up or wait for the system clock and the analog clock to come together.

clock:

#!/usr/bin/env python
import time, os, sys

direction=0
b0='\000'
b1='\001'
b2='\002'

if len(sys.argv) > 1:
clock = int(sys.argv[1]) * 3600 + int(sys.argv[2])*60 + int(sys.argv[3])
else:
if os.path.isfile("/root/clock"):
clock = int( open("/root/clock").readline().strip() )
else:
clock = 0
wait = 0.97
p=open("/dev/port","w")
try:
while 1:
if direction: d = b2
else: d = b1
direction = 1-direction
p.seek(0x378)
p.write(d)
p.flush()
time.sleep( 0.0350 )
p.seek(0x378)
p.write(b0)
p.flush()
clock += 1
if clock >= 46800: clock = 3600
cur = list(time.localtime()[3:6])
if cur[0] > 12: cur[0]-=12
if cur[0] < 1: cur[0] += 12
cur = cur[0]* 3600 + cur[1]*60 + cur[2]

dif = cur - clock
# print "clock",clock," cur",cur," dif",dif, " wait",wait
if dif < -5: time.sleep(5)
elif dif < -1: time.sleep(2)
elif dif == -1:
time.sleep(1)
wait = min( 2, wait+0.001)
elif dif == 0: time.sleep(wait)
elif dif == 1:
time.sleep(0.9)
wait = max( 0.5, wait-0.001 )
elif dif > 1: time.sleep(0.4)
except:
open("/root/clock","w").write( "%s\n"%clock )

1 comment: