-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheeprom_controller.py
More file actions
239 lines (168 loc) · 6.03 KB
/
eeprom_controller.py
File metadata and controls
239 lines (168 loc) · 6.03 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from serial import Serial
from tqdm import tqdm
import time
import types
import random
import os
def int16Bytes(int16: int) -> bytes:
return int16.to_bytes(2, "big")
def readEEPROM(serial: Serial, address: int) -> bytes:
serial.reset_input_buffer()
serial.write(b"r" + int16Bytes(address))
return serial.read()
def writeEEPROM(serial: Serial, address: int, data: int):
serial.write(b"w" + int16Bytes(address) + data.to_bytes(1, "big"))
while serial.in_waiting == 0: # wait for okay byte
continue
serial.read() # clean up okay byte
debugFill = []
def loadEEPROM(serial: Serial, startAddress: int, data: bytes, callback: types.FunctionType = None):
global debugFill
serial.reset_input_buffer()
targetFill = 16 # target number of bytes to be filled in the arduino's buffer
count = len(data)
offset = 0
readCount = 0
command = b"l" + int16Bytes(startAddress) + int16Bytes(count)
serial.write(command)
iterations = targetFill
fill = targetFill
while offset < count:
if iterations > count - offset: # cap iterations to prevent over-iterating
iterations = count - offset
for i in range(iterations):
serial.write(data[offset])
offset += 1
waitingCount = serial.in_waiting
if waitingCount > 0:
readCount += waitingCount
serial.read(waitingCount)
if callback is not None:
callback(waitingCount)
# assume all sent bytes are not processed and end up in arduino's buffer
# fill = sendBytes - writtenBytes
fill = offset - readCount
print(fill)
# perform the number of iterations necessary to fill the arduino's buffer to target fill
iterations = targetFill - fill
while serial.in_waiting != count - readCount: # wait for last writes to okay
continue
if callback is not None:
callback(serial.in_waiting) # final bytes
serial.reset_input_buffer()
def dumpEEPROM(serial: Serial, startAddress: int, count: int, callback: types.FunctionType = None) -> bytes:
data = bytearray(count)
index = 0
serial.write(b"d" + int16Bytes(startAddress) + int16Bytes(count))
while index < count:
readBytes = serial.read()[0]
if callback is not None:
callback(1)
data[index] = readBytes
index += 1
return data
def writeFileEEPROM(filePath: str, serial: Serial):
data = []
# 1 byte file name length
# n bytes file name
# 2 byte file length
# n bytes file
fileName = os.path.basename(filePath)
print(fileName)
fileNameBytes = bytes(fileName, 'utf-8')
print(fileNameBytes)
print(len(fileNameBytes).to_bytes(1, "big"))
data.append(len(fileNameBytes).to_bytes(1, "big"))
data.append(fileNameBytes)
with open(filePath, "rb") as f:
binData = f.read()
data.append(len(binData).to_bytes(2, "big"))
for b in binData:
data.append(b.to_bytes(1, "big"))
loadEEPROM(serial, 0, data)
def readFileEEPROM(fileDirectory: str, serial: Serial):
fileNameLength = int.from_bytes(readEEPROM(serial, 0), "big")
fileNameBytes = dumpEEPROM(serial, 1, fileNameLength)
fileName = fileNameBytes.decode("utf-8")
fileLength = int.from_bytes(dumpEEPROM(serial, 1 + fileNameLength, 2), "big")
fileData = dumpEEPROM(serial, 1 + fileNameLength + 2, fileLength)
print(f"File name length: {fileNameLength}")
print(f"File name: {fileName}")
print(f"File length: {fileLength}")
print(os.path.join(fileDirectory, fileName))
with open(os.path.join(fileDirectory, fileName), "wb") as f:
f.write(fileData)
def tqdmDataBar(desc: str, total: int):
return tqdm(desc=desc, total=total, unit="b", unit_scale=True, unit_divisor=1024)
def testEEPROM(serial: Serial):
data = []
for i in range(32768):
r = random.randrange(256)
data.append(r.to_bytes(1, "big"))
t = tqdmDataBar("Load", len(data))
loadEEPROM(serial, 0, data, t.update)
t.close()
t = tqdmDataBar("Dump", len(data))
dataDump = dumpEEPROM(serial, 0, len(data), t.update)
t.close()
for i in range(len(data)):
readByte = dataDump[i]
expectedByte = data[i]
expectedInt = int.from_bytes(expectedByte, "big")
if readByte != expectedInt:
continue
print(
f"Read {readByte} but expected {expectedInt} at address {i.to_bytes(2, 'big').hex()}"
)
print("Done!")
def main():
global debugFill
serial = Serial("COM6", 115200, timeout=0.1)
time.sleep(2)
serial.reset_input_buffer()
serial.reset_output_buffer()
testEEPROM(serial)
serial.close()
return
#writeEEPROM(serial, 0, 0x0D)
with open("dump.bin", "wb") as f:
f.write(dumpEEPROM(serial, 0, 32768))
#writeFileEEPROM("cutemouse.jpg", serial)
#readFileEEPROM("./", serial)
return
data = []
startAddr = 0
# for i in range(32768):
# r = random.randrange(256)
# data.append(r.to_bytes(1, "big"))
with open("cat.jpg", "rb") as f:
binData = f.read()
for b in binData:
data.append(b.to_bytes(1, "big"))
print(f"Data length: {len(data)}")
t = tqdmDataBar("Load", len(data))
loadEEPROM(serial, startAddr, data, t.update)
t.close()
t = tqdmDataBar("Dump", len(data))
dataDump = dumpEEPROM(serial, startAddr, len(data), t.update)
t.close()
print(debugFill)
f = open("output.jpg", "wb")
f.write(dataDump)
f.close()
# return
for j in range(len(data)):
i = j + startAddr
readByte = dataDump[j]
expectedByte = data[j]
expectedInt = int.from_bytes(expectedByte, "big")
#print(i, end="\t")
if readByte != expectedInt:
continue
print(
f"Read {readByte} but expected {expectedInt} at address {i.to_bytes(2, 'big').hex()}"
)
print("Done!")
serial.close()
if __name__ == "__main__":
main()