SwiftyGPIO icon indicating copy to clipboard operation
SwiftyGPIO copied to clipboard

Libraries based on SwiftyGPIO, some ideas

Open uraimo opened this issue 8 years ago • 14 comments

This issue collects interesting libraries that could be built from SwiftyGPIO and that could be useful for other people building embedded projects with Swift using specific sensors/devices, feel free to chime in if you want to build one of these or if you have other (better) ideas/requests:

  • [ ] MAX7219, for both led matrix and seven segment arrays based on this driver (SPI)
  • [ ] HMC8553L Magnetometer (I2C)
  • [x] SSD1306 / SH1106 128x64 OLED (sometimes available with SPI other times with I2C)
  • [ ] ST7735 TFT color LCD (SPI)
  • [x] Wii Nunchuk (I2C)
  • [x] WS281X RGB LED strips or matrixes (e.g. Unicorn HAT, NeoPixels), can be done manually with bit banging or via PWM
  • [ ] APA102/C RGB LED strips (SPI)
  • [ ] nRF24L01 radio module (SPI)
  • [ ] DCF77 radio time receiver, custom protocol.
  • [x] DS130X real time clock, custom protocol.

~~ 2018-> ~~

  • [ ] CC2531 modules/usb (UART)
  • [ ] TMP102 temp sensor (I2C), #89
  • [ ] ADC1115, 16 bits ADC (I2C), #81
  • [ ] CCS-811/CJMCU-811, indoor air quality sensor (CO2,VOCs,MOXs) (I2C)
  • [ ] SDS011, outdoor PM2.5/PM10 sensor (UART)
  • [ ] PN532 RFID/NFC card/tag reader (I2C,SPI,UART)

uraimo avatar Feb 24 '16 21:02 uraimo

I have bought an Raspberry Pi2 and Unicorn HAT (8x8 WS2812 LEDs) some weeks ago. Which I would like to see blink, i played around with a C Lib and Swift but it didn't worked out the way I want. Only 5 LEDs light up. So I would prefer a Swift only solution with SwiftyGPIO. But I need some help, this low level stuff is unchartered terrain for me.

damuellen avatar Feb 29 '16 21:02 damuellen

I see potential in a WS2812 library! My horrible solution consists of calling the Python library from Swift right now.

richardneitzke avatar Mar 01 '16 00:03 richardneitzke

@damuellen i'm not sure it could be related, but are you converting the raspberry 3v3 gpios to 5v (more likely that what you see is a timing issue)?

I can't find much documentation about this but i suppose the Unicorn Hat should be like a strip of 64 leds in series, so i suppose we could make it work sending a sequence of 1536 impulses (64 * 8bit * rgb) manually from a gpio respecting the WS2812 timing:

This could be easily done setting gpio.value and then nanosleep_ing_ for the time described above, al this repeated in a loop that sends the 1500+ bits and then send the reset signal to apply the configuration (0 for more than 50 micros). For example, to send a single bit at 1:

private func sleepnanos(nanos:Int){
        var t:timespec = timespec()
        t.tv_sec = 0
        t.tv_nsec = nanos
        nanosleep(&t,nil)
 } 

gpio.value = 1
sleepnanos(UInt32(700))
gpio.value = 0
sleepnanos(UInt32(600))

It _could_ be fast enough and precise enough to do something meaningful (the colors could be a bit or a lot off if the timing is not precise). I just bought a NeoPixel matrix that should be more or less similar to what you have, to play a bit with it.

@richardxyx yep, i agree :smile: . What kind of ws2812 device do you have?

More info here

uraimo avatar Mar 01 '16 09:03 uraimo

@uraimo I'm currently on a project that involves 115 WS2812b 5050 LEDs (Adafruit calls them NeoPixels but they're significantly cheaper from China) from a LED Strip. I still have some at home for testing and also have some 74AHCT125 to convert the signal from 3V3 to 5V.

It is well known that the WS2812 LEDs require pretty strict timing and I could only find one libary that was actually working with my Raspberry Pi 2 (https://github.com/jgarff/rpi_ws281x).

Edit: This datasheet provides more information about the required signal.

richardneitzke avatar Mar 01 '16 12:03 richardneitzke

@richardxyx great, so we'll be able to test it on a longer strip too.

I've created a new repo at https://github.com/uraimo/WS2812_RGBLCD.swift with an initial implementation, i'll be able to test it at the end of the week, but i don't expect that this code written blindly should work :) , feel free to play around with it if you want guys (depends on SwiftyGPIO, needs a main.swift to compile correctly).

So, for the timing constant i'm using this for the WS2812 and this for the WS2812B.

Now that i look at the reference more carefully it looks like that the data sent needs to be continuously refreshed after the reset delay, so, to work that setColor should be enclosed in a loop... not really an ideal implementation...

uraimo avatar Mar 01 '16 17:03 uraimo

I'll try my best helping. I'll play around with the repo, looks like you did a nice job setting everything up.

richardneitzke avatar Mar 01 '16 18:03 richardneitzke

WS281X done: https://github.com/uraimo/WS281x.swift DS130X I2C done: https://github.com/uraimo/DS1307.swift

uraimo avatar May 10 '17 19:05 uraimo

WS281x.swift works like a charm for me, so thanks a lot for this great lib. Easter holidays was long enough to write some Swift code, that let me control my 🦄Hat with an 📱 app. 😎

damuellen avatar May 10 '17 20:05 damuellen

Thanks @damuellen, glad to hear that someone is using it! :)

uraimo avatar May 10 '17 21:05 uraimo

I got the AdaFruit 8x8 via HT16K33 working this weekend:

https://github.com/jrahaim/swift-raspberry-pi-adafruit-led

jrahaim avatar Mar 26 '18 02:03 jrahaim

Nice one @jrahaim , I've added it to the libraries section.

uraimo avatar Mar 26 '18 08:03 uraimo

Also added and tested the Adafruit 4 digit 14 segment Alpha numeric display to https://github.com/jrahaim/swift-raspberry-pi-adafruit-led.

jrahaim avatar Mar 26 '18 13:03 jrahaim

Wrote up a quick driver for the 16-channel PCA9685 I2C PWM controller. Adafruit uses this on their PWM/Servo bonnet/HAT.

https://github.com/Kaiede/PCA9685

Kaiede avatar Aug 14 '18 20:08 Kaiede

Great! I know that board and had it in my TODO list, I'll update the readme right away, extremely useful for those who need a lot of PWMs. (Yep, I've seen your other message too, sorry for the delay :) )

uraimo avatar Aug 22 '18 14:08 uraimo