140 lines
4.3 KiB
Swift
140 lines
4.3 KiB
Swift
//
|
|
// ViewController.swift
|
|
// PillTracker
|
|
//
|
|
// Created by Max Hunt on 16/01/2021.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class ViewController: UIViewController {
|
|
var simpleBluetoothIO: SimpleBluetoothIO!
|
|
|
|
@IBOutlet var virtualButton:UISwitch!
|
|
@IBOutlet weak var ledToggleButton: UIButton!
|
|
@IBOutlet weak var statusLabel: UILabel!
|
|
@IBOutlet weak var cStatus: UILabel!
|
|
@IBOutlet weak var pillsConsumed: UILabel!
|
|
@IBOutlet weak var pillsRemaining: UILabel!
|
|
@IBOutlet weak var pillsTotal: UILabel!
|
|
@IBOutlet weak var pillsAttached: UILabel!
|
|
@IBOutlet weak var refreshToggle: UISwitch!
|
|
|
|
let totalPills: Int = 6
|
|
var usedPills: Int = 0
|
|
var remainingPills: Int = 0
|
|
let updateFreq: Float = 2.0
|
|
weak var timer: Timer?
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
overrideUserInterfaceStyle = .light
|
|
simpleBluetoothIO = SimpleBluetoothIO(serviceUUID: "4fafc201-1fb5-459e-8fcc-c5c9c331914b", delegate: self)
|
|
// startTimer()
|
|
|
|
}
|
|
|
|
deinit {
|
|
stopTimer()
|
|
}
|
|
|
|
func sendUpdateCommand(){
|
|
print("Sending update command...")
|
|
simpleBluetoothIO.writeValue(value: 51)
|
|
}
|
|
|
|
func startTimer() {
|
|
timer?.invalidate() // just in case you had existing `Timer`, `invalidate` it before we lose our reference to it
|
|
timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(updateFreq), repeats: true) { [weak self] _ in
|
|
self?.sendUpdateCommand()
|
|
}
|
|
}
|
|
|
|
func stopTimer() {
|
|
timer?.invalidate()
|
|
}
|
|
|
|
@IBAction func ledToggleButtonDown(_ sender: UIButton) {
|
|
simpleBluetoothIO.writeValue(value:49)
|
|
}
|
|
|
|
@IBAction func ledToggleButtonUp(_ sender: UIButton) {
|
|
simpleBluetoothIO.writeValue(value: 50)
|
|
}
|
|
|
|
@IBAction func forcePillRefresh(_ sender: UIButton) {
|
|
self.sendUpdateCommand()
|
|
}
|
|
@IBAction func RefreshChanged(_ sender: Any) {
|
|
if self.refreshToggle.isOn {
|
|
startTimer()
|
|
} else {
|
|
stopTimer()
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension ViewController: SimpleBluetoothIODelegate {
|
|
func simpleBluetoothIO(simpleBluetoothIO: SimpleBluetoothIO, didReceiveValue value: Int8) {
|
|
self.statusLabel.text = String(value)
|
|
print("GOT: \(value)")
|
|
if value == 98 {
|
|
//view.backgroundColor = UIColor.yellow
|
|
virtualButton.setOn(true, animated: true)
|
|
} else if value == 99 {
|
|
//view.backgroundColor = UIColor.black
|
|
virtualButton.setOn(false, animated: true)
|
|
} else if value == 55 {
|
|
self.pillsConsumed.text = "X"
|
|
self.pillsRemaining.text = "X"
|
|
self.pillsTotal.text = "X"
|
|
self.pillsAttached.text = "Detached"
|
|
self.pillsAttached.textColor = UIColor.red
|
|
} else {
|
|
self.remainingPills = Int(value)
|
|
self.usedPills = self.totalPills - self.remainingPills
|
|
self.pillsConsumed.text = String(usedPills)
|
|
self.pillsRemaining.text = String(remainingPills)
|
|
self.pillsTotal.text = String(self.totalPills)
|
|
self.pillsAttached.text = "Attached"
|
|
self.pillsAttached.textColor = UIColor.green
|
|
}
|
|
}
|
|
|
|
func simpleBluetoothIO(simpleBluetoothIO: SimpleBluetoothIO, didConnect value: Bool) {
|
|
print("VC: connect: \(value)")
|
|
self.cStatus.text = "Connected!"
|
|
self.cStatus.textColor = UIColor.green
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
|
|
print("Sending update commend")
|
|
self.sendUpdateCommand()
|
|
}
|
|
}
|
|
|
|
func simpleBluetoothIO(simpleBluetoothIO: SimpleBluetoothIO, didDisconnect value: Bool) {
|
|
print("VC: disconnect: \(value)")
|
|
self.cStatus.text = "Disconnected..."
|
|
self.cStatus.textColor = UIColor.red
|
|
self.pillsAttached.text = "Detached"
|
|
self.pillsAttached.textColor = UIColor.red
|
|
}
|
|
|
|
|
|
}
|
|
|
|
extension String {
|
|
|
|
func index(at position: Int, from start: Index? = nil) -> Index? {
|
|
let startingIndex = start ?? startIndex
|
|
return index(startingIndex, offsetBy: position, limitedBy: endIndex)
|
|
}
|
|
|
|
func character(at position: Int) -> Character? {
|
|
guard position >= 0, let indexPosition = index(at: position) else {
|
|
return nil
|
|
}
|
|
return self[indexPosition]
|
|
}
|
|
}
|