NaitLee / Cat-Printer

Application supporting Bluetooth thermal β€œCat Printers”, for everyone!
GNU General Public License v3.0
334 stars 33 forks source link

I'm working on an upgrade, expecting better stuffs #1

Closed NaitLee closed 2 years ago

NaitLee commented 2 years ago

It's a long time not updated this repo. Half years ago I got a laptop (now using) but it's not my development platform at that time, while I need bluetooth on it. So after a rush the code is very poor, in the sight from 2022 πŸ˜Άβ€πŸŒ«οΈ

Now that I'm cleaning up the main python script while making a brand new frontend -- the earlier is done, now the later Going to commit them all together, because at the moment there's no actual difference at all. I'm also planing to host this project (also) to somewhere else. Here really have problems accessing GitHub.

I see python-for-android while investigating bleak (the bluetooth lib), found that I can build an working Android version with just what a PC version will have -- Python, basehttp, bleak, and a common web interface. This way we are all free from the original weird app... And, feel free to discuss what you guys really need in an "app" πŸ˜ƒ

Emm, maybe the image monochrome filter needs enhancement as well...

Wish I'll complete this goal πŸ˜‰

NaitLee commented 2 years ago

This day I costed 10 hours investigating python-for-android, and bleak integration with it. It's a long process suffering from the dirty job made by those java developers, but I finally made it... My test python script successfully being compiled to an apk, installed on my phone, printed the pre-defined data. There are many, many hacks made for the work. Going to summarize these later. Well, let's see the final result...

frankenstein91 commented 2 years ago

@NaitLee in an other project we see CUPS as a good option https://github.com/rbaron/catprinter/issues/24

NaitLee commented 2 years ago

Hey, current version of this project has a very basic CUPS/IPP support πŸ˜„ EDIT: no longer legacy-only. See newest pre-release for testing! Though, it's really very basic, and it can only receive printed content and print it, no further "status" at all.

  1. Enable bluetooth and launch the printer, follow the readme to execute server.py.
  2. In CUPS, add URL ipp://127.0.0.1:8095/ as a Generic PostScript Printer. You can (not required) append string after the url, like GB02 or 61:06:ff:7d:8d:fd, for picking the correct printer.
  3. While printing, it will directly invoke Ghostscript (gs in Linux) for converting to PBM image format. Be sure to install it.
  4. Try it with something like LibreOffice! Use an A4 portrait paper, and a bigger font (probably more than 32pt)

Note: currently there is no error reporting mechanism. The way to do (via ipp protocol or cmdline) is not determined either.

CUPS/IPP is kinda complex. If it's going to be fully supported, around 80% of the code will be of that. You can see the IPP specification here. It's not something short after all.

Worthy to mention is that this project is different: it aims at a user interface, not just backend. So the desired user-friendliness is already on the Web interface, and thanks to Web you can actually copy-paste almost anything to it, then print.

NaitLee commented 2 years ago

I'm wondering if I should make all the stuffs (like "full" wysiwyg editing and stickers/borders etc) or just keep it simple. There are all editors around (libreoffice, gimp, inkscape, etc), while most of them are on PC. Some Android users will still feel "uncomfortable", though. I tried fabric.js and html2canvas previously, and I think making full use of them here have some chance of wasting the time. "A software should only do one thing, and do it well." But users won't take it serious. What a dilemma. I know I can't satisfy everyone, and that's a reason for me to make web interface, satisfying low-end poor users. I'll just do the basic now... Going to deal with these later...

frankenstein91 commented 2 years ago

I would love to see templates for barcodes, business cards, shopping lists and route information.

NaitLee commented 2 years ago

You mean a full interface πŸ˜ƒ Well, after some thoughts I think I should switch the direction: just make a full featured Printer frontend, and let backends can integrate with it. And there are already many versions of backends across here, just let them expose some functions or a class, or I adapt them.

I'd tell the full reason later. But you need to understand the situation, of the dirty "intellectual property". In doubt? Read the EULA of iPrint app. (if have ability, read EULA of 精准学习 the Chinese/original version.) I don't need to tell further if you spot the points. I did nothing wrong, but it's still too big risk for me a native person, and local users. Let's help Free Software, while keeping everyone well...

brysonreece commented 2 years ago

If it's possible, the addition of GB03 support would be nice - I haven't found any project compatible with that model yet, which is what I unknowingly ordered. For reference, here's a link to Amazon where the GB03 model was purchased.

Another differentiating factor I've noticed is that the iPrint app has to be launched and printer chosen before the printer will show up in my phone's Bluetooth Devices menu. Despite all efforts, my Mac refuses to acknowledge the printer is broadcasting at all and refuses to list the device, even with an external BLE dongle.

NaitLee commented 2 years ago

From Free Software Is Even More Important Now:

... It also wrongs others if you make a promise not to share. It is evil to keep such a promise, and a lesser evil to break it; to be truly upright, you should not make the promise at all.

Fuck it up (β•―βŽΊΠ”βŽΊ)β•―οΈ΅ ┻━┻ Let's do it.

$ jadx iprint.apk

I've found that in GB03 model the Bluetooth command buffer sent to printer is "compressed" with a small algorithm. Along with this there are some other things that different. EDIT: there's also an additional procedure for checking GB03. and the typos like data -> date suck It is really nothing to implement these shit, but we need a physical printer to check if it works. Amazon did a bad job for this, it's 2x-4x money to buy one. Completely NOT affordable for me. I've consulted local seller where I bought my GB02, they say they have no GB03. Compared to other, these printers are very cheap -- I bought one for around οΏ₯80 (\~$12.5), and now they've discounted to οΏ₯40 (\~$6) They have more than cat printers. In fact they have everything, and some printers are even AI compatible. That's a lot of work, if going to do all of them.

Also, to check what's up in BLE we need some special tools. I've found this. I bought a compatible dongle just now for οΏ₯60 (\~$10) but I suddenly think I have no idea to make use of it.

Sorry for the inconvenience, the biggest obstacle is money, and communication. I want anonymity for continuing further jobs, and of course users' collaboration of sharing/testing the printers... Any brave guy to provide such a solution? Totally unbelievable such a job turned to be on my head... πŸ˜Άβ€πŸŒ«οΈ

... By the way the new frontend is going to be usable. Now it have better monochrome algorithm. Doing the finalization works... I think putting it on a new branch could be fine... πŸ˜‡

frankenstein91 commented 2 years ago

I ordered a printer yesterday on AliExpress... I don't know which version it would be... But I think I will be brave enough for testing πŸ™‚

claudiorrrr commented 2 years ago

Oh crap, maybe that's the reason this is not working to me. I just bought a model and it seems to be GB03. How different is it from the older models?

NaitLee commented 2 years ago

I'm wondering if I could:

  1. Get/Fake a programmable BLE device
  2. Program the device, lead iPrint to regard it as a certain working model (in our case GB03)
  3. Do actions with iPrint (like print), but our device actually dump the traffic
  4. Analyze the traffic, try to make a equivalent in our way

Well, the nRF52840 dongle I've bought have use now... 😜

In addition there's also a "canPrintLabel" stuff that happens in GB03's bitmap procedure. Along with compression and detection, it's very different...

In their code those Chinglish function names really suck. Give them some color to see see.

NaitLee commented 2 years ago

Sorry for the long wait, but it's finally released. You can test the 0.1.0 releases immediately, and play with the Android version!

At the moment there's few function. Going to do them later. The Android building how-to is not there yet. Patience... As for GB03, I'll take a look when I feel appropriate, with the bluetooth dongle hack. My C programming skill is not good enough, though... (doing with the dongle needs to write C/C++ firmware programs)

claudiorrrr commented 2 years ago

Oh still waiting for that GB03 update! :(

NaitLee commented 2 years ago

Well, just now I set up the bluetooth low energy development environment, modified an example code (a typical "blink" program on all IoT stuffs, but have bluetooth interface, I changed the broadcast name to "GB03" and BLE characteristic to the desired) and successfully fooled iprint to list my dongle 😜 But I need real data transferring techniques, serial communication to dump the traffic, and some analytic tricks, will manage to do/learn all of them. Every day is a new progress πŸ˜„

BTW do you really hate "iprint"? I see it just-so-so. Yes it's big. My reason to do all of these is I hate the chinese version. It even wants my phone number. And everyone else's. Of course I fight for true freedom of worldwide πŸ˜‰

claudiorrrr commented 2 years ago

BTW do you really hate "iprint"?

Oh yes I do. The whole point for me of getting this little printer is to be able to manage it on CLI and not install any fishy app on my phone. Thanks @NaitLee for all your work!

NaitLee commented 2 years ago

For CLI what you need is (currently) just printer.py, and the Bleak dependencie. I also concern about these, while I want to follow the Unix philosophy: do one thing and do it well. That means it will not bother geeks to force many dependencies. But I also want normal users to enjoy it.

The script accepts a PBM image file, either with path to file or use stdin. For other formats there's imagemagick, usually already there in GNU/Linux, simply do image manipulation with that (the convert command) then pipe to printer.py I once did that but lost the command parameters. It takes some time to get the correct parameters. Going to build it in later. In the mean while there's actually a procedure for PBM generation in the Web frontend. You can modify the code of either side slightly to dump the result. There is also GIMP for general image manipulation with GUI. It can export PBM as well.

A note: my script supports "dry run". You can test configuration with it without wasting paper!

I come up the idea to support "text printing", you enter the command line, then just type some text (or pipe to stdin!), and printer will print, in a "streaming" way. For the same reason I want to use the PF2 font format, it's easy to make (use grub-mkfont with a common ttf/otf font file), easy to support, Unicode compatible, and for it's bitmap font it can do the "typewriter" hack!

Also the current printer.py is actually just a purged version of legacy version. There are many things to be changed.

Seems now I should put GB03 support on the top. Stay tuned!

claudiorrrr commented 2 years ago

I’m absolutely tuned. My GB03 printer is still waiting to be used. Thanks again!

sickplanet commented 2 years ago

Hello guys, just got GB03 to print with latest release 0.1.0. easy as f### ! Also, thank you NaitLee for you work, it;s awesome! PS: I want to be a tester for GB03 printer.

NaitLee commented 2 years ago

I would like to say something now...

Yesterday I put the effort totally to the dongle hack, but I faced big problems:

Of course I wanted to by another open ble thing that just works. But an idiot brought covid from shanghai to my county and everything messed up, express/delivery stopped and I can't buy any hardware or snacks now.

Too many bad things happened these days. What I can do is just hope everyone well 😒

Today I have other work. If I want I would continue to try on a Windows VM. That's what those suckers love.

NaitLee commented 2 years ago

Okay, I just found Android itself is capable of BLE emulation. There's nRF Connect APP, going to use it on my another phone now rather than wasting time πŸ™ƒ

NaitLee commented 2 years ago

Hi guys, I managed to dump all the data between iprint and GB03 (and "original" GB02 for comparison), and analyze... but found maybe it's just a matter of a 0x12 byte. Please test with the attached script below, by replacing the same file in a working 0.1.0 release with it. I don't know if it will work yet. Please say...

EDIT: if it doesn't work, please also test if BLE Characteristic is a problem. In the code find & replace this:

0000ae01-0000-1000-8000-00805f9b34fb

with one of these. It's better to try all... especially the ae81 one:

0000ae81-0000-1000-8000-00805f9b34fb
0000ab01-0000-1000-8000-00805f9b34fb
0000ff02-0000-1000-8000-00805f9b34fb
49535343-8841-43f4-a8d4-ecbe34729bb3

Be verbosely, though I finally understood how it works, the GB03 "compress" procedure seems being optional, and actually have some chance to make data chunk bigger -- then iprint will rather just send that chunk in the old way, just like e.g. GB02.

But implementing it will be cool and save connection time... "Low Energy", right? πŸ˜„

Mathematics alert! These are found with my eyes on a calculator (KCalc) with bit-mode. The original code is too messy... (Semi-)Detailed statement of that compression: Note: The "compressed" data is in a command 0xBF rather than normal 0xA2.

Okay, there maybe few mistakes... Let's check later...

Examples:


Oh right, let me put the attachment now... Decompress it first... printer.zip

EDIT: In case you need frontend to work, also replace this, in the www folder: main.zip Oh no, and also this, in the main folder... sorry for inconvenience... server.zip

sickplanet commented 2 years ago

Hi guys, I managed to dump all the data between iprint and GB03 (and "original" GB02 for comparison), and analyze... but found maybe it's just a matter of a 0x12 byte. Please test with the attached script below, by replacing the same file in a working 0.1.0 release with it. I don't know if it will work yet. Please say...

EDIT: if it doesn't work, please also test if BLE Characteristic is a problem. In the code find & replace this:

0000ae01-0000-1000-8000-00805f9b34fb

with one of these. It's better to try all... especially the ae81 one:

0000ae81-0000-1000-8000-00805f9b34fb
0000ab01-0000-1000-8000-00805f9b34fb
0000ff02-0000-1000-8000-00805f9b34fb
49535343-8841-43f4-a8d4-ecbe34729bb3

Be verbosely, though I finally understood how it works, the GB03 "compress" procedure seems being optional, and actually have some chance to make data chunk bigger -- then iprint will rather just send that chunk in the old way, just like e.g. GB02.

But implementing it will be cool and save connection time... "Low Energy", right? πŸ˜„

Mathematics alert! These are found with my eyes on a calculator (KCalc) with bit-mode. The original code is too messy... (Semi-)Detailed statement of that compression: Note: The "compressed" data is in a command 0xBF rather than normal 0xA2.

* While parsing bitmap, Pick next chunk of continuous bits, that have the same value (i.e. all are `0` or all are `1`), to a maximum count of 127

* If the chunk size is bigger than 16, do the following:

  * Take a number from 96, 64, 48, 32, 24, 16 (they can all modulus 384 to 0). Adapt the maximum but in the range of original size, except when size is above 96 then just use 96.
  * Divide 384 with the number, get another integer number. Put its value as the command parameter.
  * Allocate a byte, do this:

    * Make the **first bit** `0`, if the chunk is full of `0`. Otherwise `1`.
    * Let the other 7 bits be the size of this chunk. Of course maximum value is 127.
  * Do the same to the other bits... Until the payload is enough big. Put the result in the command payload.

* If the chunk is too small, simply use the old format, that just regard a bit as a pixel.

* Loop 'till end...

Okay, there maybe few mistakes... Let's check later...

Examples:

* HEX `FF FF 00 00`: this is the old "uncompressed" format, that represents 16 black pixels and following 16 white pixels.

  * Binary as `1111 1111  1111 1111  0000 0000  0000 0000`

* HEX `C0 40`: this is the "compressed" format, that represents 64 black pixels and following 64 white pixels.

  * Binary as `1100 0000  0100 0000`. It's (somewhat) 8 times smaller! Taste the feeling πŸ˜™
  * Especially useful with text printing... There are many continuous white pixels there

Oh right, let me put the attachment now... Decompress it first... printer.zip .

I tested and is working... with some random disconnects but it's fine!

frankenstein91 commented 2 years ago

Hi @NaitLee I have got my printer today... but I can not get the server.py working all I see is grafik

I can print via printer.py

any idea?

frankenstein91 commented 2 years ago

Hey, current version of this project has a very basic CUPS/IPP support smile You can see some related code at server.py. I just kept it secret for a long time... Though, it's really very basic, and it can only receive printed content and print it, no further "status" at all. EDIT: The newest commit is not released yet. To test the following please git clone the repo and get the dependencies (can just pick most from a release) It requires ghostscript being installed, and add the ipp://127.0.0.1:8095/GB02 as a PostScript printer. (replace GB02 as your printer bluetooth name) It directly invokes gs in the current shell. Be sure the gs is not alias-ed by some advanced shells to git status... (if it happens, try unalias gs) Now just launch python3 server.py, enable bluetooth and open the printer. Try it with something like LibreOffice! Use an A4 portrait paper, and a bigger font (probably more than 32pt)

CUPS/IPP is kinda complex. If it's going to be fully supported, around 80% of the code will be of that. You can see the IPP specification here. It's not something short after all.

Worthy to mention is that this project is different: it aims at a user interface, not just backend. So the desired user-friendliness is already on the Web interface, and thanks to Web you can actually copy-paste almost anything to it, then print. Going to make it better after all the main jobs, at least mimicking a stable just-work CUPS interface, if ya reilly want stuck_out_tongue_winking_eye

I did not get this working as well

NaitLee commented 2 years ago

Sorry for the inconvenience, the CUPS/IPP feature is in legacy, but not this new version yet. I updated the comment to make it clear.

As for frontend, check if that: a) you have an old browser, that doesn't support newer ecma script features, like firefox esr. and b) you are using the repo alone, without putting i18n script and/or transpiling a compatibility script (see the points in development.md), or c) you didn't enable javascript

anyway, press F12 in browser and see what's inside the console. you may try with a new browser and a published release (like 0.1.0)

frankenstein91 commented 2 years ago

d) VPN somehow

claudiorrrr commented 2 years ago

with one of these. It's better to try all... especially the ae81 one:

It didn't work either. Still waiting for the support! Thanks again

NaitLee commented 2 years ago

It didn't work either. Still waiting for the support! Thanks again

Oh, no 😒 Maybe I should check if iprint sent something during initialization... Just tough.

frankenstein91 commented 2 years ago

@claudioruiz did I get it... you have an none working GB03? I have a GB03 too... works great

claudiorrrr commented 2 years ago

@claudioruiz did I get it... you have an none working GB03? I have a GB03 too... works great

Yup, no luck at all even connecting the printer to the computer. It seems as the printer doesn't even exist for the computer (I did test it with my phone using nRF Connect and it's capable to find it). I can't use it so far :(

frankenstein91 commented 2 years ago

@claudiorrrr could you give me the output of lsusb | grep Bluetooth

claudiorrrr commented 2 years ago

@claudiorrrr could you give me the output of lsusb | grep Bluetooth

lsusb | grep Bluetooth shows no output on MacOS. Same with Ubuntu on a x220. :/

NaitLee commented 2 years ago

i remember bleak said macos is different in some cases. you can look into it in its document site... and the printer actually isn't a 'normal' bluetooth stuff. 'normal' connection doesn't work on it, and pairing does nothing useful. Bluetooth Low Energy should not be confused with 'normal' bluetooth though... you can know more on internet...

frankenstein91 commented 2 years ago

@claudiorrrr could you give me the output of lsusb | grep Bluetooth

lsusb | grep Bluetooth shows no output on MacOS. Same with Ubuntu on a x220. :/

Try to change usb to pci

claudiorrrr commented 2 years ago

@claudiorrrr could you give me the output of lsusb | grep Bluetooth

lsusb | grep Bluetooth shows no output on MacOS. Same with Ubuntu on a x220. :/

Try to change usb to pci

same

NaitLee commented 2 years ago

I'm wondering if it's just problem of Mac. If could, you can try with a Linux VM in Mac. I recommend Virtualbox. It's easy to do basics. And I just mention that you need to attach the bluetooth adapter to VM in virtualbox (in my case it's called "Intel Corp." in USB devices selection), and to do this you likely need "virtualbox extension pack" installed first. In Linux env you can follow the readme to get work. Don't forget check the common myths like didn't enable bluetooth etc. The printer is able to be scanned & shown, but not able to be connected nor paired (with 'common' bluetooth protocol). usb cable does no work. I will publish a pre-release now, including current code, for testing... Still, feel free to ask any problems encountered πŸ˜„

frankenstein91 commented 2 years ago

I had the idea that the Bluetooth adapter isn't BLE capable... But it wasn't there as a USB or PCI device. So we can't really check that.

NaitLee commented 2 years ago

I had the idea that the Bluetooth adapter isn't BLE capable... But it wasn't there as a USB or PCI device. So we can't really check that.

Well, why not just test πŸ™ƒ My bluetooth adapter is built inside my computer, but still shown as USB device. Apple MacOS alike is "painful", said by many (at least) web developers, for it's lockdown nature. But Mac computer is yet expensive. I never wanted to waste money on such a stuff... πŸ˜…

And BLE have some concepts, thing like our printer is called "peripheral", and our computer is called "central" when it controls the printer. My computer bluetooth adapter is not BLE peripheral compatible either (unsure), but it can communicate to printer! I can even see the traffic in Wireshark. My cheap bluetooth earbuds are identified as BLE too. If there is AirPods, why isn't Mac computer BLE compatible 🀫 Android phones can mostly do both, though just one at once. That's how I've dumped iprint traffic with 2 phones 😊

I will also put apk in the release, for also trying on android. Also in the pre-release there's cups/ipp. Try that, please πŸ˜‰