diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index cb3580c..69c7e49 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -15,7 +15,6 @@ class ArduinoAlvik: - _update_thread_running = False _update_thread_id = None _touch_events_thread_running = False @@ -32,9 +31,9 @@ def __init__(self): self.right_wheel = _ArduinoAlvikWheel(self._packeter, ord('R')) self._led_state = list((None,)) self.left_led = self.DL1 = _ArduinoAlvikRgbLed(self._packeter, 'left', self._led_state, - rgb_mask=[0b00000100, 0b00001000, 0b00010000]) + rgb_mask=[0b00000100, 0b00001000, 0b00010000]) self.right_led = self.DL2 = _ArduinoAlvikRgbLed(self._packeter, 'right', self._led_state, - rgb_mask=[0b00100000, 0b01000000, 0b10000000]) + rgb_mask=[0b00100000, 0b01000000, 0b10000000]) self._battery_perc = None self._touch_byte = None self._behaviour = None @@ -234,7 +233,7 @@ def _stop_update_thread(cls): def _wait_for_target(self, idle_time): start = ticks_ms() while True: - if ticks_diff(ticks_ms(), start) >= idle_time*1000 and self.is_target_reached(): + if ticks_diff(ticks_ms(), start) >= idle_time * 1000 and self.is_target_reached(): break else: # print(self._last_ack) @@ -280,7 +279,7 @@ def rotate(self, angle: float, unit: str = 'deg', blocking: bool = True): uart.write(self._packeter.msg[0:self._packeter.msg_size]) self._waiting_ack = ord('R') if blocking: - self._wait_for_target(idle_time=(angle/MOTOR_CONTROL_DEG_S)) + self._wait_for_target(idle_time=(angle / MOTOR_CONTROL_DEG_S)) def move(self, distance: float, unit: str = 'cm', blocking: bool = True): """ @@ -296,7 +295,7 @@ def move(self, distance: float, unit: str = 'cm', blocking: bool = True): uart.write(self._packeter.msg[0:self._packeter.msg_size]) self._waiting_ack = ord('M') if blocking: - self._wait_for_target(idle_time=(distance/MOTOR_CONTROL_MM_S)) + self._wait_for_target(idle_time=(distance / MOTOR_CONTROL_MM_S)) def stop(self): """ @@ -1337,3 +1336,44 @@ def register_callback(self, event_name: str, callback: callable, args: tuple = N if event_name not in self.__class__.available_events: return super().register_callback(event_name, callback, args) + + +# UPDATE FIRMWARE METHOD # + +def update_firmware(file_path: str): + """ + + :param file_path: path of your FW bin + :return: + """ + + from sys import exit + from stm32_flash import ( + CHECK_STM32, + STM32_endCommunication, + STM32_startCommunication, + STM32_NACK, + STM32_eraseMEM, + STM32_writeMEM, ) + + if CHECK_STM32.value() is not 1: + print("Turn on your Alvik to continue...") + while CHECK_STM32.value() is not 1: + sleep_ms(500) + + ans = STM32_startCommunication() + if ans == STM32_NACK: + print("Cannot establish connection with STM32") + exit(-1) + + print('\nSTM32 FOUND') + + print('\nERASING MEM') + STM32_eraseMEM(0xFFFF) + + print("\nWRITING MEM") + STM32_writeMEM(file_path) + print("\nDONE") + print("\nLower Boot0 and reset STM32") + + STM32_endCommunication() diff --git a/arduino_alvik/firmware_updater.py b/arduino_alvik/firmware_updater.py deleted file mode 100644 index 0367704..0000000 --- a/arduino_alvik/firmware_updater.py +++ /dev/null @@ -1,24 +0,0 @@ -from sys import exit -from stm32_flash import * - -if CHECK_STM32.value() is not 1: - print("Turn on your Alvik to continue...") - while CHECK_STM32.value() is not 1: - sleep_ms(500) - -ans = STM32_startCommunication() -if ans == STM32_NACK: - print("Cannot establish connection with STM32") - exit(-1) - -print('\nSTM32 FOUND') - -print('\nERASING MEM') -STM32_eraseMEM(0xFFFF) - -print("\nWRITING MEM") -STM32_writeMEM("firmware.bin") -print("\nDONE") -print("\nLower Boot0 and reset STM32") - -STM32_endCommunication() diff --git a/examples/flash_firmware.py b/examples/flash_firmware.py new file mode 100644 index 0000000..e96a3cf --- /dev/null +++ b/examples/flash_firmware.py @@ -0,0 +1,5 @@ +# from machine import reset +from arduino_alvik import update_firmware + +update_firmware('./firmware.bin') +# reset() diff --git a/install.bat b/install.bat index 85801b2..efbd576 100644 --- a/install.bat +++ b/install.bat @@ -25,6 +25,7 @@ python -m mpremote %port_string% fs cp arduino_alvik/constants.py :lib/arduino_a python -m mpremote %port_string% fs cp arduino_alvik/conversions.py :lib/arduino_alvik/conversions.py python -m mpremote %port_string% fs cp arduino_alvik/pinout_definitions.py :lib/arduino_alvik/pinout_definitions.py python -m mpremote %port_string% fs cp arduino_alvik/robot_definitions.py :lib/arduino_alvik/robot_definitions.py +python -m mpremote %port_string% fs cp arduino_alvik/stm32_flash.py :lib/arduino_alvik/stm32_flash.py python -m mpremote %port_string% fs cp arduino_alvik/uart.py :lib/arduino_alvik/uart.py echo Installing dependencies diff --git a/install.sh b/install.sh index b0f9611..e606f57 100644 --- a/install.sh +++ b/install.sh @@ -50,6 +50,7 @@ $python_command -m mpremote $connect_string fs cp arduino_alvik/constants.py :li $python_command -m mpremote $connect_string fs cp arduino_alvik/conversions.py :lib/arduino_alvik/conversions.py $python_command -m mpremote $connect_string fs cp arduino_alvik/pinout_definitions.py :lib/arduino_alvik/pinout_definitions.py $python_command -m mpremote $connect_string fs cp arduino_alvik/robot_definitions.py :lib/arduino_alvik/robot_definitions.py +$python_command -m mpremote $connect_string fs cp arduino_alvik/stm32_flash.py :lib/arduino_alvik/stm32_flash.py $python_command -m mpremote $connect_string fs cp arduino_alvik/uart.py :lib/arduino_alvik/uart.py echo "Installing dependencies" diff --git a/package.json b/package.json index 90b6c77..1bb63ac 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,10 @@ ["arduino_alvik/pinout_definitions.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/pinout_definitions.py"], ["arduino_alvik/robot_definitions.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/robot_definitions.py"], ["arduino_alvik/uart.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/uart.py"], - ["arduino_alvik/firmware_updater.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/firmware_updater.py"], ["arduino_alvik/stm32_flash.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/stm32_flash.py"] ], "deps": [ ["github:arduino/ucPack-mpy", "0.1.5"] ], - "version": "0.3.0" + "version": "0.4.0" } \ No newline at end of file diff --git a/utilities/flash_firmware.bat b/utilities/flash_firmware.bat index f883fee..8fc2d1b 100644 --- a/utilities/flash_firmware.bat +++ b/utilities/flash_firmware.bat @@ -24,11 +24,6 @@ if "%1"=="" ( set "filename=%1" ) -echo Installing flash firmware utilities... - -python -m mpremote %port_string% fs cp ../arduino_alvik/firmware_updater.py :firmware_updater.py -python -m mpremote %port_string% fs cp ../arduino_alvik/stm32_flash.py :stm32_flash.py - echo Uploading %filename% python -m mpremote %port_string% fs cp %filename% :firmware.bin @@ -36,7 +31,7 @@ python -m mpremote %port_string% fs cp %filename% :firmware.bin set /p userInput=Do you want to flash the firmware right now? (y/N): if /i "%userInput%"=="y" ( - python -m mpremote %port_string% run ../arduino_alvik/firmware_updater.py + python -m mpremote %port_string% run ../examples/flash_firmware.py ) else ( echo The firmware will not be written to the device. ) diff --git a/utilities/flash_firmware.sh b/utilities/flash_firmware.sh index c973ad8..81bd12d 100644 --- a/utilities/flash_firmware.sh +++ b/utilities/flash_firmware.sh @@ -51,11 +51,6 @@ fi # Uncomment the following line on windows machines # python_command="python" -echo "Installing flash firmware utilities..." - -$python_command -m mpremote $connect_string fs cp ../arduino_alvik/firmware_updater.py :firmware_updater.py -$python_command -m mpremote $connect_string fs cp ../arduino_alvik/stm32_flash.py :stm32_flash.py - echo "Uploading $filename..." $python_command -m mpremote $connect_string fs cp $filename :firmware.bin @@ -64,7 +59,7 @@ echo "Do you want to flash the firmware right now? (y/N)" read do_flash if [ "$do_flash" == "y" ] || [ "$do_flash" == "Y" ]; then - $python_command -m mpremote $connect_string run ../arduino_alvik/firmware_updater.py + $python_command -m mpremote $connect_string run ../examples/flash_firmware.py else echo "The firmware will not be written to the device." fi