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
pull/7/head
Julius 6 years ago
parent db980b4172
commit 75d652461a

@ -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()

@ -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))
self.image.blit(s, (0, 0))

@ -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)

@ -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

@ -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)

@ -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()

@ -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)

@ -4,4 +4,4 @@ sensor = ultrasonic.Sensor()
sensor.init(11, 7)
print('Beginn der Messung')
print(sensor.echo())
sensor.clean()
sensor.cleanup()

Loading…
Cancel
Save