Building Intelligent, Web-based Control Systems
by Tom Milligan and Steve Coffin


Example 1: 
emitjri emJri = new emitjri(4, 11, "Sprinkler1");


Example 2: 
waterOnOff.setJriVariable("Water", "ActionEvent");


Example 3:
(a)
emJri.setJriServer(this);
emJri.setJSJriLink("Hrs");
emJri.setJSJriLink("Min");
emJri.setJSJriLink("Sec");

(b)
public void notifyChange(String variable, Object value) {
    if( variable.equals("Sec")) {
        seconds = ((Integer)value).intValue();
        timeDisplay.setSeconds(seconds);
    }
    else if( variable.equals("Hrs") ) {
        hours = ((Integer)value).intValue();     
        set_hours();
    }
    else if( variable.equals("Min")) {
        minutes = ((Integer)value).intValue();
        timeDisplay.setMinutes(minutes); 
    }
}

(c)
timeDisplay.setJriLink("Sec", "Seconds");


Listing One
;***************************************************************************
;************************* Timer0 Interrupt Handler ************************
;***************************************************************************
; emWare doesn't use or require the use of the timer interrupt.
; System clock is 18.432Mhz
; One machine cycle is 1/12 system cycle or .65104us
; Timer 0 counts up once every machine cycle.
; Timer will fire an interrupt when it counts up and rolls over to 0.
; We want a  10 ms interrupt rate .010/.65104 = 15360 machine cycles.
;                                             = 3c00 hex
;                                             10000 - c300 = c400
; So t0hi = 0xc3 and t0lo = 0
INT_T0          push    PSW                
                push    ACC                
                mov     TH0,  #t0hi   ;Load timer reload register
                mov     A, #t0low          
                add     A, TL0        ;Account for interrupt latency
                mov     TL0, A        ;by adding preload value current value
                setb    tenMSbit      ;set this bit once every 10ms
                setb    GreenLED      ;Turn off green LED
                pop     ACC
                pop     PSW
                reti                   ;Return from Interrupt


Listing Two
;********************************* Real Time Clock ***********************
;Once every 10ms the timer ISR sets the tenMSbit.
;use this routine to update the real time clock
;
RealTimeClock
              jnb   tenMSbit,RTCdone     ;Just exit if temMSbit isn't set
              clr   tenMSbit             ;only count the tick once
              inc   Hundredths           ;100 ticks to 1 second
              clr   A
              mov   R1, Hundredths
              cjne  R1, #100, RTCdone    ;Has one second elapsed?
              mov   Hundredths, A
              inc   Seconds              ;Yes increment seconds
              setb  waterbit             ;
              setb  ManSecBit
              mov   R1, Seconds
              clr   GreenLED             ;Turn on green LED
                                         ;ISR will turn it off in 10ms
              cjne  R1, #60, RTCdone     ;Has one minute elapsed?
              mov   Seconds, A
              inc   Minutes              ;Yes, increment minutes
              mov   R1, Skip
              cjne  R1, #0, RTCskp       ;Don't check if Skip is not zero
              setb  CheckWater           ;See if it is time to start watering
RTCskp        mov   R1, Minutes
              cjne  R1, #60, RTCdone     ;Has one hour expired?
              mov   Minutes, A
              inc   Hours                ;Yes, increment hours
              mov   R1, Hours
              cjne  R1, #24, RTCdone     ;Has one Day expired?
              mov   A, Skip
              jz    RTCnoskip            ;is the skip watering variable zero?
              dec   Skip                 ;decrement skip-watering variable
              clr   A
RTCnoskip     mov   Hours, A
              inc   Days
              mov   R1, Days             ;Has one week expired
              cjne  R1, #7, RTCmod
              mov   Days, A
RTCmod
              inc   DayMod               ;Increment modulus 120 day counter
              mov   R1, DayMod           ;Have 120 days expired?
              cjne  R1, #120, RTCdone
              mov   DayMod, A
RTCdone
              ret


Listing Three
;*** variable table send in response to GetVarSymbols request from host ****
EMvartable      .byte  37                 ;= # of variables
                .byte  0                  ; ATTRIBUTE byte for each variable
                .byte  BYTETYPE+READWRITE ; TYPE byte for each variable 
                .byte  0
                .byte  BYTETYPE+READWRITE   
                .byte  0
                .byte  BYTETYPE+READWRITE   
                    . . .
                .text "water\000"                
                .text "DayMod\000"                
                .text "Days\000"   ;Null terminated string for each variable
                .text "Hrs\000"                
                .text "Min\000"                
                .text "Sec\000"
                    . . .

Listing Four
;************************** Main Idle Loop *******************************
; Cooperative multitasking main calling loop. Each process should release the 
; processor whenever possible. No stalling allowed. User and em processes
; are called from here. On processors with more than 2K program memory, the 
; acall's should be replaced with lcalls. emMicroEntry must be called 
; once per character tx time (at 9600 baud this is approx once per 960us )

main
         acall   emMicroEntry  ;Emware app layer also calls Link layer
         acall   debug         ;general purpose debugging stuff
         acall   RealTimeClock ;Update the RTC when necessary
         acall   Water         ;Water the grass when appropriate
         acall   CheckTime     ;Check if its appropriate to water
         acall   ManualCntrl   ;allow for manual control
         ajmp    main          ;loop forever

;********************** Main Idle Loop End *******************************


Listing Five
    // Select 24 hour clock mode
void hr24RadioButton_Action(Event event) {
    // get the value of the embedded variable "State"
      Object object = emJri.getJSJriVariable("State"); 
          int state;
        // disable am/pm buttons
          amRadioButton.enable(false); 
      pmRadioButton.enable(false);
      isClock12 = false;
      state = ((Integer)object).intValue();
/* instead of using a separate variable (wasted resource) to hold the clock 
   mode set bit 0 of the "State" variable to indicate 24 hour clock mode.  */
emJri.setJSJriVariable("State", 
new Integer(state | 0x0001));
      set_hours(); // update clock
    }
    // change the clock to am
    void amRadioButton_Action(Event event) {
/* reset the embedded variable "Hrs." Device runs in 24-hour mode so simply 
subtract 12 from the current hour. Since there is a JriLink to this 
variable "notifyChange" will be called and complete the clock update */
        emJri.setJSJriVariable("Hrs", new Integer(hours-12));
    }
    // set the current day
    void dayChoice_Action(Event event) {
            int day = dayChoice.getSelectedIndex();
/* set the current day from 0 - 6 depending on the selected choice item */
            emJri.setJSJriVariable("Days", new Integer(day));
            dayLabel.setText(dayNames[day]);
    }

4


