<commit_before>//
//  ViewController.swift
//  BLECentralManager_iOS_Swift
//
//  Created by <NAME> on 6/30/16.
//  Copyright © 2016 kil-dev. All rights reserved.
//

import UIKit
import CoreBluetooth
import BLECentralManager


class ViewController: UIViewController {

    private static let _kMaxKbps: Float = 1024.0 * 100.0

    private var _manager: BLECManager!
    private var _timer: NSTimer?
    private var _dataSize: Int = 0
    private var _device: BLECDevice?

    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var rssiLabel: UILabel!
    @IBOutlet var logView: UITextView!


    required init?(coder: NSCoder) {
        super.init(coder: coder)

        let dataChar = DataCharacteristic()
        dataChar.delegate = self;

        var infoChars = [
            InfoCharacteristic(name: "Manufacturer"),
            InfoCharacteristic(name: "Firmware"),
            InfoCharacteristic(name: "HwRev"),
            InfoCharacteristic(name: "SwRev")
        ]
        for i in 0..<infoChars.endIndex { infoChars[i].delegate = self }

        let config = BLECConfig(type: .OnePheriperal, services: [
            BLECServiceConfig(
                type: [.Advertised, .Required],
                UUID: "965F6F06-2198-4F4F-A333-4C5E0F238EB7",
                characteristics: [
                    BLECCharacteristicConfig(
                        type: .Required,
                        UUID: "89E63F02-9932-4DF1-91C7-A574C880EFBF",
                        delegate: dataChar)
                ]),
            BLECServiceConfig(type: .Optional,
                UUID: "180a",
                characteristics: [
                    // Manufacturer Name String characteristic.
                    BLECCharacteristicConfig(
                        type: .Required,
                        UUID: "2a29",
                        delegate: infoChars[0]),

                    // board
                    BLECCharacteristicConfig(
                        type: .Optional,
                        UUID: "2a26",
                        delegate: infoChars[1]),

                    // HwRev
                    BLECCharacteristicConfig(
                        type: .Optional,
                        UUID: "2a27",
                        delegate: infoChars[2]),

                    // SwRev
                    BLECCharacteristicConfig(
                        type: .Optional,
                        UUID: "2a28",
                        delegate: infoChars[3])
                ])
            ])

        let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
        _manager = BLECManager(config: config, queue: queue)
        _manager.delegate = self;
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc private func _update() {
        //---- compute progress ----
        self.progressView.setProgress(Float(_dataSize) / (ViewController._kMaxKbps / 8.0), animated: true)
        _dataSize = 0

        //---- read RSSI ----
        _device?.readRSSI()
    }
}


//............................................................................
// MARK: - Device extension.
//............................................................................

extension ViewController: BLECDeviceDelegate {

    private func _stateName(state: BLECentralState) -> String {
        switch (state) {
        case .Init:
            return "BLECStateInit"<commit_msg>Workaround to fix logView after rotation.<commit_after>//
//  ViewController.swift
//  BLECentralManager_iOS_Swift
//
//  Created by <NAME> on 6/30/16.
//  Copyright © 2016 kil-dev. All rights reserved.
//

import UIKit
import CoreBluetooth
import BLECentralManager


class ViewController: UIViewController {

    private static let _kMaxKbps: Float = 1024.0 * 100.0

    private var _manager: BLECManager!
    private var _timer: NSTimer?
    private var _dataSize: Int = 0
    private var _device: BLECDevice?

    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var rssiLabel: UILabel!
    @IBOutlet var logView: UITextView!


    required init?(coder: NSCoder) {
        super.init(coder: coder)

        let dataChar = DataCharacteristic()
        dataChar.delegate = self;

        var infoChars = [
            InfoCharacteristic(name: "Manufacturer"),
            InfoCharacteristic(name: "Firmware"),
            InfoCharacteristic(name: "HwRev"),
            InfoCharacteristic(name: "SwRev")
        ]
        for i in 0..<infoChars.endIndex { infoChars[i].delegate = self }

        let config = BLECConfig(type: .OnePheriperal, services: [
            BLECServiceConfig(
                type: [.Advertised, .Required],
                UUID: "965F6F06-2198-4F4F-A333-4C5E0F238EB7",
                characteristics: [
                    BLECCharacteristicConfig(
                        type: .Required,
                        UUID: "89E63F02-9932-4DF1-91C7-A574C880EFBF",
                        delegate: dataChar)
                ]),
            BLECServiceConfig(type: .Optional,
                UUID: "180a",
                characteristics: [
                    // Manufacturer Name String characteristic.
                    BLECCharacteristicConfig(
                        type: .Required,
                        UUID: "2a29",
                        delegate: infoChars[0]),

                    // board
                    BLECCharacteristicConfig(
                        type: .Optional,
                        UUID: "2a26",
                        delegate: infoChars[1]),

                    // HwRev
                    BLECCharacteristicConfig(
                        type: .Optional,
                        UUID: "2a27",
                        delegate: infoChars[2]),

                    // SwRev
                    BLECCharacteristicConfig(
                        type: .Optional,
                        UUID: "2a28",
                        delegate: infoChars[3])
                ])
            ])

        let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
        _manager = BLECManager(config: config, queue: queue)
        _manager.delegate = self;
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //---- logView workaround at resize/rotatation ----
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        logView.scrollEnabled = false
        logView.scrollEnabled = true
    }

    @objc private func _update() {
        //---- compute progress ----
        self.progressView.setProgress(Float(_dataSize) / (ViewController._kMaxKbps / 8.0), animated: true)
        _dataSize = 0

        //---- read RSSI ----
        _device?.readRSSI()
    }
}


//............................................................................
// MARK: - Device extension.
//............................................................................

extension ViewController: BLECDeviceDelegate {

    private func _stateName(state: BLECentralState) -> String {
        switch (state) {
        case .Init:
            return "BLECStateInit"
{'fraction_non_alphanumeric': 0.1287261061355434, 'fraction_numerical': 0.02299157866595375, 'mean_word_length': 1.4119922630560928, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5709511', 'n_tokens_mistral': 2158, 'n_tokens_neox': 1907, 'n_words': 433}