Skip to content

Commit

Permalink
fix flake warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
s-martin committed Oct 18, 2023
1 parent f139314 commit 02b8c68
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys

sys.path.append(".") # This command should be before imports of components
sys.path.append(".") # This command should be before imports of components

import logging
from evdev import categorize, ecodes, KeyEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys

sys.path.append(".") # This command should be before imports of components
sys.path.append(".") # This command should be before imports of components

from evdev import categorize, ecodes, KeyEvent
from io_buttons_usb_encoder import current_device, write_button_map
Expand Down
16 changes: 8 additions & 8 deletions components/displays/HD44780-i2c/i2c_lcd_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import smbus
from time import sleep


class i2c_device:
def __init__(self, addr, port=I2CBUS):
self.addr = addr
Expand Down Expand Up @@ -100,12 +101,13 @@ def read_block_data(self, cmd):
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00

En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit
En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit


class lcd:
#initializes objects and lcd
# initializes objects and lcd
def __init__(self):
self.lcd_device = i2c_device(ADDRESS)

Expand All @@ -120,7 +122,6 @@ def __init__(self):
self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
sleep(0.2)


# clocks EN to latch command
def lcd_strobe(self, data):
self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
Expand Down Expand Up @@ -165,16 +166,15 @@ def lcd_clear(self):
self.lcd_write(LCD_RETURNHOME)

# define backlight on/off (lcd.backlight(1); off= lcd.backlight(0)
def backlight(self, state): # for state, 1 = on, 0 = off
def backlight(self, state): # for state, 1 = on, 0 = off
if state == 1:
self.lcd_device.write_cmd(LCD_BACKLIGHT)
elif state == 0:
self.lcd_device.write_cmd(LCD_NOBACKLIGHT)

# add custom characters (0 - 7)
def lcd_load_custom_chars(self, fontdata):
self.lcd_write(0x40);
self.lcd_write(0x40)
for char in fontdata:
for line in char:
self.lcd_write_char(line)

1 change: 0 additions & 1 deletion components/gpio_control/GPIODevices/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ def __init__(self, pin, name='StatusLED'):
time.sleep(1)
self.logger.info('phoniebox-startup-scripts service active')
self.on()

46 changes: 23 additions & 23 deletions components/gpio_control/GPIODevices/shutdown_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ def set_led(self, status):
logger.debug('cannot set LED to {}: no LED pin defined'.format(status))

def callbackFunctionHandler(self, *args):
if self.is_pressed: # Should not be necessary, but handler gets called on rising edge too
logger.debug('ShutdownButton pressed, ensuring long press for {} seconds, checking each {}s'.format(
self.hold_time, self.iteration_time
))
t_passed = 0
led_state = True
while t_passed < self.hold_time:
self.set_led(led_state)
time.sleep(self.iteration_time)
t_passed += self.iteration_time
led_state = not led_state
if not self.is_pressed:
break
if t_passed >= self.hold_time:
# trippel off period to indicate command accepted
self.set_led(GPIO.HIGH)
time.sleep(.6)
# leave it on for the moment, it will be off when the system is down
self.when_pressed(*args)
else:
# switch off LED if pressing was cancelled early (during flashing)
self.set_led(GPIO.LOW)
if self.is_pressed: # Should not be necessary, but handler gets called on rising edge too
logger.debug('ShutdownButton pressed, ensuring long press for {} seconds, checking each {}s'.format(
self.hold_time, self.iteration_time
))
t_passed = 0
led_state = True
while t_passed < self.hold_time:
self.set_led(led_state)
time.sleep(self.iteration_time)
t_passed += self.iteration_time
led_state = not led_state
if not self.is_pressed:
break
if t_passed >= self.hold_time:
# trippel off period to indicate command accepted
self.set_led(GPIO.HIGH)
time.sleep(.6)
# leave it on for the moment, it will be off when the system is down
self.when_pressed(*args)
else:
# switch off LED if pressing was cancelled early (during flashing)
self.set_led(GPIO.LOW)

def __repr__(self):
return '<ShutdownButton-{}(pin={},hold_time={},iteration_time={},led_pin={},edge={},bouncetime={},antibouncehack={},pull_up_down={})>'.format(
self.name, self.pin, self.hold_time, self.iteration_time, self.led_pin, print_edge_key(self.edge), self.bouncetime,self.antibouncehack, print_pull_up_down(self.pull_up_down)
self.name, self.pin, self.hold_time, self.iteration_time, self.led_pin, print_edge_key(self.edge), self.bouncetime, self.antibouncehack, print_pull_up_down(self.pull_up_down)
)
25 changes: 15 additions & 10 deletions components/gpio_control/GPIODevices/simple_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

logger = logging.getLogger(__name__)

map_edge_parse = {'falling':GPIO.FALLING, 'rising':GPIO.RISING, 'both':GPIO.BOTH}
map_pull_parse = {'pull_up':GPIO.PUD_UP, 'pull_down':GPIO.PUD_DOWN, 'pull_off':GPIO.PUD_OFF}
map_edge_parse = {'falling': GPIO.FALLING, 'rising': GPIO.RISING, 'both': GPIO.BOTH}
map_pull_parse = {'pull_up': GPIO.PUD_UP, 'pull_down': GPIO.PUD_DOWN, 'pull_off': GPIO.PUD_OFF}
map_edge_print = {GPIO.FALLING: 'falling', GPIO.RISING: 'rising', GPIO.BOTH: 'both'}
map_pull_print = {GPIO.PUD_UP:'pull_up', GPIO.PUD_DOWN: 'pull_down', GPIO.PUD_OFF: 'pull_off'}
map_pull_print = {GPIO.PUD_UP: 'pull_up', GPIO.PUD_DOWN: 'pull_down', GPIO.PUD_OFF: 'pull_off'}


def parse_edge_key(edge):
if edge in [GPIO.FALLING, GPIO.RISING, GPIO.BOTH]:
Expand All @@ -21,6 +22,7 @@ def parse_edge_key(edge):
raise KeyError('Unknown Edge type {edge}'.format(edge=edge))
return result


def parse_pull_up_down(pull_up_down):
if pull_up_down in [GPIO.PUD_UP, GPIO.PUD_DOWN, GPIO.PUD_OFF]:
return pull_up_down
Expand All @@ -31,20 +33,23 @@ def parse_pull_up_down(pull_up_down):
raise KeyError('Unknown Pull Up/Down type {pull_up_down}'.format(pull_up_down=pull_up_down))
return result


def print_edge_key(edge):
try:
result = map_edge_print[edge]
except KeyError:
result = edge
return result


def print_pull_up_down(pull_up_down):
try:
result = map_pull_print[pull_up_down]
except KeyError:
result = pull_up_down
return result


# This function takes a holding time (fractional seconds), a channel, a GPIO state and an action reference (function).
# It checks if the GPIO is in the state since the function was called. If the state
# changes it return False. If the time is over the function returns True.
Expand Down Expand Up @@ -93,7 +98,7 @@ def callbackFunctionHandler(self, *args):
args = args[1:]
logger.debug('args after: {}'.format(args))

if self.antibouncehack:
if self.antibouncehack:
time.sleep(0.1)
inval = GPIO.input(self.pin)
if inval != GPIO.LOW:
Expand Down Expand Up @@ -132,32 +137,32 @@ def longPressHandler(self, *args):
# instant action (except Postpone mode)
if self.hold_mode != "Postpone":
self.when_pressed(*args)

# action(s) after hold_time
if self.hold_mode == "Repeat":
# Repeated call of main action (multiple times if button is held long enough)
while checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW):
self.when_pressed(*args)

elif self.hold_mode == "Postpone":
# Postponed call of main action (once)
if checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW):
self.when_pressed(*args)
while checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW):
pass

elif self.hold_mode == "SecondFunc":
# Call of secondary action (once)
if checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW):
self.when_held(*args)
while checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW):
pass

elif self.hold_mode == "SecondFuncRepeat":
# Repeated call of secondary action (multiple times if button is held long enough)
while checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW):
self.when_held(*args)

def __del__(self):
logger.debug('remove event detection')
GPIO.remove_event_detect(self.pin)
Expand All @@ -170,7 +175,7 @@ def is_pressed(self):

def __repr__(self):
return '<SimpleButton-{}(pin={},edge={},hold_mode={},hold_time={},bouncetime={},antibouncehack={},pull_up_down={})>'.format(
self.name, self.pin, print_edge_key(self.edge), self.hold_mode, self.hold_time, self.bouncetime,self.antibouncehack,print_pull_up_down(self.pull_up_down)
self.name, self.pin, print_edge_key(self.edge), self.hold_mode, self.hold_time, self.bouncetime, self.antibouncehack, print_pull_up_down(self.pull_up_down)
)


Expand Down
16 changes: 8 additions & 8 deletions components/gpio_control/GPIODevices/two_button_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ def __init__(self,
hold_mode=None,
hold_time=0.3,
bouncetime=500,
antibouncehack=False,
edge='falling',
antibouncehack=False,
edge='falling',
name='TwoButtonControl'):
self.bcmPin1 = bcmPin1
self.bcmPin2 = bcmPin2
self.functionCallBtn1 = functionCallBtn1
self.functionCallBtn2 = functionCallBtn2
self.functionCallTwoBtns = functionCallTwoBtns
self.pull_up_down=pull_up_down
self.hold_mode=hold_mode
self.hold_time=hold_time
self.bouncetime=bouncetime
self.antibouncehack=antibouncehack
self.edge=edge
self.pull_up_down = pull_up_down
self.hold_mode = hold_mode
self.hold_time = hold_time
self.bouncetime = bouncetime
self.antibouncehack = antibouncehack
self.edge = edge
self.btn1 = SimpleButton(
pin=bcmPin1,
name=name + 'Btn1',
Expand Down
13 changes: 7 additions & 6 deletions components/gpio_control/config_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from shutil import copyfile


def Ini_CheckAndUpgrade(config):
has_changed = False
for section in config.sections():
Expand Down Expand Up @@ -37,8 +38,8 @@ def Ini_CheckAndUpgrade(config):
has_changed = True
if not config.has_option(section, 'hold_time'):
config.set(section, 'hold_time', str(v))
#PinUp: <int> --> Pin1 <int>
#PinDown: <int> --> Pin2 <int>
# PinUp: <int> --> Pin1 <int>
# PinDown: <int> --> Pin2 <int>
if config.has_option(section, 'PinUp'):
v = config.getint(section, 'PinUp')
config.remove_option(section, 'PinUp')
Expand Down Expand Up @@ -67,17 +68,17 @@ def Ini_CheckAndUpgrade(config):
config.set(section, 'functionCall2', v)

return has_changed


def ConfigCompatibilityChecks(config, config_path):
# Check for deprecated settings in gpio_settings.ini
if not Ini_CheckAndUpgrade(config):
return

# If we reach here, gpio_settings.ini needed some patching...

# Try creating a backup of the previous ini file
backup_path = config_path+'.bak'
backup_path = config_path + '.bak'
if os.path.isfile(backup_path):
return
copyfile(config_path, backup_path)
Expand Down
2 changes: 1 addition & 1 deletion components/gpio_control/gpio_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def gpio_loop(self):
config = configparser.ConfigParser(inline_comment_prefixes=";", delimiters=(':', '='))
config_path = os.path.expanduser('/home/pi/RPi-Jukebox-RFID/settings/gpio_settings.ini')
config.read(config_path)

ConfigCompatibilityChecks(config, config_path)

phoniebox_function_calls = function_calls.phoniebox_function_calls()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ def regex(needle, hay, exception="-"):
else:
return exception


def getDuration(status):
""" Find the duration of the track in the output from mpd status"""

Expand All @@ -463,9 +464,10 @@ def getDuration(status):
# this attribute value is split into two parts by ":"
# first is the elapsed time and the second part is the duration
duration = regex("\ntime: .*:(.*)\n", status, "0")

return int(float(duration))


REPEAT_MODE_OFF = "off"
REPEAT_MODE_SINGLE = "single"
REPEAT_MODE_PLAYLIST = "playlist"
Expand Down
2 changes: 1 addition & 1 deletion scripts/Reader.py.pcsc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Reader:
'Failed to release context: ' + \
SCardGetErrorMessage(hresult))

return(toHexString(response, PACK))
return (toHexString(response, PACK))

except error as e:
print(e)
2 changes: 1 addition & 1 deletion scripts/helperscripts/organizeFiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def readFolders(audioDir, relpath=None, isFirst=True):
elif os.path.isdir(absf):
childResult = readFolders(audioDir=absf, relpath=os.path.join(relpath, f), isFirst=False)
for k, v in childResult.items():
assert(k not in result)
assert (k not in result)
result[k] = v
if hasAudioFiles:
result[relpath] = os.path.exists(os.path.join(audioDir, "folder.conf"))
Expand Down

0 comments on commit 02b8c68

Please sign in to comment.