Go to the content
Daniel Ashpes

Hardware projects

ESPMacroKeypad

SpaceDeck: a Bluetooth macro keypad with 8 MX keys, a rotary knob and a 2.13" e-paper screen on an ESP32. Firmware, wiring and printable case.

Stage
DoneThis project is complete.

Repository

Stars
1
Languages
  • C++ 51%
  • HTML 25%
  • Python 23%
Read on

README

From GitHub

SpaceDeck is a Bluetooth macro keypad for a desk. It has eight MX keys, one EC11 rotary encoder (the knob) and a 2.13 inch e-paper screen. An ESP32 sends the keys to the computer as a BLE keyboard. The screen shows the layer and the label of each key.

The firmware is PlatformIO with the Arduino framework. The default keymap uses macOS shortcuts.

SpaceDeck render with the 45 degree screen pod

TODO (photo): Add a photo of the assembled pad here.

Words such as BLE, HID, layer and host slot are defined in the glossary.

Features

  • Eight keys and a knob on each layer. Two layers are in the default keymap: MEDIA and DEV.
  • A tap action and a hold action on each key. A hold is 600 ms or longer.
  • Media keys (play, next, volume, mute) and keyboard shortcuts with Cmd, Ctrl, Option and Shift.
  • Text snippets. One key types a saved text, and a hold on that key opens a list of snippets.
  • Three host slots. Each slot is a separate Bluetooth device, so three computers can each pair with the pad.
  • An e-paper screen that shows the layer, the key labels, the link state and the active host.
  • A refresh policy that limits wear on the screen. The boot screen shows the lifetime refresh count.
  • A hardware test firmware that finds each key and encoder pin and prints the result for config.h.
  • A two-part printed case with an 8° typing angle and a screen bay. The bay takes a 45° screen pod, a flat screen cartridge or a blank cover.

Repository layout

Path Contents
platformio.ini Two build environments: firmware (the default) and hwtest.
include/config.h All the pins, the timings and the Bluetooth settings. Edit this file first.
src/actions.cpp The keymap, the hold actions and the snippets.
src/main.cpp The firmware loop: keys, knob, holds and host switch.
src/input.cpp The key debounce and the interrupt encoder decoder.
src/hid.cpp The BLE keyboard and the host slots.
src/ui.cpp, src/display.cpp The screen state and the e-paper drawing task.
src/hwtest/hwtest.cpp The hardware test.
design/ The wiring diagram, its generator and the screen mockups.
case/stl/ The printable parts, in three groups: case, screen, covers.
case/source/ The Python scripts that make each STL.
case/previews/ Renders of the parts.

Bill of materials

Qty Part Notes
1 DOIT ESP32 DevKit V1, 30-pin, USB-C A classic ESP32. It has no native USB, so the pad uses BLE.
1 Carrier board for the DevKit, 70 x 50 mm The base has rails for a 70 x 50 mm board, 12 mm tall with the DevKit. The DevKit USB-C connector is at the left 50 mm edge. TODO: add the carrier board part.
1 Waveshare 2.13 inch e-Paper HAT (V4), 250 x 122, black and white The pod and the HAT cartridge fit its 65 x 30.2 mm board. GxEPD2 driver GxEPD2_213_GDEY0213B74 (SSD1680).
8 MX-style key switches, plate mount The top part has 14.0 mm switch cutouts at 19.05 mm pitch. TODO: add the switch model.
8 MX 1u keycaps, 13 mm tall or less The travel covers have room for keycaps up to about 13 mm tall.
1 EC11 rotary encoder on a breakout board with pull-ups on A, B and SW It mounts through a 7.3 mm hole. The printed knob fits a 6 mm D-shaft. TODO: add the breakout model.
4 M3 heat-set inserts, about 5.7 mm long The holes are 4.0 mm in diameter and 6.0 mm deep. Derived from the model.
2 M3 x 12 mm screws Front posts.
1 M3 x 16 mm screw Left middle post.
1 M3 x 20 mm screw Back right post.
4 Rubber feet, 10 mm diameter or less The base has four pockets, 10.5 mm in diameter and 0.8 mm deep. Derived from the model.
1 9-pin male header strip For the e-paper pins on the ESP32 (D18 to 3V3). Do not solder a pin into D2.
- Hookup wire For the keys and the encoder. TODO: add the wire gauge.
1 USB-C data cable Power and firmware upload.
- Printed parts See Case.

The screw lengths come from SCREW_LEN in case/source/build4.py. The screw head type is derived from the model. The counterbores are 6.2 mm in diameter. The front counterbores are only 2.0 mm deep. An M3 button head screw (ISO 7380, head 5.7 mm by 1.65 mm) fits. A socket head screw (head height 3 mm) does not fit fully in the front counterbores.

Wiring

Wiring diagram

design/wiring_diagram.py makes this diagram from include/config.h. If you change a pin, run python3 design/wiring_diagram.py again. design/spacedeck_epaper_pins.svg shows the e-paper header alone.

Connect each switch from its pin to GND. The firmware turns on the internal pull-up on each key pin, so a pressed key reads LOW.

Part Signal ESP32 pin
Key 1 switch to GND GPIO13 (D13)
Key 2 switch to GND GPIO14 (D14)
Key 3 switch to GND GPIO26 (D26)
Key 4 switch to GND GPIO27 (D27)
Key 5 switch to GND GPIO19 (D19)
Key 6 switch to GND GPIO21 (D21)
Key 7 switch to GND GPIO22 (D22)
Key 8 switch to GND GPIO23 (D23)
Encoder A GPIO32 (D32)
Encoder B GPIO33 (D33)
Encoder SW (push) GPIO25 (D25)
Encoder + 3V3
Encoder GND GND
E-paper CLK GPIO18 (D18)
E-paper DIN GPIO17 (TX2)
E-paper CS GPIO5 (D5)
E-paper DC GPIO16 (RX2)
E-paper RST GPIO15 (D15)
E-paper BUSY GPIO4 (D4)
E-paper VCC 3V3
E-paper GND GND

Key 1 is the top-left key. Key 8 is the bottom-right key. The keys are in two rows of four.

Rules for the pins:

  • Connect the encoder + to 3V3. Do not connect it to VIN. The breakout pull-ups then keep A, B and SW at 3.3 V.
  • Do not use GPIO6 to GPIO11. The flash chip uses them.
  • Do not use GPIO1 and GPIO3. The USB serial port uses them.
  • GPIO34 to GPIO39 have no internal pull-ups. A key on one of these pins needs an external pull-up resistor.
  • Do not connect BUSY to GPIO12. If GPIO12 is HIGH at power-up, the ESP32 does not start.
  • The e-paper does not use the default SPI pins, because GPIO23 (MOSI) is key 8 and GPIO19 (MISO) is key 5. The firmware sends SPI through other pins.
  • The firmware gives SPI the unused pin GPIO35 as MISO (EPD_PIN_MISO_UNUSED). Keep that pin unused. See Troubleshooting.

Case

The case has two printed parts, the base and the top. Four M3 screws go up through the base into heat-set inserts in the top. The footprint is 103 x 100 mm, and the top has an 8° typing angle. The ESP32 lies in the base. The USB-C slot is in the left wall.

The top has a screen bay behind the keys. Put one of the three screen parts or the blank cover into the bay. Each of these parts has a flange and a skirt that drop into the bay opening. The opening is 75.4 x 34.4 mm, with a 79.4 x 38.4 mm flange recess 2 mm deep.

Screen pod

Printable parts

File Qty Use
case/stl/1_print_case/base.stl 1 The lower part. It holds the ESP32 carrier board and the screw heads.
case/stl/1_print_case/top.stl 1 The upper part. It holds the switches, the encoder and the screen bay.
case/stl/1_print_case/knob.stl 1 The knob, 22 mm in diameter, for a 6 mm D-shaft.
case/stl/1_print_case/faceted_cover_no_screen.stl 0 or 1 A blank cover for the screen bay, for a pad without a screen.
case/stl/2_print_screen/screen_pod_45deg_waveshare_hat.stl 0 or 1 A pod that holds the Waveshare HAT at 45° to the desk. This is the screen part in the renders.
case/stl/2_print_screen/flat_screen_cartridge_waveshare_hat.stl 0 or 1 A flat cartridge for the Waveshare HAT. The window is 54 x 26 mm.
case/stl/2_print_screen/flat_screen_cartridge_generic_2.13in.stl 0 or 1 A flat cartridge for a different 2.13 inch module. It fits a board up to 72 x 31 mm and 7 mm thick. The window is 50 x 25 mm.
case/stl/3_print_covers/full_travel_lid_fits_pod.stl 0 or 1 A travel lid for the full pad. It clears the pod and the knob.
case/stl/3_print_covers/key_cover.stl 0 or 1 A travel cover for the keys only.
case/spacedeck_screen_pod.3mf - The Bambu Studio project for the pod, with the print orientation and the settings.
case/reference/assembly_VIEW_ONLY_do_not_print.stl 0 The base, the top, the pod and the knob in one mesh. Use it for reference only. Do not print it.

Print one screen part or the blank cover. The pod is the default.

The renders show the parts on the pad. The right image of pod_vs_encoder_topdown.png shows the clearance between the pod and the encoder nut.

Pod and encoder, top view

Covers

Full travel lid

Key cover

Both covers clip onto the case with snap tabs. They have finger slots to help you remove them.

Print orientation

  • top.stl is face down in the STL: the tilted top surface is flat on the bed.
  • screen_pod_45deg_waveshare_hat.stl stands on its plug in the STL. The 3MF has a different orientation, with tree supports. That is the orientation in which the pod was sliced.
  • key_cover.stl prints roof down without supports.
  • faceted_cover_no_screen.stl rests fully on the bed, with no bridges.

CAUTION: In key_cover.stl, the two snap lips on the front wall are separate bodies, 0.01 mm from the wall. Make sure that the slicer joins them to the wall.

Print settings for the screen pod

These settings come from case/spacedeck_screen_pod.3mf. Open the 3MF to get the same orientation.

Setting Value
Printer Bambu Lab P1S, 0.4 mm nozzle
Material PLA (Generic PLA High Speed profile)
Layer height 0.2 mm
Walls 2
Top and bottom layers 5 top, 3 bottom
Infill 15 %, grid
Supports Yes. Tree supports, on the build plate only.
Brim Auto, 5 mm
Plate Textured PEI
Temperatures Nozzle 220 °C, bed 55 °C
Estimate About 1 h 2 min and 25 g of filament

TODO (print settings): Add the print settings for the base, the top, the knob, the cartridges and the covers.

Assemble the case

  1. Install the four heat-set inserts in the four posts of the top part.
  2. Install the eight switches in the top part.
  3. Install the encoder through the 7.3 mm hole in the top part. Attach it with its nut.
  4. Put the ESP32 carrier board on the rails in the base, with the USB-C connector at the slot.
  5. Do the wiring. See Wiring.
  6. Put the e-paper HAT into the pocket of the pod, glass to the front. The crush ribs hold the board. The wires go out through the slot at the top of the pod.
  7. Put the plug of the pod into the screen bay.
  8. Put the top on the base.
  9. Install the four M3 screws from below. Use M3 x 12 at the front, M3 x 16 at the left middle and M3 x 20 at the back right.
  10. Push the knob onto the encoder shaft.
  11. Put the keycaps on the switches.

The pod pocket uses nominal HAT dimensions. The script notes that nobody measured the HAT with calipers. If the board does not fit, change BRD_A and BRD_S in case/source/pod.py and make the STL again. See case/source/README.md.

TODO (assembly): Confirm the assembly sequence on a real build. Steps 1 to 11 come from the model, not from a build log.

Build and flash

Install the tools

  1. Install PlatformIO. Use the VS Code extension or the pio command line.
  2. Clone this repository.
  3. Connect the ESP32 to the computer with a USB data cable.

Do the hardware test

Do the hardware test before you install the firmware. It shows a wiring fault before you start to look for a keymap fault.

  1. Disconnect the e-paper. The wiring checks then also examine the e-paper pins for short circuits.
  2. Run pio run -e hwtest -t upload -t monitor.
  3. Do not touch the keys or the knob while the wiring checks run.
  4. Read the wiring checks. A FAIL line shows a short circuit or a pin that is in KEY_PINS two times.
  5. Press KEY 1 to KEY 8 in sequence when the guided test tells you to.
  6. Turn the knob clockwise 3 clicks.
  7. Turn the knob counterclockwise 3 clicks.
  8. Push the knob.
  9. Read the summary.
  10. If the summary says config.h is out of date, copy the printed block into include/config.h.
  11. If you changed config.h, upload hwtest again and type g to do the test again.
  12. When the summary says ALL INPUTS FOUND AND config.h MATCHES, the test goes into monitor mode. Each key press prints one line. Each knob click prints ENCODER +1 or ENCODER -1.

Serial commands in the hardware test:

Command Effect
g Starts the guided test again.
d Discover mode. Prints each GPIO that changes. Use it when a key does nothing.
m Monitor mode. Uses the real drivers and the pins in config.h.
s Skips the current guided step.
h Shows the help.

Install the firmware

  1. Connect the e-paper if it is not connected.
  2. Run pio run -e firmware -t upload -t monitor.
  3. On the Mac, open System Settings > Bluetooth.
  4. Click Connect on SpaceDeck HOME.
  5. Make sure that the monitor shows [hid] paired / encrypted OK.
  6. Open TextEdit and push the keys. Compare the result with the default keymap.

The pad bonds with the Mac. After a restart or a sleep, the Mac connects again automatically. This can take a few seconds.

The firmware environment uses the huge_app.csv partition table: a 3 MB application and no OTA update. BLE alone needs about 1.1 MB, so the default partition table is too small.

Use

Default keymap

Layer Input Tap Hold
MEDIA Key 1 Play / pause -
MEDIA Key 2 Previous track -
MEDIA Key 3 Next track -
MEDIA Key 4 Cmd+Space (Spotlight) -
MEDIA Key 5 Cmd+C -
MEDIA Key 6 Cmd+V -
MEDIA Key 7 Cmd+Shift+4 (screenshot) -
MEDIA Key 8 Go to the DEV layer Ambient screen
MEDIA Knob Volume up / down, one step for each click -
MEDIA Knob push Mute HOSTS menu
DEV Key 1 Cmd+S -
DEV Key 2 Cmd+F -
DEV Key 3 Ctrl+` -
DEV Key 4 Cmd+/ -
DEV Key 5 Cmd+Z -
DEV Key 6 Cmd+Shift+Z -
DEV Key 7 Types the current snippet Snippet menu
DEV Key 8 Go to the MEDIA layer Ambient screen
DEV Knob Ctrl+Tab / Ctrl+Shift+Tab (next and previous tab) -
DEV Knob push Nothing HOSTS menu

A key without a hold action sends its action when you push it. A key with a hold action sends the tap action when you release it. If you hold that key for 600 ms, it sends the hold action. A small dot on the screen shows the keys that have a hold action.

The pad drops a key press when no computer is connected. It does not keep the press for later.

Snippets

  1. Go to the DEV layer.
  2. Hold key 7 to open the snippet menu.
  3. Turn the knob to select a snippet.
  4. Push the knob. The pad types the snippet and makes it the current snippet.

Push any key to close the menu. The menu also closes after 10 s without input. The pad keeps the current snippet after a power cycle.

Host slots

  1. Hold the knob for 0.8 s to open the HOSTS menu.
  2. Turn the knob to select HOME, WORK or MINI.
  3. Push the knob. The screen shows SWITCHING, and the pad restarts in about 2 s.
  4. On a new computer, pair with the device that has the slot name, for example SpaceDeck WORK.

Each slot has its own Bluetooth address, so each computer pairs only one time. Slot 1 (HOME) uses the factory address of the ESP32. The top bar of the screen shows the active slot.

Ambient screen

The ambient screen is a slow wave picture. It comes on after 5 minutes without input while a computer is connected. Hold key 8 to show it immediately. The first push on any key or the knob after that only wakes the screen. The wave moves one frame every 5 s for 15 minutes, and then it stops.

Serial commands in the firmware

Open the serial monitor at 115200 baud.

Command Effect
i Shows the Bluetooth address, the bonded hosts, the link state and the screen refresh count.
f Removes all the bonded hosts and disconnects the current host.
a Shows the ambient screen.
h Shows the help.

Change the keymap

The keymap is in src/actions.cpp. Each entry of LAYERS is one layer:

{"Dev", "TABS",                                   // layer name, knob label in the header
 {KEY("Save", MOD_GUI, 's'), ... NEXT_LAYER()},   // keys 1 to 8, tap actions
 KEY("Tab>", MOD_CTRL, '\t'),                     // knob clockwise
 KEY("<Tab", MOD_CTRL | MOD_SHIFT, '\t'),         // knob counterclockwise
 NONE(),                                          // knob push
 {NONE(), ..., SNIPPET_MENU(), AMBIENT()}},       // keys 1 to 8, hold actions

Action types:

Action Effect
KEY(label, mods, key) Sends one key with modifiers.
MEDIA(label, Media::X) Sends a media key: PlayPause, Next, Prev, VolUp, VolDown or Mute.
TEXT(label, "text") Types a fixed text.
SNIPPET(label) Types the current snippet.
SNIPPET_MENU() Opens the snippet menu.
NEXT_LAYER() Goes to the next layer. After the last layer, it goes to the first layer.
AMBIENT() Shows the ambient screen.
NONE() Does nothing.

Modifiers are MOD_CTRL, MOD_SHIFT, MOD_ALT (Option) and MOD_GUI (Cmd). Join two modifiers with |.

The key is one ASCII character: a letter, a digit, a space, a symbol, '\t' (Tab), '\n' (Enter), '\b' (Backspace) or 0x1B (Esc). The firmware uses the US layout. It cannot send arrow keys or function keys.

The screen shows the first 5 characters of a label. The firmware makes the small line under each label from the action, for example CMD+C.

Edit the snippet list in SNIPPETS at the top of src/actions.cpp. Use plain ASCII only. '\n' presses Enter.

To add a layer, add one more entry to LAYERS. NEXT_LAYER() includes it in the sequence.

Other settings are in include/config.h:

Setting Effect
BLE_DEVICE_NAME The first part of the Bluetooth name.
NUM_HOSTS, HOST_NAMES The number and the names of the host slots. A name has 5 characters or fewer.
KEY_HOLD_MS, KNOB_HOLD_MS The hold time for a key and for the knob.
UI_PRESS_FLASH Shows a marker on a key while you push it. Each marker costs two screen refreshes.
UI_AMBIENT_AFTER_MS The time without input before the ambient screen.
EPD_ROTATION 1 or 3. Use the other value if the picture is upside down.
BLE_SEND_DELAY_MS The time between two HID reports.

Upload the firmware again after each change.

Troubleshooting

Symptom Cause Remedy
A key fires when a finger is near it, but does not touch it. The key pin floats. Earlier firmware gave GPIO19 (key 5) to SPI as MISO, and SPI removed the pull-up (fixed in 870b204). Use the current firmware. If you move the pins, keep EPD_PIN_MISO_UNUSED on an unused input-only pin.
The old layer shows through the new layer. A partial refresh does not fully clear a new picture (fixed in ecc5e8a). Use the current firmware. It does a full refresh when the screen, the layer or the menu changes.
Gray marks stay on the screen after a menu or a key marker. Ghosting from partial refreshes. Wait. After 2 minutes without a screen update, the pad does one full refresh. Set UI_PRESS_FLASH = false to stop the key markers.
The pad does not connect again after you upload the firmware. The Mac and the pad do not have the same bond. The monitor can show pairing FAILED. On the Mac, select Forget This Device for SpaceDeck. Then pair again.
The Mac shows the old name after a name change. macOS keeps the old name. Forget the device on the Mac. Then pair again.
Keystrokes are missing or doubled. The HID reports go out too quickly for the Mac. Increase BLE_SEND_DELAY_MS in config.h.
The monitor shows [hid] not connected - dropped. No computer is connected. Pair the pad, or wait for the connection. The pad does not keep these presses.
The boot screen shows n STUCK. n keys read as pressed at power-up. This is a short circuit to GND or a pressed key. Run the hardware test.
The ESP32 does not start with the e-paper connected. BUSY is on GPIO12. Move BUSY to a different pin. The default is GPIO4.
The screen stays blank. An e-paper wire is wrong or loose. Run the hardware test. Read the e-paper header line of the wiring checks.
The picture is upside down in the case. The screen rotation is wrong for your case. Set EPD_ROTATION to the other value (1 or 3).
The knob turns the wrong direction. A and B are swapped. Set ENC_REVERSE = true, or swap the A and B pins.
One click gives 2 steps, or 2 clicks give 1 step. The encoder has a different number of steps for each click. Set ENC_STEPS_PER_DETENT to the value that the hardware test prints (4 or 2).
The hardware test shows many invalid transitions. The encoder contacts are noisy. Add 10 kΩ pull-up resistors and 10 nF capacitors on A and B.
The upload stops at Connecting.... The ESP32 is not in download mode. Hold the BOOT button on the board until the upload starts.
The firmware build fails because the program is too large. The build does not use the large partition table. Build the firmware environment. It sets huge_app.csv.

Screen wear

The panel has a rating of about 1,000,000 refreshes. The firmware saves the lifetime count to flash after each 50 refreshes. The boot screen and the serial command i show the count.

  • Small changes, such as a key marker or a menu scroll, use a partial refresh.
  • A new screen, a new layer or a menu that opens or closes uses a full refresh.
  • After 100 partial refreshes in sequence, the next refresh is full.
  • The link screens move for 1 minute and then stop. The ambient wave moves for 15 minutes and then stops.
  • The firmware turns off the panel high voltage 1 s after each refresh.

design/spacedeck-screens.html shows each screen in a browser. The firmware uses the same 5x7 font (include/font5x7.h).

Glossary

Term Meaning
BLE Bluetooth Low Energy. The radio link between the pad and the computer.
HID Human Interface Device. The USB and Bluetooth standard for keyboards.
Bond A pairing that the pad and the computer keep after a restart.
E-paper A screen that keeps its picture without power and changes slowly.
Full refresh A screen update that flashes the panel and fully clears the old picture. It takes about 2 s.
Partial refresh A fast screen update without a flash. It can leave ghosting.
Ghosting Gray marks from an old picture that stay on an e-paper screen.
Layer One set of actions for the eight keys and the knob.
Host slot One Bluetooth identity of the pad, for one computer.
Detent One click of the knob.
Heat-set insert A threaded brass insert that you push into a printed hole with a hot soldering iron.
Screen bay The opening in the top part for the pod, a cartridge or the blank cover.
Cartridge A flat part that holds a screen module in the screen bay.
Debounce A short wait that ignores the electrical noise of a switch contact.

License

  • The code, the firmware, the design/ files and the documentation: MIT. See LICENSE.
  • The files in case/ (STL models, the 3MF project, the CAD scripts and the previews): CC BY 4.0. See case/LICENSE.

The firmware uses the GxEPD2 library, which has the GPL-3.0 license. PlatformIO downloads it at build time. If you share a built firmware file, the GPL-3.0 terms apply to that file.