'WC VACANT/OCCUPIED display with 8*8x8 modules MAX7218 matrix
'
' After starting, briefly shows your own IP address and
' the text "right", "left" and "center".
' Then it will display endlessly in the middle...
' - the current time for 3 seconds
' - for 2 seconds the text "WC VACANT" or "WC BUSY"
'   depending on the status of an external switch between pin 33 and GND.
' The pin is pulled up to 3V3 with PULLUP.
' If the cable to the switch is longer, pin 33 should should be pulled up more
' as a precautionary measure with a 1k resistor to 3V3.



WLOG "START"      ' Definition of some variables that should have certain values at start-up
VERSION$        = "2.0" 'TextVariable with the current program version
'DISPLAY_NUMBER = 10 'Number of 8*8 modules
DISPLAY_NUMBER  = 8 'Number of 8*8 modules
DISPLAY_PIN     = 15 'CS pin of the display
DISPLAY_PIXEL   = DISPLAY_NUMBER*8 'Number of horizontal pixels in the display
CHAR_PIXEL      = 6 'Number of pixels for a single character
POSITION        = 0 'Variable for the text position (starting from the right!)
SECONDS_COUNTER = 0 'Variable for loop counter
SWITCH_PIN      = 33 'Variable with the pin on which a switch to GND is attached
DOOR_STATUS     = 1 ' 1 = switch open, O = switch closed
OPEN            = 1 'Variable for status comparison when the switch is open
CLOSED          = 0 'Variable for status comparison when the switch is closed

TEXT_BUSY$      = "WC BUSY" 'Preset for busy text
TEXT_VACANT$    = "WC VACANT" 'Preset for vacant text


PIN.MODE SWITCH_PIN, INPUT, PULLUP 'Define a pin as an input for the switch
                              ' Pulls the PIN as an INPUT pin with a resistor to 3V3 level
                              ' The closed switch then pulls the pin to 0 volt level

'PIN.MODE SWITCH_PIN, INPUT, PULLDOWN 'If this line is not commented out,
                                      ' this simulates a closed switch with resistance on GND
                                      ' switches on the BUSY text as a test
WLOG "INIT Display ..."
MaxScroll.Setup DISPLAY_NUMBER,DISPLAY_PIN 'Initialize the display


' Briefly display the IP address when starting
'---------------------------------------- -------------
Text$      = Word$(IP$,1) 'The own IP address is the first word in the string IP$
TEXT_PIXEL = Len(TEXT$)*CHAR_PIXEL 'Number of horizontal pixels of this text
POSITION   = TEXT_PIXEL 'so that the right part of the address is always visible
GOSUB SHOW_THE_TEXT
PAUSE 1500


' Output text to the right
'------------------------------------------------- ----------------------------
TEXT$       = "right"
TEXT_PIXEL  = Len(TEXT$)*CHAR_PIXEL 'Number of horizontal pixels of this text
POSITION    = TEXT_PIXEL 'Position shifted to the left by text length
GOSUB SHOW_THE_TEXT
PAUSE 1000

' Output text on the left
'------------------------------------------------- -----------------------------
TEXT$       = "left"
TEXT_PIXEL  = Len(TEXT$)*CHAR_PIXEL 'Number of horizontal pixels for this text
POSITION    = DISPLAY_PIXEL 'Position is equal to number of display pixels
GOSUB SHOW_THE_TEXT
PAUSE 1000

' Output text centered
'------------------------------------------------ ----------------------------
TEXT$       = "center"
TEXT_PIXEL  = Len(TEXT$)*CHAR_PIXEL 'Number of horizontal pixels for this text
POSITION    = (DISPLAY_PIXEL/2)+(TEXT_PIXEL/2) 'Calculate the central output position
GOSUB SHOW_THE_TEXT
PAUSE 1000

' Now the Timer0 takes over the regular control once per second
'------------------------------------------------ ----------------------------
BREAK_TIME = 1000
TIMER0 BREAK_TIME, MAIN_ROUTINE 'The Timer0 now calls the subprgm endlessly every 1000ms

WAIT 'From now on only background processes will be processed;
' here is the timer0 and the ONHTMLRELOAD
END
' ################################################# ######################################
' ################################################# ######################################
' From here on there are only subprograms behind WAIT and END,
' which are called by their name from the script code above,
' and which have to end with RETURN.


' - - - - - - - S U B P R O G R A M S   - - - - - - - - - - - -


'================================================= =========================
MAIN_ROUTINE:
'================================================= =========================
'This subprgm is called once per second by TIMER0.
'The current time should be called up for the first three calls.
'The free or busy text should then be displayed for two seconds.
'To do this, the input pin is queried with the external switch.
SECONDS_COUNTER = SECONDS_COUNTER + 1' increase counter
SELECT CASE SECONDS_COUNTER
  CASE 1, 2, 3      'Second 1 to 3:
    TEXT$ = TIME$   'TEXT$ now contains the current time
  CASE 4 , 5        'Second 4 and 5
    DOOR_STATUS    = PIN(SWITCH_PIN) 'DOOR_STATUS is now 0 or 1, depending on switch status
    IF DOOR_STATUS = OPEN then TEXT$ = TEXT_VACANT$ 'FREE!!
    IF DOOR_STATUS = CLOSED then TEXT$ = TEXT_BUSY$ 'OCCUPY!!
END SELECT

SECONDS_COUNTER = SECONDS_COUNTER mod 5  'Set SECONDS_COUNTER AT 5 back to 0
GOSUB SHOW_THE_TEXT_CENTERED             'Show Text$
RETURN

'================================================= =========================
SHOW_THE_TEXT:
'================================================= =========================
MAXSCROLL.PRINT TEXT$   'here TEXT$ only goes into the buffer
MAXSCROLL.SHOW POSITION 'Only now will the buffer be displayed at the position
WLOG TEXT$              'In addition only for feedback in the development interface
RETURN                  'Back to the calling line

'================================================= =========================
SHOW_THE_TEXT_CENTERED:
'================================================= =========================
TEXT_PIXEL = Len(TEXT$)*CHAR_PIXEL            'Number of horizontal pixels for this text
POSITION   = (DISPLAY_PIXEL/2)+(TEXT_PIXEL/2) 'Calculate the central output position
GOSUB SHOW_THE_TEXT
RETURN