diff --git a/PillTracker/PillTracker/ViewController.swift b/PillTracker/PillTracker/ViewController.swift index 5cf07bf..f6be7b9 100644 --- a/PillTracker/PillTracker/ViewController.swift +++ b/PillTracker/PillTracker/ViewController.swift @@ -23,6 +23,7 @@ class ViewController: UIViewController { let totalPills: Int = 6 let updateFreq: Float = 2.0 weak var timer: Timer? + var pillIndex = [3, 3, 3, 3, 3, 3] override func viewDidLoad() { super.viewDidLoad() @@ -52,6 +53,20 @@ class ViewController: UIViewController { timer?.invalidate() } + func updatePillArray(data: String) { + for i in 0...5 { + let dataSlice = String(data.character(at: i) ?? "3") + let isIntact = Int(dataSlice) ?? 3 + print("Index: \(i) and value: \(isIntact)") + self.pillIndex[i] = isIntact + } + print("PillIndex: \(self.pillIndex)") + } + + func updatePillArrayUI() { + + } + @IBAction func ledToggleButtonDown(_ sender: UIButton) { simpleBluetoothIO.writeValue(value:49) } @@ -76,7 +91,7 @@ class ViewController: UIViewController { extension ViewController: SimpleBluetoothIODelegate { func simpleBluetoothIO(simpleBluetoothIO: SimpleBluetoothIO, didReceiveValue value: Int8) { self.statusLabel.text = String(value) - print(value) + print("GOT: \(value)") if value == 98 { //view.backgroundColor = UIColor.yellow virtualButton.setOn(true, animated: true) @@ -89,7 +104,7 @@ extension ViewController: SimpleBluetoothIODelegate { self.pillsTotal.text = "X" self.pillsAttached.text = "Detached" self.pillsAttached.textColor = UIColor.red - } else { + } else if String(value).prefix(2) == "7" { let remainingPills = Int(value) let usedPills = self.totalPills - remainingPills self.pillsConsumed.text = String(usedPills) @@ -97,6 +112,8 @@ extension ViewController: SimpleBluetoothIODelegate { self.pillsTotal.text = String(self.totalPills) self.pillsAttached.text = "Attached" self.pillsAttached.textColor = UIColor.green + self.updatePillArray(data: String(value)) + self.updatePillArrayUI() } } @@ -120,3 +137,18 @@ extension ViewController: SimpleBluetoothIODelegate { } + +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] + } +} diff --git a/embedded/main/main.ino b/embedded/main/main.ino index b9de4b9..ec7c64a 100644 --- a/embedded/main/main.ino +++ b/embedded/main/main.ino @@ -117,21 +117,34 @@ int prevVal = LOW; int prevVal2 = LOW; int getPillCount() { - int totalIntact = 0; + String bay0Intact = "0"; + String bay1Intact = "0"; + String bay2Intact = "0"; + String bay3Intact = "0"; + String bay4Intact = "0"; + String bay5Intact = "0"; + String result = "7"; + if(digitalRead(DETECT_PIN) == LOW) { Serial.println("PROBE_ATTACHED"); - if(digitalRead(PILL_1_PIN) == LOW){totalIntact++;Serial.println("25:FULL");} - if(digitalRead(PILL_2_PIN) == LOW){totalIntact++;Serial.println("13:FULL");} - if(digitalRead(PILL_3_PIN) == LOW){totalIntact++;Serial.println("12:FULL");} - if(digitalRead(PILL_4_PIN) == LOW){totalIntact++;Serial.println("26:FULL");} - if(digitalRead(PILL_5_PIN) == LOW){totalIntact++;Serial.println("27:FULL");} - if(digitalRead(PILL_6_PIN) == LOW){totalIntact++;Serial.println("14:FULL");} + if(digitalRead(PILL_1_PIN) == LOW){bay0Intact = "1";Serial.println("25:FULL");} + if(digitalRead(PILL_2_PIN) == LOW){bay1Intact = "1";Serial.println("13:FULL");} + if(digitalRead(PILL_3_PIN) == LOW){bay2Intact = "1";Serial.println("12:FULL");} + if(digitalRead(PILL_4_PIN) == LOW){bay3Intact = "1";Serial.println("26:FULL");} + if(digitalRead(PILL_5_PIN) == LOW){bay4Intact = "1";Serial.println("27:FULL");} + if(digitalRead(PILL_6_PIN) == LOW){bay5Intact = "1";Serial.println("14:FULL");} + result = result + bay0Intact + bay1Intact + bay2Intact + bay3Intact + bay4Intact + bay5Intact; + Serial.println("**********RESULT**********"); + Serial.println(result); + Serial.println("**********RESULT**********"); } else { Serial.println("PROBE_DETACHED"); - totalIntact = 55; + result = "55"; } - return totalIntact; + Serial.print("#########################################Sending: "); + Serial.println(result.toInt()); + return result.toInt(); } void loop() {