8000 Add a Bluetooth LE transport by lagnat · Pull Request #452 · inkcut/inkcut · GitHub
[go: up one dir, main page]

Skip to content

Add a Bluetooth LE transport - #452

Open
lagnat wants to merge 5 commits into
inkcut:masterfrom
lagnat:ble-transport-asyncio
Open

lagnat wants to merge 5 commits into
inkcut:masterfrom
lagnat:ble-transport-asyncio

Conversation

@lagnat
@lagnat lagnat commented Nov 27, 2026
Copy link
Copy Markdown
Contributor

Replaces #441, which closed when its base branch asyncio was merged and deleted. Same commits, now against master, plus one moving append_etx into the GPGL protocol as requested there.

Adds a Bluetooth LE transport (ble) using bleak,
for cutters that take their commands over a BLE GATT service, such as the
Silhouette Cameo 4. Also adds a Cameo 4 Plus driver.

Behaviour worth knowing

  • Write with response is on by default. The Cameo silently ignores
    write-without-response.
  • append_etx is now a GPGL protocol option (GPGLConfig, with a
    checkbox in the protocol settings) rather than a BLE one. It ends each
    command with ETX; GPGL already puts ETX before each command, so without
    it the latest command isn't run until the next arrives. The Cameo 4 driver
    turns it on under default_config['protocol']['gpgl'].
  • Replies and status come on separate characteristics. The Cameo answers
    queries (FG, ESC ENQ, ESC NAK) on one, and on another reports 1
    (busy) / 0 or 2 (ready, with or without media) unasked whenever it
    moves. Replies (read_uuid) go to protocol.data_received and
    last_read, like a serial port's input. Status (notify_uuid) is only
    logged, so an unrelated "busy" can't be taken as the answer to a query; a
    protocol that wants the state asks with ESC ENQ, as it would over USB. A
    machine with no read_uuid answers on notify_uuid, which is then the
    reply stream.
  • A scan is always needed, even with a saved address: a BLE connection
    can only be made to a device that is advertising, and a device connected
    elsewhere (Silhouette Studio, a phone) stops advertising. The error says so.

Cameo 4 Plus

The 15" Cameo 4 Plus takes the same commands and has the same BLE service
and characteristics (the Cameo 4 values were in fact taken from a 4 Plus),
so only its cutting width differs. Rather than copy the Cameo 4 entry, it is
moved into a Cameo4Driver enamldef that both models declare:

Cameo4Driver:
    pass

Cameo4Driver:
    model = 'Cameo 4 Plus'
    length = '38.1cm'

For discussion: where the UUIDs live

The transport needs a service UUID and a write characteristic UUID (plus
optional read and notify ones) to work at all, so they are really part of
the driver's definition rather than defaults. They currently go in
default_config['connection']['ble'], read through the existing
get_connection_config:

connections = ['printer', 'ble']
default_config = {
  ...
  'connection': {
    'ble': {
      'service_uuid': 'e2088282-4fde-42f9-bb22-6ec3c7ed8f91',
      'write_uuid': '6d92661d-f429-4d67-929b-28e7a9780912',
      'read_uuid': '8dcf199a-30e7-4bd4-beb6-beb57dca866c',
      'notify_uuid': '61490654-b5b4-458c-a867-9e15bc1471e0',
      'name_filter': 'cameo,silhouette',
    }
  }
}

We didn't see a better place. Top-level DeviceDriver attributes would put
BLE-specific fields on every driver. One consequence: a saved device keeps a
copy of its driver from when it was added, so existing Cameo 4 devices don't
get these values and need to be added again to use BLE.

For discussion: transport shutdown

Transports get no notification when Inkcut quits, and by then the event loop
has stopped. If a BLE scan is still running at exit (e.g. quitting during a
connect), CoreBluetooth delivers a callback into a half torn down interpreter
and the process aborts on macOS. Cancelling outstanding tasks after
run_forever() returns and running the loop until they finish, as
asyncio.run() does, fixes it in a quick test, since bleak stops scanning
when its task is cancelled. I've left this out since it's core app code and
you may have a shutdown hook for transports in mind.

bleak is added to install_requires. Happy to make it an optional extra
instead if you'd prefer.

🤖 Generated with Claude Code

lagnat and others added 4 commits October 14, 2026 22:26
Add a transport for cutters that take their commands over a BLE GATT
service, such as the Silhouette Cameo 4. It uses bleak, running on the
application's asyncio event loop. Writes are serialized so commands
stay ordered, and status notifications are fed to protocol.data_received.
A BLE connect takes a while, and each jog asks to connect while one is
still running, so concurrent requests share the connect in progress;
otherwise two clients end up attached to one machine.

The service and characteristics are set by the transport config rather
than hard coded. Drivers provide them under
default_config['connection']['ble']: service_uuid and write_uuid are
required, notify_uuid is optional for machines that report status.
Nothing is detected from the device. They are not shown in the settings
view; the module docstring explains how to find the values for a new
cutter.

Cameo 4 gains 'ble' alongside 'printer' with its values, taken from a
GATT dump of a Cameo 4 Plus. Its service has two characteristics with
identical write/indicate properties; only testing showed which one
reports status.

Two settings are needed to make a Cameo respond, and the Cameo 4 driver
turns both on:

  - writes must be made with a response. The machine ignores
    write-without-response entirely.
  - each write must end with ETX. The GPGL protocol puts ETX before each
    command, so the latest one is not run until the next is sent.

The scan is filtered by name so the list shows cutters instead of every
phone and TV nearby. A BLE connection can only be made to a device that
is advertising, so connecting always finds the device by a scan first,
even with a saved address. A device connected elsewhere stops
advertising, and the error says so.

Tested against a Cameo 4 Plus: connects, homes, and reports busy/ready
status back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Cameo 4 Plus is the 15" model of the Cameo 4. It takes the same
commands and has the same BLE service and characteristics (checked on a
Cameo 4 Plus, which is where the Cameo 4 values came from), so only the
cutting width differs: 38.1cm.

Rather than copy the Cameo 4 entry, it is moved into a Cameo4Driver
enamldef that both models declare, the Plus overriding model and
length. Overrides replace an attribute whole, so a variant that needs a
different default_config has to restate it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Cameo answers queries (FG for its name, ESC ENQ for its state, ESC
NAK for the tools loaded) on a different characteristic from the one
it reports status on, and the transport only subscribed to the status
one, so a protocol could send a query but never see the answer.

Add read_uuid for the reply characteristic. Replies on it go to
protocol.data_received and last_read, like a serial port's input. The
status characteristic is still subscribed but now only logged: the
machine sends "1" (busy) and "0"/"2" (ready, with or without media)
unasked whenever it moves, and passing those on would let a protocol
take an unrelated "busy" as the answer to its query. A protocol that
wants the state asks with ESC ENQ, which works the same over USB. A
machine with no read_uuid answers on notify_uuid, which then stays the
reply stream as before.

Cameo 4 gets read_uuid 8dcf199a-..., found by subscribing to both
characteristics on a Cameo 4 Plus and sending each query. The
docstring's GATT table and status values are corrected to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Terminating each command with ETX is a GPGL matter, not a BLE one, so it
moves from the BLE transport config to a new GPGLConfig, with a checkbox
in the protocol settings. The Cameo 4 driver sets it under
protocol/gpgl, and it now also applies over USB.

The ETX characters GPGL already puts before each command were raw
control characters in the source; they are written as \x03 escapes now
so they can be seen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On macOS a checkbox is laid out at a smaller size than it is drawn,
leaving room for its focus ring, so in a container with no padding the
widget runs past the container's edge and is clipped: 9 px at the right
of both checkboxes here, and 4 px at the bottom of the last. Pad those
containers by as much.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0