Device Driver

In the generated code, you hava a project called “device drivers”, this is where the access to the hardware happens. If you hava a look at the classes you can see that for every modeled sensor or actuator a corresponding class was generated. They all have an activate(), deactivate() and getData() method. The actuators also have an setData() method.

For the access to the hardware we use the OpenJDK Device I/O, which leverages standard Java ME Device I/O APIs to Java SE. Good examples and an introduction are found in the Kura documentation.

   @Override
   public void activate() throws IOException {
       //DA-START:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.activate:DA-START
       try {
           GPIOPinConfig pinConfig = new GPIOPinConfig(0, setup.getConnection().getPinNumber(), GPIOPinConfig.DIR_OUTPUT_ONLY, GPIOPinConfig.DEFAULT, GPIOPinConfig.TRIGGER_BOTH_EDGES, false);
           pin = DeviceManager.open(GPIOPin.class, pinConfig);
           pin.setInputListener(new PinListener() {

                   @Override
                   public void valueChanged(PinEvent pinEvent) {
                   DigitalGroveActuatorData data = new DigitalGroveActuatorData();
                       data.setData(new DigitalGroveActuatorStatusType(pinEvent.getValue()));
                       interruptCallback.onInterrupt(data);
                   }
           });
       } catch (IOException e) {
           throw new DigitalFaultException(e);
       }
       logger.debug("activated digital actuator");
       //DA-ELSE:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.activate:DA-ELSE
       // TODO add code here to activate the hardware 
       //DA-END:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.activate:DA-END
   }

   @Override
   public void deactivate() throws IOException {
       //DA-START:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.deactivate:DA-START
       try {
           pin.close();
       } catch (IOException e) {
           throw new DigitalFaultException(e);
       }
       pin = null;
       logger.debug("deactivated digital actuator");
       //DA-ELSE:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.deactivate:DA-ELSE
       // TODO add code here to deactivate the hardware 
       //DA-END:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.deactivate:DA-END
   }

   @Override
   public synchronized DigitalGroveActuatorData getData() throws IOException {
       //DA-START:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.getData.DigitalGroveActuatorData:DA-START
       try {
           DigitalGroveActuatorData data = new DigitalGroveActuatorData();
           data.setData(new DigitalGroveActuatorStatusType(pin.getValue()));
           return data;
       } catch (IOException e) {
           throw new DigitalFaultException(e);
       }
       //DA-ELSE:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.getData.DigitalGroveActuatorData:DA-ELSE
       // TODO add code here to get the sensor data and return it 
       //return null;
       //DA-END:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.getData.DigitalGroveActuatorData:DA-END
   }

   public synchronized void setData(DigitalGroveActuatorData data) throws IOException {
       //DA-START:org.gs.iot.sample.monitoring.gateway.driver.DigitalGroveActuatorDeviceDriver.setData.DigitalGroveActuatorData:DA-START
       try {
           pin.setValue(data.getData().isStatus());
           interruptCallback.onInterrupt(data);
       } catch (IOException e) {
           throw new DigitalFaultException(e);
       }
   }

For access to the I2C Bus and the ADC hava a look at the source code.