Last active
August 30, 2023 12:57
-
-
Save Neumi/c86cba0c2c50c7d461175ff41380cf65 to your computer and use it in GitHub Desktop.
Ethersweep Remote IOS app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Network | |
struct ContentView: View { | |
@State private var ipAddress = "192.168.1.184" | |
@State private var port = "8888" | |
@State private var speed: Double = 200 | |
@State private var steps: Double = 100 | |
@State private var selectedStepMode = 32 | |
@State private var hold = false | |
var body: some View { | |
VStack { | |
Text("Ethersweep Remote") | |
.font(.title) | |
.padding(.bottom, 20) | |
HStack { | |
Text("IP Address:") | |
TextField("Enter IP Address", text: $ipAddress) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding(.horizontal) | |
} | |
.padding(.vertical) | |
HStack { | |
Text("Port:") | |
TextField("Enter Port", text: $port) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.keyboardType(.numberPad) | |
.padding(.horizontal) | |
} | |
.padding(.vertical) | |
VStack { | |
Text("Speed") | |
Slider(value: $speed, in: 0...600, step: 10) { | |
Text("Step delay in μs: \(Int(speed))") | |
} | |
Text("\(Int(speed))") | |
} | |
.padding(.vertical) | |
VStack { | |
Text("Steps") | |
Slider(value: $steps, in: 0...2400, step: 10) { | |
Text("Steps: \(Int(steps))") | |
} | |
Text("\(Int(steps))") | |
} | |
.padding(.vertical) | |
VStack { | |
Text("Stepmode") | |
Picker("Step Mode", selection: $selectedStepMode) { | |
Text("8").tag(8) | |
Text("16").tag(16) | |
Text("32").tag(32) | |
Text("64").tag(64) | |
} | |
.pickerStyle(SegmentedPickerStyle()) | |
} | |
.padding(.vertical) | |
Toggle("Hold", isOn: $hold) | |
.padding(.vertical) | |
HStack { | |
Button(action: { sendDirection(0) }) { | |
Text("Open") | |
.foregroundColor(.white) | |
.padding() | |
.background(Color.blue) | |
.cornerRadius(10) | |
} | |
.padding(.horizontal) | |
Button(action: { sendDirection(1) }) { | |
Text("Close") | |
.foregroundColor(.white) | |
.padding() | |
.background(Color.blue) | |
.cornerRadius(10) | |
} | |
.padding(.horizontal) | |
} | |
.padding(.vertical) | |
} | |
.padding() | |
} | |
func sendDirection(_ direction: Int) { | |
guard let portNumber = UInt16(port), | |
let ipAddressString = IPv4Address(ipAddress)?.description else { | |
print("Invalid IP address or port") | |
return | |
} | |
let machineControl = MachineControl( | |
drivemode: 0, speed: Int(speed), slope: 100, steps: Int(steps), | |
stepmode: selectedStepMode, direction: direction, hold: hold ? 1 : 0 | |
) | |
let queue = DispatchQueue(label: "udpQueue") | |
let connection = NWConnection(host: NWEndpoint.Host(ipAddressString), port: NWEndpoint.Port(rawValue: portNumber)!, using: .udp) | |
connection.start(queue: queue) | |
do { | |
let content = try JSONEncoder().encode(machineControl) | |
connection.send(content: content, completion: .contentProcessed { error in | |
if let error = error { | |
print("Send error: \(error)") | |
} else { | |
print("Message sent successfully") | |
} | |
connection.cancel() | |
}) | |
} catch { | |
print("JSON encoding error: \(error)") | |
connection.cancel() | |
} | |
} | |
} | |
struct MachineControl: Codable { | |
let drivemode: Int | |
let speed: Int | |
let slope: Int | |
let steps: Int | |
let stepmode: Int | |
let direction: Int | |
let hold: Int | |
} | |
// Helper to validate IP address | |
struct IPv4Address { | |
let parts: [UInt8] | |
init?(_ string: String) { | |
let parts = string.split(separator: ".").compactMap { UInt8($0) } | |
guard parts.count == 4 else { return nil } | |
self.parts = parts | |
} | |
var description: String { | |
return parts.map { String($0) }.joined(separator: ".") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ethersweep_controllerApp.swift | |
// ethersweep_controller | |
// | |
// Created by Jan Neumann on 23.08.23. | |
// | |
import SwiftUI | |
@main | |
struct ethersweep_controllerApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment