Maker.io main logo

PyRuler as a Keyboard and Using it on a Mac

2019-08-21 | By Kevin Walseth

It seems rulers are a very impactful part of an Engineer’s toolbox. They have been coming in all shapes and sizes. But this is the first time I have seen a ruler with a built-in functional circuit board. To no surprise, this comes from Adafruit. The goal behind the PyRuler is to give the functionality of Circuit Python at your fingertips – literally. This ruler has 4 capacitive touchpads that are pre-programmed to help you type those crazy characters needed while building circuit boards, writing code, or bragging to your friends in an email about your recent engineering feat. Ω, µ, and π are the most frequently used and time consuming to type. This is what we all have needed for a long time.

Simply plug the Trinket M0 powered ruler into your computer with a micro USB cable. A file drive will pop up labeled CircuitPY.

PyRuler as a Keyboard and Using it on a Mac

If you would like to use this ruler as a keyboard, you will have to change one simple line of code. Open the Code.py file in any notepad or text editor and change the following line:

ENABLE_KEYBOARD = False

To

ENABLE_KEYBOARD = True

And that’s it you are all set. Click save and it will reset the board and work as a keyboard. As I’m sure many of you are aware, a keyboard on a Mac is a little different. Here is the code found in the Adafruit Forums:

Copy Code
import os
import board
from digitalio import DigitalInOut, Direction
import time
import touchio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

# Set this to True to turn the touchpads into a keyboard
ENABLE_KEYBOARD = True

# Set this to true if you're on windows, false if you're on mac
WINDOWS_COMPUTER = True

# Used if we do HID output, see below
kbd = Keyboard()
layout = KeyboardLayoutUS(kbd)

# Print a little about ourselves
print(dir(board), os.uname())
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

touches = [DigitalInOut(board.CAP0)]
for p in (board.CAP1, board.CAP2, board.CAP3):
    touches.append(touchio.TouchIn(p))

leds = []
for p in (board.LED4, board.LED5, board.LED6, board.LED7):
    led = DigitalInOut(p)
    led.direction = Direction.OUTPUT
    led.value = False
    leds.append(led)

cap_touches = [False, False, False, False]

def read_caps():
    t0_count = 0
    t0 = touches[0]
    t0.direction = Direction.OUTPUT
    t0.value = True
    t0.direction = Direction.INPUT
    # funky idea but we can 'diy' the one non-hardware captouch device by hand
    # by reading the drooping voltage on a tri-state pin.
    t0_count = t0.value + t0.value + t0.value + t0.value + t0.value + \
               t0.value + t0.value + t0.value + t0.value + t0.value + \
               t0.value + t0.value + t0.value + t0.value + t0.value
    cap_touches[0] = t0_count > 2
    cap_touches[1] = touches[1].raw_value > 3000
    cap_touches[2] = touches[2].raw_value > 3000
    cap_touches[3] = touches[3].raw_value > 3000
    return cap_touches

def type_alt_code(code):
    kbd.press(Keycode.ALT)
    for c in str(code):
        if c == '0':
            keycode = Keycode.KEYPAD_ZERO
        elif '1' <= c <= '9':
            keycode = Keycode.KEYPAD_ONE + ord(c) - ord('1')
        else:
            raise RuntimeError("Only number codes permitted!")
        kbd.press(keycode)
        kbd.release(keycode)
    kbd.release_all()

while True:
    caps = read_caps()
    print(caps)
    # light up the matching LED
    for i,c in enumerate(caps):
        leds[i].value = c
    if caps[0]:
        if ENABLE_KEYBOARD:
            if WINDOWS_COMPUTER:
                type_alt_code(234)
            else:
                kbd.send(Keycode.ALT, Keycode.Z)
    if caps[1]:
        if ENABLE_KEYBOARD:
            if WINDOWS_COMPUTER:
                type_alt_code(230)
            else:
                kbd.send(Keycode.ALT, Keycode.M)
    if caps[2]:
        if ENABLE_KEYBOARD:
            if WINDOWS_COMPUTER:
                type_alt_code(227)
            else:
                kbd.send(Keycode.ALT, Keycode.P)
    if caps[3]:
        if ENABLE_KEYBOARD:
            layout.write('https://www.digikey.com/python\n')
    time.sleep(0.1)

The opportunities are endless with this functional measuring stick. Try changing the text, use it to open your favorite website. Not recommended, but you can use it to store your password to unlock your computer if you work at a large corporation that requires ridiculous passwords! Speaking for a friend.

制造商零件编号 4319
PYRULER WITH EMBEDDED TRINKET M0
Adafruit Industries LLC
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.