Skip to content

Commit

Permalink
Added withResponse option to account for new ESP32 behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurence Bank authored and Laurence Bank committed Aug 22, 2021
1 parent e0d51b2 commit 262d203
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
63 changes: 42 additions & 21 deletions src/Thermal_Printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
// Written by Larry Bank ([email protected])
// 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 <http://www.gnu.org/licenses/>.
//
#include <Arduino.h>
// uncomment this line to see debug info on the serial monitor
Expand All @@ -27,7 +27,7 @@
#include <BLEDevice.h>
#endif

#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_ARDUINO_NANO33BLE)
#ifdef ARDUINO_ARDUINO_NANO33BLE
#include <ArduinoBLE.h>
#endif

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 23 additions & 11 deletions src/Thermal_Printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://www.gnu.org/licenses/>.
//

#ifndef __THERMAL_PRINTER_H__
Expand Down Expand Up @@ -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
//
Expand Down Expand Up @@ -100,4 +111,5 @@ int tpScan(const char *szName, int iSeconds);
//
int tpConnect(void);
void tpDisconnect(void);
int tpIsConnected(void);
#endif // __THERMAL_PRINTER_H__

0 comments on commit 262d203

Please sign in to comment.