Skip to content

Instantly share code, notes, and snippets.

@bengerman13
Last active May 2, 2023 04:55
Show Gist options
  • Select an option

  • Save bengerman13/5f15735fd2184d78d03f8bd2dba34587 to your computer and use it in GitHub Desktop.

Select an option

Save bengerman13/5f15735fd2184d78d03f8bd2dba34587 to your computer and use it in GitHub Desktop.
import time
from adafruit_circuitplayground.express import cpx
minolta_speeds = [1,2,4,8,15,30,60,125,250,500]
yashica_speeds = [1,2,5,10,25,50,100,300]
class ShutterSpeed:
def __init__(self, reciprocal):
self.reciprocal = reciprocal
self.nanos = speed_to_nano(reciprocal)
def __str__(self) -> str:
if self.reciprocal == 1:
return str(self.reciprocal)
else:
return "1/{}".format(self.reciprocal)
def calibrate():
vals = []
for _ in range(10):
time.sleep(0.1)
vals.append(cpx.light)
# nothing special about 1.4. I found this by testing.
# 1.3 gives false highs. 1.4 doesn't
return max(vals) * 1.4
def speed_to_nano(speed):
return int(1e9/speed)
def get_speeds():
if cpx.switch:
speeds = yashica_speeds
print("I think this is a yashica. toggle the switch for minolta")
else:
speeds = minolta_speeds
print("I think this is a minolta. toggle the switch for yashica")
return [ShutterSpeed(i) for i in speeds]
def closest_speed(speed: int, potentials: list[ShutterSpeed]) -> ShutterSpeed:
last = None
closest = None
for potential in sorted(potentials, key=lambda x: x.nanos):
if speed < potential.nanos:
if last is None:
# speed is faster than the fastest
closest = potential
else:
ldiff = abs(last.nanos - speed)
pdiff = abs(potential.nanos - speed)
if pdiff > ldiff:
closest = last
else:
closest = potential
else:
last = potential
if closest is None:
# speed is slower than any potential
closest = potential
return closest
def report(speed: int, closest: ShutterSpeed):
print("shutter was open for approximately {} ns".format(speed))
print("this is closest to {}".format(str(closest)))
difference = 100 * speed/closest.nanos
print("The speed is {:.2f}% the speed of named speed".format(difference))
def main():
threshold = calibrate()
speeds = get_speeds()
print("calibrated!")
cross_up = None
cross_down = None
low = False
while True:
if low:
if cpx.light > threshold:
cross_up = time.monotonic_ns()
low = False
elif cpx.light < threshold:
cross_down = time.monotonic_ns()
if cross_up is not None:
diff = cross_down - cross_up
closest = closest_speed(diff, speeds)
report(diff, closest)
low = True
if __name__ == "__main__":
main()
@gravitytrope
Copy link

So good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment