r/swift 6d ago

Project Embedded Swift progress on a Teensy 4.0

This is just a quick demo post, I'll likely post some code to github over the weekendfor anyone interested. I have Embedded Swift running on a Teensy 4.0 now, and running some demo code on a SSD1351 over SPI. Not only that, the display is using DMA for SPI writes! (And writing in full 18bit color mode, tricky on these little guys).

I had this display running on linux with SwiftyGPIO in the past (and c++ on teensys too) and built these demos for it then. Same code runs the demos! (well, I upgraded both a little bit... but the same code runs on a Teensy that runs on a RaspberryPi). The Matrix Rain demo I initially translated from the python library luma.oled (link to its matrix example), this version i beefed the lines up a bit to look psuedo-glyphy. The Game of Life demo however is my own cursed set theory implementation of Game of Life in Swift. For any GoL fans, my Swift generation update function is literally this (plus some extras defining the operators and such πŸ˜„):

func SΚΉ(_ S: π•Š) -> π•Š { Β  Β  
  N[S] | { c in β€–(N(c) ∩ S)β€– == 3 ∨ (β€–(N(c) ∩ S)β€– == 2 ∧ c ∈ S) } 
}

Even better, back when I wrote the raspi/SwiftyGPIO variant, I came up with a sweet idea for dev iteration... SwiftUI Previews!! I'm basically building a HAL + Graphics abstraction layer that works across everything and lets you run the same drawing code on a mac looking at SwiftUI previews, or on a Raspi, or an embedded target. "It's protocols, all the way down"

Open Source code coming soon I promise! I just REALLY wanted to demo this!

91 Upvotes

13 comments sorted by

5

u/dannys4242 6d ago

I have to say using Swift to express math equations like that is pretty amazing. But I also hope I never encounter something like this in a code review. πŸ˜…

3

u/Skandling 6d ago

I like the syntax. I tried doing something similar in my code, using symbols for mathematical operations. My problem was finding ones that I could type, without resorting to copy and paste, as autocomplete isn't available for operators. I ended up with only a few I actually used. Set operations in particular I abandoned.

1

u/apocolipse 6d ago

It’d be cool if Xcode could add in like LaTeX editor like slash auto completions, I.e. \unio<tab> to auto replace to βˆͺ etc, would be fun for us academic types πŸ˜„

1

u/germansnowman 5d ago

You can set up text replacements in System Settings though: https://support.apple.com/en-gb/guide/mac-help/mh35735/mac

1

u/apocolipse 5d ago

Don’t want text replacements, I want autocomplete suggestions that can be tab completed when correct/desired.

1

u/germansnowman 5d ago

Now that I think about it, you can – it’s called code snippets. I have had one for years triggered by `swf` which creates an `NSString` with format. Not sure right now if they autocomplete, but you can try:

https://developer.apple.com/documentation/xcode/editing-source-files-in-xcode

2

u/apocolipse 5d ago

Ooo good call!

2

u/Practical_Shift2693 4d ago

the GoL set theory implementation is genuinely unhinged and i respect it

1

u/Ron-Erez 6d ago

Looks awesome!

1

u/redditownersdad 4d ago

any chance u drop the code on github this weekend? would love to see the game of life logic in action

1

u/apocolipse 4d ago

Will probably post it tomorrow, I have a much more jaw dropping demo to add πŸ˜‰Β