Skip to content

Instantly share code, notes, and snippets.

@erica
Created May 3, 2018 16:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/1d895f106d28ee8f963c4c3b78e894a0 to your computer and use it in GitHub Desktop.
Save erica/1d895f106d28ee8f963c4c3b78e894a0 to your computer and use it in GitHub Desktop.
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