/*@ ensures
    @ (sb.getSensorsLength() > 0) ==>
    @ ((g_sensor.isEnabled()) ==>
 @ ((g_value >= 1488 && g_value <= 2232) ==> (g_value == packet.accelerometerY)));
    @*/
 private void setAccelerometerY(/*@non_null*/ final FormalInstancePacket packet) {
   if (sb.getSensorsLength() > 0) {
     final ISensor sensor = sb.getSensor(SensorIndexer.ACCEL_Y);
     // @ set g_sensor = sensor;
     if (sensor.isEnabled()) {
       sensor.measure();
       final short value = (short) sensor.getValue();
       // @ set g_value = value;
       if (value >= 1488 && value <= 2232) {
         packet.setAccelerometerY(value);
       }
     }
   }
 }
 /*@ ensures
 @ (sb.getSensorsLength() > 0) ==>
 @ ((g_sensor.isEnabled()) ==>
 @ ((g_value >= 310 && g_value <= 5585) ==> (g_value == packet.pressure)));
 @*/
 private void setPressure(/*@non_null*/ final FormalInstancePacket packet) {
   if (sb.getSensorsLength() > 0) {
     final ISensor sensor = sb.getSensor(SensorIndexer.PRESS);
     // @ set g_sensor = sensor;
     if (sensor.isEnabled()) {
       sensor.measure();
       final short value = (short) sensor.getValue();
       // @ set g_value = value;
       if (value >= 310 && value <= 5585) {
         packet.setPressure(value);
       }
     }
   }
 }
 /*@ ensures
 @ (sb.getSensorsLength() > 0) ==>
 @ ((g_sensor.isEnabled()) ==>
 @ ((g_value >= 0 && g_value <= 1000) ==> (g_value == packet.lightLevel)));
 @*/
 private void setLightLevel(/*@non_null*/ final FormalInstancePacket packet) {
   if (sb.getSensorsLength() > 0) {
     final ISensor sensor = sb.getSensor(SensorIndexer.LIGHT);
     // @ set g_sensor = sensor;
     if (sensor.isEnabled()) {
       sensor.measure();
       final short value = (short) sensor.getValue();
       // @ set g_value = value;
       if (value >= 0 && value <= 1000) {
         packet.setLightLevel(value);
       }
     }
   }
 }
  /*@ ensures
  @ (sb.getSensorsLength() > 0) ==>
  @ ((g_sensor.isEnabled()) ==>
  @ ((g_value >= -880 && g_value <= 2047) ==> (g_value == packet.temperature)));
  @*/
  private void setTemperature(/*@non_null*/ final FormalInstancePacket packet) {
    if (sb.getSensorsLength() > 0) {
      final ISensor sensor = sb.getSensor(SensorIndexer.THERM);
      // @ set g_sensor = sensor;

      if (sensor.isEnabled()) {
        sensor.measure();
        final short value = (short) sensor.getValue();
        // @ set g_value = value;
        if (value >= -880 && value <= 2047) {
          packet.setTemperature(value);
        }
      }
    }
  }
  private void readAudio(final int channel, /*@non_null@*/ final FormalInstancePacket packet) {

    for (int index = 0; index < Packet.FRAMES; index++) {
      boolean isValidChannel = channel >= 0 && channel < ab.getAudioArrayLength();
      boolean isValidIndex = index >= 0 && index < Packet.FRAMES;

      if (isValidChannel && isValidIndex) {
        final ISensor audio = ab.getAudio(channel);

        if (audio.isEnabled()) {
          Frame frame = packet.getFrame(index);

          if (frame instanceof FormalInstanceFrame) {
            FormalInstanceFrame audioFrame = (FormalInstanceFrame) frame;
            final char sample = (char) audio.getValue();
            audioFrame.setAudio(channel, sample);
          }
        }
      }
    }
  }