Skip to content

Instantly share code, notes, and snippets.

@chinmaydd
Last active June 24, 2018 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chinmaydd/96e42fd75cc74446b27190efb5c9f060 to your computer and use it in GitHub Desktop.
Save chinmaydd/96e42fd75cc74446b27190efb5c9f060 to your computer and use it in GitHub Desktop.
Wired CSV
import csv
import pdb
# 0: Time
# 1: Wire6 Analog
# 2: Wire7 Analog
# 3: Time
# 4: Wire 0 Digital
# 5: Time
# 6: Wire 1 Digital
# 7: Time
# 8: Wire 2 Digital
# 9: Time
# 10: Wire 3 Digital
# 11: Time
# 12: Wire 4 Digital
# 13: Time
# 14: Wire 5 Digital
# 15: Time
# 16: Wire KR1 Digital
# 17: Time
# 18: Wire KR2 Digital
# Write changes in KR1 to a file (write uptick timings only?)
def kr1_changes_writer():
with open('data.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
# Skip first line
reader.next()
time_changes_in_kr1 = [row[15] for row in reader if (row[15] != " " and int(row[16]) == 1)]
print len(time_changes_in_kr1)
f = open('kr1_changes.txt', 'w')
for time in time_changes_in_kr1:
f.write(time)
f.write("\n")
return time_changes_in_kr1
# Once KR1 changes are written to a file, load them next time to save on computation
def load_kr1_change_file():
return open('kr1_changes.txt', 'r').read().splitlines()
# Wire Class PogChamp
class Wire:
def __init__(self, wireNumber=None, time_changes_in_kr1=[]):
self.krIndex = 0
self.currTime = 0.0
self.currValue = 0
self.wireNumber = wireNumber
self.time_changes_in_kr1 = time_changes_in_kr1
self.Values = []
# Update internal state
def update_curr_values(self, newData):
if self.krIndex == len(time_changes_in_kr1):
return
newTime = newData[0]
newValue = newData[1]
if newTime > self.time_changes_in_kr1[self.krIndex]:
self.Values.append((self.currTime, self.currValue))
self.krIndex += 1
self.currValue = newValue
self.currTime = newTime
# Write values to file at the end
def write_values(self):
f = open("wire_" + str(self.wireNumber), 'w')
for datapoint in self.Values:
f.write(str(datapoint[0]))
f.write(',')
f.write(str(datapoint[1]))
f.write('\n')
# Get values for each wire from row of data
def get_curr_values(row):
curr_values = []
for i in [3,5,7,9,11,13,17]:
try:
curr_values.append((float(row[i]), int(row[i+1])))
except:
curr_values.append((0.0, 0))
return curr_values
if __name__=="__main__":
# Should be True for the first run
WriteKR1 = True
if WriteKR1:
time_changes_in_kr1 = kr1_changes_writer()
else:
time_changes_in_kr1 = load_kr1_change_file()
# Load up floats
time_changes_in_kr1 = [float(x) for x in time_changes_in_kr1]
# Wire inits
wire0 = Wire(0, time_changes_in_kr1)
wire1 = Wire(1, time_changes_in_kr1)
wire2 = Wire(2, time_changes_in_kr1)
wire3 = Wire(3, time_changes_in_kr1)
wire4 = Wire(4, time_changes_in_kr1)
wire5 = Wire(5, time_changes_in_kr1)
# Special number
kr_2 = Wire(6, time_changes_in_kr1)
with open('data.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
reader.next()
# Read only once
for row in reader:
curr_values = get_curr_values(row)
# Update internal state based on current values
wire0.update_curr_values(curr_values[0])
wire1.update_curr_values(curr_values[1])
wire2.update_curr_values(curr_values[2])
wire3.update_curr_values(curr_values[3])
wire4.update_curr_values(curr_values[4])
wire5.update_curr_values(curr_values[5])
kr_2.update_curr_values(curr_values[6])
# Write all data to a file
wire0.write_values()
wire1.write_values()
wire2.write_values()
wire3.write_values()
wire4.write_values()
wire5.write_values()
kr_2.write_values()
{"6 ": "+", "56": "F", "42": "E", "8 ": "O", "22": "X", "29": "5", "60": "CAPS/LOWR", "61": "G", "62": "S", "63": "A", "35": "N", "32": ",", "24": "4", "26": "3", "27": "6", "20": "F4", "21": "B", "48": "9", "23": "Z", "46": "W", "47": "Q", "44": "TAB", "45": "T", "28": "ESC", "43": "Y", "40": "R", "0 ": "L", "2 ": ";", "5 ": "K", "4 ": "F2", "7 ": "*", "33": "SPACE BAR", "39": "Atari logo", "34": ".", "38": "/", "19": "F3", "58": "D", "11": "U", "10": "P", "13": "I", "12": "RETURN", "15": "=", "14": "-", "17": "HELP", "16": "V", "55": ">", "54": "<", "31": "1", "30": "2", "51": "7", "50": "0", "53": "8", "52": "DEL", "1 ": "J", "37": "M", "18": "C", "3 ": "F1", "57": "H"}
# IMP: File uses data from data_collector.py run
import json
# Load all wire data
def load_data():
wireData = {}
for i in range(0, 7):
wireData[i] = []
f = open('wire_' + str(i), 'r').read().splitlines()
for row in f:
wireData[i].append(int(row.split(',')[1]))
return wireData
# Class to handle button click event on keyboard (connected)
class Event:
def __init__(self, wireValues=[], isKR2=False):
self.isShift = False
self.isCtrl = False
self.isBreak = False
self.wireValues = wireValues
self.isKR2 = isKR2
if isKR2:
if wireValues[0] == 1 and wireValues[1] == 1 and wireValues[2] == 1 and wireValues[3] == 1 and wireValues[4] == 0 and wireValues[5] == 0:
self.isBreak = True
if wireValues[0] == 1 and wireValues[1] == 1 and wireValues[2] == 1 and wireValues[3] == 1 and wireValues[4] == 0 and wireValues[5] == 1:
self.isShift = True
if wireValues[0] == 1 and wireValues[1] == 1 and wireValues[2] == 1 and wireValues[3] == 1 and wireValues[4] == 1 and wireValues[5] == 1:
self.isCtrl = True
self.keyCode = int(''.join(str(x) for x in reversed(wireValues)), 2)
# Load KR1 changes file
def load_kr1_change_file():
return open('kr1_changes.txt', 'r').read().splitlines()
if __name__=="__main__":
# Load all data
wireData = load_data()
KR1Changes = load_kr1_change_file()
events = []
# Create event for each data
# TODO: Debounce
for i in range(0, len(KR1Changes)):
wireValues = []
for j in range(0, 6):
wireValues.append(wireData[j][i])
try:
isKR2 = wireData[6][i]
except:
isKR2 = False
event = Event(wireValues, isKR2)
events.append(event)
# We now have all events
# https://www.atariarchives.org/c3ba/page004.php
keyboard_map = json.load(open('keyboard_map', 'r'))
# Holding down SHIFT while depressing another key produces that same number plus 64. Holding down CONTROL produces that numer plus 128.
for event in events:
try:
if event.isShift:
print keyboard_map[str(event.keyCode - 64)],
elif event.isCtrl:
print keyboard_map[str(event.keyCode - 128)],
else:
print keyboard_map[str(event.keyCode)],
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment