-\ Functions for managing GPIO pins
+\ Words for managing GPIO pins
\ User bank IO registers
$40014000 constant IO_BANK0_BASE
+$00000004 constant GPIO_CTRL
-: pins ( -- )
- $30 $00 DO I u. 4 +LOOP ;
+\ GPIO pin functions
+1 constant SPI0
+2 constant UART
+3 constant I2C
+4 constant PWM
+5 constant SIO
+6 constant PIO0
+7 constant PIO1
+9 constant USB
+31 constant NULL
-: pin-status ( -- )
- cr
- 30 0 DO
- I u.
- I 8 * IO_BANK0_BASE + @ hex. cr LOOP ;
+\ Given a pin #, leave its address on the stack
+: pin ( n -- addr ) 8 * IO_BANK0_BASE + ;
+
+\ Given a pin address, leave its selected function value on the stack
+\ example: 13 pin funcsel
+: funcsel ( addr -- n ) GPIO_CTRL + @ ;
+
+\ Given a pin address, set its function
+\ example: 13 pin SIO funcset
+: funcset ( addr n -- ) swap GPIO_CTRL + ! ;
+
+\ Display a list of all gpio pins and their current function
+: pins ( -- ) cr 30 0 DO I dup u. pin funcsel u. cr LOOP ;
+\ format codes
+0 constant PLAIN
+1 constant BRIGHT
+2 constant DIM
+3 constant ITALIC
+4 constant UNDERLINE
+5 constant BLINK
+7 constant REVERSE
+
+\ color codes
+0 constant BLACK
+1 constant RED
+2 constant GREEN
+3 constant YELLOW
+4 constant BLUE
+5 constant MAGENTA
+6 constant CYAN
+7 constant WHITE
+30 constant FG
+40 constant BG
+
+: WITH ( -- ) 59 emit ;
+
\ Emit escape codes
: ESC[ ( -- ) 27 emit 91 emit ;
+\ TUI mode, these need better names
+: TI ( -- ) ESC[ ." ?1047h" ; \ enter TUI mode
+: TE ( -- ) ESC[ ." ?1049l" ; \ exit TUI mode
+
+: at-xy ( column row -- ) 1+ swap 1+ swap ESC[ u. ." ;" u. ." H" ;
+: page ( -- ) ESC[ ." 2J" 0 0 at-xy ;
+
+: hide-cursor ( -- ) ESC[ ." ?25l" ;
+: show-cursor ( -- ) ESC[ ." ?25h" ;
+
+: cleanup ( -- ) page TE show-cursor ;
+
\ Movement
: [home] ( -- ) ESC[ ." H" ;
-: at-xy ( column row -- ) 1+ swap 1+ swap ESC[ u. ." ;" u. ." H" ;
\ Foreground colors
-: [color] ( n -- ) ESC[ u. ." m" ;
-: [black] ( -- ) 30 [color] ;
-: [red] ( -- ) 31 [color] ;
-: [green] ( -- ) 32 [color] ;
-: [yellow] ( -- ) 33 [color] ;
-: [blue] ( -- ) 34 [color] ;
-: [magenta] ( -- ) 35 [color] ;
-: [cyan] ( -- ) 36 [color] ;
+: [format] ( n -- ) ESC[ u. ." m" ;
+: [black] ( -- ) FG BLACK + [format] ;
+: [red] ( -- ) FG RED + [format] ;
+: [green] ( -- ) FG GREEN + [format] ;
+: [yellow] ( -- ) FG YELLOW + [format] ;
+: [blue] ( -- ) FG BLUE + [format] ;
+: [magenta] ( -- ) FG MAGENTA + [format] ;
+: [cyan] ( -- ) FG CYAN + [format] ;
\ Background colors
-: [black/bg] ( -- ) ESC[ ." 40m" ;
-: [red/bg] ( -- ) ESC[ ." 41m" ;
-: [green/bg] ( -- ) ESC[ ." 42m" ;
-: [yellow/bg] ( -- ) ESC[ ." 43m" ;
-: [blue/bg] ( -- ) ESC[ ." 44m" ;
-: [magenta/bg] ( -- ) ESC[ ." 45m" ;
-: [cyan/bg] ( -- ) ESC[ ." 46m" ;
+: [black/bg] ( -- ) BG BLACK + [format] ;
+: [red/bg] ( -- ) BG RED + [format] ;
+: [green/bg] ( -- ) BG GREEN + [format] ;
+: [yellow/bg] ( -- ) BG YELLOW + [format] ;
+: [blue/bg] ( -- ) BG BLUE + [format] ;
+: [magenta/bg] ( -- ) BG MAGENTA + [format] ;
+: [cyan/bg] ( -- ) BG CYAN + [format] ;
\ Attributes
-: [bold] ( -- ) ESC[ ." 1m" ;
-: [dim] ( -- ) ESC[ ." 2m" ;
-: [smso] ( -- ) ESC[ ." 3m" ;
-: [underline] ( -- ) ESC[ ." 4m" ;
-: [blink] ( -- ) ESC[ ." 5m" ;
-: [reverse] ( -- ) ESC[ ." 7m" ;
+: [bold] ( -- ) BOLD [format] ;
+: [dim] ( -- ) DIM [format] ;
+: [italic] ( -- ) ITALIC [format] ;
+: [underline] ( -- ) UNDERLINE [format] ;
+: [blink] ( -- ) BLINK [format] ;
+: [reverse] ( -- ) REVERSE [format] ;
\ Reset to default colors
: [reset] ( -- ) ESC[ ." 0m" ;
-: rainbow ( -- )
- [red] ." r"
- [yellow] ." a"
- [green] ." i"
- [blue] ." n"
- [cyan] ." b"
- [magenta] ." o"
- [red] ." w"
- [reset] ;
+: rainbow ( -- )
+ [red] ." r" [yellow] ." a" [green] ." i" [blue] ." n"
+ [cyan] ." b" [magenta] ." o" [red] ." w" [reset] ;