TACHYON V4 IFDEF sw_1.fth @rest org FORGET sw_1.fth } pub sw_1.fth ." switch 2017/06/14 9:49:19 " ; @org W@ == @rest --- remember { 2017/06/14 9:48:50 Translated sw to Tachyon 3V3 | 10kohm | P0 -------| | sw | GND |\| P1 ----| | ----220ohm---GND |/| LED } \ =========================================================================== \ Constants \ =========================================================================== #P0 == swpin #P1 == ledpin #800000 == 10msec swpin MASK == swMask ledpin MASK == ledMask \ =========================================================================== \ Variables \ =========================================================================== long lastDebounceTime byte swState byte debounce byte lastswState byte alternate \ =========================================================================== \ Main \ =========================================================================== \ read current sw-state \ ( -- n1 ) n1:True[sw:off] False[sw:on] : rd_sw INA COG@ swMask AND IF 1 ELSE 0 ENDIF ; : init \ Set initial values 0 lastDebounceTime ! 1 swState C! 0 debounce C! 1 lastswState C! 1 alternate C! ; \ On/Off switch \ LED on when pushed sw, LED off when release sw \ ( -- ) : sw1 init BEGIN \ Read current sw and check if pushed or released rd_sw DUP lastswState C@ <> ( 1/0 1/0 ) IF \ If sw is under debouncong debounce C@ 0= IF CNT@ lastDebounceTime ! 1 debounce C! ENDIF ELSE 0 debounce C! ENDIF debounce C@ ( 1/0 ) IF CNT@ lastDebounceTime @ - 10msec > IF \ Update current swState and lastswState DUP swState C! lastswState C! ELSE DROP ENDIF ELSE DROP ENDIF ( -- ) \ Activate LED when sw-on ledMask swState C@ IF OUTCLR ELSE OUTSET ENDIF KEY \ Break loop if hitting any key UNTIL ; \ Alternative switch \ Switching LED on/off when pushed sw \ ( -- ) : sw2 init BEGIN \ Read current sw and check if pushed or released rd_sw DUP lastswState C@ <> ( 1/0 1/0 ) IF \ If sw is under debouncong debounce C@ 0= IF CNT@ lastDebounceTime ! 1 debounce C! ENDIF ELSE 0 debounce C! ( 1/0 ) ENDIF debounce C@ IF CNT@ lastDebounceTime @ - 10msec > IF \ Treat only when pushed switch lastswState C@ IF lastswState C! \ Update current lastswState alternate C@ IF 0 ELSE 1 ENDIF swState C! \ Set swState alternate C@ IF 0 ELSE 1 ENDIF alternate C! \ set alternate ELSE lastswState C! \ Update current lastswState ENDIF ELSE DROP ENDIF ELSE DROP ENDIF ( -- ) \ Activate LED when sw-on ledMask swState C@ IF OUTCLR ELSE OUTSET ENDIF KEY \ Break loop if hitting any key UNTIL ; END \ ?BACKUP