zeroflag / punyforth

Forth inspired programming language for the ESP8266
Other
412 stars 43 forks source link

Just for fun #48

Open Jos-Ven opened 5 years ago

Jos-Ven commented 5 years ago

marker: LedsStrip \ Testing an APA201 ledstrip connected through SPI

\ SPI connections: \ Name ESP8266 APA102 ledstrip \ MISO = GPIO 12 \ NOT USED \ MOSI = GPIO 13 --> DAT \ CLK = GPIO 14 --> CLK

\ Note for the ESP8266 ESP-12-F: \ Do not use the SPI connections \ at GPIO22 GPIO25 and GPIO24 !

4 constant: #Leds \ Number of used leds 0 constant: false -1 constant: true 1 constant: spi_word_size_8bit 1 constant: bus 8 constant: buffer_size 224 constant: PrefixCmd

: .s ( - ) stack-print ; : tp ( - ) cr .s cr ; : off ( addr -- ) false swap ! ; : on ( addr -- ) true swap ! ; : 4dup ( n1 n2 n3 n4 - n1 n2 n3 n4 n1 n2 n3 n4 ) 2over 2over ; : freq ( divider count -- freq ) 16 lshift swap 65535 and or ;

: .c ( n - ) dup 10 < if space space . else dup 100 < if space . else . then then space ;

: .c@ ( adr - ) c@ .c ;

1 10 freq constant: spi_freq_div_4m

: init-spi ( - flag ) TRUE 0 ( little endian ) TRUE spi_freq_div_4m 0 ( SPI_MODE0 ) bus spi-init dup 0= if print: " init-spi FAILED " then ;

struct 1 field: LedCmd \ For 0xE0 and 31 levels of brightness 1 field: LedBlue 1 field: LedGreen 1 field: LedRed constant: /ledframe

Leds 2 + /ledframe * constant: /ledstrip

create: &ledstrip /ledstrip allot

: initApa201 ( - flag ) cr print: " Start>" init-spi ; : >led ( led# - adr ) 1+ /ledframe * &ledstrip + ;

: >led! ( Red Green Blue Cmdlevel led# - )

led dup >r c! r@ 1+ c! r@ 2 + c! r> 3 + c! ;

: DumpStrip ( - ) cr print: " # Level red green blue"

Leds 0

   do   cr i  .c space i >led
        dup     c@ 15 and .c
        dup 3 + .c@
        dup 2 + .c@
        1+      .c@
   loop    ;

: SetCmd ( Brightness - LedCmd ) PrefixCmd or ; : ResetStartFrame ( - ) &ledstrip off ; : SetEndFrame ( - ) #Leds 1+ /ledframe * &ledstrip + 240 over c! 255 swap 1+ c! ;

: .Strip ( - ) \ Writes to the ledstrip over the SPI connection spi_word_size_8bit /ledstrip 0 ( ignore output ) &ledstrip bus spi-send drop ;

: FillLeds ( red green blue brightness #Leds FirstLed# - ) ResetStartFrame >r >r SetCmd r> r> do 4dup i >led! loop 2drop 2drop SetEndFrame ;

: StripRed ( - ) 1 0 0 10 #Leds 0 FillLeds ; : StripBlue ( - ) 0 0 1 10 #Leds 0 FillLeds ;

: .BlueRed ( - ) initApa201 if 10 0 do StripBlue .Strip 500 ms StripRed .Strip 500 ms loop then ;

.BlueRed

\ End, I leave the fancy show to you.