init commit

This commit is contained in:
2025-08-21 22:58:56 -04:00
parent a6de5f641f
commit 5245ae176a
7 changed files with 172 additions and 0 deletions

73
src/main.c Normal file
View File

@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "printer.h"
int main(void)
{
int err;
bool detached = false;
libusb_device_handle *dev_handler;
/* Init BEGIN */
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 */
}

49
src/printer.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include "printer.h"
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);
if (err != 0)
perror("libusb_bulk_transfer");
}
void printer_initialize(libusb_device_handle* dev_handler, bool beep)
{
unsigned char data[2] = {0x1B, 0x40}; // 0x1B 0x40 is printer initialize
send_msg(dev_handler, data, 2);
if (!beep)
return;
data[1] = 0x07; // 0x1B 0x07 is beep
for (int i = 0; i < 2; i++)
send_msg(dev_handler, data, 2);
}
void printer_feed(libusb_device_handle* dev_handler, uint8_t lines)
{
unsigned char data[2] = {0x14, lines};
send_msg(dev_handler, data, 2);
}
void printer_cut(libusb_device_handle* dev_handler, bool partial)
{
unsigned char data[1] = {0x19};
if (partial)
data[0] = 0x1A;
send_msg(dev_handler, data, 1);
}
void printer_print(libusb_device_handle* dev_handler, char* text, size_t len)
{
send_msg(dev_handler, (unsigned char*) text, len);
unsigned char data[1] = {0x17};
send_msg(dev_handler, data, 1);
}

26
src/printer.h Normal file
View File

@ -0,0 +1,26 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <libusb-1.0/libusb.h>
#ifndef PRINTER_H
#define PRINTER_H
// 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);
void printer_initialize(libusb_device_handle* dev_handler, bool beep);
void printer_feed(libusb_device_handle* dev_handler, uint8_t lines);
void printer_cut(libusb_device_handle* dev_handler, bool partial);
void printer_print(libusb_device_handle* dev_handler, char* text, size_t len);
#endif