-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicturePainter.py
85 lines (73 loc) · 2.44 KB
/
PicturePainter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import csv
import tkinter as tk
from tkinter import filedialog
import tkMessageBox
# Start of Colour Constants
B = "#$00"
W = "#$01"
R = "#$02"
C = "#$03"
P = "#$04"
G = "#$05"
BL= "#$06"
Y = "#$07"
O = "#$08"
BR = "#$09"
LR = "#$0A"
DG = "#$B"
GR = "#$0C"
LG = "#$0D"
LB = "#$0E"
# End of Colour Constants
def getPictureFilenameFromUser():
file_opt = options = {}
options['defaultextension'] = '.csv'
options['filetypes'] = [('CSV files', '.csv')]
options['title'] = 'This is a title'
file_path = filedialog.askopenfilename(**file_opt)
if(file_path == ""):
tkMessageBox.showerror("No File Selected!","You didn't select a file for the converter to convert as a result the programme will now terminate!")
exit()
return file_path
def parseCsvFileIntoList(filename):
raw_data = open(filename)
csv_data = csv.reader(raw_data)
data_as_list = list(csv_data)
return data_as_list
def generateAssemblyCode(csvData):
code_blocks = []
currentMemoryLocation = 511
count=0
for row in csvData:
for value in row:
currentMemoryLocation += 1
count +=1
if not (value == "B"):
try:
code_blocks.append( "LDA " + eval(str(value).upper())+"\nSTA $0"+hex(currentMemoryLocation)[2:5] + "\n")
except:
tkMessageBox.showerror("Invalid CSV File","The CSV file you requested to be converted is invalid. The program will then terminate")
exit()
print count
return code_blocks
def getOutputFilenameFromUser():
file_opt = options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('Text files', '.txt')]
options['title'] = 'This is a title'
file_path = filedialog.asksaveasfilename(**file_opt)
return file_path
def saveAssemblyCodeToFile(instructionsToSave,path):
output=open(path,'w')
for block in instructionsToSave:
output.write(block)
tkMessageBox.showinfo("Conversion Successful","The CSV file has successfully been converted into assembly code. The output of the converter has been saved to "+path)
def main():
root = tk.Tk()
root.withdraw()
picture_file_path = getPictureFilenameFromUser()
values_list = parseCsvFileIntoList(picture_file_path)
instruction_list = generateAssemblyCode(values_list)
output_path = getOutputFilenameFromUser()
saveAssemblyCodeToFile(instruction_list,output_path)
main()