OpenSolo/shotmanager/settings.py
Matt 296a8af950 BUTTONS: Rework and expand button handling
This adds functionality for addition button options.  Adds short and
long hold options for the A, B, and trigger buttons, plus long hold
options for the gimbal preset buttons.  This also significantly cleans
up the handling, making it more robust.  Junk and unused functions are
dumped.  This is required to marry up with similar updates to the Artoo
STM32.  As such, you do need to have Open Solo on both the controller
and solo.  Also adds additional logging.  Additional commits to follow
make corresponding button handling changes to the smart shots, which are
also required.
2017-12-18 18:04:56 -05:00

94 lines
2.8 KiB
Python

#
# This file handles reading/writing settings from shotmanager.conf
#
import os
import threading
import shotLogger
import ConfigParser
logger = shotLogger.logger
settingsLock = threading.Lock()
if 'SOLOLINK_SANDBOX' in os.environ:
CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'sim/shotmanager.sandbox.conf')
CONFIG_FILE_BACKUP = os.path.join(os.path.dirname(__file__), 'sim/shotmanager.back')
else:
CONFIG_FILE = "/etc/shotmanager.conf"
CONFIG_FILE_BACKUP = "/etc/shotmanager.back"
CONFIG_FILE_EXT = "/usr/bin/extSettings.conf"
def writeSettingsThread(name, value):
settingsLock.acquire()
# write to the config file
config = ConfigParser.SafeConfigParser()
config.optionxform=str
config.read(CONFIG_FILE)
try:
config.set("shotManager", name, value)
except:
logger.log("Failed to write setting")
# back up the file
os.system("cp %s %s" % (CONFIG_FILE, CONFIG_FILE_BACKUP))
os.system("md5sum %s > %s.md5" % (CONFIG_FILE_BACKUP, CONFIG_FILE_BACKUP))
os.system("sync")
# modify config file and set md5
with open(CONFIG_FILE, 'wb') as configfile:
config.write(configfile)
os.system("md5sum %s > %s.md5" % (CONFIG_FILE, CONFIG_FILE))
os.system("sync")
os.system("rm %s %s.md5" % (CONFIG_FILE_BACKUP, CONFIG_FILE_BACKUP))
os.system("sync")
logger.log("wrote setting: %s: %s"%(name, value))
settingsLock.release()
# reads and returns setting of the given name
# perhaps we shouldn't read the file for each setting, but that's something we can address
# when we have more settings
def readSetting(name):
# get our saved button mappings
config = ConfigParser.SafeConfigParser()
settingsLock.acquire()
# if the config file is not found, an empty list is returned and the "get"
# operations below fail
config.read(CONFIG_FILE)
settingsLock.release()
try:
return config.get("shotManager", name)
except:
logger.log("error reading %s"%(CONFIG_FILE,))
raise
return 0
def readSettingExt(name):
# get our saved button mappings for extended button functions
config = ConfigParser.SafeConfigParser()
settingsLock.acquire()
# if the config file is not found, an empty list is returned and the "get"
# operations below fail
config.read(CONFIG_FILE_EXT)
settingsLock.release()
try:
return config.get("extFunctions", name)
except:
logger.log("[Ext Func Settings]: Unable to read %s from %s"%(name, CONFIG_FILE_EXT,))
return 0
# starts our thread which writes out our setting
# note both name and value should be strings
def writeSetting(name, value):
thread = threading.Thread(name = "writeSettingsThread", target = writeSettingsThread, args = (name, value))
thread.daemon = True
thread.start()