Skip to content

Commit

Permalink
fix: Show connection status in the menu bar (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmorley authored Jun 21, 2024
1 parent 4efe0fb commit b104c47
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 22 deletions.
Binary file added PsiMac/Assets.xcassets/Connected.imageset/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PsiMac/Assets.xcassets/Connected.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "5.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file added PsiMac/Assets.xcassets/Disconnected.imageset/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PsiMac/Assets.xcassets/Disconnected.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "4.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
16 changes: 13 additions & 3 deletions PsiMac/PsiMacApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ import SwiftUI
@main
struct PsiMacApp: App {

let server = Server()
@State var server = Server()

var body: some Scene {
WindowGroup {
ContentView()

MenuBarExtra {
Button("Connect") {

}
} label: {
if server.isConnected {
Image("Connected")
} else {
Image("Disconnected")
}
}

}
}
38 changes: 20 additions & 18 deletions PsiMac/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,32 @@

import Foundation

struct Server {
@Observable
class Server {

func threadEntryPoint() {

let arguments = ["ncpd", "-d", "-s", "/dev/tty.usbserial-A91MGK6M", "-b", "115200", "-v", "nl"]
var isConnected: Bool = false

let argc = Int32(arguments.count)
var argv: [UnsafeMutablePointer<CChar>?] = arguments.map { strdup($0) }
argv.append(nil) // Null-terminate the array
func threadEntryPoint() {

// Convert the array to an UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>
let argvPointer = UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>.allocate(capacity: argv.count)
argvPointer.initialize(from: &argv, count: argv.count)
let context = Unmanaged.passRetained(self).toOpaque()
let callback: statusCallback_t = { context, status in
guard let context else {
return
}
print("status = \(status)")
let server = Unmanaged<Server>.fromOpaque(context).takeUnretainedValue()
DispatchQueue.main.sync {
server.isConnected = status == 1 ? true : false
}
}

// Call the C function
let result = run(argc, argvPointer)
print(result)
let device = "/dev/tty.usbserial-AL00AYCG"
// let device = "/dev/tty.usbserial-A91MGK6M"

// Free the memory allocated for the C strings
for arg in argv {
free(arg)
}
argvPointer.deallocate()
// let log: UInt16 = 1 | 2 | 4 | 8 | 18 | 32 | 64
let log: UInt16 = 0

ncpd(7501, 115200, "127.0.0.1", device, log, callback, context)
}

init() {
Expand Down
2 changes: 1 addition & 1 deletion dependencies/plptools
Submodule plptools updated 4 files
+69 −56 ncpd/main.cc
+10 −1 ncpd/ncp.cc
+8 −1 ncpd/ncp.h
+16 −0 ncpd/ncpd.h

0 comments on commit b104c47

Please sign in to comment.