Exemplo n.º 1
0
 def __init__(self):
     self.km = KMotor()
     display.show(Image.HAPPY)
     self.dispatch = {
         'fwd': self.go_forward,
         'rev': self.go_reverse,
         'left': self.go_left,
         'right': self.go_right,
         'spin_right': self.spin_right,
         'spin_left': self.spin_left,
         'stop': self.stop,
     }
     radio.on()
     while True:
         if not pin15.read_digital():
             self.go_reverse()
             sleep(1000)
             self.go_right()
             sleep(1000)
             self.stop()
         cmd = radio.receive()
         if cmd is None:
             pass
         else:
             op = self.dispatch.get(cmd, None)
             if op:
                 op()
Exemplo n.º 2
0
def dip():
    dip = [
        pin12.read_digital(),
        pin13.read_digital(),
        pin14.read_digital(),
        pin15.read_digital()
    ]
    switch = str(dip[0]) + str(dip[1]) + str(dip[2]) + str(dip[3])
    return switch
Exemplo n.º 3
0
    def __init__(self):
        """
        Instantiate an instance of KMotor.
        Display a "smile" to let the use know that upon
        first power up, we are alive.
        Create a dispatch table for each radio command.
        Turn on the radio and wait for commands to come in.
        """

        # instantiate a motor object
        self.km = KMotor()

        # display something so that we know the board is
        # alive
        display.show(Image.HAPPY)

        # build a dispatch dictionary to allow us
        # to quickly execute each incoming command
        self.dispatch = {
            'fwd': self.go_forward,
            'rev': self.go_reverse,
            'left': self.go_left,
            'right': self.go_right,
            'spin_right': self.spin_right,
            'spin_left': self.spin_left,
            'stop': self.stop,
        }
        # turn on the micro:bit radio so that we can receive commands.
        radio.on()

        while True:
            # check to see if we need to perform an avoidance maneuver.
            # when an object is near, the distance sensor returns a digital 0.
            # if a crash is imminent, reverse and go right
            if not pin15.read_digital():
                self.go_reverse()
                sleep(1000)
                self.go_right()
                sleep(1000)
                self.stop()

            # get next command
            cmd = radio.receive()
            if cmd is None:
                pass
            else:
                # move motors based on command
                # fetch command from command table
                op = self.dispatch.get(cmd, None)
                # if a command has been fetched, execute it
                if op:
                    op()
Exemplo n.º 4
0
    def play(self):
        p1 = pin1.read_analog()
        p2 = pin2.read_analog()
        if p1 <= 300:
            if p2 < 300:
                display.show(Image.ARROW_SW)
            elif p2 >= 900:
                display.show(Image.ARROW_NW)
            else:
                display.show(Image.ARROW_W)
        elif p1 >= 900:
            if p2 < 300:
                display.show(Image.ARROW_SE)
            elif p2 >= 900:
                display.show(Image.ARROW_NE)
            else:
                display.show(Image.ARROW_E)
        elif 300 < p1 < 900:
            if p2 < 300:
                display.show(Image.ARROW_S)
            elif p2 >= 900:
                display.show(Image.ARROW_N)
            else:
                if not pin15.read_digital():
                    display.show(Image.HAPPY)
                    music.play(music.JUMP_UP)
                elif not pin14.read_digital():
                    display.show(Image.SURPRISED)
                    music.play(music.POWER_UP)
                elif not pin13.read_digital():
                    display.show(Image.SAD)
                    music.play(music.JUMP_DOWN)
                elif not pin12.read_digital():
                    display.show(Image.ASLEEP)
                    music.play(music.POWER_DOWN)
                elif not pin8.read_digital():
                    display.show(Image.HEART)
                    music.play(["C1:1", "C#1:1"])

        display.clear()
Exemplo n.º 5
0
    if utime.ticks_diff(utime.ticks_ms(), prev) > delay:
        prev = utime.ticks_ms()
        grid = update_grid(grid)
        plot(grid)

        ship_pos = grid[0][ship]
        if ship_pos == constants.POWER:
            points = power_up(points)
        if ship_pos == constants.MISSILE:
            missile.acquire()
            music.pitch(200, duration=50, pin=pin2)
            music.pitch(400, duration=300, wait=False, pin=pin2)
        elif grid[0][ship] == constants.ASTEROID:
            points = hit(points)

    new_ship = update_ship(ship)

    if missile.got_missile() and pin15.read_digital() == 0:
        missile.fire(ship)

    if missile.is_away():
        grid = missile.step_and_plot(grid)

    if not new_ship == ship:
        ship = new_ship
        sleep(100)

    plot_ship(ship, missile)

    np.show()
Exemplo n.º 6
0
 def btn_pressed(self):
     return not pin15.read_digital() or not pin14.read_digital(
     ) or not pin13.read_digital() or not pin12.read_digital(
     ) or not pin8.read_digital()