initial second commit
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
75
src/main.c
75
src/main.c
@ -7,67 +7,20 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int err;
|
||||
bool detached = false;
|
||||
libusb_device_handle *dev_handler;
|
||||
|
||||
/* Init BEGIN */
|
||||
SETUP_PRINTER(PRINTER_VENDOR_ID, PRINTER_PRODUCT_ID);
|
||||
|
||||
|
||||
printer_feed(0x10);
|
||||
|
||||
char* str = "This is a test string.";
|
||||
|
||||
printer_print(str, strlen(str));
|
||||
|
||||
printer_feed(0x10);
|
||||
|
||||
printer_cut(false);
|
||||
|
||||
disconnect_from_printer();
|
||||
|
||||
err = libusb_init_context(NULL, NULL, 0);
|
||||
if (err != 0) {
|
||||
perror("LIBUSB Error");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
||||
dev_handler = libusb_open_device_with_vid_pid(NULL,
|
||||
PRINTER_VENDOR_ID, PRINTER_PRODUCT_ID);
|
||||
|
||||
err = libusb_open(libusb_get_device(dev_handler), &dev_handler);
|
||||
if (err != 0) {
|
||||
perror("libusb_open");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
err = libusb_kernel_driver_active(dev_handler, 0);
|
||||
if (err != 0) {
|
||||
libusb_detach_kernel_driver(dev_handler, 0);
|
||||
detached = true;
|
||||
}
|
||||
|
||||
err = libusb_claim_interface(dev_handler, 0);
|
||||
if (err != 0)
|
||||
perror("libusb_claim_interface");
|
||||
|
||||
/* Init END */
|
||||
|
||||
|
||||
printer_initialize(dev_handler, true);
|
||||
|
||||
printer_feed(dev_handler, 0x10);
|
||||
|
||||
char* str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
|
||||
|
||||
printer_print(dev_handler, str, strlen(str));
|
||||
|
||||
printer_feed(dev_handler, 0x10);
|
||||
|
||||
printer_cut(dev_handler, false);
|
||||
|
||||
|
||||
/* Cleanup BEGIN */
|
||||
|
||||
err = libusb_release_interface(dev_handler, 0);
|
||||
if (err)
|
||||
perror("libusb_release_interface");
|
||||
|
||||
if (detached)
|
||||
libusb_attach_kernel_driver(dev_handler, 0);
|
||||
|
||||
libusb_close(dev_handler);
|
||||
|
||||
|
||||
libusb_exit(NULL);
|
||||
|
||||
/* Cleanup END */
|
||||
}
|
||||
|
@ -1,7 +1,70 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <libusb-1.0/libusb.h>
|
||||
|
||||
#include "printer.h"
|
||||
|
||||
struct usb_state {
|
||||
libusb_device_handle* dev;
|
||||
bool kernel_detached;
|
||||
};
|
||||
|
||||
struct usb_state usb_dev;
|
||||
|
||||
int connect_to_printer(uint16_t vid, uint16_t pid)
|
||||
{
|
||||
int err;
|
||||
usb_dev.kernel_detached = false;
|
||||
|
||||
err = libusb_init_context(NULL, NULL, 0);
|
||||
if (err) {
|
||||
perror("libusb_init_context");
|
||||
return err;
|
||||
}
|
||||
|
||||
usb_dev.dev = libusb_open_device_with_vid_pid(NULL, vid, pid);
|
||||
|
||||
err = libusb_open(libusb_get_device(usb_dev.dev), &(usb_dev.dev));
|
||||
if (err) {
|
||||
perror("libusb_open");
|
||||
return err;
|
||||
}
|
||||
|
||||
err = libusb_kernel_driver_active(usb_dev.dev, 0);
|
||||
if (err) {
|
||||
libusb_detach_kernel_driver(usb_dev.dev, 0);
|
||||
usb_dev.kernel_detached = true;
|
||||
}
|
||||
|
||||
err = libusb_claim_interface(usb_dev.dev, 0);
|
||||
if (err) {
|
||||
perror("libusb_claim_interface");
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int disconnect_from_printer(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = libusb_release_interface(usb_dev.dev, 0);
|
||||
if (err) {
|
||||
perror("libusb_release_interface");
|
||||
return err;
|
||||
}
|
||||
|
||||
if (usb_dev.kernel_detached)
|
||||
libusb_attach_kernel_driver(usb_dev.dev, 0);
|
||||
|
||||
libusb_close(usb_dev.dev);
|
||||
|
||||
libusb_exit(NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void send_msg(libusb_device_handle* dev_handler, unsigned char* data, size_t len)
|
||||
{
|
||||
int err = libusb_bulk_transfer(dev_handler, PRINTER_ENDPOINT, data, len, NULL, 0);
|
||||
@ -9,11 +72,11 @@ void send_msg(libusb_device_handle* dev_handler, unsigned char* data, size_t len
|
||||
perror("libusb_bulk_transfer");
|
||||
}
|
||||
|
||||
void printer_initialize(libusb_device_handle* dev_handler, bool beep)
|
||||
void printer_initialize(bool beep)
|
||||
{
|
||||
unsigned char data[2] = {0x1B, 0x40}; // 0x1B 0x40 is printer initialize
|
||||
|
||||
send_msg(dev_handler, data, 2);
|
||||
send_msg(usb_dev.dev, data, 2);
|
||||
|
||||
|
||||
if (!beep)
|
||||
@ -21,29 +84,36 @@ void printer_initialize(libusb_device_handle* dev_handler, bool beep)
|
||||
|
||||
data[1] = 0x07; // 0x1B 0x07 is beep
|
||||
for (int i = 0; i < 2; i++)
|
||||
send_msg(dev_handler, data, 2);
|
||||
send_msg(usb_dev.dev, data, 2);
|
||||
}
|
||||
|
||||
void printer_feed(libusb_device_handle* dev_handler, uint8_t lines)
|
||||
void printer_clear(void)
|
||||
{
|
||||
unsigned char data[1] = {0x10};
|
||||
|
||||
send_msg(usb_dev.dev, data, 1);
|
||||
}
|
||||
|
||||
void printer_feed(uint8_t lines)
|
||||
{
|
||||
unsigned char data[2] = {0x14, lines};
|
||||
|
||||
send_msg(dev_handler, data, 2);
|
||||
send_msg(usb_dev.dev, data, 2);
|
||||
}
|
||||
|
||||
void printer_cut(libusb_device_handle* dev_handler, bool partial)
|
||||
void printer_cut(bool partial)
|
||||
{
|
||||
unsigned char data[1] = {0x19};
|
||||
if (partial)
|
||||
data[0] = 0x1A;
|
||||
|
||||
send_msg(dev_handler, data, 1);
|
||||
send_msg(usb_dev.dev, data, 1);
|
||||
}
|
||||
|
||||
void printer_print(libusb_device_handle* dev_handler, char* text, size_t len)
|
||||
void printer_print(char* text, size_t len)
|
||||
{
|
||||
send_msg(dev_handler, (unsigned char*) text, len);
|
||||
send_msg(usb_dev.dev, (unsigned char*) text, len);
|
||||
|
||||
unsigned char data[1] = {0x17};
|
||||
send_msg(dev_handler, data, 1);
|
||||
send_msg(usb_dev.dev, data, 1);
|
||||
}
|
||||
|
@ -10,17 +10,19 @@
|
||||
// would love for this to be cli arguments
|
||||
#define PRINTER_VENDOR_ID 0x0404
|
||||
#define PRINTER_PRODUCT_ID 0x0312
|
||||
|
||||
#define PRINTER_ENDPOINT 0x02
|
||||
|
||||
void send_msg(libusb_device_handle* dev_handler, unsigned char* data, size_t len);
|
||||
int connect_to_printer(uint16_t vid, uint16_t pid);
|
||||
int disconnect_from_printer(void);
|
||||
|
||||
void printer_initialize(libusb_device_handle* dev_handler, bool beep);
|
||||
void printer_initialize(bool beep);
|
||||
void printer_clear(void);
|
||||
|
||||
void printer_feed(libusb_device_handle* dev_handler, uint8_t lines);
|
||||
void printer_feed(uint8_t lines);
|
||||
void printer_cut(bool partial);
|
||||
void printer_print(char* text, size_t len);
|
||||
|
||||
void printer_cut(libusb_device_handle* dev_handler, bool partial);
|
||||
|
||||
void printer_print(libusb_device_handle* dev_handler, char* text, size_t len);
|
||||
#define SETUP_PRINTER(vid, pid) connect_to_printer(vid, pid); printer_initialize(true); printer_clear()
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user