Skip to content

Commit

Permalink
Merge pull request #5 from Hurumy/main
Browse files Browse the repository at this point in the history
add some funcs for setting the name of printers from your code.
  • Loading branch information
TheNitek committed Aug 24, 2023
2 parents 6103aa8 + 79340ca commit 798c4af
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
5 changes: 5 additions & 0 deletions examples/print_demo/print_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ void setup() {
// Hand buffer to printer
cat.begin(buffer, sizeof(buffer));

// Set the name of the printer to be searched
cat.printNameArray();
cat.resetNameArray();
cat.addNameArray((char *)"MX09");

// Every non-zero color is white
cat.fillScreen(1);
cat.setTextWrap(true);
Expand Down
74 changes: 65 additions & 9 deletions src/CatGFX.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
#include "CatGFX.h"

CatPrinter::CatPrinter(uint16_t h):
Adafruit_GFX(384, h),
WIDTH_BYTE((WIDTH + 7)/8),
SERVICE_UUID("0000AE30-0000-1000-8000-00805F9B34FB"),
CHAR_UUID_DATA("0000AE01-0000-1000-8000-00805F9B34FB")
{
if (this->NAME_ARRAY_SIZE >= 5)
{
strcpy(this->printerNames[0], "GT01");
strcpy(this->printerNames[1], "GB01");
strcpy(this->printerNames[2], "GB02");
strcpy(this->printerNames[3], "MX09");
for (int i = 4; i < this->NAME_ARRAY_SIZE; i ++)
strcpy(this->printerNames[i], "");
}
else if (this->NAME_ARRAY_SIZE >= 1)
strcpy(this->printerNames[0], "");
}

void CatPrinter::begin(byte *buffer, uint16_t size) {
pixelBuffer = buffer;
pixelBufferSize = size;
Expand Down Expand Up @@ -108,15 +127,19 @@ void CatPrinter::printBuffer(void) {
}

void CatPrinter::onResult(BLEAdvertisedDevice advertisedDevice) {
if (strcmp(advertisedDevice.getName().c_str(), "GT01") == 0 ||
strcmp(advertisedDevice.getName().c_str(), "GB01") == 0 ||
strcmp(advertisedDevice.getName().c_str(), "GB02") == 0 ||
strcmp(advertisedDevice.getName().c_str(), "MX09") == 0 ) {
blePrinterAddress = new BLEAddress(advertisedDevice.getAddress());
Serial.print("Found Printer: ");
Serial.println(blePrinterAddress->toString().c_str());
bleScan->stop();
}
uint8_t i = 0;

while (i < this->NAME_ARRAY_SIZE && strcmp(this->printerNames[i], "") != 0){
if (strcmp(advertisedDevice.getName().c_str(), this->printerNames[i]) == 0) {
blePrinterAddress = new BLEAddress(advertisedDevice.getAddress());
Serial.print("Found Printer: ");
Serial.println(blePrinterAddress->toString().c_str());
bleScan->stop();
break ;
}
else
i ++;
}
}

void CatPrinter::sendCmd(const CatPrinter::Cmd cmd, const byte *data, const uint8_t len) {
Expand Down Expand Up @@ -185,3 +208,36 @@ void CatPrinter::drawPixel(int16_t x, int16_t y, uint16_t color) {
void CatPrinter::fillScreen(uint16_t color) {
fillBuffer((!color ? 0xFF : 0x00));
}

void CatPrinter::resetNameArray(void)
{
for (uint8_t i = 0; i < this->NAME_ARRAY_SIZE; i ++)
strcpy(this->printerNames[i], "");
}

bool CatPrinter::addNameArray(char *newname)
{
uint8_t i = 0;

if (strlen(newname) >= NAME_STRING_SIZE)
return (false);
while (i < this->NAME_ARRAY_SIZE)
{
if (strcmp(this->printerNames[i], "") != 0)
i ++;
else
break ;
}
if (i >= this->NAME_ARRAY_SIZE - 1)
return (false);
strcpy(this->printerNames[i], newname);
return (true);
}

void CatPrinter::printNameArray(void)
{
for (uint8_t i = 0; i < NAME_ARRAY_SIZE; i ++)
Serial.println(this->printerNames[i]);
}


18 changes: 11 additions & 7 deletions src/CatGFX.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ class CatPrinter: public Adafruit_GFX, public BLEAdvertisedDeviceCallbacks {
DRAWING_MODE = 0xBE,
PRINT_COMPRESSED = 0xBF
};
CatPrinter(uint16_t h):
Adafruit_GFX(384, h),
WIDTH_BYTE((WIDTH + 7)/8),
SERVICE_UUID("0000AE30-0000-1000-8000-00805F9B34FB"),
CHAR_UUID_DATA("0000AE01-0000-1000-8000-00805F9B34FB")
{};
CatPrinter(uint16_t h);
~CatPrinter() {
delete bleClient;
}
Expand All @@ -38,6 +33,10 @@ class CatPrinter: public Adafruit_GFX, public BLEAdvertisedDeviceCallbacks {
void fillScreen(uint16_t color) override;
void startGraphics(void);
void endGraphics(void);
void resetNameArray(void);
bool addNameArray(char *newname);
void printNameArray(void);

private:
byte *pixelBuffer = nullptr;
uint16_t pixelBufferSize = 0;
Expand All @@ -52,6 +51,11 @@ class CatPrinter: public Adafruit_GFX, public BLEAdvertisedDeviceCallbacks {
BLEScan *bleScan = BLEDevice::getScan();
BLEClient *bleClient = BLEDevice::createClient();;

//Indicates the end of the array with an empty string instead of NULL.
const static uint8_t NAME_ARRAY_SIZE = 6;
const static uint8_t NAME_STRING_SIZE = 8;
char printerNames[NAME_ARRAY_SIZE][NAME_STRING_SIZE];

const byte LATTICE_START[11] = {0xAA, 0x55, 0x17, 0x38, 0x44, 0x5F, 0x5F, 0x5F, 0x44, 0x38, 0x2C};
const byte LATTICE_END[11] = {0xAA, 0x55, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17};
const byte FEED_LINE_DATA[2] = {0x01, 0x00};
Expand Down Expand Up @@ -109,4 +113,4 @@ class CatPrinter: public Adafruit_GFX, public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) override;
uint8_t crc(const byte *data, const uint8_t len);
void writeData(byte *data, uint8_t len);
};
};

0 comments on commit 798c4af

Please sign in to comment.