diff --git a/install.sh b/install.sh index 531fe66..50515ab 100644 --- a/install.sh +++ b/install.sh @@ -9,11 +9,11 @@ sudo pip install adafruit-pca9685 sudo apt install vsftpd sudo apt install vlc sudo modprobe bcm2835-v4l2 -if [hash pip3];then +if hash pip3; then pip install pygame exit 0 fi -if [hash pip];then +if hash pip;then pip install pygame exit 0 fi diff --git a/lib/controllib.py b/lib/controllib.py index d47614d..e27fda5 100644 --- a/lib/controllib.py +++ b/lib/controllib.py @@ -21,45 +21,43 @@ class Joystick: self.pressed = [] self.splaying = False + def _button_check(self, name, i, c, button): + """ Checks if a button was pressed and wasn't pressed before. """ + if i == c[name]: + if button == 1 and i not in self.pressed: + self.pressed.append(i) + return 1 + elif button == 0 and i in self.pressed: + self.pressed.remove(i) + return 2 + return 0 + 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 - if i == c['LIGHT'] and button == 1 and i not in self.pressed: + if self._button_check('LIGHT', i, c, button) == 1: self.light.switch() - self.pressed.append(i) - elif i == c['LIGHT'] and button == 0 and i in self.pressed: - self.pressed.remove(i) - elif i == c['MUSIC'] and button == 1 and i not in self.pressed: + elif self._button_check('MUSIC', i, c, button) == 1: if self.splaying: self.sound.stop() self.splaying = False else: self.sound.play() self.splaying = True - self.pressed.append(i) - elif i == c['MUSIC'] and button == 0 and i in self.pressed: - self.pressed.remove(i) - elif i == c['VOLIN'] and button == 1 and i not in self.pressed: + elif self._button_check('VOLIN', i, c, button) == 1: self.sound.set_volume(self.sound.get_volume() + 0.1) - self.pressed.append(i) - elif i == c['VOLIN'] and button == 0 and i in self.pressed: + elif self._button_check('VOLIN', i, c, button) == 2: self.sound.set_volume(self.sound.get_volume() - 0.1) - self.pressed.remove(i) - elif i == c['VOLDE'] and button == 1 and i not in self.pressed: - self.pressed.append(i) - elif i == c['VOLDE'] and button == 0 and i in self.pressed: - self.pressed.remove(i) + elif self._button_check('VOLDE', i, c, button): + pass - elif i == c['REC'] and button == 1 and i not in self.pressed: + elif self._button_check('REC', i, c, button) == 1: self._save_camimg() - 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): diff --git a/lib/gyro.py b/lib/gyro.py index 33d1d51..9e6eaac 100644 --- a/lib/gyro.py +++ b/lib/gyro.py @@ -26,8 +26,7 @@ def read_word_2c(reg): val = read_word(reg) if val >= 0x8000: return -((65535 - val) + 1) - else: - return val + return val def dist(a, b):