Maker.io main logo

Raspberry Pi Pico Space Mouse for OnShape CAD Design

349

2023-02-22 | By Travis Foss

License: See Original Project Raspberry Pi MCU

For as long as I have worked in CAD programs, I’ve always thought it would be nice to have a β€Žspace mouse to control certain aspects of the software. If you aren’t familiar, a space mouse is β€Ža specialty mouse used to rotate or control a 3D object in a CAD Program. While looking into β€Žsome of the DIY space mouse builds out there, I noticed that a lot of them are just using β€ŽAdafruit’s HID library to control some of the CAD commands, and it got me thinking of a β€Žproject. Could I build a CAD Keypad?β€Ž

I was looking through the various products I had collected that were not in use yet and realized I β€Žhad one of Pimoroni’s Pico RGB keypads (DK part number 1778-PIM551-ND). The Pimoroni Pico β€ŽRGB keypad uses a Raspberry Pi Pico for the microcontroller, one of the most readily available β€Žmicrocontrollers. So, this would be a perfect project to learn more about the Pico and the Pico β€ŽRGB keypad.β€Ž

With OnShape being my most frequently used CAD software, I started researching what β€Žkeyboard shortcuts exist for it. During my research, I discovered over 100 keyboard shortcuts β€Žthat can be used with this software. There are so many shortcuts that they have them divided β€Žinto different sections depending on what part of the program you are in. There is a general β€Žsection, assembly section, part studio section, 3D view section, sketch section, and a drawings β€Žsection.β€Ž

To test if my idea would work, I wanted to see if I could get the rotation of the object to work β€Žfirst. In order to get that to work, I would need to be able to send the 4 keyboard directional β€Žbutton commands to the software. After looking at the keypad, I decided to assign the button β€Žlabeled 1 as the up arrow, 4 as the left arrow, 6 as the right arrow, and 9 as the down arrow.β€Ž

keypad

The next thing that I had to decide was whether I wanted to program these with either the β€ŽArduino IDE or CircuitPython. Since I had rarely used CircuitPython, I thought this might be a β€Žgood learning experience to program with it.

β€ŽI started by downloading the latest version of CircuitPython for the Keybow 2040 as directed by β€ŽPimoroni in this getting started guide, https://github.com/pimoroni/pmk-circuitpython. Once β€Žthat was completed my Pico RGB Keypad was showing up as a CircuitPython device on the β€Žcomputer, so I knew I was ready to start programming it. I had previously used Thonny as the β€ŽIDE for programming so that was already installed on my computer.β€Ž

I then began to research what libraries were needed to get this up and running. While looking β€Žthrough various projects other people had created, I noticed that there are a few commonly β€Žused libraries. They are time, board, busio, and usb_hid. To get the LEDs on the board to work, I β€Žalso had to add the Adafruit_dotstar library. Lastly, to add the capability for the device to β€Žemulate keyboard functions, I Imported 3 libraries, Adafruit_hid.keyboard, β€ŽAdafruit_hid.keyboard_layout_us, and Adafruit_hid.keycode.β€Ž

Copy Code
import time
import board
import busio
import usb_hid

from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_dotstar

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

From there I dug into the code in Adafruit’s Keycode library, β€Žhttps://docs.circuitpython.org/projects/hid/en/latest/, to determine which codes were used to β€Žcall out the directional buttons. The commands were fairly straightforward as the up and down β€Ždirectional arrows were called out by sending a keyboard command of Keycode.UP_ARROW β€Žand Keycode.DOWN_ARROW and left and right were Keycode.LEFT_ARROW and β€ŽKeycode.RIGHT_ARROW. I opened Thonny and started putting together the program.β€Ž

After modifying the code, I had found from a couple of different sources, I ended up with the β€Žfollowing code.

Copy Code
import time
import board
import busio
import usb_hid

from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_dotstar

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from digitalio import DigitalInOut, Direction, Pull


cs = DigitalInOut(board.GP17)β€Ž
cs.direction = Direction.OUTPUT
cs.value = 0β€Ž
num_pixels = 16β€Ž
pixels = adafruit_dotstar.DotStar(board.GP18, board.GP19, num_pixels, brightness=0.1, β€Žauto_write=True)β€Ž

i2c = busio.I2C(board.GP5, board.GP4)β€Ž

device = I2CDevice(i2c, 0x20)β€Ž

kbd = Keyboard(usb_hid.devices)β€Ž
layout = KeyboardLayoutUS(kbd)β€Ž

def read_button_states(x, y):β€Ž
β€Ž    pressed = [0] * 16β€Ž
β€Ž    with device:β€Ž
β€Ž        device.write(bytes([0x0]))β€Ž
β€Ž        result = bytearray(2)β€Ž
β€Ž        device.readinto(result)β€Ž
β€Ž        b = result[0] | result[1] << 8β€Ž
β€Ž        for i in range(x, y):β€Ž
β€Ž            if not (1 << i) & b:β€Ž
β€Ž                pressed[i] = 1β€Ž
β€Ž            else:β€Ž
β€Ž                pressed[i] = 0β€Ž
β€Ž    return pressed
held = [0] * 16β€Ž

while True:β€Ž
β€Ž    pressed = read_button_states(0, 16)β€Ž
β€Ž    β€Ž
β€Ž    if pressed[1]: #check to see if button 1 is pressed

β€Ž        β€Ž
β€Ž        if not held[1]:#check to make sure the held variable isn't true
β€Ž            print("Up Arrow") #print action to serial monitor
β€Ž            kbd.send(Keycode.UP_ARROW)#send the keyboard command Up arrow to the device
β€Ž            held[1] = 1β€Ž

β€Ž    elif pressed[4]:   #check to see if button 4 is pressed
β€Ž        if not held[4]: #check to make sure the held variable isn't true   β€Ž
β€Ž            print("Left Arrow") #print action to serial monitor
β€Ž            kbd.send(Keycode.LEFT_ARROW)#send the keyboard command Left Arrrow to the device
β€Ž            held[4] = 1β€Ž
β€Ž                β€Ž
β€Ž    β€Ž
β€Ž    elif pressed[6]:  #check to see if button 6 is pressed        β€Ž
β€Ž        if not held[6]: #check to make sure the held variable isn't true
β€Ž            print("Right Arrow") #print action to serial monitor
β€Ž            kbd.send(Keycode.RIGHT_ARROW)#send the keyboard command Right Arrrow to the device
β€Ž            held[6] = 1β€Ž

β€Ž    elif pressed[9]:   #check to see if button 9 is pressed
β€Ž        if not held[9]:#check to make sure the held variable isn't true
β€Ž            print("Down Arrow") #print action to serial monitor
β€Ž            kbd.send(Keycode.DOWN_ARROW)#send the keyboard command Down Arrow to the device
β€Ž            held[9] = 1β€Ž

β€Ž    else:  # Released state
β€Ž        for i in range(16):β€Ž
β€Ž            held[i] = 0  # Set held states to off
β€Ž        time.sleep(0.1) # Debounce

β€ŽIt was time to test to see if this would work as anticipated.

β€ŽFor the initial test, I pressed the β€œRun Current Script” button in Thonny and started pressing the β€Žassigned keypad buttons. I was getting the printouts of each of the directional keys in the Serial β€Žmonitor, and it was also moving my cursor around in the code. Next up was to open an instance β€Žof OnShape and test it in that environment. Once the software was open and one of my β€Ždrawings active, I tried out the keypad by pushing the 4 buttons. It was rotating the object. It β€Žwas working! The one thing that I did notice was that it was only rotating 15 degrees at a time, β€Žand it wouldn’t rotate further unless I released and pressed the button again. β€Ž

Based on the initial test, I decided the next three things I would tackle would be adding color to β€Žthe 4 buttons to keep track of which ones I was using, getting the continuous rotation working, β€Žand adding an option so I could pan the object around the screen instead of rotating.β€Ž

Adding the color to the 4 buttons was fairly easy. To standardize the color, I wanted to create a β€Žvariable (green) and assign it the RGB color code of 0,255,0. One thing I found out after β€Žsearching the web to find out more about variables is that when declaring a variable, it does β€Žnot need to be called out as a certain type of variable. Once the initial value is assigned, it is β€Žautomatically classified behind the scenes. This makes creating variables simple. After creating β€Žthe variable and assigning the values, I assigned them to the pixels for the four buttons that we β€Žwere using - 1, 4, 6, and 9.β€Ž

The variable I created looked like this.β€Ž

Copy Code
green = (0,255,0)β€Ž

β€ŽUnder the while true statement, I also added these lines to get the pixels to light:

Copy Code
β€Ž#Color coding the buttons according to thier functionβ€Ž
β€Ž    pixels[1] = green
β€Ž    pixels[4] = green
β€Ž    pixels[6] = green
β€Ž    pixels[9] = green

I tested this by saving the code which automatically updates it on the board. The 4 buttons were β€Žnow lit in green.β€Žβ€Ž

Copy Code
if pressed[1]: #check to see if button 1 is pressed β€Ž
β€Ž        if not held[1]:#check to make sure the held variable isn't true
β€Ž            print("Up Arrow") #print action to serial monitor
β€Ž            kbd.send(Keycode.UP_ARROW)#send the keyboard command Up arrow to the device
β€Ž            held[1] = 1β€Ž

β€Ž    elif pressed[4]:   #check to see if button 4 is pressed
β€Ž        if not held[4]: #check to make sure the held variable isn't true   β€Ž
β€Ž            print("Left Arrow") #print action to serial monitor
β€Ž            kbd.send(Keycode.LEFT_ARROW)#send the keyboard command Left Arrrow to the device
β€Ž            held[4] = 1β€Ž

Next up was to try getting a constant rotation while the button was held. Looking deeper into β€Žthe code (shown above), I noticed after the β€œif or elif pressed” statements, there was an "If not β€Žheld” statement. Also, at the end of that statement, there was a variable called β€œheld” that was β€Žbeing changed to a β€œ1”. My first thought was to comment out both the β€œif not held” statement, β€Žas well as the β€œheld” variable assignment for each of the buttons. Here is an example of what β€Žthe code looked like for each button.

Copy Code
if pressed[1]: #check to see if button 1 is pressed
β€Ž#        if not held[1]:#check to make sure the held variable isn't true
β€Ž                print("Up Arrow") #print action to serial monitor
β€Ž                kbd.send(Keycode.UP_ARROW)#send the keyboard command Up arrow to the device
β€Ž#                held[1] = 1β€Ž

I then saved the code and opened OnShape to see if this would work. I switched my screen to β€ŽOnShape, pressed the right directional button, and held it down. My drawing rotated β€Žcontinuously. However, it was too fast to accurately control where I wanted the object to stop. I β€Žthought for a few moments and began to wonder, what would happen if I added a slight delay β€Žto the code. Will that slow it down at all? I entered a sleep command of 0.1 seconds or 100 β€Žmilliseconds. I saved the code and uploaded it again.β€Žβ€Ž 

Copy Code
if pressed[1]: #check to see if button 1 is pressed
β€Ž#        if not held[1]:#check to make sure the held variable isn't trueβ€Ž
β€Ž            if pan != 1:β€Ž
β€Ž                print("Up Arrow") #print action to serial monitor
β€Ž                kbd.send(Keycode.UP_ARROW)#send the keyboard command Up arrow to the device
β€Ž                time.sleep(0.1)β€Ž
β€Ž#                held[1] = 1β€Ž

This time the part was continuously rotating but at a much more manageable speed.

β€ŽThe next step that I was looking to add was a Pan function for the controls. I knew I wanted to β€Žtoggle this on and off without holding down a button. My first thought was to set up a variable. I β€Žcalled it "pan" to be able to change back and forth between a 0 and 1 or true and false. Next, I β€Ždecided to add a color to the button that would change depending on if the pan variable was β€Žtrue or not. I decided that I would set the pixel to a light red color if the pan variable was false β€Žand to orange if the pan variable was true. I then added code to change the variable to the β€Žopposite condition when pushed, essentially setting it up similarly to a switch. Next, I went into β€Žeach of the code sections for the 4 directional buttons and updated the code. So, there were β€Žtwo conditions used, one for when the pan variable is false and one for when it is true. The β€Žcurrent code I had up to this point was put under the β€œif pan does not equal 1” set of code. β€ŽUnder the β€œif pan does equal 1” set I updated the keyboard send command to include 3 β€Žcommands, Keycode.CONTROL, Keycode.SHIFT, and the command for the directional arrow I β€Žwas using such as Keycode.UP_ARROW. This would cause the object that I am looking at in β€ŽOnShape to pan in whichever direction I’m pressing instead of rotating. Here is an example of β€Žthat code.β€Žβ€Ž 

Copy Code
elif pressed[5]:  #check to see if button 5 is pressed
β€Ž        β€Ž
β€Ž        if not held[5]:#check to make sure the held variable isn't true    β€Ž
β€Ž           if pan != 1: # check to see if the pan variable is not equal to 1β€Ž
β€Ž                pixels[5] = light_red #set the pixel to light red
β€Ž                pan = 1 #set the pan variable to 1β€Ž
β€Ž                print("Control Shift ON") #print action to serial monitor
β€Ž                held[5] = 1  #change the held variable for this button to true
β€Ž           β€Ž
β€Ž           elif pan == 1: # if the pan variable is equal to 1β€Ž
β€Ž                pixels[5] = orange #set the pixel to orange
β€Ž                pan = 0 #set the pan variable to 0β€Ž
β€Ž                print("Control Shift OFF") #print action to serial monitor
β€Ž                held[5] = 1  #change the held variable for this button to true

Next, I saved the code and went back to OnShape. I pressed button 5 and noticed the color on β€Žthe key had changed. I then pressed the up button, and the object panned upwards. I then tried β€Žpanning down while holding the button. It did pan downwards continuously however, there was β€Ža slight delay after every step. To correct this, I tried commenting out the delay that I had β€Žadded for the rotation commands. After updating the code, I saved it and went back to OnShape β€Žagain. This time when I activated the pan button and held a directional button the object β€Žpanned perfectly with no delay and not overly fast.β€Ž

Perfect!!

β€ŽThe commands to this point were all working out well so, I thought the next thing I could add β€Žwas zoom functionality.

β€ŽLooking through the keyboard shortcuts available, I noticed that there were 4 options that I β€Žthought would be good to include, Zoom in, Zoom out, Zoom to fit, and Zoom to Window. Next, β€ŽI decided that the next 4 buttons I would use were the ones labeled 0, 2, 8, and A. I decided to β€Žuse button 0 as Zoom In, button 2 as Zoom out, button 8 as Zoom to Fit, and button A as Zoom β€Žto Window. I also decided to set these buttons to a white color to differentiate them from the β€Žother buttons used so far. After choosing these buttons, I proceeded to add the commands. The β€Žcommand for Zoom In is SHIFT Z, for Zoom Out it is Z, for Zoom to Fit it is F, and Zoom to β€ŽWindow is W. Once these were all coded in and the code saved, I opened OnShape once again β€Žto test the functionality. All worked as expected. I hadn’t used the zoom to window command β€Žmuch before however adding it to the keypad I am finding myself using it more often all the β€Žtime. Essentially zoom to window allows the user to draw a box around the section of the object β€Žthey are working on, and it will zoom into that area.β€Ž

Example of the Zoom in code:β€Žβ€Ž

Copy Code
if pressed[0]: #check to see if button 0 is pressed
β€Ž        if not held[0]: #check to make sure the held variable isn't true
β€Ž            print("Zoom in or shift z") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT,Keycode.Z) #send the keyboard command Shift Z to the device
β€Ž            held[0] = 1 #change the held variable for this button to true

The next three commands I thought I would add would be to go directly to certain views. I β€Ždecided to use buttons 3, 7, and 11 and include the commands to snap to the front view, top β€Žview, and isometric view. The commands for these three are SHIFT 1 for front view, SHIFT 5 for β€ŽTop view, and SHIFT 7 for isometric view. I find myself using these buttons quite often to jump β€Žto these three views so I thought these would be good choices for this device. I also chose to β€Žhave this colored blue, so I created a variable and applied that color to those pixels.β€Ž

Example of the snap to front view code:β€Žβ€Ž 

Copy Code
elif pressed[3]: #check to see if button 3 is pressed
β€Ž        if not held[3]:#check to make sure the held variable isn't true
β€Ž            print("Front View or Shift 1") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT, Keycode.ONE)#send the keyboard command Shift, One to the deviceβ€Ž
β€Ž            held[3] = 1  #change the held variable for this button to true

The next two actions I decided to add were New Sketch and Extrude. I use these probably the β€Žmost out of all the commands up to this point. For these two, I chose to use the buttons labeled β€ŽC and D. As has been a theme with this project and CircuitPython, the code was very easy to β€Žadd to the buttons. I chose to color these two buttons to be Violet. After setting up a variable for β€Žthe color, I then proceeded to code in the keycodes for each, SHIFT S for a new sketch and SHIFT β€ŽE for extrude. After saving and updating the code, I went back to OnShape to test. Just like β€Žbefore these were working flawlessly. β€Ž

Code for new sketch and new extrude:β€Ž

Copy Code
elif pressed[12]:    #check to see if button 12 is pressed
β€Ž        if not held[12]:#check to make sure the held variable isn't true
β€Ž            print("New Sketch or Shift s") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT, Keycode.S)#send the keyboard command Shift, S to the device
β€Ž            held[12] = 1  #change the held variable for this button to true
β€Ž            β€Ž
β€Ž    elif pressed[13]:    #check to see if button 13 is pressed
β€Ž        if not held[13]:#check to make sure the held variable isn't true
β€Ž            print("New Extrude or Shift e") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT, Keycode.E)#send the keyboard command Shift, E to the device
β€Ž            held[13] = 1  #change the held variable for this button to true

While testing out the new sketch command, I thought it would be nice to have the function to β€Žtoggle between a regular line and a construction line in the sketch command. I decided to use β€Žthe button labeled E for this. I set the color to orange and entered the keycode Q into the code. β€Ž

Construction/ regular line toggle code:β€Žβ€Ž

Copy Code
elif pressed[14]:    #check to see if button 14 is pressed
β€Ž        if not held[14]:#check to make sure the held variable isn't true
β€Ž            print("Toggle Construction line in Sketch or Q") #print action to serial monitor
β€Ž            kbd.send(Keycode.Q)#send the keyboard command Q to the device
β€Ž            held[14] = 1  #change the held variable for this button to true

For the last button, it took me a while to decide what command to use. After looking through all β€Žof the shortcuts, I decided to add a clear selection button. I added the variable for the color red β€Žand then added the keycode command of SPACEBAR to the code for button F. I then saved the β€Žcode and went back to Onshape for a final test. For the final test, I opened one of my previous β€Ždesigns and started by zooming way in and then way out. Next, I pushed the zoom to fit button β€Žand the object filled the screen. I then pressed the zoom to window button and used my mouse β€Žto zoom in to the top left corner of the object. I then proceeded to rotate the part to the left β€Žand right, followed by up and down. From there, I snapped to the front view, the top view, then β€Žthe isometric view. After changing the views, I pressed the new sketch button and clicked on β€Žone of the faces of the object. I chose the line command and started drawing a line. As I was β€Ždragging the line across the object, I pressed the line type toggle button, and the line changed β€Žover to a construction line. Excellent!! I then pressed the red X to cancel out of the new sketch. β€ŽOnce that was canceled, I pressed the new extrude button and selected the face of the object β€Žagain. Again, this worked flawlessly. Last, I selected two faces of the object, and after they were β€Žhighlighted, I pressed the clear selection button.

β€ŽAfter finishing this project, I was shocked by how easy it was to program and get up and β€Žrunning. Also, I was somewhat taken aback by how easy it was to make changes to the code and β€Žtest those changes. If you have not tried CircuitPython, I highly recommend you check it out. β€ŽThere are tons of resources out there that make using it very easy. β€Ž

Going forward, I’ve had the idea to adapt this so that it can also work for Fusion 360. In the little β€Žbit of looking into using this for fusion 360, I did notice some changes need that need to be β€Žmade to the code to get this to work as it does in OnShape. For example, it looks like I will have β€Žto emulate a mouse to be able to make the part rotate. Fusion 360 uses a middle mouse button β€Žto enable rotation. This will require using the Adafruit HID library for the mouse, however, I β€Žthink it should be easy to implement. Another option would be to try out Adafruit’s Macropad, β€Žwhich would enable a lot more commands by setting up a number of different pages of β€Žcommands by using the encoder to select which page and then programming each page for a β€Ždifferent area of the CAD software I’m using.

β€ŽHopefully, you have found this project writeup very useful and it has inspired you to create a β€Žsimilar project of your own. β€Ž

I took a short video of the device operating in OnShape.

 

Here is the full code for this project:β€Ž

Copy Code
import time
import board
import busio
import usb_hid

from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_dotstar

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from digitalio import DigitalInOut, Direction, Pull
cs = DigitalInOut(board.GP17)β€Ž
cs.direction = Direction.OUTPUT
cs.value = 0β€Ž
num_pixels = 16β€Ž
pixels = adafruit_dotstar.DotStar(board.GP18, board.GP19, num_pixels, brightness=0.1, β€Žauto_write=True)β€Ž
i2c = busio.I2C(board.GP5, board.GP4)β€Ž
device = I2CDevice(i2c, 0x20)β€Ž
kbd = Keyboard(usb_hid.devices)β€Ž
layout = KeyboardLayoutUS(kbd)β€Ž

def read_button_states(x, y):β€Ž
β€Ž    pressed = [0] * 16β€Ž
β€Ž    with device:β€Ž
β€Ž        device.write(bytes([0x0]))β€Ž
β€Ž        result = bytearray(2)β€Ž
β€Ž        device.readinto(result)β€Ž
β€Ž        b = result[0] | result[1] << 8β€Ž
β€Ž        for i in range(x, y):β€Ž
β€Ž            if not (1 << i) & b:β€Ž
β€Ž                pressed[i] = 1β€Ž
β€Ž            else:β€Ž
β€Ž                pressed[i] = 0β€Ž
β€Ž    return pressed
held = [0] * 16β€Ž

β€Ž#The below colors are set up so that we don't have to repeat the RGB code multiple timesβ€Ž
green = (0,255,0)β€Ž
white = (100,100,100)β€Ž
red = (255,0,0)β€Ž
orange = (255,128,0)β€Ž
light_red = (255,25,25)β€Ž
violet =  (89,0,179)β€Ž
blue = (0,0,255)β€Ž

pan = 0 # Pan 1 means that the Control and shift key will be on
pixels[5] = orange #turn pixel 5 on prior to starting the sketch

while True:β€Ž
β€Ž    pressed = read_button_states(0, 16)β€Ž

β€Ž#Color coding the buttons according to thier functionβ€Ž
β€Ž    pixels[0] = white
β€Ž    pixels[1] = green
β€Ž    pixels[2] = white
β€Ž    pixels[3] = blue
β€Ž    pixels[4] = green
β€Ž    pixels[6] = green
β€Ž    pixels[7] = blue
β€Ž    pixels[8] = white
β€Ž    pixels[9] = green
β€Ž    pixels[10] = white
β€Ž    pixels[11] = blue
β€Ž    pixels[12] = violet
β€Ž    pixels[13] = violet
β€Ž    pixels[14] = orange
β€Ž    pixels[15] = red
β€Ž    β€Ž
β€Ž    if pressed[0]: #check to see if button 0 is pressed
β€Ž        if not held[0]: #check to make sure the held variable isn't true
β€Ž            print("Zoom in or shift z") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT,Keycode.Z) #send the keyboard command Shift Z to the device
β€Ž            held[0] = 1 #change the held variable for this button to true
β€Ž        β€Ž
β€Ž    β€Ž
β€Ž    elif pressed[1]: #check to see if button 1 is pressed


β€Ž#        if not held[1]:#check to make sure the held variable isn't trueβ€Ž
β€Ž            if pan != 1:β€Ž
β€Ž                print("Up Arrow") #print action to serial monitor
β€Ž                kbd.send(Keycode.UP_ARROW)#send the keyboard command Up arrow to the device
β€Ž                time.sleep(0.1)β€Ž
β€Ž#                held[1] = 1β€Ž
β€Ž                β€Ž
β€Ž            else:β€Ž
β€Ž                print("Up Arrow with Control and Shift") #print action to serial monitor
β€Ž                kbd.send(Keycode.CONTROL, Keycode.SHIFT, Keycode.UP_ARROW)#send the keyboard β€Žcommand Shift, Up arrow to the device
β€Ž#                time.sleep(0.1)β€Ž
β€Ž#                held[1] = 1β€Ž
β€Ž                β€Ž
β€Ž    elif pressed[2]: #check to see if button 2 is pressed
β€Ž        if not held[2]: #check to make sure the held variable isn't true
β€Ž            print("Zoom in or z") #print action to serial monitor
β€Ž            kbd.send(Keycode.Z)  #send the keyboard command Z to the device
β€Ž            held[2] = 1  #change the held variable for this button to true
β€Ž            β€Ž
β€Ž            β€Ž
β€Ž    elif pressed[3]: #check to see if button 3 is pressed
β€Ž        if not held[3]:#check to make sure the held variable isn't true
β€Ž            print("Front View or Shift 1") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT, Keycode.ONE)#send the keyboard command Shift, One to the device
β€Ž            held[3] = 1  #change the held variable for this button to true
β€Ž     β€Ž
β€Ž    elif pressed[4]:   #check to see if button 4 is pressed

β€Ž    β€Ž
β€Ž#        if not held[4]: #check to make sure the held variable isn't true   β€Ž
β€Ž            if pan != 1:β€Ž
β€Ž                print("Left Arrow") #print action to serial monitor
β€Ž                kbd.send(Keycode.LEFT_ARROW)#send the keyboard command Left Arrrow to the device
β€Ž                time.sleep(0.1)β€Ž
β€Ž#                held[4] = 1β€Ž
β€Ž            else:β€Ž
β€Ž                print("Left Arrow with Control and Shift") #print action to serial monitor
β€Ž                kbd.send(Keycode.CONTROL, Keycode.SHIFT, Keycode.LEFT_ARROW)#send the keyboard β€Žcommand Shift, Left Arrrow to the device
β€Ž                time.sleep(0.1)β€Ž
β€Ž#                held[4] = 1β€Ž
β€Ž                β€Ž
β€Ž    elif pressed[5]:  #check to see if button 5 is pressed

β€Ž    β€Ž
β€Ž        if not held[5]:#check to make sure the held variable isn't true    β€Ž
β€Ž           if pan != 1: # check to see if the pan variable is not equal to 1β€Ž
β€Ž                pixels[5] = light_red #set the pixel to light red
β€Ž                pan = 1 #set the pan variable to 1β€Ž
β€Ž                print("Control Shift ON") #print action to serial monitor
β€Ž                held[5] = 1  #change the held variable for this button to true
β€Ž           β€Ž
β€Ž           elif pan == 1: # if the pan variable is equal to 1β€Ž
β€Ž                pixels[5] = orange #set the pixel to orange
β€Ž                pan = 0 #set the pan variable to 0β€Ž
β€Ž                print("Control Shift OFF") #print action to serial monitor
β€Ž                held[5] = 1  #change the held variable for this button to true

β€Ž                β€Ž
β€Ž            β€Ž
β€Ž    β€Ž
β€Ž    elif pressed[6]:  #check to see if button 6 is pressed

β€Ž        β€Ž
β€Ž#        if not held[6]: #check to make sure the held variable isn't trueβ€Ž
β€Ž            if pan != 1:β€Ž
β€Ž                print("Right Arrow") #print action to serial monitor
β€Ž                kbd.send(Keycode.RIGHT_ARROW)#send the keyboard command Right Arrrow to the device
β€Ž                time.sleep(0.1)β€Ž
β€Ž#                held[6] = 1β€Ž
β€Ž            else:β€Ž
β€Ž                print("Right Arrow with Control and Shift") #print action to serial monitor
β€Ž                kbd.send(Keycode.CONTROL, Keycode.SHIFT, Keycode.RIGHT_ARROW)#send the keyboard β€Žcommand Shift, Right Arrrow to the device
β€Ž                time.sleep(0.1)β€Ž
β€Ž#                held[1] = 1β€Ž
β€Ž                β€Ž
β€Ž    elif pressed[7]:  #check to see if button 7 is pressed
β€Ž        if not held[7]:#check to make sure the held variable isn't true
β€Ž            print("Top View or Shift 5") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT, Keycode.FIVE)#send the keyboard command Shift, Five to the device
β€Ž            held[7] = 1  #change the held variable for this button to true
β€Ž                β€Ž
β€Ž    elif pressed[8]:  #check to see if button 8 is pressed
β€Ž        if not held[8]:#check to make sure the held variable isn't true
β€Ž            print("Zoom to fit or f") #print action to serial monitor
β€Ž            kbd.send(Keycode.F)#send the keyboard command F to the device
β€Ž            held[8] = 1  #change the held variable for this button to true

β€Ž    elif pressed[9]:   #check to see if button 9 is pressedβ€Ž

β€Ž        β€Ž
β€Ž#        if not held[9]:#check to make sure the held variable isn't trueβ€Ž
β€Ž            if pan != 1:β€Ž
β€Ž                print("Down Arrow") #print action to serial monitor
β€Ž                kbd.send(Keycode.DOWN_ARROW)#send the keyboard command Down Arrow to the device
β€Ž                time.sleep(0.1)β€Ž
β€Ž#                held[9] = 1β€Ž
β€Ž            else:β€Ž
β€Ž                print("Down Arrow with Control and Shift") #print action to serial monitor
β€Ž                kbd.send(Keycode.CONTROL, Keycode.SHIFT, Keycode.DOWN_ARROW)#send the keyboard β€Žcommand Shift, Down Arrow to the device
β€Ž                time.sleep(0.1)β€Ž
β€Ž#                held[1] = 1β€Ž
β€Ž                β€Ž
β€Ž                β€Ž
β€Ž    elif pressed[10]:    #check to see if button 10 is pressed
β€Ž        if not held[10]:#check to make sure the held variable isn't true
β€Ž            print("Zoom to window or w") #print action to serial monitor
β€Ž            kbd.send(Keycode.W)#send the keyboard command W to the device
β€Ž            held[10] = 1  #change the held variable for this button to true
β€Ž            β€Ž
β€Ž    elif pressed[11]:    #check to see if button 11 is pressed
β€Ž        if not held[11]:#check to make sure the held variable isn't true
β€Ž            print("Isometric View or Shift 7") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT, Keycode.SEVEN)#send the keyboard command Shift, Seven to the β€Ždevice
β€Ž            held[11] = 1  #change the held variable for this button to true
β€Ž            β€Ž
β€Ž    elif pressed[12]:    #check to see if button 12 is pressed
β€Ž        if not held[12]:#check to make sure the held variable isn't true
β€Ž            print("New Sketch or Shift s") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT, Keycode.S)#send the keyboard command Shift, S to the device
β€Ž            held[12] = 1  #change the held variable for this button to true
β€Ž            β€Ž
β€Ž    elif pressed[13]:    #check to see if button 13 is pressed
β€Ž        if not held[13]:#check to make sure the held variable isn't true
β€Ž            print("New Extrude or Shift e") #print action to serial monitor
β€Ž            kbd.send(Keycode.SHIFT, Keycode.E)#send the keyboard command Shift, E to the device
β€Ž            held[13] = 1  #change the held variable for this button to true
β€Ž            β€Ž
β€Ž    elif pressed[14]:    #check to see if button 14 is pressed
β€Ž        if not held[14]:#check to make sure the held variable isn't true
β€Ž            print("Toggle Construction line in Sketch or Q") #print action to serial monitor
β€Ž            kbd.send(Keycode.Q)#send the keyboard command Q to the device
β€Ž            held[14] = 1  #change the held variable for this button to trueβ€Ž
β€Ž            β€Ž
β€Ž    elif pressed[15]:    #check to see if button 15 is pressed
β€Ž        if not held[15]:#check to make sure the held variable isn't true
β€Ž            print("Clear Selection or SPACEBAR") #print action to serial monitor
β€Ž            kbd.send(Keycode.SPACEBAR)#send the keyboard command Spacebar to the device
β€Ž            held[15] = 1  #change the held variable for this button to true


β€Ž    else:  # Released state
β€Ž        for i in range(16):β€Ž
β€Ž            held[i] = 0  # Set held states to off
β€Ž        time.sleep(0.1) # Debounce

 

Codice produttore PIM551
PICO RGB KEYPAD BASE
Pimoroni Ltd
21,75 €
Visualizza altro Details
Codice produttore SC0915
RASPBERRY PI PICO RP2040
Raspberry Pi
Codice produttore 5128
MACROPAD RP2040 STARTER KIT
Adafruit Industries LLC
42,77 €
Visualizza altro Details
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.