This commit is contained in:
Max 2021-01-21 12:09:25 +00:00
parent 6bb4845930
commit c968213ec9
4 changed files with 135 additions and 190 deletions

View File

@ -17,7 +17,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="gsL-Nw-5C6"> <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="gsL-Nw-5C6">
<rect key="frame" x="161" y="145" width="91" height="30"/> <rect key="frame" x="303" y="44" width="91" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="TOGGLE LED"/> <state key="normal" title="TOGGLE LED"/>
<connections> <connections>
@ -26,61 +26,61 @@
</connections> </connections>
</button> </button>
<switch opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="dHC-eT-3y1"> <switch opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="dHC-eT-3y1">
<rect key="frame" x="217" y="44" width="49" height="31"/> <rect key="frame" x="86" y="44" width="49" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
</switch> </switch>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Debug" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="l2I-9J-rAh"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Debug" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="l2I-9J-rAh">
<rect key="frame" x="151" y="49" width="58" height="21"/> <rect key="frame" x="20" y="48" width="58" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/> <fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="0.63153754340000001" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.63153754340000001" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Pills remaining:" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eKc-uP-L0m"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Pills remaining:" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eKc-uP-L0m">
<rect key="frame" x="78" y="494" width="131" height="24"/> <rect key="frame" x="67" y="572" width="131" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/> <fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Pills consumed: " textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="llf-Dy-Aua"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Pills consumed: " textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="llf-Dy-Aua">
<rect key="frame" x="67" y="444" width="142" height="24"/> <rect key="frame" x="56" y="522" width="142" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/> <fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="?" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="SZM-0X-3rS"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="?" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="SZM-0X-3rS">
<rect key="frame" x="235" y="444" width="29" height="24"/> <rect key="frame" x="224" y="522" width="58" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/> <fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="?" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Sjf-mb-Bwa"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="?" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Sjf-mb-Bwa">
<rect key="frame" x="235" y="494" width="29" height="24"/> <rect key="frame" x="224" y="572" width="58" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/> <fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="egw-kZ-OI5"> <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="egw-kZ-OI5">
<rect key="frame" x="155" y="317" width="102" height="30"/> <rect key="frame" x="162" y="406" width="91" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Debug: update"/> <state key="normal" title="Force update"/>
<connections> <connections>
<action selector="forcePillRefresh:" destination="BYZ-38-t0r" eventType="touchUpInside" id="UQT-N9-laF"/> <action selector="forcePillRefresh:" destination="BYZ-38-t0r" eventType="touchUpInside" id="UQT-N9-laF"/>
</connections> </connections>
</button> </button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Total pills" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="enT-hS-KDW"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Total pills" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="enT-hS-KDW">
<rect key="frame" x="127" y="402" width="82" height="24"/> <rect key="frame" x="116" y="480" width="82" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/> <fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="?" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="RoE-tt-hdb"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="?" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="RoE-tt-hdb">
<rect key="frame" x="235" y="402" width="29" height="24"/> <rect key="frame" x="224" y="480" width="58" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/> <fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
@ -100,13 +100,6 @@
<color key="textColor" white="0.63153754340000001" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.63153754340000001" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="null" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ypx-xB-OqG">
<rect key="frame" x="179" y="562" width="58" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="0.63153754340000001" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="QDW-f0-1g2"> <switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="QDW-f0-1g2">
<rect key="frame" x="335" y="831" width="49" height="31"/> <rect key="frame" x="335" y="831" width="49" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
@ -121,6 +114,13 @@
<color key="textColor" white="0.63153754340000001" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> <color key="textColor" white="0.63153754340000001" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="null" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ypx-xB-OqG">
<rect key="frame" x="178" y="44" width="58" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="0.63153754340000001" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
</subviews> </subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/> <viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/> <color key="backgroundColor" systemColor="systemBackgroundColor"/>
@ -139,7 +139,7 @@
</viewController> </viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/> <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects> </objects>
<point key="canvasLocation" x="122" y="119"/> <point key="canvasLocation" x="121.73913043478262" y="118.52678571428571"/>
</scene> </scene>
</scenes> </scenes>
<resources> <resources>

View File

@ -48,14 +48,14 @@ extension SimpleBluetoothIO: CBCentralManagerDelegate {
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.discoverServices(nil) peripheral.discoverServices(nil)
self.connected = true self.connected = true
print("Connected! With \(peripheral.name)") print("Connected! With \(String(describing: peripheral.name))")
delegate?.simpleBluetoothIO(simpleBluetoothIO: self, didConnect: true) delegate?.simpleBluetoothIO(simpleBluetoothIO: self, didConnect: true)
} }
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
connectedPeripheral = peripheral connectedPeripheral = peripheral
if (peripheral.name != "[TV] Samsung Q7 Series (55)" && peripheral.name != "MacDeMax"){ if (peripheral.name != "[TV] Samsung Q7 Series (55)" && peripheral.name != "MacDeMax"){
print("Discovered \(peripheral.name)") print("Discovered \(String(describing: peripheral.name))")
} }
if (peripheral.name != nil) { if (peripheral.name != nil) {
if(((peripheral.name?.contains("PILL_TRACKER"))!)) if(((peripheral.name?.contains("PILL_TRACKER"))!))
@ -114,7 +114,7 @@ extension SimpleBluetoothIO: CBPeripheralDelegate {
} }
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("Disconnected: \(peripheral.name)") print("Disconnected: \(String(describing: peripheral.name))")
print("Restarting scan...") print("Restarting scan...")
centralManager.scanForPeripherals(withServices: nil, options: nil) centralManager.scanForPeripherals(withServices: nil, options: nil)
delegate?.simpleBluetoothIO(simpleBluetoothIO: self, didDisconnect: true) delegate?.simpleBluetoothIO(simpleBluetoothIO: self, didDisconnect: true)

View File

@ -21,9 +21,10 @@ class ViewController: UIViewController {
@IBOutlet weak var refreshToggle: UISwitch! @IBOutlet weak var refreshToggle: UISwitch!
let totalPills: Int = 6 let totalPills: Int = 6
var usedPills: Int = 0
var remainingPills: Int = 0
let updateFreq: Float = 2.0 let updateFreq: Float = 2.0
weak var timer: Timer? weak var timer: Timer?
var pillIndex = [3, 3, 3, 3, 3, 3]
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -53,20 +54,6 @@ class ViewController: UIViewController {
timer?.invalidate() 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) { @IBAction func ledToggleButtonDown(_ sender: UIButton) {
simpleBluetoothIO.writeValue(value:49) simpleBluetoothIO.writeValue(value:49)
} }
@ -104,16 +91,14 @@ extension ViewController: SimpleBluetoothIODelegate {
self.pillsTotal.text = "X" self.pillsTotal.text = "X"
self.pillsAttached.text = "Detached" self.pillsAttached.text = "Detached"
self.pillsAttached.textColor = UIColor.red self.pillsAttached.textColor = UIColor.red
} else if String(value).prefix(2) == "7" { } else {
let remainingPills = Int(value) self.remainingPills = Int(value)
let usedPills = self.totalPills - remainingPills self.usedPills = self.totalPills - self.remainingPills
self.pillsConsumed.text = String(usedPills) self.pillsConsumed.text = String(usedPills)
self.pillsRemaining.text = String(remainingPills) self.pillsRemaining.text = String(remainingPills)
self.pillsTotal.text = String(self.totalPills) self.pillsTotal.text = String(self.totalPills)
self.pillsAttached.text = "Attached" self.pillsAttached.text = "Attached"
self.pillsAttached.textColor = UIColor.green self.pillsAttached.textColor = UIColor.green
self.updatePillArray(data: String(value))
self.updatePillArrayUI()
} }
} }

View File

@ -1,7 +1,6 @@
#include <BLEDevice.h> #include <BLEDevice.h>
#include <BLEUtils.h> #include <BLEUtils.h>
#include <BLEServer.h> #include <BLEServer.h>
// See the following for generating UUIDs: // See the following for generating UUIDs:
// https://www.uuidgenerator.net/ // https://www.uuidgenerator.net/
@ -10,7 +9,6 @@
#define LED_PIN 2 #define LED_PIN 2
#define BTN_PIN 5 #define BTN_PIN 5
#define PILL_1_PIN 25 #define PILL_1_PIN 25
#define PILL_2_PIN 13 #define PILL_2_PIN 13
#define PILL_3_PIN 12 #define PILL_3_PIN 12
@ -19,162 +17,124 @@
#define PILL_6_PIN 14 #define PILL_6_PIN 14
#define DETECT_PIN 18 #define DETECT_PIN 18
int deviceConnected = false; int deviceConnected = false;
int waitingForUpdate = 0; int waitingForUpdate = 0;
class MyCallbacks:public BLECharacteristicCallbacks {
class MyCallbacks: public BLECharacteristicCallbacks { void onConnect(BLEServer * pServer) {
void onConnect(BLEServer* pServer) {
deviceConnected = true; deviceConnected = true;
Serial.println("device connected"); Serial.println("device connected");
}; };
void onDisconnect(BLEServer * pServer) {
void onDisconnect(BLEServer* pServer) {
deviceConnected = false; deviceConnected = false;
Serial.println("device disconnected"); Serial.println("device disconnected");
} }
void onWrite(BLECharacteristic *pCharacteristic) { void onWrite(BLECharacteristic * pCharacteristic) {
std::string value = pCharacteristic->getValue(); std::string value = pCharacteristic -> getValue();
if (value.length() > 0) { if (value.length() > 0) {
Serial.print("*********"); Serial.print("*********");
Serial.print(value.c_str()); Serial.print(value.c_str());
Serial.print("-"); Serial.print("-");
Serial.print(atoi(value.c_str())); Serial.print(atoi(value.c_str()));
Serial.print("-"); Serial.print("-");
if(atoi(value.c_str())==1) if (atoi(value.c_str()) == 1) {
{ digitalWrite(LED_PIN, HIGH);
digitalWrite(LED_PIN,HIGH);
Serial.print("LEDON"); Serial.print("LEDON");
} } else if (atoi(value.c_str()) == 2) {
else if (atoi(value.c_str())==2) digitalWrite(LED_PIN, LOW);
{
digitalWrite(LED_PIN,LOW);
Serial.print("LEDOFF"); Serial.print("LEDOFF");
} } else if (atoi(value.c_str()) == 3) {
else if (atoi(value.c_str())==3)
{
Serial.print("UPDATE_PILL_TRACK"); Serial.print("UPDATE_PILL_TRACK");
waitingForUpdate = 1; waitingForUpdate = 1;
} } else {
else {
Serial.println(); Serial.println();
Serial.println(atoi(value.c_str())); Serial.println(atoi(value.c_str()));
Serial.println(); Serial.println();
} }
Serial.println("*********"); Serial.println("*********");
} }
} }
}; };
BLECharacteristic *pCharacteristic; BLECharacteristic * pCharacteristic;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
pinMode(LED_PIN,OUTPUT); pinMode(LED_PIN, OUTPUT);
pinMode(PILL_1_PIN, INPUT_PULLUP);
pinMode(PILL_1_PIN,INPUT_PULLUP); pinMode(PILL_2_PIN, INPUT_PULLUP);
pinMode(PILL_2_PIN,INPUT_PULLUP); pinMode(PILL_3_PIN, INPUT_PULLUP);
pinMode(PILL_3_PIN,INPUT_PULLUP); pinMode(PILL_4_PIN, INPUT_PULLUP);
pinMode(PILL_4_PIN,INPUT_PULLUP); pinMode(PILL_5_PIN, INPUT_PULLUP);
pinMode(PILL_5_PIN,INPUT_PULLUP); pinMode(PILL_6_PIN, INPUT_PULLUP);
pinMode(PILL_6_PIN,INPUT_PULLUP); pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(DETECT_PIN, INPUT_PULLUP);
pinMode(BTN_PIN,INPUT_PULLUP); digitalWrite(LED_PIN, LOW);
pinMode(DETECT_PIN,INPUT_PULLUP);
digitalWrite(LED_PIN,LOW);
Serial.println("1- Download and install an BLE scanner app in your phone"); Serial.println("1- Download and install an BLE scanner app in your phone");
Serial.println("2- Scan for BLE devices in the app"); Serial.println("2- Scan for BLE devices in the app");
Serial.println("3- Connect to MyESP32"); Serial.println("3- Connect to MyESP32");
Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something"); Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
Serial.println("5- See the magic =)"); Serial.println("5- See the magic =)");
BLEDevice::init("PILL_TRACKER"); BLEDevice::init("PILL_TRACKER");
BLEServer *pServer = BLEDevice::createServer(); BLEServer * pServer = BLEDevice::createServer();
BLEService * pService = pServer -> createService(SERVICE_UUID);
BLEService *pService = pServer->createService(SERVICE_UUID); pCharacteristic = pService -> createCharacteristic(
pCharacteristic= pService->createCharacteristic(
CHARACTERISTIC_UUID, CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY BLECharacteristic::PROPERTY_NOTIFY
); );
pCharacteristic -> setCallbacks(new MyCallbacks());
pCharacteristic->setCallbacks(new MyCallbacks()); pCharacteristic -> setValue("Hello World");
pService -> start();
pCharacteristic->setValue("Hello World"); BLEAdvertising * pAdvertising = pServer -> getAdvertising();
pService->start(); pAdvertising -> start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
} }
int prevVal = LOW; int prevVal = LOW;
int prevVal2 = LOW; int prevVal2 = LOW;
int getPillCount() { int getPillCount() {
String bay0Intact = "0"; int totalCount = 0;
String bay1Intact = "0"; if (digitalRead(DETECT_PIN) == LOW) {
String bay2Intact = "0";
String bay3Intact = "0";
String bay4Intact = "0";
String bay5Intact = "0";
String result = "7";
if(digitalRead(DETECT_PIN) == LOW) {
Serial.println("PROBE_ATTACHED"); Serial.println("PROBE_ATTACHED");
if(digitalRead(PILL_1_PIN) == LOW){bay0Intact = "1";Serial.println("25:FULL");} if (digitalRead(PILL_1_PIN) == LOW){totalCount++;Serial.println("25:FULL");}
if(digitalRead(PILL_2_PIN) == LOW){bay1Intact = "1";Serial.println("13:FULL");} if (digitalRead(PILL_2_PIN) == LOW){totalCount++;Serial.println("13:FULL");}
if(digitalRead(PILL_3_PIN) == LOW){bay2Intact = "1";Serial.println("12:FULL");} if (digitalRead(PILL_3_PIN) == LOW){totalCount++;Serial.println("12:FULL");}
if(digitalRead(PILL_4_PIN) == LOW){bay3Intact = "1";Serial.println("26:FULL");} if (digitalRead(PILL_4_PIN) == LOW){totalCount++;Serial.println("26:FULL");}
if(digitalRead(PILL_5_PIN) == LOW){bay4Intact = "1";Serial.println("27:FULL");} if (digitalRead(PILL_5_PIN) == LOW){totalCount++;Serial.println("27:FULL");}
if(digitalRead(PILL_6_PIN) == LOW){bay5Intact = "1";Serial.println("14:FULL");} if (digitalRead(PILL_6_PIN) == LOW){totalCount++;Serial.println("14:FULL");}
result = result + bay0Intact + bay1Intact + bay2Intact + bay3Intact + bay4Intact + bay5Intact; } else {
Serial.println("**********RESULT**********");
Serial.println(result);
Serial.println("**********RESULT**********");
}
else {
Serial.println("PROBE_DETACHED"); Serial.println("PROBE_DETACHED");
result = "55"; totalCount = 55;
} }
Serial.print("#########################################Sending: "); return totalCount;
Serial.println(result.toInt());
return result.toInt();
} }
void loop() { void loop() {
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
int currentVal = digitalRead(BTN_PIN); int currentVal = digitalRead(BTN_PIN);
if(currentVal!=prevVal) if (currentVal != prevVal) {
{ prevVal = currentVal;
prevVal=currentVal; if (currentVal == HIGH) {
if(currentVal==HIGH)
{
int value = 99; int value = 99;
pCharacteristic->setValue((uint8_t*)&value, 4); pCharacteristic -> setValue((uint8_t *) & value, 4);
pCharacteristic->notify(); pCharacteristic -> notify();
} }
else else {
{
int value = 98; int value = 98;
pCharacteristic->setValue((uint8_t*)&value, 4); pCharacteristic -> setValue((uint8_t *) & value, 4);
pCharacteristic->notify(); pCharacteristic -> notify();
} }
} }
if (waitingForUpdate == 1) {
if(waitingForUpdate == 1){ Serial.println("************************************************************");
Serial.println("**********************************");
Serial.println("**************UPDATE AND SEND PILL COUNT********************"); Serial.println("**************UPDATE AND SEND PILL COUNT********************");
Serial.println("**********************************"); Serial.println("************************************************************");
waitingForUpdate = 0; waitingForUpdate = 0;
int value = getPillCount(); int value = getPillCount();
pCharacteristic->setValue((uint8_t*)&value, 4); pCharacteristic -> setValue((uint8_t *) & value, 4);
pCharacteristic->notify(); pCharacteristic -> notify();
} }
} }