From 75d652461aa41608f6ada9687e9ebc4a67b737cc Mon Sep 17 00:00:00 2001 From: Julius Date: Tue, 4 Dec 2018 10:31:58 +0100 Subject: [PATCH 1/2] More Cleanup - changed all functions to lowercase - changed buildin shadows - changed outer_scope shadows - changed unused interation variables to _ - changed nondescribtive variablenames to more explaining ones - changed some methods to static --- lib/controllib.py | 12 ++++++------ lib/graphiclib.py | 21 +++++++++++---------- lib/gyro.py | 6 +++--- lib/hardwarelib.py | 13 +++++++++---- lib/motors.py | 6 +++--- lib/ultrasonic.py | 24 ++++++++++-------------- main.py | 10 +++++----- testscripts/ultrasonic_test.py | 2 +- 8 files changed, 48 insertions(+), 46 deletions(-) diff --git a/lib/controllib.py b/lib/controllib.py index 650f663..a8eb98e 100644 --- a/lib/controllib.py +++ b/lib/controllib.py @@ -24,11 +24,11 @@ class Joystick: def handle(self): c = self.configuration nav = self.navigator - dict = {} + axis_dict = {} # axes for i in range(self.joystick.get_numaxes()): axis = self.joystick.get_axis(i) - dict['Axis {}'.format(i)] = axis + axis_dict['Axis {}'.format(i)] = axis if i == c['GAS'] and axis > 0.1: nav.forward() @@ -37,7 +37,7 @@ class Joystick: # buttons for i in range(self.joystick.get_numbuttons()): button = self.joystick.get_button(i) - dict['Button {}'.format(i)] = button + axis_dict['Button {}'.format(i)] = button if i == c['LIGHT'] and button == 1 and i not in self.pressed: self.light.switch() @@ -77,7 +77,7 @@ class Joystick: # hats for i in range(self.joystick.get_numhats()): hat = self.joystick.get_hat(i) - dict['Hat {}'.format(i)] = hat + axis_dict['Hat {}'.format(i)] = hat if hat == (-1, 0): nav.left() @@ -86,8 +86,8 @@ class Joystick: else: nav.straight() - dict['Volume'] = self.sound.get_volume() - return dict + axis_dict['Volume'] = self.sound.get_volume() + return axis_dict def _save_camimg(self): self.camera.new_frame() diff --git a/lib/graphiclib.py b/lib/graphiclib.py index 228324a..12d7af0 100644 --- a/lib/graphiclib.py +++ b/lib/graphiclib.py @@ -5,9 +5,9 @@ import pygame.camera from pygame import * -def render_text_line(image, color, font, text, pos=(0, 0)): - render_text = font.render(text, 1, color) - image.blit(render_text, pos) +def render_text_line(rimage, rcolor, rfont, text, pos=(0, 0)): + render_text = rfont.render(text, 1, rcolor) + rimage.blit(render_text, pos) class Screen: @@ -20,7 +20,8 @@ class Screen: self.screen = pygame.display.set_mode(self.size) pygame.display.set_caption(title) - def refresh(self, rectangles=None): + @staticmethod + def refresh(rectangles=None): pygame.display.update(rectangles) def toggle_fullscreen(self): @@ -45,12 +46,12 @@ class List(pygame.sprite.Sprite): self.rect = self.image.get_rect() self.rect.topleft = position self.font = pygame.font.SysFont('Arial', 25) - self.dict = {} + self.display_dict = {} self.updated = True self.txtsize = self.font.size('__||__') - def set_dict(self, dict): - self.dict = dict + def set_dict(self, dispdict): + self.display_dict = dispdict self.updated = True def update(self): @@ -58,8 +59,8 @@ class List(pygame.sprite.Sprite): height = 0 self.image.fill((0, 0, 0)) - for key in self.dict.keys(): - line = '{}: {}'.format(key, self.dict[key]) + for dictkey in self.display_dict.keys(): + line = '{}: {}'.format(dictkey, self.display_dict[dictkey]) render_text_line(self.image, (255, 255, 255), self.font, line, (0, height)) height += self.txtsize[1] @@ -90,4 +91,4 @@ class PiCamera(pygame.sprite.Sprite): self.output.truncate(0) self.camera.capture(self.output, 'rgb', resize=self.camsize) s = pygame.transform.rotate(pygame.surfarray.make_surface(self.output.array), 270) - self.image.blit(s, (0, 0)) \ No newline at end of file + self.image.blit(s, (0, 0)) diff --git a/lib/gyro.py b/lib/gyro.py index 779731e..33d1d51 100644 --- a/lib/gyro.py +++ b/lib/gyro.py @@ -17,8 +17,8 @@ def read_byte(reg): def read_word(reg): h = bus.read_byte_data(address, reg) - l = bus.read_byte_data(address, reg + 1) - value = (h << 8) + l + r_data = bus.read_byte_data(address, reg + 1) + value = (h << 8) + r_data return value @@ -44,7 +44,7 @@ def get_x_rotation(x, y, z): return math.degrees(radians) -def getAllOut(): +def get_all_gyro_data(): acceleration_xout = read_word_2c(0x3b) acceleration_yout = read_word_2c(0x3d) acceleration_zout = read_word_2c(0x3f) diff --git a/lib/hardwarelib.py b/lib/hardwarelib.py index 5a20ca8..44476ae 100644 --- a/lib/hardwarelib.py +++ b/lib/hardwarelib.py @@ -36,7 +36,7 @@ class Navigator: class Light: - """Light switched with a relais""" + """Light switched with a relays""" def __init__(self, lightpin): self.pin = lightpin @@ -72,18 +72,23 @@ class Ultrasonic: return self.distance def __del__(self): - self.sensor.clean() + self.sensor.cleanup() class Temperature: """A temperature sensor""" - def get_Temperature(self): + def __init__(self): + pass + + @staticmethod + def get_temperature(): outp = check_output(['python', '-u', './lib/thermolib.py']).decode('ISO-8859-1') temp, hum = outp.split('|') return temp - def get_Humidity(self): + @staticmethod + def get_humidity(): outp = check_output(['python', '-u', './lib/thermolib.py']).decode('ISO-8859-1') temp, hum = outp.split('|') return hum diff --git a/lib/motors.py b/lib/motors.py index c2dead7..7be3eba 100644 --- a/lib/motors.py +++ b/lib/motors.py @@ -17,7 +17,7 @@ class Motor(object): GPIO.setup(p, GPIO.OUT) GPIO.output(p, 0) - def __exit__(self, type, value, traceback): + def __exit__(self, type_of_motor, value, traceback): self.clean_pins_up() def _set_rpm(self, rpm): @@ -54,7 +54,7 @@ class Motor(object): def _move_acw(self, big_steps): self.clean_pins_up() - for i in range(big_steps): + for _ in range(big_steps): GPIO.output(self.P1, 0) sleep(self._T) GPIO.output(self.P3, 1) @@ -78,7 +78,7 @@ class Motor(object): GPIO.output(self.P2, 0) GPIO.output(self.P3, 0) GPIO.output(self.P4, 0) - for i in range(big_steps): + for _ in range(big_steps): GPIO.output(self.P3, 0) sleep(self._T) GPIO.output(self.P1, 1) diff --git a/lib/ultrasonic.py b/lib/ultrasonic.py index d648352..af4a79e 100644 --- a/lib/ultrasonic.py +++ b/lib/ultrasonic.py @@ -5,13 +5,11 @@ GPIO.setmode(GPIO.BOARD) class Sensor(object): - def __init__(self): - self.TRIGGER = TRIG - self.ECHO = ECHO - self.lastValues = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] - self.lastValues = self.lastValues[1:] - - def init(self, TRIG, ECHO): + def __init__(self, trigger, echo): + self.TRIGGER = trigger + self.ECHO = echo + self.lastValue = 0 + self.lastValuesCount = 1 GPIO.setup(self.TRIGGER, GPIO.OUT) GPIO.setup(self.ECHO, GPIO.IN) GPIO.output(self.TRIGGER, False) @@ -25,12 +23,10 @@ class Sensor(object): abs_start = time.time() while GPIO.input(self.ECHO) == 0 and (time.time() - abs_start) < 0.02: - # print(time.time()-abs_start) pass pulse_start = time.time() while GPIO.input(self.ECHO) == 1 and (time.time() - abs_start) < 0.02: - # print(time.time()-abs_start) pass pulse_end = time.time() @@ -38,11 +34,11 @@ class Sensor(object): distance = pulse_duration * 17150 distance = round(distance, 2) - self.lastValues.append(distance) - distance = round((sum(self.lastValues)) / (len(self.lastValues)), 2) - # print(self.lastValues) - # print("Distance: {}".format(distance)) + lastc = self.lastValuesCount + self.lastValue = (1/lastc) * distance + ((lastc-1)/lastc) * self.lastValue + distance = self.lastValue return distance - def clean(self): + @staticmethod + def cleanup(): GPIO.cleanup() diff --git a/main.py b/main.py index cba0e56..3703144 100644 --- a/main.py +++ b/main.py @@ -24,8 +24,8 @@ def main(): # pygame stuff screen = graphiclib.Screen(size=(1000, 1000)) all_sprites = pygame.sprite.RenderUpdates() - list = graphiclib.List((0, 0), (500, 1000)) - all_sprites.add(list) + glist = graphiclib.List((0, 0), (500, 1000)) + all_sprites.add(glist) all_sprites.add(camera) clock = pygame.time.Clock() running = True @@ -38,9 +38,9 @@ def main(): print('quit event') running = False - dict = jstick.handle() - dict['Distance'] = ultrasonic.get_distance() - list.set_dict(dict) + gdict = jstick.handle() + gdict['Distance'] = ultrasonic.get_distance() + glist.set_dict(gdict) all_sprites.update() update_rects = all_sprites.draw(screen.screen) screen.refresh(rectangles=update_rects) diff --git a/testscripts/ultrasonic_test.py b/testscripts/ultrasonic_test.py index a12d4cc..8f8f43a 100644 --- a/testscripts/ultrasonic_test.py +++ b/testscripts/ultrasonic_test.py @@ -4,4 +4,4 @@ sensor = ultrasonic.Sensor() sensor.init(11, 7) print('Beginn der Messung') print(sensor.echo()) -sensor.clean() +sensor.cleanup() From f65345ab0bcc3472f84ed99c13cb60b00f9bcbd8 Mon Sep 17 00:00:00 2001 From: Julius Date: Tue, 4 Dec 2018 10:56:16 +0100 Subject: [PATCH 2/2] Quality Improvements - split a function into two for less complexity - removed wildcard import --- lib/controllib.py | 30 ++++++++++++++++-------------- lib/graphiclib.py | 3 +-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/controllib.py b/lib/controllib.py index a8eb98e..d47614d 100644 --- a/lib/controllib.py +++ b/lib/controllib.py @@ -21,20 +21,7 @@ class Joystick: self.pressed = [] self.splaying = False - def handle(self): - c = self.configuration - nav = self.navigator - axis_dict = {} - # axes - for i in range(self.joystick.get_numaxes()): - axis = self.joystick.get_axis(i) - axis_dict['Axis {}'.format(i)] = axis - - if i == c['GAS'] and axis > 0.1: - nav.forward() - elif i == c['GAS'] and axis < 0.1: - nav.stop() - # buttons + def _handle_buttons(self, axis_dict, c): for i in range(self.joystick.get_numbuttons()): button = self.joystick.get_button(i) axis_dict['Button {}'.format(i)] = button @@ -73,7 +60,22 @@ class Joystick: self.pressed.append(i) elif i == c['REC'] and button == 0 and i in self.pressed: self.pressed.remove(i) + return axis_dict + def handle(self): + c = self.configuration + nav = self.navigator + axis_dict = {} + # axes + for i in range(self.joystick.get_numaxes()): + axis = self.joystick.get_axis(i) + axis_dict['Axis {}'.format(i)] = axis + + if i == c['GAS'] and axis > 0.1: + nav.forward() + elif i == c['GAS'] and axis < 0.1: + nav.stop() + axis_dict = self._handle_buttons(axis_dict, c) # hats for i in range(self.joystick.get_numhats()): hat = self.joystick.get_hat(i) diff --git a/lib/graphiclib.py b/lib/graphiclib.py index 12d7af0..6b6238b 100644 --- a/lib/graphiclib.py +++ b/lib/graphiclib.py @@ -2,7 +2,6 @@ import picamera import picamera.array import pygame import pygame.camera -from pygame import * def render_text_line(rimage, rcolor, rfont, text, pos=(0, 0)): @@ -31,7 +30,7 @@ class Screen: else: displayinfo = pygame.display.Info() fullsize = (displayinfo.current_w, displayinfo.current_h) - pygame.display.set_mode(fullsize, FULLSCREEN | DOUBLEBUF) + pygame.display.set_mode(fullsize, pygame.FULLSCREEN | pygame.DOUBLEBUF) class List(pygame.sprite.Sprite):