Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/memory/erase-ow.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(self, link_uri):

# Variable used to keep main loop occupied until disconnect
self.is_connected = True
self.should_disconnect = False

def _connected(self, link_uri):
""" This callback is called form the Crazyflie API when a Crazyflie
Expand Down Expand Up @@ -97,7 +98,7 @@ def _data_updated(self, mem):
for key in mem.elements:
print('\t\t{}={}'.format(key, mem.elements[key]))

self._cf.close_link()
self.should_disconnect = True

def _stab_log_error(self, logconf, msg):
"""Callback from the log API when an error occurs"""
Expand Down Expand Up @@ -137,6 +138,8 @@ def _disconnected(self, link_uri):
# are just waiting until we are disconnected.
try:
while le.is_connected:
if le.should_disconnect:
le._cf.close_link()
time.sleep(1)
except KeyboardInterrupt:
sys.exit(1)
19 changes: 9 additions & 10 deletions examples/memory/flash-memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Flash the DS28E05 EEPROM via CRTP.
"""
import datetime
import os
import sys
import time

Expand Down Expand Up @@ -50,6 +49,7 @@ def __init__(self, link_uri):

# Initialize variables
self.connected = False
self.should_disconnect = False

# Public methods

Expand Down Expand Up @@ -201,11 +201,7 @@ def abort():
# Callback function when data has been written
def data_written(mem, addr):
print('Data has been written to memory!')
flasher.disconnect()

# We need to use os.kill because this is a callback
SIGTERM = 15
os.kill(os.getpid(), SIGTERM)
flasher.should_disconnect = True

# Write data
sure = input('Are you sure? [y/n] ')
Expand All @@ -214,9 +210,12 @@ def data_written(mem, addr):
abort()
mem.write_data(data_written)

# Timeout 10 seconds
# Wait for completion or timeout (10 seconds)
for _ in range(10 * 2):
if flasher.should_disconnect:
flasher.disconnect()
break
time.sleep(0.5)
print('Apparently data could not be written to memory... :(')

flasher.disconnect()
else:
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else clause on the for loop will execute when the loop completes without breaking, meaning the timeout occurred. However, if flasher.should_disconnect becomes True on the last iteration, the loop will break but flasher.disconnect() won't be called. Consider moving the timeout disconnect logic outside the loop or handling this case explicitly.

Suggested change
else:
if not flasher.should_disconnect:

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 seconds is very conservative. I am not sure it is necessary to add code for a potential race on the last 0.5 second.

print('Apparently data could not be written to memory... :(')
flasher.disconnect()
5 changes: 4 additions & 1 deletion examples/memory/read-eeprom.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, link_uri):

# Variable used to keep main loop occupied until disconnect
self.is_connected = True
self.should_disconnect = False

def _connected(self, link_uri):
""" This callback is called form the Crazyflie API when a Crazyflie
Expand All @@ -88,7 +89,7 @@ def _data_updated(self, mem):

self._mems_to_update -= 1
if self._mems_to_update == 0:
self._cf.close_link()
self.should_disconnect = True

def _stab_log_error(self, logconf, msg):
"""Callback from the log API when an error occurs"""
Expand Down Expand Up @@ -126,6 +127,8 @@ def _disconnected(self, link_uri):
# are just waiting until we are disconnected.
try:
while le.is_connected:
if le.should_disconnect:
le._cf.close_link()
time.sleep(1)
except KeyboardInterrupt:
sys.exit(1)
5 changes: 4 additions & 1 deletion examples/memory/write-eeprom.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(self, link_uri):

# Variable used to keep main loop occupied until disconnect
self.is_connected = True
self.should_disconnect = False

def _connected(self, link_uri):
""" This callback is called form the Crazyflie API when a Crazyflie
Expand Down Expand Up @@ -100,7 +101,7 @@ def _data_updated(self, mem):
for key in mem.elements:
print('\t\t{}={}'.format(key, mem.elements[key]))

self._cf.close_link()
self.should_disconnect = True

def _stab_log_error(self, logconf, msg):
"""Callback from the log API when an error occurs"""
Expand Down Expand Up @@ -138,6 +139,8 @@ def _disconnected(self, link_uri):
# are just waiting until we are disconnected.
try:
while le.is_connected:
if le.should_disconnect:
le._cf.close_link()
time.sleep(1)
except KeyboardInterrupt:
sys.exit(1)
5 changes: 4 additions & 1 deletion examples/memory/write-ow.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(self, link_uri):

# Variable used to keep main loop occupied until disconnect
self.is_connected = True
self.should_disconnect = False

def _connected(self, link_uri):
""" This callback is called form the Crazyflie API when a Crazyflie
Expand Down Expand Up @@ -103,7 +104,7 @@ def _data_updated(self, mem):
for key in mem.elements:
print('\t\t{}={}'.format(key, mem.elements[key]))

self._cf.close_link()
self.should_disconnect = True

def _stab_log_error(self, logconf, msg):
"""Callback from the log API when an error occurs"""
Expand Down Expand Up @@ -141,6 +142,8 @@ def _disconnected(self, link_uri):
# are just waiting until we are disconnected.
try:
while le.is_connected:
if le.should_disconnect:
le._cf.close_link()
time.sleep(1)
except KeyboardInterrupt:
sys.exit(1)
7 changes: 5 additions & 2 deletions examples/parameters/basicparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(self, link_uri):

# Variable used to keep main loop occupied until disconnect
self.is_connected = True
self.should_disconnect = False

self._param_check_list = []
self._param_groups = []
Expand Down Expand Up @@ -129,8 +130,8 @@ def _a_pitch_kd_callback(self, name, value):
"""Callback for pid_attitude.pitch_kd"""
print('Read back: {0}={1}'.format(name, value))

# This is the end of the example, close link
self._cf.close_link()
# This is the end of the example, signal to close link
self.should_disconnect = True

def _connection_failed(self, link_uri, msg):
"""Callback when connection initial connection fails (i.e no Crazyflie
Expand Down Expand Up @@ -160,4 +161,6 @@ def _disconnected(self, link_uri):
# alive, so this is where your application should do something. In our
# case we are just waiting until we are disconnected.
while pe.is_connected:
if pe.should_disconnect:
pe._cf.close_link()
time.sleep(1)