Skip to content

Commit e769e20

Browse files
committed
Send initial packet to HW, even a normal exit
1 parent e4ba863 commit e769e20

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

neopia/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# Boston, MA 02111-1307 USA
1818

1919
import signal
20+
import atexit
2021

2122
from neopia.scanner import Scanner
2223
from neopia.mode import Mode
@@ -63,8 +64,15 @@ def while_do(condition, do, args=None):
6364
def parallel(*functions):
6465
Runner.parallel(functions)
6566

67+
# It's called when an abnormal exit as CTRL+C
6668
def _handle_signal(signal, frame):
6769
Runner.shutdown()
6870
raise SystemExit
6971

70-
signal.signal(signal.SIGINT, _handle_signal)
72+
signal.signal(signal.SIGINT, _handle_signal)
73+
74+
# It's called for a safe exit by sending initial packet to HW, even a normal exit
75+
def exit_handler():
76+
Runner.shutdown()
77+
78+
atexit.register(exit_handler)

neopia/neosoco_neobot.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ def _release(self):
146146
connector = self._connector
147147
self._connector = None
148148
if connector:
149+
# Lastly send init packet to stop all action in the controller
150+
self._encode_init_packet()
151+
self._send(connector)
149152
connector.close()
150153

151154
def _dispose(self):
@@ -188,32 +191,32 @@ def _speed_to_gain(self, speed):
188191
elif speed < 1: speed = 1
189192
return NeosocoNeobot._SPEED_TO_GAIN[speed]
190193
return 2
191-
192-
def _encode_motoring_packet(self):
194+
195+
def _encode_init_packet(self):
193196
result = ""
194197
with self._thread_lock:
195198
result += START_BYTES
196-
result += self._to_hex(self._output_1) # OUT1
197-
result += self._to_hex(self._output_2) # OUT2
198-
result += self._to_hex(self._output_3) # OUT3
199-
result += self._to_hex(self._left_motor) # MLA
200-
result += self._to_hex(self._right_motor) # MRA
201-
result += self._to_hex(self._note) # BUZZER
199+
result += self._to_hex(0) # OUT1
200+
result += self._to_hex(0) # OUT2
201+
result += self._to_hex(0) # OUT3
202+
result += self._to_hex(0) # MLA
203+
result += self._to_hex(0) # MRA
204+
result += self._to_hex(0) # BUZZER
202205
result += self._to_hex(0) # FND
203206
result += self._to_hex(0) # Not Used
204207
result += self._to_hex(self._make_checksum(result)) # Checksum
205208
return result
206209

207-
def _encode_init_packet(self):
210+
def _encode_motoring_packet(self):
208211
result = ""
209212
with self._thread_lock:
210213
result += START_BYTES
211-
result += self._to_hex(0) # OUT1
212-
result += self._to_hex(0) # OUT2
213-
result += self._to_hex(0) # OUT3
214-
result += self._to_hex(0) # MLA
215-
result += self._to_hex(0) # MRA
216-
result += self._to_hex(0) # BUZZER
214+
result += self._to_hex(self._output_1) # OUT1
215+
result += self._to_hex(self._output_2) # OUT2
216+
result += self._to_hex(self._output_3) # OUT3
217+
result += self._to_hex(self._left_motor) # MLA
218+
result += self._to_hex(self._right_motor) # MRA
219+
result += self._to_hex(self._note) # BUZZER
217220
result += self._to_hex(0) # FND
218221
result += self._to_hex(0) # Not Used
219222
result += self._to_hex(self._make_checksum(result)) # Checksum

0 commit comments

Comments
 (0)