Skip to content

Instantly share code, notes, and snippets.

@carlos4242
Created August 26, 2019 22:46
Show Gist options
  • Save carlos4242/24c6c9b7e46d693cd411157bbb34c26f to your computer and use it in GitHub Desktop.
Save carlos4242/24c6c9b7e46d693cd411157bbb34c26f to your computer and use it in GitHub Desktop.
// Demo from live coding demo on Twitter.
// Swift for Arduino. Arduino UNO native code, using Grove 'neopixel' stick.
// Gives the effect of the scanner on 'K.I.T.T.' from Knight Rider.
//
// Libraries: iLEDHelpers.swift
//------------------------------------------------------------------------------
import AVR
typealias IntegerLiteralType = Pin
let ledPin = 4
let pixels = 10
let delayTime: MS = 60
var forward = true
pinMode(pin: ledPin, mode: OUTPUT)
var buffer = iLED_Setup_Buffered(pin: ledPin, count: pixels, hasWhiteChip: false)
func fadeUnlitPixels(litPixel: UInt8) {
// fade the pixels that have already been illuminated
for j in 0..<pixels {
if j != litPixel {
// this is a naughty hack but will work...
// we are playing with the internal implementation of
// iLEDFastColor, this needs exposing properly
buffer[Int(j)] = (buffer[Int(j)] / 3) & 0xff00
}
}
}
while true {
if forward {
for i in 0..<pixels {
buffer[Int(i)] = iLEDRed
fadeUnlitPixels(litPixel: i)
iLEDFastWriteBuffer()
delay(milliseconds: delayTime)
}
forward = false
} else {
for i in stride(from: pixels&-1, to: 0, by: -1) {
buffer[Int(i)] = iLEDRed
fadeUnlitPixels(litPixel: i)
iLEDFastWriteBuffer()
delay(milliseconds: delayTime)
}
forward = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment