Skip to content

Instantly share code, notes, and snippets.

@hospadar
Created November 25, 2014 03:57
Show Gist options
  • Save hospadar/e8706847fc588da4e36a to your computer and use it in GitHub Desktop.
Save hospadar/e8706847fc588da4e36a to your computer and use it in GitHub Desktop.
Python script to count keypresses. Used for winding pickups.
from __future__ import print_function
import sys, time
from collections import deque
import curses, traceback
'''
Simple counter script
Originally used for counting pickup windings for an electric violin pickup.
Of course it would work well for any pickup winding activity.
Also might be useful any time you want to count something robotically.
Once started, any key will increment the count.
There's also an "RPM" counter which computes a "revolutions per minute"
based on the last 60 samples.
If you botch something and need to kill the script or back off the count, the script can
be restarted with a single arg to start the counter at some non-zero value.
Usage:
to start at 0:
python counter.py
To start at some other number:
python counter.py 500
When I used this initially, I wired a microswitch up to a an old keyboard.
'''
try:
stdscr = curses.initscr()
stdscr.refresh()
count = 0
times = deque([time.time()], 60)
rpm = 0
if len(sys.argv) > 1:
count = int(sys.argv[1])
stdscr.addstr("Turns: {}\n".format(count))
stdscr.addstr("RPM: {}\n".format(rpm))
stdscr.refresh()
while True:
try:
c = stdscr.getch()
stdscr.clear()
count += 1
now = time.time()
times.append(now)
interval = now-list(times)[0]
rpm = (float(len(times))/interval)*60.0
start = now
stdscr.addstr("Turns: {}\n".format(count))
stdscr.addstr("RPM: {}\n".format(rpm))
stdscr.refresh()
except KeyboardInterrupt:
curses.endwin()
break
except:
curses.endwin()
traceback.print_exc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment