Using Swift (OS X) to communicate with Teensys - any experience here?

Status
Not open for further replies.

howiemnet

Active member
I'm writing an app in Swift that communicates with multiple Teensys (3.1 & LCs) but I've hit a few issues. Any experience / pointers much appreciated.

There's a number of ways to talk to a Teensy 3/LC from Swift on a Mac:
Serial over USB:
I had no problems communicating from a console app written in C, but now I'm using Swift (so I can have a natty GUI going on) I'm relying on a library to make the Serial stuff easier - ORSSerialPort. Trouble with it is that for some reason the Teensys take ages to close their port - if I call the ORSSerialPort close() function, the app hangs for anywhere between 1 and 30 seconds, spinning ball an' all that. Arduino Mega running very similar code doesn't. Could be a problem in the library, but thought I'd ask in case it rings a bell for anyone. Or if anyone's written serial library in Swift, that'd be ace...

USB-HID-Raw
I've got to move over to this: the only stumbling block is in setting up a callback so I don't have to poll the port. Bit of an esoteric question, but if anyone has already done it, I'd love a pointer or two: I can call Paul's USB-HID C library of functions OK by using a bridging header in Swift to declare the C functions, but I can't suss out how to wire in a callback facility so I don't have to poll the port. Pushing my knowledge of Swift *and* C.
 
Is Python an alternative for you? Platform-independent. Quick development.
Has pyserial which works with FTDI/USB and virtual serial ports via USB.

I've done so on Linux and windows and avoided the need for custom C calls.
 
Python not really viable for my needs - and I've put too much time into this Swift app now :)

I love using Python for general scripting (my day job involves Blender, which uses Python for much of its front end and scripting) but for this application I need a rich and responsive GUI (plus the fact I could basically just recompile and I'd have an iOS app is pretty cool)
 
I'm relying on a library to make the Serial stuff easier - ORSSerialPort. Trouble with it is that for some reason the Teensys take ages to close their port - if I call the ORSSerialPort close() function, the app hangs for anywhere between 1 and 30 seconds, spinning ball an' all that. Arduino Mega running very similar code doesn't. Could be a problem in the library, but thought I'd ask in case it rings a bell for anyone.

That does sound very mysterious.

Is there any way I could reproduce that here. I have a Mega and Teensy, and a Macbook with Lion (10.7.5), and a USB protocol analyzer to dig into the USB communication... but zero knowledge of Swift.
 
Hi Paul,

I'm moving over to rawhid so this will cease to be an issue for me. If you'd like I can create a minimal Xcode / Teensyduino project to illustrate the problem - but I suspect you won't be able to compile it unless you have the latest and greatest Xcode / OS X combo. Let me know if you want me to, though; if anyone else starts having issues closing a Teensy USB-Serial port it may be worth looking into.

(incidentally, would you see any value in a minimal / skeleton Swift project to show how to communicate with a Teensy via rawhid? My "hello world" app lets you control an on-screen slider with an encoder attached to a Teensy LC - it was relatively straightforward to do, but to newcomers to Swift there are a few tiny gotchas. It'd be nice to give other Teensy users a quick start in getting a proper Mac app to talk to their electronics)

h
 
I'd be interested in that 'hello world' swift app - i think. I haven't bit the bullet and upgraded to Yosemite yet, so apparently Swift 2 is unavailable to me.

Mostly I'm interested because I'm, at some point in the nearish future, attempt to get the teensy_loader_cli working in OSX (somehow, very early stages). As far as I can tell that communicates via HID-over-USB (IOKit!). What I may end up doing is just getting the CLI working with iokit and gcc (maybe later OSX versions have fixed the issue with IOHIDDeviceSetReportWithCallback?) and use Swift to act as a front-end.

Paul, if you end up reading this, I am a bit curious how you managed to work around that problem in the GUI loader...
 
Status
Not open for further replies.
Back
Top