MolsonB Regular Member
 Joined: 25/01/2015 Location: CanadaPosts: 48 |
Posted: 04:25pm 27 May 2015 |
Copy link to clipboard |
 Print this post |
|
The new ADAFruits Dotstar LEDs are pretty easy to command. They use the SPI out.
https://learn.adafruit.com/adafruit-dotstar-leds/overview
Below is a simple program to display the LEDS and a few little displays. The way to handle the brightness, I took from their Arduino library.
OPTION AUTORUN ON
OPTION DEFAULT NONE
OPTION EXPLICIT
CONST RED = &HFF0000
CONST YELLOW = &HFFFF00
CONST GREEN = &H00FF00
CONST CYAN = &H00FFFF
CONST BLUE = &H0000FF
CONST MAGENTA = &HFF00FF
CONST WHITE = &HFFFFFF
CONST BLACK = &H000000
' Global variables
DIM INTEGER LedDashNum=30
DIM INTEGER LedDashColors(LedDashNum-1)
DIM STRING LedDashOrder = "GBR" LENGTH 3
DIM INTEGER LedBrightness '0 to 256 See function for details
'********************** NOTE - AMPS draw ********************
' 1 LED 30 LEDS 120 LEDS
'Off 0.0012 0.036 0.145
'White 0.048 1.430 * 2.170 *
'Red 0.021 0.625 2.050
'Green 0.018 0.550 1.740
'Blue 0.018 0.540 1.700
Dotstar.SetBrightness(128,0)
LED.ColorTest()
'LED.NightRider(10, BLACK, RED, 1)
'LED.HeadChaseTail(10, BLACK, Dotstar.PackColor(0,10,0), 5)
'LED.Alert(RED)
'****************** LED Programs *****************
SUB LED.ColorTest()
Dotstar.SetBrightness(255,0)
Dotstar.SetColors(LedDashColors(), LedDashNum, RED)
Dotstar.Show
Pause(2000)
Dotstar.SetColors(LedDashColors(), LedDashNum, GREEN)
Dotstar.Show
Pause(2000)
Dotstar.SetColors(LedDashColors(), LedDashNum, Dotstar.PackColor(0,0,255))
Dotstar.Show
Pause(2000)
Dotstar.SetColors(LedDashColors(), LedDashNum, WHITE)
Dotstar.Show
Pause(2000)
Dotstar.SetBrightness(200,1)
Pause(1000)
Dotstar.SetBrightness(150,1)
Pause(1000)
Dotstar.SetBrightness(100,1)
Pause(1000)
Dotstar.SetBrightness(50,1)
Pause(1000)
Dotstar.SetBrightness(1,1)
Pause(1000)
Dotstar.SetBrightness(0,1)
Pause(1000)
ENd SUB
'LEDS bounce from 1 end to the other and back
SUB LED.NightRider (length as INTEGER, foreground as INTEGER, background as INTEGER, speed as INTEGER)
LOCAL INTEGER x, head = 0, tail = 0, direction = 0
'Start with the pre-length size already set
Dotstar.SetColors(LedDashColors(), LedDashNum, background)
For x=0 TO length-1
Dotstar.SetColor(LedDashColors(), x, foreground)
Next x
head = length-1
tail = -1
DO
Dotstar.Show
Dotstar.SetColor(LedDashColors(), head, foreground)
If tail >= 0 Then Dotstar.SetColor(LedDashColors(), tail, background)
PAUSE(speed) 'Pause 20 milliseconds (~50 FPS)
If direction = 0 Then
head = head + 1 'Forward
tail = tail + 1
Else
head = head - 1 'Backwards
tail = tail - 1
End If
if head = LEDDashNum Then
direction = 1
head = tail - 1
tail = LEDDashNum - 1
Else if head = -1 Then
direction = 0
head = tail + 1
tail = 0
End If
Loop
End Sub
' Runs 10 LEDs at a time along strip, cycling through red, green and blue.
' This requires about 200 mA for all the 'on' pixels + 1 mA per 'off' pixel.
SUB LED.HeadChaseTail(length as INTEGER, foreground as INTEGER, background as INTEGER, speed as INTEGER)
LOCAL INTEGER head = length, tail = 0
DO:
Dotstar.SetColor(LedDashColors(), head, foreground)
Dotstar.SetColor(LedDashColors(), tail, background)
Dotstar.Show 'Refresh strip
PAUSE(speed) 'Pause 20 milliseconds (~50 FPS)
head = (head + 1) MOD LedDashNum ' Increment head index. Off end of strip?
if head = 0 Then
If foreground = BLACK OR foreground = WHITE Then
foreground = RED
Else If foreground = RED Then
foreground = YELLOW
Else If foreground = YELLOW Then
foreground = GREEN
Else If foreground = GREEN Then
foreground = CYAN
Else If foreground = CYAN Then
foreground = BLUE
Else If foreground = BLUE Then
foreground = MAGENTA
Else If foreground = MAGENTA Then
foreground = WHITE
Else If foreground = WHITE Then
foreground = RED
End If
End IF
tail = (tail + 1) MOD LedDashNum 'Increment, reset tail index
LOOP
End Sub
SUB LED.Alert (foreground as INTEGER)
Dotstar.SetColors(LedDashColors(), LedDashNum, foreground)
DO
Dotstar.SetBrightness(255,1)
Pause(300)
Dotstar.SetBrightness(0,1)
Pause(100)
LOOP
End Sub
'****************** LED Code *****************
'Converts seperate RGB values into Hex notation
FUNCTION Dotstar.PackColor(redness as INTEGER, greenness as INTEGER, blueness as INTEGER) AS INTEGER
Dotstar.PackColor = ((redness and &HFF)<<16) + ((greenness and &HFF)<<8) + (blueness and &HFF)
END FUNCTION
SUB Dotstar.SetBrightness(brightness AS INTEGER, refresh as INTEGER)
' Stored brightness value is different than what's passed. This optimizes the actual scaling math later, allowing a fast 8x8-bit
' multiply and taking the MSB. Adding 1 here will intentionally roll over...so 0 = max brightness (color values are taken literally, no math),
'1 = min brightness (off), 255 = just below max brightness.
LedBrightness = (brightness + 1) MOD 256
If refresh = 1 THEN Dotstar.Show()
END SUB
FUNCTION Dotstar.GetBrightness() As INTEGER
Dotstar.GetBrightness = LEDBrightness - 1
END FUNCTION
'Set solid color lights
SUB Dotstar.SetColors(leds() as INTEGER, lednums as INTEGER, colors as INTEGER)
Local INTEGER x
FOR x=0 TO lednums-1
Dotstar.SetColor(leds(), x, colors)
NEXT x
End Sub
' Load RGB color into array
Sub Dotstar.SetColor(leds() as INTEGER, lednum as INTEGER, colors as INTEGER)
leds(lednum) = colors
end sub
'Sends SPI info to all LEDS
Sub Dotstar.Show()
LOCAL INTEGER LedDash(LedDashNum), LedUpper(LedUpperNum)
Dotstar.ProcessArray(LedDashColors(), LedDashNum, LedDashOrder, LedDash())
SPI OPEN 5000000, 3, 32 '5MHZ, Mode3, 32bits
SPI WRite 1, &H00000000
SPI WRITE LedDashNum, LedDash()
SPI WRITE 1, &HFFFFFFFF
SPI CLOSE
End SUb
SUB Dotstar.ProcessArray(leds() as INTEGER, lednums as INTEGER, order as String LENGTH 3, myArr() as INTEGER)
Local INTEGER x, redness, greenness, blueness
FOR x=0 TO lednums-1
redness = (leds(x) >> 16) and &HFF
greenness = (leds(x) >> 8) and &HFF
blueness = leds(x) and &HFF
If LEDBrightness > 0 Then '(color * brightness) /256
redness = (redness * LEDBrightness) >> 8
greenness = (greenness * LEDBrightness) >> 8
blueness = (blueness * LEDBrightness) >> 8
End If
If order = "GBR" Then
myArr(x) = (&HFF<<24) + ((greenness and &HFF ) <<16) + ((blueness and &HFF) <<8) + (redness and &HFF)
Else 'BGR
myArr(x) = (&HFF<<24) + ((blueness and &HFF ) <<16) + ((greenness and &HFF) <<8) + (redness and &HFF)
End If
NEXT x
End SUB
Edited by MolsonB 2015-05-29 MkII 44pin - v5.0
ColorMax 2 - v4.5 |