def time_save(): time_rtc2dictionary(time_settings.dict) time_settings.dict["reference_seconds"] = auxiliary_controller.time() time_settings.write() uos.sync() logger.info("Time saved") sync_time(logger, screen, time_settings, auxiliary_controller)
def write(self, buffer): """Write to file.""" if self.backup_count: # Remove the oldest backup file if it is there. old_backupfile = "{}.{}".format(self.filename, self.backup_count) #log.info('Removing backup file {}'.format(old_backupfile)) file_remove(old_backupfile) # Rename backup files in between oldest and newest one. for i in range(self.backup_count - 1, 0, -1): if i < self.backup_count: self.rename_file("{}.{}".format(self.filename, i), "{}.{}".format(self.filename, i + 1)) # Rename most recent backup file. self.rename_file(self.filename, "{}.{}".format(self.filename, 1)) # Write new most recent backup file. log.info('Writing recent backup to {}'.format(self.filename)) with open(self.filename, "w") as outfile: #log.info('copyfileobj: {} => {}'.format(buffer, outfile)) copyfileobj(buffer, outfile) time.sleep_ms(150) uos.sync()
def save(self, filename, instream): """ Save configuration file, with rotating backup. """ import uos # Protect against directory traversals. filename = os_path.basename(filename) # Only allow specific filenames. if 'settings' not in filename: raise ValueError( 'Writing arbitrary files to the system is prohibited') # Absolute path to configuration file. filepath = os_path.join(self.CONFIG_PATH, filename) # Number of backup files to keep around. backup_count = self.get('main.backup.file_count', 7) # Backup configuration file. log.info( 'Backing up file {} to {}, keeping a history worth of {} files'. format(filepath, self.BACKUP_PATH, backup_count)) backup_file(filepath, self.BACKUP_PATH, backup_count) # Overwrite configuration file. log.info('Saving configuration file {}'.format(filepath)) with open(filepath, "w") as outstream: if isinstance(instream, str): outstream.write(instream) else: copyfileobj(instream, outstream) uos.sync()
def _reset(delay): import utime import machine print("restarting...") utime.sleep(delay) uos.umount('/remote') uos.sync() machine.reset()
def rename_file(self, oldfile, newfile): #log.info('Renaming backup file {} to {}'.format(oldfile, newfile)) try: os.rename(oldfile, newfile) except OSError: pass time.sleep_ms(5) uos.sync() time.sleep_ms(5)
def save_img(self, screen_buff, temperature_min, temperature_max): if screen_buff: self.last_img_number += 1 self.last_img_name = "{}/{:04d}_{:d}_{:d}{}".format( self.media_path, self.last_img_number, round(100 * temperature_min), round(100 * temperature_max), self.img_format ) self.logger.info("Saving...", self.last_img_name) screen_buff.save(self.last_img_name) uos.sync() self.logger.info("Saved:", self.last_img_name) else: self.logger.info("No image to save") self.to_save_img = False
def __init__(self, logger): self.logger = logger self.media_path = "DCIM" self.startup_img_name = "startup.bmp" try: uos.mkdir(self.media_path) uos.sync() except OSError as e: if e.args[0] == errno.EEXIST: pass else: raise self.last_img_name, self.last_img_number = self.get_last_saved_img() self.clock = time.clock() self.fps_reset()
def saveStream(serial, logger, filename='EX_Bus_stream.txt', duration=1000): '''Write a part of the serial stream to a text file on the SD card for debugging purposes. The "memoryview" hack credits go to: https://forum.micropython.org/viewtopic.php?t=1259#p8002 NOTE: Do not use this function during normal operation. When debugging, use thsi call by NOTE: Writing to the SD card sometimes doesn't work. Do a hard reset when this function is active. After the hard reset the file 'EX_Bus_stream.txt' should exist. ''' start = utime.ticks_ms() time = 0 duration = 1000 f = open(filename, 'w') while time < duration: buf = bytearray(50) mv = memoryview(buf) idx = 0 while idx < len(buf): if serial.any() > 0: bytes_read = serial.readinto(mv[idx:]) # print('Got {} bytes of data'.format(bytes_read), # hexlify(buf[idx:idx+bytes_read], b':')) idx += bytes_read f.write(hexlify(buf, b':') + '\n') time = utime.ticks_diff(utime.ticks_ms(), start) f.close() # sync file systems (might be needed?) uos.sync() message = 'EX Bus stream recorded for {} seconds.'.format(duration / 1000.) logger.log('debug', message)
def on_mqtt(self, topic, data): mine_before = self.mine self.set_data(data) self.write_cache(data) #comparing jsons is not reliable because the ordering of elements can change. #Therefore we just compare the date the config was published if (mine_before is None) or (self.mine is None): same_date = False else: same_date = (mine_before["published"] == self.mine["published"]) if not same_date: ct.print_warning("Config changed, rebooting") # My config has changed. Reboot. sync() machine.reset() ct.print_info("Received config with identical date {}".format( self.mine["published"]))
#rename_files.py ############################################################### # Code for OpenMV M7 camera # Goal is to rename the "pulse_led.py" file to "main.py" and reset the camera - Work in progress # Author: Adam A. Koch (aakoch) # Date: 2018-04-03 # Copyright (c) 2018 Adam A. Koch # This work is licensed under the MIT license. ############################################################### import uos, machine, pyb uos.sync() print("current directory=%s" % uos.getcwd()) print("directory contents=%s" % uos.listdir()) undo = False if (undo): uos.rename("main.py", "pulse_led.py") uos.rename("main_backup.py", "main.py") else: uos.rename("main.py", "main_backup.py") uos.rename("pulse_led.py", "main.py") uos.sync() print("current directory=%s" % uos.getcwd()) print("directory contents=%s" % uos.listdir()) if (not undo):
def save_fb_as_startup(self, screen_buff): if screen_buff: screen_buff.save(self.startup_img_name) uos.sync() self.to_save_fb_as_startup = False
def delete_playback_img_name(self): uos.remove(self.playback_img_name) uos.sync() self.update_playback_img_name() print(self.playback_img_name)