Latching Relays from Momentary Inputs

Written by Kevin Cloutier on Oct 21, 2020 2:12 pm

This application will monitor the digital inputs.  The corresponding output is set when an input pulsed.  That output remains active until a different input is pulsed.  This application effectively latches the output to represent the last input that transitioned from low to high.

package com.integ.latchrelays;

import com.integpg.system.IoEvent;
import com.integpg.system.Iolog;
import com.integpg.system.JANOS;
import java.io.IOException;
import java.util.Date;

/**
 * This application will monitor the digital inputs. The corresponding output is set
 * when an input pulsed. That output remains active until a different input is pulsed.
 * This application effectively latches the output to represent the last input that
 * transitioned from low to high.
 */
public class LatchRelaysMain {

    public static void main(String[] args) throws InterruptedException, IOException {

        // create an Iolog instance and a timestamp representing the last time an 
        // event occurred.  we will start with a value of zero indicating only new events
        Iolog iolog = new Iolog();
        long timestamp = 0;

        // loop forever
        while (true) {

            // refresh the oilog with the timestamp of the last input event
            iolog.refresh(timestamp);
            IoEvent[] inputEvents = iolog.getInputEvents();

            // only process if there are events
            if (0 != inputEvents.length) {
                System.out.println("inputEvents.length = " + inputEvents.length);

                // loop through the input events
                for (int i = 0; i < inputEvents.length; i++) {
                    IoEvent inputEvent = inputEvents[i];
                    timestamp = inputEvent.timestamp;
                    System.out.println("timestamp = " + new Date(timestamp));
                    int highTransitions = inputEvent.mask & inputEvent.states;
                    boolean isTransitionHigh = (0 != highTransitions);
                    System.out.println("isTransitionHigh = " + isTransitionHigh);

                    // if the event was a transition high then set the outputs to 
                    // represent the state of the inputs that transitioned from 
                    // low to high.  we will use all outputs here
                    if (isTransitionHigh) {
                        JANOS.setOutputStates(highTransitions, 0xff);

                        // we are only looking to process the most recent inpput 
                        // transition from low to high.  so once we find one we 
                        // can abort the loop.
                        break;
                    }
                }
            }

            // sleep for a little bit of time to not monopolize the CPU
            Thread.sleep(50);
        }

    }

}
On this page