コード例 #1
0
 def __init__(self):
     self.name = ''
     self.strength = ''
     self.status = ''
     self.kanaal = ''
     self.oled = Display()
     self.wlan = network.WLAN(network.STA_IF)
     self.prepare_wifi()
     time.sleep_ms(5000)
     self.oled.print_wrapped("Performing scan...")
     time.sleep(0.5)
     self.oled.clear(0, 1)
     self.format()
コード例 #2
0
def show():
    
    oled = Display()

    for image in images:
        oled.clear(0, 1)
        oled.draw_graphic(image, 35, 2)
        time.sleep(5)
コード例 #3
0
def show():
    """
    Cycle through the images showing them.

    Returns:
        None
    """
    oled = Display()

    for image in images:
        oled.clear(0, 1)
        oled.draw_graphic(image, 35, 2)
        time.sleep(5)
コード例 #4
0
import time
import urandom
from console import Display, Button

oled = Display()
buttonSelect = Button(0)

questions = [
    "Which person around you could help you?",
    "Maybe you should consider refactoring?",
    "Have you tried step by step execution?",
    "Maybe some threads are unnecessary?",
    "Maybe you turn it off and on again?",
    "Could there be some race condition?", "Where is the lacking print()?",
    "Have you talked to your colleague?", "Why not call for a group meeting?",
    "Why not walk around your office?", "Should you really be doing this?",
    "Can the problem be broken down?", "Have you tried to unit test it?",
    "Maybe it is time for a coffee?", "Have you tried a clean build?",
    "Can you give me more details?", "Can this be hardware related?",
    "Could this not be your fault?", "Is this actually a problem?",
    "Can the code be simplified?", "Have you tried googling it?",
    "Maybe you can turn it on and off?", "I am not sure try again!",
    "Try again after having a coffee!",
    "Walk around the office and try again!",
    "Have you tried pair programming this?",
    "Maybe some of the threads are unnecessary?",
    "Can the problem be broken down more?",
    "Call for a group meeting to discuss this!",
    "Maybe you should consider some refactoring!",
    "Have you tried googling it?", "Why not try it again?"
]
コード例 #5
0
import time
from console import Display

oled = Display()

oled.print_wrapped("Scanning I2C Devices on the bus...")
time.sleep(0.5)
devices = oled.i2c.scan()

if len(devices) == 0:
    oled.clear(0, 1)
    oled.print_wrapped("No I2C devices found :(")

else:
    oled.clear(0, 1)
    oled.print_wrapped('%s I2C devices found' % str(len(devices)))
    time.sleep(1.5)

    for count in range(1, 2):
        for device in devices:
            oled.clear(0, 1)
            oled.print_on_line("Decimal address:", 2)
            oled.print_on_line(str(device), 3)
            oled.print_on_line("Hex address:", 4)
            oled.print_on_line(str(hex(device)), 5)
            time.sleep(1)
        oled.clear(0, 1)
コード例 #6
0
 def __init__(self):
     self.best = 0
     self.oled = Display()
     self.width = self.oled.width
     self.height = self.oled.height
     self.select_button = Button(0)
コード例 #7
0
class ConwaysGameOfLife:
    def __init__(self):
        self.best = 0
        self.oled = Display()
        self.width = self.oled.width
        self.height = self.oled.height
        self.select_button = Button(0)

    def center_text(self, txt):
        """
        This function will center all incomming strings to a fix size of the display
        max_char property.

        Args:
            txt: String to be centered.

        Returns:
            type: String with the text centered
        """
        return '{: ^{}}'.format(txt, self.oled.max_char)

    def intro(self):
        """
        It will print the intro on the oled display with a sleep of 1 second so
        the message will be visible.

        Returns:
            Nothing.
        """
        self.oled.display.fill(0)
        self.oled.print_on_line(self.center_text("Conway's"), 2)
        self.oled.print_on_line(self.center_text("Game"), 3)
        self.oled.print_on_line(self.center_text("of"), 4)
        self.oled.print_on_line(self.center_text("Life"), 5)
        sleep_ms(1000)

    def end(self, generations, best, size):
        """
        It will ouput on the oled display the Generations, best score and the
        size of the pixels.

        Args:
            generations: Integer with the generations created.
            best: Integer the best score.
            size: Integer with the size of the pixels.

        Returns:
            Nothing.
        """
        self.oled.clear(0, 1)
        self.oled.print_on_line(self.center_text("Generations"), 0)
        self.oled.print_on_line(self.center_text(str(generations)), 1)
        self.oled.print_on_line(self.center_text("Best Score"), 3)
        self.oled.print_on_line(self.center_text(str(best)), 4)
        self.oled.print_on_line(self.center_text("Pixel Size"), 5)
        self.oled.print_on_line(self.center_text(str(size)), 6)
        sleep_ms(2000)
        self.oled.clear(0, 1)

    def begin(self, size=4, delay=20):
        """
        Main function to check the button to stop the game or if game has ended.
        If so, then call the `end` function to show the results.

        Args:
            size: Integer with the size of the pixels.
            delay: Integer of the delay between generations (In milliseconds).

        Returns:
            Nothing.
        """
        self.size = size  # Size of lifeforms in pixels
        self.delay = delay  # Delay in ms between generations

        # Localised to avoid self lookups
        # Possible performance optimisation, TBC
        delay = self.delay
        tick = self.tick
        self.randomise()  # Randomise initial pixels

        generations = 0  # Begin
        try:
            while (tick() and self.select_button.isReleased()):
                generations = generations + 1
                self.oled.display.show()
                sleep_ms(delay)
        except KeyboardInterrupt:
            pass

        if generations > self.best:  # New high score?
            self.best = generations

        self.end(generations, self.best, self.size)  # End

    def randomise(self):
        """
        Generate a seudo-random cells on the screen.

        Returns:
            Nothing.
        """
        size = self.size
        width = self.width
        height = self.height
        self.oled.display.fill(0)

        for x in range(0, width, size):
            for y in range(0, height, size):
                # random bit: 0 = pixel off, 1 = pixel on
                self.cell(x, y, getrandbits(1))

    def cell(self, x, y, colour):
        """
        This will assign a value to a given pixel.

        Args:
            x: Integer with the `x` axis position.
            y: Integer with the `y` axis position.
            colour: Boolean with the value to be assigned.

        Returns:
            Nothing.
        """
        size = self.size
        for i in range(size):
            for j in range(size):
                self.oled.display.pixel(x + i, y + j, colour)

    def get(self, x, y):
        """
        Function to return the value of the given pixel x, y.

        Args:
            x: Integer with the `x` axis position.
            y: Integer with the `y` axis position.

        Returns:
            type: Boolean with the value of the pixel.
        """
        if not 0 <= x < self.width or not 0 <= y < self.height:
            return 0
        if self.oled.display.pixel(x, y) is None:
            bln_return = getrandbits(1)
        else:
            bln_return = self.oled.display.pixel(x, y)
        return bln_return & 1

    def tick(self):
        """
        This function will apply the game rules getting the live cells and the ones
        that have to die.

        Returns:
            Boolean to keep the game going or not.
        """
        size = self.size
        width = self.width
        height = self.height
        get = self.get
        cell = self.cell

        # If no pixels are born or die, the game ends
        something_happened = False

        for x in range(0, width, size):
            for y in range(0, height, size):

                alive = get(x, y)  # Is the current cell alive

                # Count number of neighbours
                neighbours = (get(x - size, y - size) + get(x, y - size) +
                              get(x + size, y - size) + get(x - size, y) +
                              get(x + size, y) + get(x + size, y + size) +
                              get(x, y + size) + get(x - size, y + size))

                # Apply the game rules
                if alive and not 2 <= neighbours <= 3:
                    cell(x, y, 0)  # This pixel dies
                    if not something_happened:
                        something_happened = True
                elif not alive and neighbours == 3:
                    cell(x, y, 1)  # A new pixel is born
                    if not something_happened:
                        something_happened = True
        return something_happened
コード例 #8
0
class WiFiScanner:
    def __init__(self):
        self.name = ''
        self.strength = ''
        self.status = ''
        self.kanaal = ''
        self.oled = Display()
        self.wlan = network.WLAN(network.STA_IF)
        self.prepare_wifi()
        time.sleep_ms(5000)
        self.oled.print_wrapped("Performing scan...")
        time.sleep(0.5)
        self.oled.clear(0, 1)
        self.format()

    def prepare_wifi(self):
        """
        Function to disable WiFi if it is in an active connection.

        Returns:
            Nothing.
        """
        if not self.wlan.active():
            self.oled.print_wrapped("Turning the WiFi ON.")
            time.sleep(0.5)
            self.oled.clear(0, 1)
            self.wlan.active(True)
        elif self.wlan.active() and self.wlan.isconnected():
            self.oled.print_wrapped("Disconnecting from WiFi connection.")
            time.sleep(0.5)
            self.oled.clear(0, 1)
            self.wlan.disconnect()

    def format(self):
        """
        Format information looping from the wlan.scan performed.

        Returns:
            Nothing.
        """
        try:
            wlan_list = self.wlan.scan()
        except:
            wlan_list = [['NONE', 'NONE', 'NONE', 'NONE', 'NONE', 'NONE']]
        for counter in wlan_list:
            self.name = str(counter[0], 'utf8')
            self.strength = str(counter[3]) + ' dBm'
            self.kanaal = 'Channel: ' + str(counter[2])
            self.status = self.get_secure(counter[4])
            self.show_info()
            self.oled.clear(0, 1)

    @staticmethod
    def get_secure(num):
        """
        Convert the number from wlan.scan into something human readable.

        Args:
            num: Integer from the scan regarding authmode.

        Returns:
            String with the conversion of the authmode.
        """
        s_return = ""
        try:
            if int(num) == 0:
                s_return = 'Open wifi'
            elif int(num) == 1:
                s_return = 'WEP'
            elif int(num) ==2:
                s_return = 'WPA-PSK'
            elif int(num) == 3:
                s_return = 'WPA2-PSK'
            elif int(num) == 4:
                s_return = 'WPA/WPA2-PSK'
            else:
                s_return = str("UNKNOWN %s" % num)

            return s_return
        except:
            return s_return

    def show_info(self):
        """
        Show information on the Display nicely.

        Returns:
            Nothing.
        """
        self.oled.clear(0, 1)
        if len(self.name) > 15:
            self.oled.print_on_line(self.name[0:15], 0)
            self.oled.print_on_line(self.name[15:int(len(self.name))], 1)
        else:
            self.oled.print_on_line(self.name, 0)

        self.oled.print_on_line(self.strength, 2, 30)
        self.oled.print_on_line(self.status, 3, 30)
        self.oled.print_on_line(self.kanaal, 4, 30)
        self.oled.print_on_line((str(gc.mem_free()) + " B"), 5, 30)
        time.sleep_ms(10000)
コード例 #9
0
class ConwaysGameOfLife:
    def __init__(self):
        self.best = 0
        self.oled = Display()
        self.width = self.oled.width
        self.height = self.oled.height
        self.select_button = Button(0)

    def center_text(self, txt):
        return '{: ^{}}'.format(txt, self.oled.max_char)

    def intro(self):
        self.oled.display.fill(0)
        self.oled.print_on_line(self.center_text("Conway's"), 2)
        self.oled.print_on_line(self.center_text("Game"), 3)
        self.oled.print_on_line(self.center_text("of"), 4)
        self.oled.print_on_line(self.center_text("Life"), 5)
        sleep_ms(1000)

    def end(self, generations, best, size):
        self.oled.clear(0, 1)
        self.oled.print_on_line(self.center_text("Generations"), 0)
        self.oled.print_on_line(self.center_text(str(generations)), 1)
        self.oled.print_on_line(self.center_text("Best Score"), 3)
        self.oled.print_on_line(self.center_text(str(best)), 4)
        self.oled.print_on_line(self.center_text("Pixel Size"), 5)
        self.oled.print_on_line(self.center_text(str(size)), 6)
        sleep_ms(2000)
        self.oled.clear(0, 1)

    def begin(self, size=4, delay=20):
        self.size = size
        self.delay = delay
        delay = self.delay
        tick = self.tick
        self.randomise()
        generations = 0
        try:
            while (tick() and self.select_button.isReleased()):
                generations = generations + 1
                self.oled.display.show()
                sleep_ms(delay)
        except KeyboardInterrupt:
            pass

        if generations > self.best:
            self.best = generations

        self.end(generations, self.best, self.size)

    def randomise(self):
        size = self.size
        width = self.width
        height = self.height
        self.oled.display.fill(0)

        for x in range(0, width, size):
            for y in range(0, height, size):
                self.cell(x, y, getrandbits(1))

    def cell(self, x, y, colour):
        size = self.size
        for i in range(size):
            for j in range(size):
                self.oled.display.pixel(x + i, y + j, colour)

    def get(self, x, y):
        if not 0 <= x < self.width or not 0 <= y < self.height:
            return 0
        if self.oled.display.pixel(x, y) is None:
            bln_return = getrandbits(1)
        else:
            bln_return = self.oled.display.pixel(x, y)
        return bln_return & 1

    def tick(self):
        size = self.size
        width = self.width
        height = self.height
        get = self.get
        cell = self.cell
        something_happened = False

        for x in range(0, width, size):
            for y in range(0, height, size):
                alive = get(x, y)
                neighbours = (get(x - size, y - size) + get(x, y - size) +
                              get(x + size, y - size) + get(x - size, y) +
                              get(x + size, y) + get(x + size, y + size) +
                              get(x, y + size) + get(x - size, y + size))

                if alive and not 2 <= neighbours <= 3:
                    cell(x, y, 0)
                    if not something_happened:
                        something_happened = True
                elif not alive and neighbours == 3:
                    cell(x, y, 1)
                    if not something_happened:
                        something_happened = True
        return something_happened