Initial commit
This commit is contained in:
11
mcrl-app/Assets.xcassets/AccentColor.colorset/Contents.json
Normal file
11
mcrl-app/Assets.xcassets/AccentColor.colorset/Contents.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
35
mcrl-app/Assets.xcassets/AppIcon.appiconset/Contents.json
Normal file
35
mcrl-app/Assets.xcassets/AppIcon.appiconset/Contents.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
},
|
||||
{
|
||||
"appearances" : [
|
||||
{
|
||||
"appearance" : "luminosity",
|
||||
"value" : "dark"
|
||||
}
|
||||
],
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
},
|
||||
{
|
||||
"appearances" : [
|
||||
{
|
||||
"appearance" : "luminosity",
|
||||
"value" : "tinted"
|
||||
}
|
||||
],
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
6
mcrl-app/Assets.xcassets/Contents.json
Normal file
6
mcrl-app/Assets.xcassets/Contents.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
115
mcrl-app/BluetoothManager.swift
Normal file
115
mcrl-app/BluetoothManager.swift
Normal file
@@ -0,0 +1,115 @@
|
||||
//
|
||||
// BluetoothManager.swift
|
||||
// mcrl-app
|
||||
//
|
||||
// Created by Simon Oberzier on 17.02.26.
|
||||
//
|
||||
|
||||
import CoreBluetooth
|
||||
import Combine
|
||||
|
||||
class BluetoothManager: NSObject, ObservableObject {
|
||||
@Published var sensorValues = SensorData()
|
||||
@Published var connectionStatus = "Disconnected"
|
||||
|
||||
var centralManager: CBCentralManager!
|
||||
var picoPeripheral: CBPeripheral?
|
||||
|
||||
let serviceUUID = CBUUID(string: "0000FF00-0000-1000-8000-00805F9B34FB")
|
||||
let charUUID = CBUUID(string: "0000FF01-0000-1000-8000-00805F9B34FB")
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
centralManager = CBCentralManager(delegate: self, queue: nil)
|
||||
}
|
||||
|
||||
func parseSensorString(_ str: String) {
|
||||
let cleanStr = str.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let components = cleanStr.components(separatedBy: ",")
|
||||
|
||||
guard components.count >= 7, components[0] == "S" else {
|
||||
print("Received malformed string: \(cleanStr)")
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.sensorValues.ax = Double(components[1]) ?? 0
|
||||
self.sensorValues.ay = Double(components[2]) ?? 0
|
||||
self.sensorValues.az = Double(components[3]) ?? 0
|
||||
self.sensorValues.lat = Double(components[4]) ?? 0
|
||||
self.sensorValues.lon = Double(components[5]) ?? 0
|
||||
self.sensorValues.hasFix = components[6].contains("1")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BluetoothManager: CBCentralManagerDelegate {
|
||||
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
||||
switch central.state {
|
||||
case .poweredOn:
|
||||
connectionStatus = "Scanning..."
|
||||
// Scanning with nil finds EVERYTHING. Safer for debugging.
|
||||
centralManager.scanForPeripherals(withServices: nil, options: nil)
|
||||
case .poweredOff:
|
||||
connectionStatus = "Bluetooth is Off"
|
||||
case .unauthorized:
|
||||
connectionStatus = "Permission Denied"
|
||||
default:
|
||||
connectionStatus = "Unknown State"
|
||||
}
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
||||
let name = peripheral.name ?? "Unknown"
|
||||
|
||||
if name.contains("Pico") {
|
||||
print("Discovered: \(name)")
|
||||
connectionStatus = "Pico Found!"
|
||||
|
||||
picoPeripheral = peripheral
|
||||
picoPeripheral?.delegate = self
|
||||
|
||||
centralManager.stopScan()
|
||||
centralManager.connect(peripheral, options: nil)
|
||||
}
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
||||
connectionStatus = "Connected"
|
||||
print("Connected to \(peripheral.name ?? "Device")")
|
||||
peripheral.discoverServices([serviceUUID])
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
|
||||
connectionStatus = "Disconnected"
|
||||
print("Disconnected. Restarting scan...")
|
||||
centralManager.scanForPeripherals(withServices: nil, options: nil)
|
||||
}
|
||||
}
|
||||
|
||||
extension BluetoothManager: CBPeripheralDelegate {
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||
guard let services = peripheral.services else { return }
|
||||
for service in services {
|
||||
print("Service discovered: \(service.uuid)")
|
||||
peripheral.discoverCharacteristics([charUUID], for: service)
|
||||
}
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
||||
guard let characteristics = service.characteristics else { return }
|
||||
for characteristic in characteristics {
|
||||
if characteristic.uuid == charUUID {
|
||||
print("Found Data Characteristic!")
|
||||
peripheral.setNotifyValue(true, for: characteristic)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||
if let data = characteristic.value,
|
||||
let str = String(data: data, encoding: .utf8) {
|
||||
parseSensorString(str)
|
||||
}
|
||||
}
|
||||
}
|
||||
132
mcrl-app/ContentView.swift
Normal file
132
mcrl-app/ContentView.swift
Normal file
@@ -0,0 +1,132 @@
|
||||
//
|
||||
// ContentView.swift
|
||||
// mcrl-app
|
||||
//
|
||||
// Created by Simon Oberzier on 13.01.26.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
@StateObject var bleManager = BluetoothManager()
|
||||
|
||||
var experiencedG: Double {
|
||||
let totalVector = sqrt(
|
||||
pow(bleManager.sensorValues.ax, 2) +
|
||||
pow(bleManager.sensorValues.ay, 2) +
|
||||
pow(bleManager.sensorValues.az, 2)
|
||||
)
|
||||
return max(0, totalVector - 1.0)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
VStack(spacing: 20) {
|
||||
HStack {
|
||||
Label(bleManager.connectionStatus.uppercased(), systemImage: bleManager.connectionStatus == "Connected" ? "bolt.fill" : "bolt.slash.fill")
|
||||
.font(.system(size: 10, weight: .black, design: .monospaced))
|
||||
.foregroundColor(bleManager.connectionStatus == "Connected" ? .green : .red)
|
||||
Spacer()
|
||||
Text("LIVE TELEMETRY")
|
||||
.font(.system(size: 10, weight: .bold))
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
.padding(.horizontal)
|
||||
|
||||
ZStack {
|
||||
Circle()
|
||||
.trim(from: 0.1, to: 0.9)
|
||||
.stroke(Color.primary.opacity(0.1), style: StrokeStyle(lineWidth: 30, lineCap: .round))
|
||||
.rotationEffect(.degrees(90))
|
||||
|
||||
Circle()
|
||||
.trim(from: 0.1, to: min(0.1 + (experiencedG / 3.0) * 0.8, 0.9)) // Scaled for 0-3G range
|
||||
.stroke(
|
||||
AngularGradient(
|
||||
gradient: Gradient(colors: [.green, .yellow, .orange, .red]),
|
||||
center: .center,
|
||||
angle: .degrees(180)
|
||||
),
|
||||
style: StrokeStyle(lineWidth: 30, lineCap: .round)
|
||||
)
|
||||
.rotationEffect(.degrees(90))
|
||||
.animation(.interactiveSpring(response: 0.2, dampingFraction: 0.5), value: experiencedG)
|
||||
|
||||
VStack(spacing: -2) {
|
||||
Text(String(format: "%.2f", experiencedG))
|
||||
.font(.system(size: 64, weight: .black, design: .rounded))
|
||||
Text("NET G-FORCE")
|
||||
.font(.system(size: 12, weight: .heavy))
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
.frame(width: 280, height: 280)
|
||||
.padding(.vertical)
|
||||
|
||||
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 15) {
|
||||
DataTile(title: "LATERAL X", value: bleManager.sensorValues.ax, unit: "G", color: .blue)
|
||||
DataTile(title: "LONG Y", value: bleManager.sensorValues.ay, unit: "G", color: .orange)
|
||||
DataTile(title: "VERTICAL Z", value: bleManager.sensorValues.az, unit: "G", color: .purple)
|
||||
DataTile(title: "GPS STATUS", value: bleManager.sensorValues.hasFix ? 1.0 : 0.0, unit: bleManager.sensorValues.hasFix ? "FIXED" : "NO FIX", color: .green)
|
||||
}
|
||||
.padding(.horizontal)
|
||||
|
||||
if bleManager.sensorValues.hasFix {
|
||||
HStack {
|
||||
VStack(alignment: .leading) {
|
||||
Text("COORDINATES").font(.system(size: 9, weight: .black)).foregroundColor(.secondary)
|
||||
Text("\(bleManager.sensorValues.lat, specifier: "%.5f"), \(bleManager.sensorValues.lon, specifier: "%.5f")")
|
||||
.font(.system(.caption, design: .monospaced))
|
||||
.fontWeight(.bold)
|
||||
}
|
||||
Spacer()
|
||||
Image(systemName: "location.north.circle.fill")
|
||||
.foregroundColor(.blue)
|
||||
}
|
||||
.padding()
|
||||
.background(Color.primary.opacity(0.05))
|
||||
.cornerRadius(12)
|
||||
.padding(.horizontal)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.navigationTitle("Pico Link")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DataTile: View {
|
||||
let title: String
|
||||
let value: Double
|
||||
let unit: String
|
||||
let color: Color
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(title)
|
||||
.font(.system(size: 9, weight: .black))
|
||||
.foregroundColor(color)
|
||||
|
||||
HStack(alignment: .lastTextBaseline, spacing: 2) {
|
||||
if unit == "G" {
|
||||
Text(String(format: "%.2f", value))
|
||||
.font(.system(.title2, design: .monospaced))
|
||||
.fontWeight(.bold)
|
||||
}
|
||||
Text(unit)
|
||||
.font(.system(size: 10, weight: .bold))
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding()
|
||||
.background(Color.primary.opacity(0.05))
|
||||
.cornerRadius(12)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.stroke(color.opacity(0.2), lineWidth: 1)
|
||||
)
|
||||
}
|
||||
}
|
||||
10
mcrl-app/Info.plist
Normal file
10
mcrl-app/Info.plist
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>UIBackgroundModes</key>
|
||||
<array>
|
||||
<string>bluetooth-central</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
17
mcrl-app/SensorData.swift
Normal file
17
mcrl-app/SensorData.swift
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// SensorData.swift
|
||||
// mcrl-app
|
||||
//
|
||||
// Created by Simon Oberzier on 17.02.26.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct SensorData {
|
||||
var ax: Double = 0.0
|
||||
var ay: Double = 0.0
|
||||
var az: Double = 0.0
|
||||
var lat: Double = 0.0
|
||||
var lon: Double = 0.0
|
||||
var hasFix: Bool = false
|
||||
}
|
||||
17
mcrl-app/mcrl_appApp.swift
Normal file
17
mcrl-app/mcrl_appApp.swift
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// mcrl_appApp.swift
|
||||
// mcrl-app
|
||||
//
|
||||
// Created by Simon Oberzier on 13.01.26.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
@main
|
||||
struct mcrl_appApp: App {
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user