'**************************************************************
'* 20x2 controls one 60 LED Dotstar APA102 LED strip with HSPI.
'* Uses Macros; and an IR "clicker" to set the LED brightness
'* level,the background color of the 60 LED strip, and the
'* address and color of individual LEDs.
'* SEE (clicker key functions) at the end of the code.
'* NOTE: Program is now setup to use scratchpad memory. This
'* can be changed to use EEPROM by replacing the commands
'* ( PUT to WRITE and GET to READ ).
'***************************************************************
; 20X2 +5V -.- Light strip
; .-----__-----. |
; | | `--> RED +V
' | B.7 |11---> SCK --------> YEL CK
; | C.1 |9----> SDO --------> GRN DI
; | | .--> BLK 0V
; | | 0V _|_ (Colors are for my LED strip)
' | |
' | C.2 |8<----- CLICKER
' | B.1 |17----> OLED
' | B.0 |18----> SPEAKER
' .------------.
Symbol CLICKER = C.2 'Pin 8; Clicker will select routine or interrupt.
Symbol OLED = B.1 'Pin 17.
symbol speaker = B.0 'Pin 1.
symbol irdata = b0
symbol MemAddr = b1
symbol red = b2
symbol green = b3
symbol blue = b4
symbol color = b7
symbol counter = b8
symbol value = b9
symbol brightness = b10
symbol LEDAddr = b11
Symbol HOW_MANY_LEDS = 60 '* Number of LED modules in the strip.
;** Initialise the HSPI interface
#macro Init()
hspisetup spimode00, spifast
#endmacro
;** Send the start of data header
#macro head()
sendPacket( $00, $00, $00, $00 )
#endmacro
;** Send the end of data tail
#macro tail()
sendPacket( $FF, $FF, $FF, $FF )
#endmacro
;** Send a four byte packet out via HSPI
#macro sendPacket( n1, n2, n3, n4 )
hspiout( n1, n2, n3, n4 )
#endmacro
;** Send a LED controlling command
#macro send( red, green, blue )
sendPacket( $FF, green,blue, red )
#endmacro
;** Turns all LED modules off.
#macro AllOff
head ; Turn all LED modules off
for b0 = 1 To HOW_MANY_LEDS
send(0,0,0)
next
tail ; Send the end of data tail.
#endmacro
#picaxe 20x2
pause 1000
serout oled,n2400,(254,1):pause 30 'Clear OLED.
Init 'Macro initializes the HSPI interface.
AllOff 'Macro turns all LEDS OFF.
Main:
serout oled,n2400,(254,1):pause 30
serout oled,n2400,(254,128,"Use Clicker ")
pause 500
do
call SetBrightness
call FillMemory 'Fills memory with background color
call SendData 'Sends background color to LED strip.
call GetLEDAddr 'Get data for individual LEDs.
call SendData 'Sends data to all LEDs.
serout oled,n2400,(254,1):pause 30
loop
'* Sets LED brightness level from 0 to 9.
SetBrightness:
serout oled,n2400,(254,1):pause 30
serout oled,n2400,(254,128,"Set Brightness:_") 'Positions the underscore for data entry.
serout oled,n2400,(254,143) 'Places cursor.
call One_Digit
let Brightness = IRData
pause 50
return
'* Fill Scratchpad/EEPROM memory with background color.
FillMemory:
serout oled,n2400,(254,128," Set Background ")
serout oled,n2400,(254,192," Color: _ ")
serout oled,n2400,(254,203) 'Places cursor.
call One_Digit
let MemAddr =0
for MemAddr = 0 to HOW_MANY_LEDS
put MemAddr,IrData
next
return
'* Send data to LED strip.
SendData:
let MemAddr = 0
head ;* Send the start of data header.
for MemAddr = 0 to HOW_MANY_LEDS
get MemAddr,b0 'value
red = bit0 *Brightness
green = bit1 *Brightness
blue = bit2 *Brightness
send(red,green,blue)
'sertxd (red,green,blue,cr,lf) 'Use for testing.
next MemAddr
tail ;* Send the end of data tail.
return
'* Use "clicker' to set beginning memory address for colors.
GetLEDAddr:
serout oled,n2400,(254,1):pause 30
serout oled,n2400,(254,128," LED Addr: __ ") 'Positions the underscore.
serout oled,n2400,(254,139) 'Places cursor.
let color = value
call two_digits
let LEDAddr = value
'* Use "clicker" to enter colors. Loop here until
'* clicker "power" code (21), or other non-numeric
'* key is pushed (see NOTE at end of code).
GetColor:
serout oled,n2400,(254,192," Color: _ ") 'Positions the underscore.
serout oled,n2400,(254,200) 'Places cursor.
do
let value = 0
call one_digit
if irdata >9 then goto GetLEDAddr 'Non-Numeric key, see NOTE at end of code.
put LEDAddr,IrData
call SendData
inc LEDAddr
serout oled,n2400,(254,128," LED Addr: ",#LEDAddr," ")
serout oled,n2400,(254,192,"Enter Color: _ ") 'Position the underscore.
serout oled,n2400,(254,205) 'Places cursor.
loop
return
'* Get the 10's digit.
two_digits:
let value = 0
call GetIR 'Use "clicker" to enter data.
value=irdata*10+value '10's digit is "x10".
serout oled,n2400,(#irdata):pause 10 'Places cursor.
'* Get the units digit.
one_digit:
call GetIR 'Use "clicker" to enter data.
value=irdata*1+value '1's digit is "x1".
serout oled,n2400,(#irdata):pause 10 'Places cursor.
pause 50
return
'*Subroutine reads data from IR Remote "clicker" & Corrects numeric key offset.
GetIR:
irin clicker,irdata 'Read data from IR clicker.
if irdata = 21 then reset:endif '"POWER" key resets & starts over.
do:let b5=0:count C.2,80,b5:loop until b5=0 'Loop until key is released.
if irdata<10 then let irdata=irdata+1//10:endif 'Corrects numeric key offset.
return
' CLICKER KEY FUNCTIONS
' KEY CODE FUNCTION
'Power 21 Reset chip & start over
' XX XX See NOTE.
' 0 000 LED OFF
' 1 001 LED Red
' 2 010 LED Green
' 3 011 LED Yellow
' 4 100 LED Blue
' 5 101 LED Purple
' 6 110 LED Light Blue
' 7 111 LED White
' 8 000 LED OFF
' 9 001 LED Red
' NOTE: Any non-numeric key except 21 goes to
' enter new LED Address.