Skip to content

Commit 3118896

Browse files
committed
daplink_flash: Address Copilot review on PR #164.
1 parent d21737a commit 3118896

3 files changed

Lines changed: 22 additions & 19 deletions

File tree

lib/daplink_flash/daplink_flash/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
# Protocol limits
3131
MAX_WRITE_CHUNK = const(30)
3232
SECTOR_SIZE = const(256)
33+
MAX_SECTORS = const(32768)
3334
FILENAME_LEN = const(8)
3435
EXT_LEN = const(3)

lib/daplink_flash/daplink_flash/device.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def busy(self):
5555
"""Return True if flash is busy."""
5656
return bool(self._status() & STATUS_BUSY)
5757

58-
def _wait_busy(self, timeout_ms=1000):
58+
def _wait_busy(self, timeout_ms=30000):
5959
"""Poll busy bit until clear. Raises OSError on timeout."""
6060
elapsed = 0
6161
while self.busy():
@@ -114,6 +114,10 @@ def write(self, data):
114114
buf[i] = 0
115115
self.i2c.writeto(self.address, buf)
116116
offset += chunk_len
117+
self._wait_busy()
118+
err = self._error()
119+
if err:
120+
raise OSError("DAPLink Flash write error: 0x{:02X}".format(err))
117121
return length
118122

119123
def write_line(self, text):
@@ -152,12 +156,16 @@ def read(self, length=None):
152156
"""
153157
result = bytearray()
154158
sector = 0
155-
while True:
159+
while sector < MAX_SECTORS:
156160
data = self.read_sector(sector)
157161
for i in range(SECTOR_SIZE):
158-
if data[i] == 0xFF:
159-
return bytes(result)
160-
result.append(data[i])
161-
if length is not None and len(result) >= length:
162-
return bytes(result)
162+
if length is not None:
163+
result.append(data[i])
164+
if len(result) >= length:
165+
return bytes(result)
166+
else:
167+
if data[i] == 0xFF:
168+
return bytes(result)
169+
result.append(data[i])
163170
sector += 1
171+
return bytes(result)

lib/daplink_flash/examples/sensor_log.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,12 @@
7575
hum_sum += h
7676
pres_sum += p
7777

78-
if t < temp_min:
79-
temp_min = t
80-
if t > temp_max:
81-
temp_max = t
82-
if h < hum_min:
83-
hum_min = h
84-
if h > hum_max:
85-
hum_max = h
86-
if p < pres_min:
87-
pres_min = p
88-
if p > pres_max:
89-
pres_max = p
78+
temp_min = min(temp_min, t)
79+
temp_max = max(temp_max, t)
80+
hum_min = min(hum_min, h)
81+
hum_max = max(hum_max, h)
82+
pres_min = min(pres_min, p)
83+
pres_max = max(pres_max, p)
9084

9185
count += 1
9286

0 commit comments

Comments
 (0)