Skip to content

Instantly share code, notes, and snippets.

@erica erica/buzz.swift
Created May 3, 2018

Embed
What would you like to do?
import UIKit
class ViewController: UIViewController {
var btHelper: BTHelper = BTHelper()
var timer: Timer? = nil
var bgTask = BackgroundTask()
@objc func stop() { btHelper.shutOff() }
@objc func action() { btHelper.startUp() }
// User selected a new time interval in minutes
// The 1 minute time out is for testing.
// App defaults to 5 minutes refocus time out.
@IBAction func poke(_ sender: Any) {
guard let sc = sender as? UISegmentedControl else { return }
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 60.0 * [1.0, 3.0, 5.0, 10.0, 15.0, 30.0][sc.selectedSegmentIndex], target: self, selector: #selector(action), userInfo: nil, repeats: true)
action()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Live forever
bgTask.startBackgroundTask()
// Listen for device. Buzz on connect.
// Observer lives forever. I don't bother to
// save it for deallocation.
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "2A06"), object: nil, queue: .main) { note in
print("Triggered:")
self.btHelper.vibrate(degree: .low)
self.perform(#selector(self.stop), with: nil, afterDelay: 5.0)
}
// Repeat every n seconds
action()
timer = Timer.scheduledTimer(timeInterval: 60.0 * 10, target: self, selector: #selector(action), userInfo: nil, repeats: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.