📄 onewirecontainer20.java
字号:
* Detects if this device has seen a Power-On-Reset (POR). If this has * occured it may be necessary to set the state of the device to the * desired values. The register buffer is retrieved from the * <CODE>readDevice()</CODE> method. * * @param state current state of the device * returned from <CODE>readDevice()</CODE> * * @return <CODE>false</CODE> if output is conducting to ground and * <CODE>true</CODE> if not conducting */ public boolean getDevicePOR (byte[] state) { return (Bit.arrayReadBit(7, 1, state) == 1); } /** * Extracts the state of the external power indicator from the provided * register buffer. Use 'setPower' to set or clear the external power * indicator flag. The register buffer is retrieved from the * <CODE>readDevice()</CODE> method. * * @param state current state of the * device returned from <CODE>readDevice()</CODE> * * @return <CODE>true</CODE> if set to external power operation */ public boolean isPowerExternal (byte[] state) { return (state [EXPOWER_OFFSET] != 0); } //-------- //-------- A/D 'set' Methods //-------- /** * Sets the alarm voltage value of the specified channel in the * provided state buffer. The state buffer is retrieved from the * <CODE>readDevice()</CODE> method. The method <CODE>writeDevice()</CODE> * must be called to finalize these changes to the device. Note that * multiple 'set' methods can be called before one call to * <CODE>writeDevice()</CODE>. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param alarmType desired alarm, <CODE>ALARM_HIGH (1) * or ALARM_LOW (0)</CODE> * @param alarm alarm value (will be reduced to 8 bit resolution) * @param state current state of this * device returned from <CODE>readDevice()</CODE> * * @throws IllegalArgumentException Invalid channel number passed */ public void setADAlarm (int channel, int alarmType, double alarm, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); int offset = ALARM_OFFSET + channel * 2 + alarmType; state [offset] = ( byte ) ((voltageToInt(alarm, getADRange(channel, state)) >>> 8) & 0x00FF); // set bitmap field to indicate this register has changed Bit.arrayWriteBit(1, offset, BITMAP_OFFSET, state); } /** * Sets the alarm enable value of the specified channel in the * provided state buffer. The state buffer is retrieved from the * <CODE>readDevice()</CODE> method. The method <CODE>writeDevice()</CODE> * must be called to finalize these changes to the device. Note that * multiple 'set' methods can be called before one call to * <CODE>writeDevice()</CODE>. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param alarmType desired alarm, <CODE>ALARM_HIGH (1) * or ALARM_LOW (0)</CODE> * @param alarmEnable alarm enable value * @param state current state of this * device returned from <CODE>readDevice()</CODE> * * @throws IllegalArgumentException Invalid channel number passed */ public void setADAlarmEnable (int channel, int alarmType, boolean alarmEnable, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); // change alarm enable Bit.arrayWriteBit(((alarmEnable) ? 1 : 0), 2 + alarmType, channel * 2 + 1, state); // set bitmap field to indicate this register has changed Bit.arrayWriteBit(1, channel * 2 + 1, BITMAP_OFFSET, state); } /** * Sets the conversion resolution value for the specified channel in * the provided state buffer. The state buffer is retrieved from the * <CODE>readDevice()</CODE> method. The method <CODE>writeDevice()</CODE> * must be called to finalize these changes to the device. Note that * multiple 'set' methods can be called before one call to * <CODE>writeDevice()</CODE>. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param resolution resolution to use in volts * @param state current state of this * device returned from <CODE>readDevice()</CODE> * * @throws IllegalArgumentException Invalid channel number passed */ public void setADResolution (int channel, double resolution, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); // convert voltage resolution into bit resolution int div = ( int ) (getADRange(channel, state) / resolution); int res_bits = 0; do { div >>>= 1; res_bits++; } while (div != 0); res_bits -= 1; if (res_bits == 16) res_bits = 0; // check for valid bit resolution if ((res_bits < 0) || (res_bits > 15)) throw new IllegalArgumentException("Invalid resolution"); // clear out the resolution state [channel * 2] &= ( byte ) 0xF0; // set the resolution state [channel * 2] |= ( byte ) ((res_bits == 16) ? 0 : res_bits); // set bitmap field to indicate this register has changed Bit.arrayWriteBit(1, channel * 2, BITMAP_OFFSET, state); } /** * Sets the input range for the specified channel in the provided state * buffer. The state buffer is retrieved from the <CODE>readDevice()</CODE> * method. The method <CODE>writeDevice()</CODE> must be called to finalize * these changes to the device. Note that multiple 'set' methods can * be called before one call to <CODE>writeDevice()</CODE>. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param range max volt range, use * getRanges() method to get available ranges * @param state current state of this * device returned from <CODE>readDevice()</CODE> * * @throws IllegalArgumentException Invalid channel number passed */ public void setADRange (int channel, double range, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); // convert range into bit value int range_bit; if ((range > 5.00) & (range < 5.30)) range_bit = 1; else if ((range > 2.40) & (range < 2.70)) range_bit = 0; else throw new IllegalArgumentException("Invalid range"); // change range bit Bit.arrayWriteBit(range_bit, 0, channel * 2 + 1, state); // set bitmap field to indicate this register has changed Bit.arrayWriteBit(1, channel * 2 + 1, BITMAP_OFFSET, state); } /** * Sets the output enable and state for the specified channel in the * provided register buffer. The register buffer is retrieved from * the <CODE>readDevice()</CODE> method. The method <CODE>writeDevice()</CODE> * must be called to finalize these changes to the device. Note that * multiple 'set' methods can be called before one call to * <CODE>writeDevice()</CODE>. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param outputEnable <CODE>true</CODE> if output is enabled * @param outputState <CODE>false</CODE> if output is conducting to * ground and <CODE>true</CODE> if not conducting. This * parameter is not used if <CODE>outputEnable</CODE> is * <CODE>false</CODE> * @param state current state of the * device returned from <CODE>readDevice()</CODE> */ public void setOutput (int channel, boolean outputEnable, boolean outputState, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); // output enable bit Bit.arrayWriteBit(((outputEnable) ? 1 : 0), 7, channel * 2, state); // optionally set state if (outputEnable) Bit.arrayWriteBit(((outputState) ? 1 : 0), 6, channel * 2, state); // set bitmap field to indicate this register has changed Bit.arrayWriteBit(1, channel * 2, BITMAP_OFFSET, state); } /** * Sets or clears the external power flag in the provided register buffer. * The register buffer is retrieved from the <CODE>readDevice()</CODE> method. * The method <CODE>writeDevice()</CODE> must be called to finalize these * changes to the device. Note that multiple 'set' methods can * be called before one call to <CODE>writeDevice()</CODE>. * * @param external <CODE>true</CODE> if setting external power is used * @param state current state of this * device returned from <CODE>readDevice()</CODE> */ public void setPower (boolean external, byte[] state) { // sed the flag state [EXPOWER_OFFSET] = ( byte ) (external ? 0x40 : 0); // set bitmap field to indicate this register has changed Bit.arrayWriteBit(1, EXPOWER_OFFSET, BITMAP_OFFSET, state); } //-------- //-------- Utility methods //-------- /** * Converts a raw voltage long value for the DS2450 into a valid voltage. * Requires the max voltage value. * * @param rawVoltage raw voltage * @param range max voltage * * @return calculated voltage based on the range */ public static double interpretVoltage (long rawVoltage, double range) { return ((( double ) rawVoltage / 65535.0) * range); } /** * Converts a voltage double value to the DS2450 specific int value. * Requires the max voltage value. * * @param voltage voltage * @param range max voltage * * @return the DS2450 voltage */ public static int voltageToInt (double voltage, double range) { return ( int ) ((voltage * 65535.0) / range); } //-------- //-------- Private methods //-------- /** * Create the memory bank interface to read/write */ private void initMem () { // readout readout = new MemoryBankAD(this); // control regs = new Vector(3); MemoryBankAD temp_mb = new MemoryBankAD(this); temp_mb.bankDescription = "A/D Control and Status"; temp_mb.generalPurposeMemory = false; temp_mb.startPhysicalAddress = 8; temp_mb.readWrite = true; temp_mb.readOnly = false; regs.addElement(temp_mb); // Alarms temp_mb = new MemoryBankAD(this); temp_mb.bankDescription = "A/D Alarm Settings"; temp_mb.generalPurposeMemory = false; temp_mb.startPhysicalAddress = 16; temp_mb.readWrite = true; temp_mb.readOnly = false; regs.addElement(temp_mb); // calibration temp_mb = new MemoryBankAD(this); temp_mb.bankDescription = "A/D Calibration"; temp_mb.generalPurposeMemory = false; temp_mb.startPhysicalAddress = 24; temp_mb.readWrite = true; temp_mb.readOnly = false; regs.addElement(temp_mb); } /** * Performs voltage conversion on all specified channels. The method * <CODE>getADVoltage()</CODE> can be used to read the result of the * conversion. * * @param inputSelectMask input select mask * @param readOutControl read out control * @param timeUs time in microseconds for conversion * @param state current state of this * device returned from <CODE>readDevice()</CODE> * * @throws OneWireIOException Data was not written correctly * @throws OneWireException Could not find part * @throws IlleaglArgumentException Invalid channel number passed */ private void doADConvert (byte inputSelectMask, byte readOutControl, int timeUs, byte[] state) throws OneWireIOException, OneWireException { // check if no conversions if (inputSelectMask == 0) { throw new IllegalArgumentException( "No conversion will take place. No channel selected."); } // Create the command block to be sent. byte[] raw_buf = new byte [5]; raw_buf [0] = CONVERT_COMMAND; raw_buf [1] = inputSelectMask; raw_buf [2] = ( byte ) readOutControl; raw_buf [3] = ( byte ) 0xFF; raw_buf [4] = ( byte ) 0xFF; // calculate the CRC16 up to and including readOutControl int crc16 = CRC16.compute(raw_buf, 0, 3, 0); // Send command block. if (adapter.select(address)) { if (isPowerExternal(state)) { // good power so send the entire block (with both CRC) adapter.dataBlock(raw_buf, 0, 5); // Wait for complete of conversion try { Thread.sleep((timeUs / 1000) + 10); } catch (InterruptedException e){} ; // calculate the rest of the CRC16 crc16 = CRC16.compute(raw_buf, 3, 2, crc16); } else { // parasite power so send the all but last byte adapter.dataBlock(raw_buf, 0, 4); // setup power delivery adapter.setPowerDuration(adapter.DELIVERY_INFINITE); adapter.startPowerDelivery(adapter.CONDITION_AFTER_BYTE); // get the final CRC byte and start strong power delivery raw_buf [4] = ( byte ) adapter.getByte(); crc16 = CRC16.compute(raw_buf, 3, 2, crc16); // Wait for power delivery to complete the conversion try { Thread.sleep((timeUs / 1000) + 1); } catch (InterruptedException e){} ; // Turn power off. adapter.setPowerNormal(); } } else throw new OneWireException("OneWireContainer20 - Device not found."); // check the CRC result if (crc16 != 0x0000B001) throw new OneWireIOException( "OneWireContainer20 - Failure during conversion - Bad CRC"); // check if still busy if (adapter.getByte() == 0x00) throw new OneWireIOException("Conversion failed to complete."); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -