Ejemplo n.º 1
0
def process_queue():
    """This will print all the letters in the queue and then keep processing
    letters as they are added to the queue.

    Use this block when you are printing text that the user is encoding using
    Morse code via the touch sensor. For example, the Printer program uses this
    block to continually process the user input.

    You can stop this block from processing text by hitting the center button
    on the EV3.
    """

    def thread1():
        while True:
            if len(variables['Queue']) > variables['QPosition']:
                qposition = variables['QPosition']
                print_letter(variables['Queue'][qposition])
                variables['QPosition'] = qposition + 1
            if variables['EStop']:
                break

    def thread2():
        buttons.wait((CENTER,), State.PRESSED)
        variables['EStop'] = True

    fork(thread1, thread2)
Ejemplo n.º 2
0
def calibrate():
    initialize()
    motor['C'].on_for_rotations(25, -2.5)

    def thread1():
        while True:
            sleep(0.25)
            carriage_move(0)
            variables['LinePosition'] = 0
            sleep(0.25)
            line_width = variables['LineWidth']
            carriage_move(line_width)
            variables['LinePosition'] = line_width
            if buttons.test((CENTER, ), State.BUMPED):
                break
        carriage_move(525)
        motor['C'].on_for_rotations(25, 3)

    def thread2():
        while True:
            buttons.wait((UP, CENTER, DOWN), State.PRESSED)
            button = buttons.read()
            if button == UP:
                motor['C'].on_for_degrees(25, 5)
            elif button == DOWN:
                motor['C'].on_for_degrees(-25, 5)
            elif button == CENTER:
                # This is not in the EV3-G program, but is needed in order for
                # the program to exit, otherwise this thread keeps running.
                raise SystemExit

    fork(thread1, thread2)
def read_code(publish=None):
    """This block is responsible for analysing the timing between pushes of the
    touch sensor to convert a Morse code signal into a letter.
    """
    # The top loop continuously measures the length of successive button
    # presses and builds an array of dots and dashes (shorts and longs, or 0s
    # and 1s) to represent the letter that is encoded.

    # The bottom loop waits until a length of time has passed without the
    # button being pressed (using the second timer, which is reset after every
    # button press in the top loop), indicating that coding the letter is now
    # complete. When that happens, the 'TranslateCode' block is called to
    # analyse the 'Code' array to see what letter was encoded.

    variables['CodeIndex'] = 0

    def thread1():
        while True:
            # Wait for the touch sensor to be depressed. When that happens
            # start the first timer and start playing the tone.
            # Wait for the touch sensor to be released. When that happens
            # measure how long the first timer has been on and stop playing the
            # tone.
            sensor['1'].wait(EV3TouchSensor.PRESSED)
            sound.play_tone(440, 1, 5, PlayType.ONCE)
            timer[1].reset()
            sensor['1'].wait(EV3TouchSensor.RELEASED)
            sound.stop()
            timer[2].reset()
            variables['Encoding'] = True
            if timer[1].elapsed_time() < 0.2:
                variables['CodeSignal'] = 0
            else:
                variables['CodeSignal'] = 1
            # Depending on the length of the press, we will get a 0 or 1.
            # Append that value to the 'Code' array.
            code_index = variables['CodeIndex']
            variables['Code'] = write_at_index(variables['Code'], code_index,
                                               variables['CodeSignal'])
            variables['CodeIndex'] = code_index + 1

    def thread2():
        while True:
            if variables['Encoding']:
                timer[2].wait(operator.gt, 0.8)
                variables['Encoding'] = False
                translate_code(not bool(publish))
                variables['CodeIndex'] = 0
                letter = variables['NewCharacter']
                if publish and letter != -1:
                    publish(letter)

    fork(thread1, thread2)
    def thread1():
        """Continuously process the queue of letters."""
        motor['C'].on_for_rotations(50, -2.5)
        carriage_move(0)
        process_queue()
        carriage_move(525)

        def thread1_1():
            motor['C'].on_for_rotations(50, 2.5)

        def thread1_2():
            feed_out()

        fork(thread1_1, thread1_2)
def main(server=SERVER, me=ME, you=YOU):
    # init MQTT
    c = MQTTClient(me, server)
    c.set_callback(handle_message)
    c.connect()
    c.subscribe(('ev3dev-telegraph-net/' + you).encode())
    topic = ('ev3dev-telegraph-net/' + me).encode()

    # init printer
    initialize()
    feed_in()

    def thread1():
        """Continuously process the queue of letters."""
        motor['C'].on_for_rotations(50, -2.5)
        carriage_move(0)
        process_queue()
        carriage_move(525)

        def thread1_1():
            motor['C'].on_for_rotations(50, 2.5)

        def thread1_2():
            feed_out()

        fork(thread1_1, thread1_2)

    def thread2():
        """Continuously monitor the touch sensor and convert the Morse code
        sequence into a letter index which gets published.
        """
        read_code(lambda l: c.publish(topic, bytes((l, ))))

    def thread3():
        """This will wait for any MQTT messages, and add them to the letter
        queue via handle_message().
        """
        while True:
            try:
                c.wait_msg()
            except OSError as err:
                if err.args[0] == EINTR:
                    continue
                raise

    fork(thread1, thread2, thread3)
Ejemplo n.º 6
0
def print_j(size):
    seg1 = variables['Seg1']
    seg2 = variables['Seg2']
    seg3 = variables['Seg3']
    seg4 = variables['Seg4']
    motor['A'].on_for_degrees(20, seg4)
    motor['A'].on_for_degrees(-20, 10)
    lower_pen()
    motor['B'].on_for_degrees(-20, seg3)
    motor['A+B'].on_for_degrees(-10, -10, seg1)
    motor['A'].on_for_degrees(-10, seg2)
    motor['A+B'].on_for_degrees(-10, 10, seg1)
    motor['B'].on_for_degrees(20, seg1)
    lift_pen()

    def thread1():
        motor['B'].on_for_degrees(20, seg3)

    def thread2():
        motor['A'].on_for_degrees(20, seg4)
        motor['A'].on_for_degrees(20, 10)

    fork(thread1, thread2)
Ejemplo n.º 7
0
def printer():
    initialize()
    feed_in()

    def thread1():
        """Continuously process the queue of letters."""
        motor['C'].on_for_rotations(50, -2.5)
        carriage_move(0)
        process_queue()
        carriage_move(525)

        def thread1_1():
            motor['C'].on_for_rotations(50, 2.5)

        def thread1_2():
            feed_out()

        fork(thread1_1, thread1_2)

    def thread2():
        """Continuously monitor the touch sensor and convert the Morse code
        sequence into a letter index which gets added to the letter queue.
        """
        read_code()

    def thread3():
        """This will wait for any bluetooth letter messages, and add them to
        the letter queue. This is only relevent if you are using 2 EV3 units to
        have a separate transmitter and receiver.
        """
        while True:
            letter = messaging.wait_update('Letter')
            queue = variables['Queue']
            variables['Queue'] = write_at_index(queue, len(queue), letter)

    fork(thread1, thread2, thread3)
Ejemplo n.º 8
0
def print_test_page():
    """This will print a full test page. It will do an entire print process and
    test various parts of the program.
    """

    # Initialize, feed in the paper, lower the pen and draw a registration
    # line.
    initialize()
    feed_in()
    motor['C'].on_for_rotations(50, -2.5)
    carriage_move(0)
    print_registration_line()

    # Set the test size and draw the 'Quick brown fox...' text.
    text_size(10)
    line_feed()
    variables['Queue'] = variables['QuickFox']
    print_queue()
    line_feed()

    # Print 'Mindstorms' several times, each at a different text size and on
    # its own line.
    variables['Queue'] = variables['Mindstorms']
    variables['QPosition'] = 0
    print_queue()
    text_size(12)
    variables['QPosition'] = 0
    print_queue()
    text_size(14)
    variables['QPosition'] = 0
    print_queue()
    text_size(16)
    variables['QPosition'] = 0
    print_queue()

    # Print the EV3 Logo.
    text_size(10)
    line_feed()
    print_ev3_logo(1)

    # Print 'JK Brickworks' and 'Keep on Building'
    line_feed()
    variables['Queue'] = variables['JKBrickworks']
    variables['QPosition'] = 0
    print_queue()
    line_feed()
    variables['Queue'] = variables['KeepBuilding']
    variables['QPosition'] = 0
    print_queue()
    line_feed()
    print_registration_line()

    # Move the pen back to the middle, lift it and feed out the paper.
    carriage_move(525)

    def thread1():
        motor['C'].on_for_rotations(50, 2.5)

    def thread2():
        feed_out()

    fork(thread1, thread2)