-
Notifications
You must be signed in to change notification settings - Fork 69
Add support for --no-resize option and --keep-awake functionality #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a1e62bc
714d6a3
19ca58c
58f4748
963f3ed
ccaa9e5
5451f5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import asyncio | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Maybe we can also mention it in the README.md instead of the nodejs application there. Something like "To prevent the printer from sleeping, call
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right that Alternatively, we could move this functionality to
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about something like adding a Then users can choose if/how to periodically run the keepalive loop. For example, some may be happy with a quick and dirty bash loop like: $ while true; do python3 print.py --send-keep-alive; sleep 30; doneSo I think we could get rid of I would still link your catprinter-manager in the README because it has nice features for advanced users (queue, better keepalive etc), and we get to keep this one very simple. What do you think? |
||
| import contextlib | ||
| from typing import Optional | ||
| import uuid | ||
| from bleak import BleakClient, BleakScanner | ||
| from bleak.backends.device import BLEDevice | ||
| from catprinter import logger | ||
|
|
||
| POSSIBLE_SERVICE_UUIDS = [ | ||
| "0000ae30-0000-1000-8000-00805f9b34fb", | ||
| "0000af30-0000-1000-8000-00805f9b34fb", | ||
| ] | ||
|
|
||
| TX_CHARACTERISTIC_UUID = "0000ae01-0000-1000-8000-00805f9b34fb" | ||
| RX_CHARACTERISTIC_UUID = "0000ae02-0000-1000-8000-00805f9b34fb" | ||
|
|
||
| PRINTER_READY_NOTIFICATION = b"\x51\x78\xae\x01\x01\x00\x00\x00\xff" | ||
|
|
||
| SCAN_TIMEOUT_S = 10 | ||
| WAIT_AFTER_EACH_CHUNK_S = 0.2 | ||
| WAIT_FOR_PRINTER_DONE_TIMEOUT = 60 | ||
|
|
||
| # Scan and connect to the printer | ||
| async def scan(name: Optional[str], timeout: int): | ||
| autodiscover = not name | ||
| if autodiscover: | ||
| logger.info("⏳ Trying to auto-discover a printer...") | ||
| else: | ||
| logger.info(f"⏳ Looking for a BLE device named {name}...") | ||
| # Filter function for devices | ||
| def filter_fn(device: BLEDevice, adv_data): | ||
| if autodiscover: | ||
| return any(uuid in adv_data.service_uuids for uuid in POSSIBLE_SERVICE_UUIDS) | ||
| else: | ||
| return device.name == name | ||
|
|
||
| device = await BleakScanner.find_device_by_filter(filter_fn, timeout=timeout) | ||
| if device is None: | ||
| raise RuntimeError("Unable to find printer, make sure it is turned on and in range") | ||
| logger.info(f"✅ Found printer: {device}") | ||
| return device | ||
|
|
||
| # Simulate a dummy print activity (ping) | ||
| async def run_dummy_print(device: Optional[str]): | ||
| try: | ||
| address = await scan(device, timeout=SCAN_TIMEOUT_S) | ||
| except RuntimeError as e: | ||
| logger.error(f"🛑 {e}") | ||
| return | ||
| logger.info(f"⏳ Connecting to {address}...") | ||
| async with BleakClient(address) as client: | ||
| logger.info(f"✅ Connected: {client.is_connected}; MTU: {client.mtu_size}") | ||
| event = asyncio.Event() | ||
|
|
||
| # Sending a dummy "ping" to the printer to keep it awake | ||
| logger.info("⏳ Sending dummy signal to the printer...") | ||
| await client.write_gatt_char(TX_CHARACTERISTIC_UUID, PRINTER_READY_NOTIFICATION) | ||
| await asyncio.sleep(WAIT_AFTER_EACH_CHUNK_S) | ||
|
|
||
| logger.info("✅ Printer activity simulated successfully. Exiting.") | ||
|
|
||
| # Run the dummy print to simulate the interaction with the printer | ||
| if __name__ == "__main__": | ||
| asyncio.run(run_dummy_print(None)) | ||
Uh oh!
There was an error while loading. Please reload this page.