Skip to content

Commit dbebfed

Browse files
committed
Added Singleton reference for the Arduino interface
The ArduinoInterface now can be access using a global reference. This commit has a big bug with Arduino Board Manager windows close (it refuse to close).
1 parent 591de05 commit dbebfed

File tree

4 files changed

+63
-24
lines changed

4 files changed

+63
-24
lines changed

MLC/GUI/Experiment/ArduinoConfigManager/ArduinoBoardManager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class ArduinoBoardManager:
1919

20-
def __init__(self, protocol_config, serial_config, parent_win=None):
20+
def __init__(self, protocol_config, serial_config, close_handler, parent_win=None):
2121
self.__setup = protocol_config
2222
self.__connection_config = serial_config
2323
self.__main_window = BoardConfigurationWindow(
@@ -30,6 +30,8 @@ def __init__(self, protocol_config, serial_config, parent_win=None):
3030
serial.STOPBITS_ONE, serial.STOPBITS_ONE_POINT_FIVE, serial.STOPBITS_TWO]
3131
self.BYTE_SIZE = [
3232
serial.EIGHTBITS, serial.FIVEBITS, serial.SIXBITS, serial.SEVENBITS]
33+
#FIXME the connection with the handler shold be made by a method of the window
34+
self.__main_window.on_close_signal.connect(close_handler)
3335

3436
def get_protocol_config(self):
3537
return self.__setup

MLC/GUI/Experiment/ArduinoConfigManager/BoardConfigurationWindow.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
from MLC.GUI.Autogenerated.autogenerated import Ui_BoardConfigurationWindow
99
from MLC.GUI.Experiment.ArduinoConfigManager.ArduinoBoardDialog import ArduinoBoardDialog
1010
from MLC.GUI.Experiment.ArduinoConfigManager.ArduinoConnectionDialog import ArduinoConnectionDialog
11+
from PyQt5.QtCore import pyqtSignal
1112

1213
from MLC.GUI.Experiment.ArduinoConfigManager.Common import create_local_full_path
1314

1415

1516
class BoardConfigurationWindow(QMainWindow):
17+
# * The setup (all the things configured)
18+
# * The serial connection setup
19+
on_close_signal = pyqtSignal([list, list])
1620

1721
def __init__(self, controller, boards, setup, parent=None):
1822
super(BoardConfigurationWindow, self).__init__(parent)
@@ -35,6 +39,12 @@ def __init__(self, controller, boards, setup, parent=None):
3539
self.update()
3640
self.ui.arduinoBoard.currentIndexChanged.connect(self.index_change)
3741

42+
def closeEvent(self, event):
43+
board_setup = [self.__controller.get_protocol_config()]
44+
connection_cfg = [self.__controller.get_connection_config()]
45+
self.on_close_signal.emit(board_setup, connection_cfg)
46+
super(BoardConfigurationWindow, self).closeEvent(event)
47+
3848
def update(self):
3949
aux_idx = 0
4050
self.ui.digitalPins.clear()

MLC/GUI/Experiment/ExperimentWindow.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
class ExperimentWindow(QMainWindow):
4242
experiment_finished = pyqtSignal([bool])
43+
4344
MAX_GENERATIONS = 30
4445

4546
def __init__(self, mlc_local,
@@ -285,29 +286,9 @@ def on_board_config_button_clicked(self):
285286
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG_BUTTON] - Executing on_board_config_button_clicked function')
286287
#FIXME Connection config name should not be related with "serial"
287288
valid_connection = False
288-
while not valid_connection:
289-
board_config_window = ArduinoBoardManager(protocol_config=self._board_config, serial_config=self._serial_conn, parent_win=self)
290-
board_config_window.start()
291-
# The namedtuples are immutables, so we must replace to reference in order to keep the changes in the configuration
292-
self._board_config = board_config_window.get_protocol_config()
293-
self._serial_conn = board_config_window.get_connection_config()
294-
print self._serial_conn
295-
296-
try:
297-
self._board_config = self._board_config._replace(connection = SerialConnection(**self._serial_conn._asdict()))
298-
valid_connection = True
299-
except:
300-
selection = QMessageBox.critical(self, "Connection failure",
301-
"The current connection setup failed during initialization. Do you want to change this configuration? \
302-
(Choosing \"no\" means that the board will not be usable in the experiment)"
303-
.format(preev_path),
304-
QMessageBox.Yes | No, QMessageBox.Yes)
305-
if selection == QMessageBox.Yes:
306-
pass
307-
else:
308-
#FIXME: If the pin setups aren't empty then the protocol init will fail
309-
self._board_config = self._board_config._replace(connection = BaseConnection())
310-
valid_connection = True
289+
board_config_window = ArduinoBoardManager(protocol_config=self._board_config, serial_config=self._serial_conn,
290+
close_handler=self.on_board_config_button_clicked, parent_win=self)
291+
board_config_window.start()
311292

312293
def _config_table_edited(self, left, right):
313294
config_table = self._autogenerated_object.config_table
@@ -580,3 +561,34 @@ def _update_experiment(self, cancelled):
580561
self._update_individuals_per_generation_list()
581562
self._update_experiment_info()
582563
self._update_individuals_figure()
564+
565+
def _store_board_configuration(self, board_config, serial_conn):
566+
# Pass the parameter as a list to mock PyQt fixed data types
567+
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG] - '
568+
'Board has been configured'
569+
.format(self._experiment_name))
570+
571+
self._board_config = board_config[0]
572+
self._serial_conn = serial_conn[0]
573+
574+
# Init the arduino singleton
575+
try:
576+
self._board_config = self._board_config._replace(connection = SerialConnection(**self._serial_conn._asdict()))
577+
#ArduinoInterfaceSingleton.get_instance(protocol_setup=self._board_config,
578+
# conn_setup=self._serial_conn)
579+
except Exception, err:
580+
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG] - '
581+
'Serial port could not be initialized. Error Msg: {1}'
582+
.format(self._experiment_name, err))
583+
selection = QMessageBox.critical(self, "Connection failure",
584+
"The current connection setup failed during initialization. Do you want to change this configuration? \
585+
(Choosing \"no\" means that the board will not be usable in the experiment)"
586+
.format(preev_path),
587+
QMessageBox.Yes | No, QMessageBox.Yes)
588+
if selection == QMessageBox.Yes:
589+
#self.on_board_config_button_clicked()
590+
print "FUCK"
591+
else:
592+
#FIXME: If the pin setups aren't empty then the protocol init will fail
593+
self._board_config = self._board_config._replace(connection = BaseConnection())
594+

MLC/arduino/protocol.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@
2020
PIN_MODES = collections.namedtuple(
2121
'PIN_MODES', ['INPUT', 'OUTPUT'], verbose=False)(INPUT=0, OUTPUT=1)
2222

23+
class ArduinoInterfaceSingleton():
24+
_instance = None
25+
26+
def get_instance(cls, protocol_config=None, conn_setup=None):
27+
from MLC.arduino.connection.serialconnection import SerialConnection
28+
if protocol_setup and conn_setup:
29+
serial_conn = SerialConnection(**conn_setup)
30+
protocol_config._replace(connection, serial_conn)
31+
ArduinoInterfaceSingleton._instance = BuildSerial(protocol_config)
32+
33+
if ArduinoInterfaceSingleton._instance is None:
34+
raise Exception("ArduinoInterface was not configured.")
35+
36+
return ArduinoInterfaceSingleton._instance
37+
2338

2439
class ArduinoInterface:
2540
# 0=input 1=output -- wiring_constants.h

0 commit comments

Comments
 (0)