
\ Darkness measurement on Icebreaker

\ -----------------------------------------------------------------------------

\ These registers are for PMOD1B.
\ For PMOD1A use $101, $102, $104 instead.

$201 constant P2IN
$202 constant P2OUT
$204 constant P2DIR

\ -----------------------------------------------------------------------------

\ Connections of four LEDs to PMOD1B pins:

\ Pin 7 | A1 A2 A3 A4 (GND) (3.3) | Pin 12
\ Pin 1 | C1 C2 C3 C4 (GND) (3.3) | Pin 6

   1 constant cathode-1
  16 constant anode-1

   2 constant cathode-2
  32 constant anode-2

   4 constant cathode-3
  64 constant anode-3

   8 constant cathode-4
 128 constant anode-4

\ -----------------------------------------------------------------------------

: darkness-1 ( -- ud )

  anode-1 cathode-1 or P2DIR io!  \ Both anode and cathode outputs
  anode-1              P2OUT io!  \ Anode high, let it shine
     1 ms cathode-1    P2OUT io!  \ After a short flash, flip cathode high and anode low

     10 0 do loop \ Wait a few microseconds for junction capacitance to charge

          cathode-1    P2DIR io!  \ Anode input

  0.

  begin 1. d+ P2IN io@ anode-1 and until \ Wait until anode goes high, count loop runs
;

\ -----------------------------------------------------------------------------

: darkness-2 ( -- ud )

  anode-2 cathode-2 or P2DIR io!  \ Both anode and cathode outputs
  anode-2              P2OUT io!  \ Anode high, let it shine
     1 ms cathode-2    P2OUT io!  \ After a short flash, flip cathode high and anode low

     10 0 do loop \ Wait a few microseconds for junction capacitance to charge

          cathode-2   P2DIR io!  \ Anode input

  0.

  begin 1. d+ P2IN io@ anode-2 and until \ Wait until anode goes high, count loop runs
;

\ -----------------------------------------------------------------------------

: darkness-3 ( -- ud )

  anode-3 cathode-3 or P2DIR io!  \ Both anode and cathode outputs
  anode-3              P2OUT io!  \ Anode high, let it shine
     1 ms cathode-3    P2OUT io!  \ After a short flash, flip cathode high and anode low

     10 0 do loop \ Wait a few microseconds for junction capacitance to charge

          cathode-3   P2DIR io!  \ Anode input

  0.

  begin 1. d+ P2IN io@ anode-3 and until \ Wait until anode goes high, count loop runs
;

\ -----------------------------------------------------------------------------

: darkness-4 ( -- ud )

  anode-4 cathode-4 or P2DIR io!  \ Both anode and cathode outputs
  anode-4              P2OUT io!  \ Anode high, let it shine
     1 ms cathode-4    P2OUT io!  \ After a short flash, flip cathode high and anode low

     10 0 do loop \ Wait a few microseconds for junction capacitance to charge

           cathode-4   P2DIR io!  \ Anode input

  0.

  begin 1. d+ P2IN io@ anode-4 and until \ Wait until anode goes high, count loop runs
;

\ -----------------------------------------------------------------------------

: esc? ( -- ? ) key? if key 27 = else false then ;

: darkness ( -- )
  begin
    cr
    darkness-1 16 d.r
    darkness-2 16 d.r
    darkness-3 16 d.r
    darkness-4 16 d.r
  esc? until
;

darkness
