<commit_before>/**
 * @file       BlynkSimpleGenericBLE.h
 * @author     <NAME>
 * @license    This project is released under the MIT License (MIT)
 * @copyright  Copyright (c) 2015 <NAME>
 * @date       May 2016
 * @brief
 *
 */

#ifndef BlynkSimpleGenericBLE_h
#define BlynkSimpleGenericBLE_h

#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "BLE"
#endif

#define BLYNK_SEND_ATOMIC
#define BLYNK_SEND_CHUNK 20
//#define BLYNK_SEND_THROTTLE 20
#define BLYNK_MSG_LIMIT 0

#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo2.h>
#include <CurieBLE.h>

BLEPeripheral     blePeripheral;
BLEService        bleService  ("713D0000-503E-4C75-BA94-3148F18D941E");
BLECharacteristic rxChar      ("713D0003-503E-4C75-BA94-3148F18D941E", BLEWrite | BLEWriteWithoutResponse, BLE_MAX_ATTR_DATA_LEN);
BLECharacteristic txChar      ("713D0002-503E-4C75-BA94-3148F18D941E", BLERead  | BLENotify, BLE_MAX_ATTR_DATA_LEN);

class BlynkTransportGenericBLE
{
public:
    BlynkTransportGenericBLE()
        : mConn (false)
    {}

    bool connect() {
        mBuffRX.clear();
        mConn = true;
        return true;
    }

    void disconnect() {
    	mConn = false;
    }

    bool connected() {
        return mConn;
    }

    size_t read(void* buf, size_t len) {
        uint32_t start = millis();
        while (millis() - start < 1500) {
            if (available() < len) {
                BLYNK_LOG1("poll");
                blePeripheral.poll();
            } else {
                break;
            }
        }
        noInterrupts();
        size_t res = mBuffRX.get((uint8_t*)buf, len);
        interrupts();
        return res;
    }

    size_t write(const void* buf, size_t len) {
        //BLYNK_DBG_DUMP("<< ", buf, len);
        txChar.setValue((uint8_t*)buf, len);
        return len;
    }

    void process(const void* data, size_t len) {
        noInterrupts();
        //BLYNK_DBG_DUMP(">> ", data, len);
        mBuffRX.put((uint8_t*)data, len);
        interrupts();
    }

    size_t available() {
        noInterrupts();
        size_t rxSize = mBuffRX.size();
        interrupts();
        return rxSize;
    }

private:<commit_msg>Update BLE test<commit_after>/**
 * @file       BlynkSimpleGenericBLE.h
 * @author     <NAME>
 * @license    This project is released under the MIT License (MIT)
 * @copyright  Copyright (c) 2015 <NAME>
 * @date       May 2016
 * @brief
 *
 */

#ifndef BlynkSimpleGenericBLE_h
#define BlynkSimpleGenericBLE_h

#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "BLE"
#endif

#define BLYNK_SEND_ATOMIC
#define BLYNK_SEND_CHUNK 20
//#define BLYNK_SEND_THROTTLE 20
#define BLYNK_MSG_LIMIT 0

#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo2.h>
#include <CurieBLE.h>

BLEPeripheral     blePeripheral;
BLEService        bleService  ("713D0000-503E-4C75-BA94-3148F18D941E");
BLECharacteristic rxChar      ("713D0003-503E-4C75-BA94-3148F18D941E", BLEWriteWithoutResponse, BLE_MAX_ATTR_DATA_LEN);
BLECharacteristic txChar      ("713D0002-503E-4C75-BA94-3148F18D941E", BLENotify, BLE_MAX_ATTR_DATA_LEN);

class BlynkTransportGenericBLE
{
public:
    BlynkTransportGenericBLE()
        : mConn (false)
    {}

    bool connect() {
        mBuffRX.clear();
        mConn = true;
        return true;
    }

    void disconnect() {
    	mConn = false;
    }

    bool connected() {
        return mConn;
    }

    size_t read(void* buf, size_t len) {
        uint32_t start = millis();
        while (millis() - start < 1500) {
            if (available() < len) {
                BLYNK_LOG1("poll");
                blePeripheral.poll();
            } else {
                break;
            }
        }
        uint32_t key = interrupt_lock();
        size_t res = mBuffRX.get((uint8_t*)buf, len);
        interrupt_unlock(key);
        return res;
    }

    size_t write(const void* buf, size_t len) {
        //BLYNK_DBG_DUMP("<< ", buf, len);
        txChar.setValue((uint8_t*)buf, len);
        return len;
    }

    void process(const void* data, size_t len) {
        uint32_t key = interrupt_lock();
        //BLYNK_DBG_DUMP(">> ", data, len);
        mBuffRX.put((uint8_t*)data, len);
        interrupt_unlock(key);
    }

    size_t available() {
        uint32_t key = interrupt_lock();
        size_t rxSize = mBuffRX.size();
        interrupt_unlock(key);
        return rxSize;
    }

private:
{'fraction_non_alphanumeric': 0.11403508771929824, 'fraction_numerical': 0.044534412955465584, 'mean_word_length': 2.4988198269079467, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2189938', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1578, 'n_words': 313}