示例#1
0
    def __init__(self):
        """ Initialize a GPIO simulator and perform setup """
        self.gpio = GPIOSimulator()

        high = self.gpio.HIGH
        low = self.gpio.LOW
        out = self.gpio.OUT
        inn = self.gpio.IN

        # Dictionary that shows which state pins need to be in for a specific LED
        self.led_mapping = {
            0: [high, low, low],
            1: [low, high, low],
            2: [low, high, low],
            3: [low, low, high],
            4: [high, low, low],
            5: [low, low, high],
        }
        # Dictionary for mapping which pins should be out and in for a specific LED
        self.charlie_mapping = {
            0: [out, out, inn],
            1: [out, out, inn],
            2: [inn, out, out],
            3: [inn, out, out],
            4: [out, inn, out],
            5: [out, inn, out],
        }
示例#2
0
 def __init__(self) -> None:
     self.gpio_sim = GPIOSimulator()
     self.led_lighting_states = (
         ((0, 1, 1), (1, 1, 0)),
         ((1, 1, 1), (0, 1, 0)),
         ((1, 1, 1), (2, 1, 0)),
         ((2, 1, 1), (1, 1, 0)),
         ((0, 1, 1), (2, 1, 0)),
         ((2, 1, 1), (0, 1, 0)),
     )
     self._all_to_input()
示例#3
0
class Keypad:
    """Class for interacting with Keypad"""
    def __init__(self) -> None:
        self.stream = ""
        self.gpio = GPIOSimulator()
        self.prev_btn = None
        self.setup()

    def setup(self):
        """Set up GPIO-pins for use with Keypad"""

        for row_pin in keypad_row_pins:
            #Set up row-pins
            self.gpio.setup(row_pin, self.gpio.OUT)

        for col_pin in keypad_col_pins:
            #Set up col-pins
            self.gpio.setup(col_pin, self.gpio.IN)

    def get_keypad_data(self):
        """Poll GPIO for button press"""
        active_col, active_row = None, None

        for row, row_pin in enumerate(keypad_row_pins):
            #Check Rows
            self.gpio.output(row_pin, self.gpio.HIGH)
            for col, col_pin in enumerate(keypad_col_pins):
                #Check Columns
                if self.gpio.input(col_pin) == self.gpio.HIGH:
                    active_col = col
                    active_row = row
            self.gpio.output(row_pin, self.gpio.LOW)
        return active_col, active_row

    def setup_poll(self):
        """Sets up poll and starts it"""
        while True:
            try:
                self.do_polling()
                time.sleep(0.01)
            except KeyboardInterrupt:
                print(self.get_stream())
                exit()

    def do_polling(self):
        """Polls the keypad"""
        keypad_press = self.get_keypad_data()
        current_button = Keypad.parse_result(keypad_press[0], keypad_press[1])

        if current_button != self.prev_btn:
            #Changed button
            if not self.prev_btn is None:
                self.stream += self.prev_btn
            self.prev_btn = current_button

    def get_next_signal(self):
        """Get a signal from keypad"""
        keypress = None

        while not keypress:
            #While no keypress received
            self.do_polling()
            if self.stream:
                keypress = self.get_stream()[0]
            time.sleep(0.01)

        return keypress

    def get_stream(self):
        """Returns and clears stream"""
        result = self.stream
        self.stream = ""
        return result

    @staticmethod
    def parse_result(col: int, row: int):
        """Parses active col and row to keypad input """
        try:
            result = KEYPAD_BUTTONS[row][col]
        except (IndexError, TypeError):
            # Too big index, or row/column = None
            result = None  # If not allowed index or no press
        return result
示例#4
0
 def __init__(self) -> None:
     self.stream = ""
     self.gpio = GPIOSimulator()
     self.prev_btn = None
     self.setup()
示例#5
0
"""Module for the Keypad class"""

import time
from GPIOSimulator_v5 import GPIOSimulator, keypad_row_pins, keypad_col_pins

GPIO = GPIOSimulator()


class Keypad:
    """  Keypad """
    def __init__(self):
        """
        Init method
        """
        self.rowpins = keypad_row_pins
        self.columnpins = keypad_col_pins
        self.setup()

    def setup(self):
        """
        initialize the row pins as outputs and the column pins as inputs.
        :return: None
        """

        for row_pin, rp in enumerate(self.rowpins):
            GPIO.setup(rp, GPIO.OUT)

        for col_pin, cp in enumerate(self.columnpins):
            GPIO.setup(cp, GPIO.IN, state=GPIO.LOW)

    def do_polling(self):
示例#6
0
class LEDBoard:
    """ An interface to the simulated Charlieplexed LED board """
    def __init__(self):
        """ Initialize a GPIO simulator and perform setup """
        self.gpio = GPIOSimulator()

        high = self.gpio.HIGH
        low = self.gpio.LOW
        out = self.gpio.OUT
        inn = self.gpio.IN

        # Dictionary that shows which state pins need to be in for a specific LED
        self.led_mapping = {
            0: [high, low, low],
            1: [low, high, low],
            2: [low, high, low],
            3: [low, low, high],
            4: [high, low, low],
            5: [low, low, high],
        }
        # Dictionary for mapping which pins should be out and in for a specific LED
        self.charlie_mapping = {
            0: [out, out, inn],
            1: [out, out, inn],
            2: [inn, out, out],
            3: [inn, out, out],
            4: [out, inn, out],
            5: [out, inn, out],
        }

    def light_led(self, led, duration):
        """ Light a specific LED on the charlieplexed LED board """
        # Get the settings for lighting the specific LED
        self.__led_settings(led)
        # Print the LED states to the console
        self.gpio.show_leds_states()
        time.sleep(duration)
        self.gpio.cleanup()
        self.gpio.show_leds_states()

    def flash_all_leds(self, duration):
        """ Flash all LEDs on and off for k seconds"""
        for i in range(N_LEDS):
            self.__led_settings(i)
        # Print the LED states to the console
        self.gpio.show_leds_states()
        # Do nothing for the specified k seconds
        time.sleep(duration)
        # Reset pins
        self.gpio.cleanup()
        # Print the LED states to the console
        self.gpio.show_leds_states()

    def twinkle_all_leds(self, duration):
        """ Turn LEDs on and off in sequence for k seconds """
        # For each LED, twinkle that LED
        for i in range(N_LEDS):
            self.__led_settings(i)
            self.gpio.show_leds_states()
            # Let the LED "twinkle" for duration / N_LEDS time
            time.sleep(duration / N_LEDS)
            self.gpio.cleanup()

    def power_up_blink(self):
        """ Light pattern associated with powering up the system """
        # Blinks 4 middle LEDs and first+last LEDs in turn
        for i in range(N_LEDS - 1):
            if i % 2 == 0:
                self.__led_settings(1)
                self.__led_settings(2)
                self.__led_settings(3)
                self.__led_settings(4)
                self.gpio.show_leds_states()
            else:
                self.__led_settings(0)
                self.__led_settings(5)
                self.gpio.show_leds_states()
            time.sleep(0.5)
            self.gpio.cleanup()

    def power_down_blink(self):
        """ Light pattern associated with powering down the system """
        # First twinkle LEDs in correct order (0-5)
        self.twinkle_all_leds(1)
        # Then twinkle LEDs in reversed order (5-0)
        for i in range(5):
            self.__led_settings(4 - i)
            self.gpio.show_leds_states()
            # Let the LED "twinkle" for duration / N_LEDS time
            time.sleep(0.2)
            self.gpio.cleanup()

    def __led_settings(self, led):
        """ Sets up the charlieplexed pins according to some settings """
        # Get the states for a particular LED
        led_state_setting = self.led_mapping.get(led)
        # Get specified settings for which modes pins should be in
        charlie_mode_setting = self.charlie_mapping.get(led)

        # Set up charlieplexed pins with correct mode
        for i in range(len(charlieplexing_pins)):
            self.gpio.setup(i, charlie_mode_setting[i])

        # Set let state on output pins only
        for i in range(len(charlieplexing_pins)):
            if charlie_mode_setting[i] == self.gpio.OUT:
                self.gpio.output(i, led_state_setting[i])
示例#7
0
class LedDriver():
    """Led driver class"""
    def __init__(self) -> None:
        self.gpio_sim = GPIOSimulator()
        self.led_lighting_states = (
            ((0, 1, 1), (1, 1, 0)),
            ((1, 1, 1), (0, 1, 0)),
            ((1, 1, 1), (2, 1, 0)),
            ((2, 1, 1), (1, 1, 0)),
            ((0, 1, 1), (2, 1, 0)),
            ((2, 1, 1), (0, 1, 0)),
        )
        self._all_to_input()

    def _all_to_input(self) -> None:
        for i in range(0, 6):
            self.gpio_sim.setup(i, 0)

    def _light_diode(self, led: int) -> None:
        states = self.led_lighting_states[led]
        self.gpio_sim.setup(states[0][0], 1)
        self.gpio_sim.setup(states[1][0], 1)
        self.gpio_sim.output(states[0][0], states[0][2])
        self.gpio_sim.output(states[1][0], states[1][2])
        self.gpio_sim.show_leds_states()
        self._all_to_input()

    def _light_sequence(self, sequence: Iterable) -> None:
        for led, state in enumerate(sequence):
            if state:
                self._light_diode(led)
        sleep(0.001)

    def _light_simult(self, sequence: Iterable, frame_count: int = 60) -> None:
        i = 0
        while i < frame_count:
            self._light_sequence(sequence)
            i += 1

    def _light_anim(self, sequence: Iterable, frame_count: int = 60) -> None:
        for frame in sequence:
            self._light_simult(frame, frame_count)

    def power_up(self) -> None:
        """Light power up sequence"""
        self._light_anim(
            ((1, 0, 0, 0, 0, 0), (1, 1, 0, 0, 0, 0), (1, 1, 1, 0, 0, 0),
             (1, 1, 1, 1, 0, 0), (1, 1, 1, 1, 1, 0), (1, 1, 1, 1, 1, 1)))

    def power_down(self) -> None:
        """Light power down sequence"""
        self._light_anim((
            (1, 1, 1, 1, 1, 1),
            (1, 1, 1, 1, 1, 0),
            (1, 1, 1, 1, 0, 0),
            (1, 1, 1, 0, 0, 0),
            (1, 1, 0, 0, 0, 0),
            (1, 0, 0, 0, 0, 0),
        ))

    def twinkle(self) -> None:
        """Light twinkle sequence"""
        self._light_anim((
            (0, 0, 0, 1, 1, 0),
            (0, 0, 1, 0, 0, 1),
            (1, 0, 1, 1, 0, 0),
            (0, 1, 0, 0, 1, 0),
            (1, 0, 1, 0, 0, 1),
            (0, 1, 0, 0, 1, 0),
            (0, 1, 0, 0, 1, 0),
            (0, 0, 1, 0, 0, 1),
            (1, 0, 1, 1, 0, 0),
            (0, 1, 0, 0, 1, 0),
            (1, 0, 1, 0, 0, 1),
            (0, 1, 0, 0, 1, 0),
        ), 10)

    def flash(self) -> None:
        """Light flash sequence"""
        self._light_anim((
            (1, 1, 1, 1, 1, 1),
            (0, 0, 0, 0, 0, 0),
            (1, 1, 1, 1, 1, 1),
            (0, 0, 0, 0, 0, 0),
            (1, 1, 1, 1, 1, 1),
            (0, 0, 0, 0, 0, 0),
            (1, 1, 1, 1, 1, 1),
            (0, 0, 0, 0, 0, 0),
            (1, 1, 1, 1, 1, 1),
            (0, 0, 0, 0, 0, 0),
            (1, 1, 1, 1, 1, 1),
            (0, 0, 0, 0, 0, 0),
        ), 10)

    def light_single(self, led: int, dur: float) -> None:
        """Light single LED"""
        self._light_diode(led)
        sleep(dur)
        self._all_to_input()
        self.gpio_sim.show_leds_states()
示例#8
0
"""Keypad simulation"""
from time import sleep, time
from GPIOSimulator_v5 import GPIOSimulator, keypad_row_pins, keypad_col_pins

GPIO = GPIOSimulator()

for row_pin in keypad_row_pins:
    GPIO.setup(row_pin, GPIO.OUT)
for col_pin in keypad_col_pins:
    GPIO.setup(col_pin, GPIO.IN, state=GPIO.LOW)

KEYS = list(i for i in range(1, 10))
KEYS.extend(["*", 0, "#"])

MAX_PRESS_DURATION = 1


def poll():
    """Returns current key pressed, None is nothing is pressed"""
    for _r, r_pin in enumerate(keypad_row_pins):
        GPIO.output(r_pin, GPIO.HIGH)

        for _c, c_pin in enumerate(keypad_col_pins):
            if GPIO.input(c_pin) == GPIO.HIGH:
                GPIO.output(r_pin, GPIO.LOW)
                return KEYS[_r * 3 + _c]

        GPIO.output(r_pin, GPIO.LOW)
    return None