diff --git a/src/Thermal_Printer.cpp b/src/Thermal_Printer.cpp index 72c7de0..4390ea7 100644 --- a/src/Thermal_Printer.cpp +++ b/src/Thermal_Printer.cpp @@ -5,18 +5,18 @@ // Written by Larry Bank (bitbank@pobox.com) // Project started 1/6/2020 // -// Copyright 2020 BitBank Software, Inc. All Rights Reserved. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//=========================================================================== - +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . // #include // uncomment this line to see debug info on the serial monitor @@ -27,7 +27,7 @@ #include #endif -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_ARDUINO_NANO33BLE) +#ifdef ARDUINO_ARDUINO_NANO33BLE #include #endif @@ -42,6 +42,7 @@ static int bb_width, bb_height; // back buffer width and height in pixels static int tp_wrap, bb_pitch; static int iCursorX = 0; static int iCursorY = 0; +static uint8_t bWithResponse = 1; // default to wait for response static uint8_t *pBackBuffer = NULL; static uint8_t bConnected = 0; static void tpWriteData(uint8_t *pData, int iLen); @@ -152,12 +153,17 @@ static BLEClient* pClient; static char Scanned_BLE_Name[32]; #endif -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_ARDUINO_NANO33BLE) +#ifdef ARDUINO_ARDUINO_NANO33BLE static BLEDevice peripheral; static BLEService prtService; static BLECharacteristic pRemoteCharacteristicData; #endif +void tpSetWriteMode(uint8_t bWriteMode) +{ + bWithResponse = bWriteMode; +} /* tpSetWriteMode() */ + #ifdef HAL_ESP32_HAL_H_ // Called for each device found during a BLE scan by the client class tpAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks @@ -389,7 +395,22 @@ uint8_t bFlipped = false; } // for y return 0; } /* tpLoadBMP() */ - +// +// Connection status +// true = connected, false = disconnected +// +int tpIsConnected(void) +{ + if (bConnected == 1) { + // we are/were connected, check... +#ifdef HAL_ESP32_HAL_H_ + if (pClient && pClient->isConnected()) + return 1; + bConnected = 0; // change status to disconnected +#endif // ESP32 + } + return 0; // not connected +} /* tpIsConnected() */ // // After a successful scan, connect to the printer // returns 1 if successful, 0 for failure @@ -433,7 +454,7 @@ int tpConnect(void) } return 0; #endif -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_ARDUINO_NANO33BLE) // Arduino BLE +#ifdef ARDUINO_ARDUINO_NANO33BLE // Arduino BLE if (!peripheral) { #ifdef DEBUG_OUTPUT @@ -515,7 +536,7 @@ void tpDisconnect(void) bConnected = 0; } #endif -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_ARDUINO_NANO33BLE) +#ifdef ARDUINO_ARDUINO_NANO33BLE if (peripheral && bConnected) { if (peripheral.connected()) @@ -576,7 +597,7 @@ int iLen = strlen(szName); } } #endif -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_ARDUINO_NANO33BLE) // Arduino API +#ifdef ARDUINO_ARDUNIO_NANO33BLE // Arduino API // initialize the BLE hardware BLE.begin(); // start scanning for the printer service UUID @@ -668,10 +689,10 @@ static void tpWriteData(uint8_t *pData, int iLen) // Write BLE data without response, otherwise the printer // stutters and takes much longer to print #ifdef HAL_ESP32_HAL_H_ - pRemoteCharacteristicData->writeValue(pData, iLen, false); + pRemoteCharacteristicData->writeValue(pData, iLen, bWithResponse); #endif -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_ARDUINO_NANO33BLE) - pRemoteCharacteristicData.writeValue(pData, iLen); +#ifdef ARDUINO_ARDUINO_NANO33BLE + pRemoteCharacteristicData.writeValue(pData, iLen, bWithResponse); #endif #ifdef ARDUINO_NRF52_ADAFRUIT myDataChar.write((const void *)pData, (uint16_t)iLen); diff --git a/src/Thermal_Printer.h b/src/Thermal_Printer.h index 5278ab0..9804b3b 100644 --- a/src/Thermal_Printer.h +++ b/src/Thermal_Printer.h @@ -3,17 +3,18 @@ // written by Larry Bank // Copyright (c) 2020 BitBank Software, Inc. // -// Copyright 2020 BitBank Software, Inc. All Rights Reserved. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//=========================================================================== +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . // #ifndef __THERMAL_PRINTER_H__ @@ -57,6 +58,16 @@ int tpPrint(char *pString); // the paper one line // int tpPrintLine(char *pString); + +#define MODE_WITH_RESPONSE 1 +#define MODE_WITHOUT_RESPONSE 0 +// +// Set the BLE write mode +// MODE_WITH_RESPONSE asks the receiver to ack each packet +// it will be slower, but might be necessary to successfully transmit +// every packet. The default is to wait for a response for each write +// +void tpSetWriteMode(uint8_t bWriteMode); // // Draw text into the graphics buffer // @@ -100,4 +111,5 @@ int tpScan(const char *szName, int iSeconds); // int tpConnect(void); void tpDisconnect(void); +int tpIsConnected(void); #endif // __THERMAL_PRINTER_H__