2525// Two sets of code - one for ESP32
2626#ifdef HAL_ESP32_HAL_H_
2727#include < BLEDevice.h>
28- #else // and another for Arduino's BLE API
28+ #endif
29+
30+ #ifdef ARDUINO_ARDUINO_NANO33BLE
2931#include < ArduinoBLE.h>
3032#endif
33+
34+ #ifdef ARDUINO_NRF52_ADAFRUIT
35+ #include < bluefruit.h>
36+ #endif
37+
3138#include " Thermal_Printer.h"
3239
40+ static char szPrinterName[32 ];
41+ static int bb_width, bb_height; // back buffer width and height in pixels
42+ static int tp_wrap, bb_pitch;
43+ static int iCursorX = 0 ;
44+ static int iCursorY = 0 ;
45+ static uint8_t *pBackBuffer = NULL ;
46+ static uint8_t bConnected = 0 ;
47+ static void tpWriteData (uint8_t *pData, int iLen);
48+ extern " C" {
49+ extern unsigned char ucFont[], ucBigFont[];
50+ };
51+
52+ #ifdef ARDUINO_NRF52_ADAFRUIT
53+ // Bluetooth support for Adafruit nrf52 boards
54+ const uint8_t myServiceUUID[16 ] = {0x55 , 0xe4 , 0x05 , 0xd2 , 0xaf , 0x9f , 0xa9 , 0x8f , 0xe5 , 0x4a , 0x7d , 0xfe , 0x43 , 0x53 , 0x53 , 0x49 };
55+ const uint8_t myDataUUID[16 ] = {0xb3 , 0x9b , 0x72 , 0x34 , 0xbe , 0xec , 0xd4 , 0xa8 , 0xf4 , 0x43 , 0x41 , 0x88 , 0x43 , 0x53 , 0x53 , 0x49 };
56+ // #define myServiceUUID 0xFEA0
57+ // #define myDataUUID 0xFEA1
58+ static ble_gap_evt_adv_report_t the_report;
59+ static uint16_t the_conn_handle;
60+ static int bNRFFound;
61+ // BLEClientCharacteristic myDataChar(myDataUUID);
62+ // BLEClientService myService(myServiceUUID);
63+ BLEClientService myService (0x18f0 );
64+ BLEClientCharacteristic myDataChar (0x2af1 );
65+ /* *
66+ * Callback invoked when an connection is established
67+ * @param conn_handle
68+ */
69+ static void connect_callback (uint16_t conn_handle)
70+ {
71+ // Serial.println("Connected!");
72+ // Serial.print("Discovering FEA0 Service ... ");
73+ the_conn_handle = conn_handle;
74+ // If FEA0 is not found, disconnect and return
75+ if ( !myService.discover (conn_handle) )
76+ {
77+ #ifdef DEBUG_OUTPUT
78+ Serial.println (" Didn't find our service, disconnecting..." );
79+ #endif
80+ // disconect since we couldn't find our service
81+ Bluefruit.disconnect (conn_handle);
82+ return ;
83+ }
84+
85+ // Once FEA0 service is found, we continue to discover its characteristics
86+ if ( !myDataChar.discover () )
87+ {
88+ // Data char is mandatory, if it is not found (valid), then disconnect
89+ #ifdef DEBUG_OUTPUT
90+ Serial.println (" Data characteristic is mandatory but not found" );
91+ #endif
92+ Bluefruit.disconnect (conn_handle);
93+ return ;
94+ }
95+ bConnected = 1 ; // success!
96+ } /* connect_callback() */
97+ /* *
98+ * Callback invoked when a connection is dropped
99+ * @param conn_handle
100+ * @param reason
101+ */
102+ static void disconnect_callback (uint16_t conn_handle, uint8_t reason)
103+ {
104+ (void ) conn_handle;
105+ (void ) reason;
106+ bConnected = 0 ;
107+ // Serial.println("Disconnected");
108+ } /* disconnect_callback() */
109+
110+ static void scan_callback (ble_gap_evt_adv_report_t * report)
111+ {
112+ // Serial.printf("found something %s\n", report->data.p_data);
113+ // if (Bluefruit.Scanner.checkReportForUuid(report, myServiceUUID))
114+ // char *name = (char *)report->data.p_data;
115+ // int i;
116+ // for (i=0; i<report->data.len; i++)
117+ // if (name[i] == szPrinterName[0]) break; // "parse" for the name in the adv data
118+ // if (name && memcmp(&name[i], szPrinterName, strlen(szPrinterName)) == 0)
119+ {
120+ #ifdef DEBUG_OUTPUT
121+ Serial.println (" Found Printer!" );
122+ #endif
123+ bNRFFound = 1 ;
124+ Bluefruit.Scanner .stop ();
125+ // Serial.print("RemoteDisplay UUID detected. Connecting ... ");
126+ memcpy (&the_report, report, sizeof (ble_gap_evt_adv_report_t ));
127+ // Bluefruit.Central.connect(report);
128+ }
129+ // else // keep looking
130+ // {
131+ // For Softdevice v6: after received a report, scanner will be paused
132+ // We need to call Scanner resume() to continue scanning
133+ // Bluefruit.Scanner.resume();
134+ // }
135+ } /* scan_callback() */
136+
137+ static void notify_callback (BLEClientCharacteristic* chr, uint8_t * data, uint16_t len)
138+ {
139+ } /* notify_callback() */
140+
141+ #endif // Adafruit nrf52
142+
33143#ifdef HAL_ESP32_HAL_H_
34144static BLEUUID SERVICE_UUID (" 49535343-FE7D-4AE5-8FA9-9FAFD205E455" );
35145static BLEUUID CHAR_UUID_DATA (" 49535343-8841-43F4-A8D4-ECBE34729BB3" );
@@ -40,22 +150,13 @@ static BLERemoteCharacteristic* pRemoteCharacteristicData;
40150static BLEScan *pBLEScan;
41151static BLEClient* pClient;
42152static char Scanned_BLE_Name[32 ];
43- #else
153+ #endif
154+
155+ #ifdef ARDUINO_ARDUINO_NANO33BLE
44156static BLEDevice peripheral;
45157static BLEService prtService;
46158static BLECharacteristic pRemoteCharacteristicData;
47159#endif
48- static char szPrinterName[32 ];
49- static int bb_width, bb_height; // back buffer width and height in pixels
50- static int tp_wrap, bb_pitch;
51- static int iCursorX = 0 ;
52- static int iCursorY = 0 ;
53- static uint8_t *pBackBuffer = NULL ;
54- static uint8_t bConnected = 0 ;
55- static void tpWriteData (uint8_t *pData, int iLen);
56- extern " C" {
57- extern unsigned char ucFont[], ucBigFont[];
58- };
59160
60161#ifdef HAL_ESP32_HAL_H_
61162// Called for each device found during a BLE scan by the client
@@ -331,7 +432,8 @@ int tpConnect(void)
331432// Serial.println("data Service not found");
332433 }
333434 return 0 ;
334- #else // Arduino BLE
435+ #endif
436+ #ifdef ARDUINO_ARDUINO_NANO33BLE // Arduino BLE
335437 if (!peripheral)
336438 {
337439#ifdef DEBUG_OUTPUT
@@ -392,7 +494,16 @@ int tpConnect(void)
392494 Serial.println (" connection failed" );
393495#endif
394496 return 0 ;
395- #endif
497+ #endif // NANO33
498+ #ifdef ARDUINO_NRF52_ADAFRUIT
499+ Bluefruit.Central .connect (&the_report);
500+ long ulTime = millis ();
501+ while (!bConnected && (millis () - ulTime) < 4000 ) // allow 4 seconds for the connection to occur
502+ {
503+ delay (20 );
504+ }
505+ return bConnected;
506+ #endif // ADAFRUIT
396507} /* tpConnect() */
397508
398509void tpDisconnect (void )
@@ -403,7 +514,8 @@ void tpDisconnect(void)
403514 pClient->disconnect ();
404515 bConnected = 0 ;
405516 }
406- #else
517+ #endif
518+ #ifdef ARDUINO_ARDUINO_NANO33BLE
407519 if (peripheral && bConnected)
408520 {
409521 if (peripheral.connected ())
@@ -413,6 +525,13 @@ void tpDisconnect(void)
413525 }
414526 }
415527#endif
528+ #ifdef ARDUINO_NRF52_ADAFRUIT
529+ if (bConnected)
530+ {
531+ bConnected = 0 ;
532+ Bluefruit.disconnect (the_conn_handle);
533+ }
534+ #endif
416535} /* tpDisconnect() */
417536
418537//
@@ -456,7 +575,8 @@ int iLen = strlen(szName);
456575 delay (10 ); // if you don't add this, the ESP32 will reset due to watchdog timeout
457576 }
458577 }
459- #else // Arduino API
578+ #endif
579+ #ifdef ARDUINO_ARDUNIO_NANO33BLE // Arduino API
460580 // initialize the BLE hardware
461581 BLE.begin ();
462582 // start scanning for the printer service UUID
@@ -492,22 +612,70 @@ int iLen = strlen(szName);
492612 }
493613 } // while scanning
494614#endif
615+ #ifdef ARDUINO_NRF52_ADAFRUIT
616+ bConnected = 0 ;
617+ // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
618+ // SRAM usage required by SoftDevice will increase dramatically with number of connections
619+ Bluefruit.begin (0 , 1 );
620+ /* Set the device name */
621+ Bluefruit.setName (" Bluefruit52" );
622+ /* Set the LED interval for blinky pattern on BLUE LED */
623+ Bluefruit.setConnLedInterval (250 );
624+ // Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
625+ // Bluefruit.configCentralBandwidth(BANDWIDTH_MAX);
626+ /* Start Central Scanning
627+ * - Enable auto scan if disconnected
628+ * - Filter out packet with a min rssi
629+ * - Interval = 100 ms, window = 50 ms
630+ * - Use active scan (used to retrieve the optional scan response adv packet)
631+ * - Start(0) = will scan forever since no timeout is given
632+ */
633+ bNRFFound = 0 ;
634+ Bluefruit.Scanner .setRxCallback (scan_callback);
635+ Bluefruit.Scanner .restartOnDisconnect (true );
636+ // Bluefruit.Scanner.filterRssi(-72);
637+ Bluefruit.Scanner .filterUuid (myService.uuid );
638+ Bluefruit.Scanner .setInterval (160 , 80 ); // in units of 0.625 ms
639+ Bluefruit.Scanner .useActiveScan (true ); // Request scan response data
640+ Bluefruit.Scanner .start (0 ); // 0 = Don't stop
641+ // allow the timeout for the scan
642+ ulTime = millis ();
643+ while (!bNRFFound && (millis () - ulTime) < (unsigned )iSeconds*1000UL )
644+ {
645+ delay (10 );
646+ }
647+ Bluefruit.Scanner .stop ();
648+ // Serial.println("Stopping the scan");
649+ myService.begin (); // start my client service
650+ // Initialize client characteristics of VirtualDisplay.
651+ // Note: Client Chars will be added to the last service that is begin()ed.
652+ myDataChar.setNotifyCallback (notify_callback);
653+ myDataChar.begin ();
654+ // Callbacks for Central
655+ Bluefruit.Central .setConnectCallback (connect_callback);
656+ Bluefruit.Central .setDisconnectCallback (disconnect_callback);
657+ bFound = bNRFFound;
658+ #endif // ADAFRUIT
495659 return bFound;
496660} /* tpScan() */
497661//
498662// Write data to the printer over BLE
499663//
500664static void tpWriteData (uint8_t *pData, int iLen)
501665{
502- if (!bConnected || !pRemoteCharacteristicData)
666+ if (!bConnected) // || !pRemoteCharacteristicData)
503667 return ;
504668 // Write BLE data without response, otherwise the printer
505669 // stutters and takes much longer to print
506670#ifdef HAL_ESP32_HAL_H_
507671 pRemoteCharacteristicData->writeValue (pData, iLen, false );
508- #else
672+ #endif
673+ #ifdef ARDUINO_ARDUINO_NANO33BLE
509674 pRemoteCharacteristicData.writeValue (pData, iLen);
510675#endif
676+ #ifdef ARDUINO_NRF52_ADAFRUIT
677+ myDataChar.write ((const void *)pData, (uint16_t )iLen);
678+ #endif
511679} /* tpWriteData() */
512680//
513681// Select one of 2 available text fonts along with attributes
0 commit comments