📄 onewirecontainer20.java
字号:
Bit.arrayWriteBit(1, index, BITMAP_OFFSET, state); } } // only allow physical address 0x1C to be written in calibration bank state [BITMAP_OFFSET + 2] = ( byte ) (state [BITMAP_OFFSET + 2] & 0x10); // loop through the three memory banks collecting changes for (bank = 0; bank < 3; bank++) { start_offset = 0; len = 0; got_block = false; mb = ( MemoryBankAD ) regs.elementAt(bank); // loop through each byte in the memory bank for (i = 0; i < 8; i++) { // check to see if this byte needs writing (skip control register for now) if (Bit.arrayReadBit(bank * 8 + i, BITMAP_OFFSET, state) == 1) { // check if already in a block if (got_block) len++; // new block else { got_block = true; start_offset = i; len = 1; } // check for last byte exception, write current block if (i == 7) mb.write(start_offset, state, bank * 8 + start_offset, len); } else if (got_block) { // done with this block so write it mb.write(start_offset, state, bank * 8 + start_offset, len); got_block = false; } } } // clear out the bitmap state [24] = 0; state [25] = 0; state [26] = 0; } /** * Reads the voltage values. Must be used after a <CODE>doADConvert()</CODE> * method call. Also must include the last valid state from the * <CODE>readDevice()</CODE> method and this A/D must support multi-channel * read <CODE>canMultiChannelRead()</CODE> if there are more then 1 channel. * * @param state current state of this device returned from * <CODE>readDevice()</CODE> * * @return voltage values for all channels * * @throws OneWireIOException Data was not read correctly * @throws OneWireException Could not find part */ public double[] getADVoltage (byte[] state) throws OneWireIOException, OneWireException { byte[] read_buf = new byte [8]; double[] ret_dbl = new double [4]; // get readout page readout.readPageCRC(0, false, read_buf, 0); // convert to array of doubles for (int ch = 0; ch < 4; ch++) { ret_dbl [ch] = interpretVoltage(Convert.toLong(read_buf, ch * 2, 2), getADRange(ch, state)); } return ret_dbl; } /** * Reads a channels voltage value. Must be used after a * <CODE>doADConvert()</CODE> method call. Also must include * the last valid state from the <CODE>readDevice()</CODE> method. * Note, if more then one channel is to be read then it is more * efficient to use the <CODE>getADVoltage(byte[])</CODE> method that returns * all channel values. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param state current state of this * device returned from <CODE>readDevice()</CODE> * * @return voltage value for the specified * channel * * @throws OneWireIOException Data was not read correctly * @throws OneWireException Could not find part * @throws IllegalArgumentException Invalid channel number passed */ public double getADVoltage (int channel, byte[] state) throws OneWireIOException, OneWireException { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); // get readout page byte[] read_buf = new byte [8]; readout.readPageCRC(0, false, read_buf, 0); return interpretVoltage(Convert.toLong(read_buf, channel * 2, 2), getADRange(channel, state)); } /** * Performs voltage conversion on specified channel. The method * <CODE>getADVoltage()</CODE> can be used to read the result * of the conversion. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @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 */ public void doADConvert (int channel, byte[] state) throws OneWireIOException, OneWireException { // call with set presets to 0 doADConvert(channel, PRESET_TO_ZEROS, state); } /** * Performs voltage conversion on all specified channels. The method * <CODE>getADVoltage()</CODE> can be used to read the result of the * conversion. This A/D must support multi-channel read * <CODE>canMultiChannelRead()</CODE> if there are more then 1 channel * is specified. * * @param doConvert which channels to perform conversion on. * @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 */ public void doADConvert (boolean[] doConvert, byte[] state) throws OneWireIOException, OneWireException { // call with set presets to 0 int[] presets = new int [4]; for (int i = 0; i < 4; i++) presets [i] = PRESET_TO_ZEROS; doADConvert(doConvert, presets, state); } /** * Performs voltage conversion on specified channel. The method * <CODE>getADVoltage()</CODE> can be used to read the result * of the conversion. * * @param channel 0,1,2,3 representing the channels A,B,C,D * @param preset preset value: * <CODE>NO_PRESET (0), PRESET_TO_ZEROS (1), and PRESET_TO_ONES (2)</CODE> * @param state state of this * device returned from <CODE>readDevice()</CODE> * * @throws OneWireIOException Data could not be written correctly * @throws OneWireException Could not find part * @throws IllegalArgumentException Invalid channel number passed */ public void doADConvert (int channel, int preset, byte[] state) throws OneWireIOException, OneWireException, IllegalArgumentException { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); // perform the conversion (do fixed max conversion time) doADConvert(( byte ) (0x01 << channel), ( byte ) (preset << channel), 1440, state); } /** * Performs voltage conversion on all specified channels. * The method <CODE>getADVoltage()</CODE> can be used to read the result * of the conversion. * * @param doConvert which channels to perform conversion on * @param preset preset values * <CODE>NO_PRESET (0), PRESET_TO_ZEROS (1), and PRESET_TO_ONES (2)</CODE> * @param state current state of this * device returned from <CODE>readDevice()</CODE> * * @throws OneWireIOException Data could not be written correctly * @throws OneWireException Could not find part */ public void doADConvert (boolean[] doConvert, int[] preset, byte[] state) throws OneWireIOException, OneWireException { byte input_select_mask = 0; byte read_out_control = 0; int time = 160; // Time required in micro Seconds to covert. // calculate the input mask, readout control, and conversion time for (int ch = 3; ch >= 0; ch--) { // input select input_select_mask <<= 1; if (doConvert [ch]) input_select_mask |= 0x01; // readout control read_out_control <<= 2; if (preset [ch] == PRESET_TO_ZEROS) read_out_control |= 0x01; else if (preset [ch] == PRESET_TO_ONES) read_out_control |= 0x02; // conversion time time += (80 * getADResolution(ch, state)); } // do the conversion doADConvert(input_select_mask, read_out_control, time, state); } //-------- //-------- A/D 'get' Methods //-------- /** * Extracts the alarm voltage value of the specified channel from the * provided state buffer. The state buffer is retrieved from the * <CODE>readDevice()</CODE> method. * * @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 state current state of this * device returned from <CODE>readDevice()</CODE> * * @return alarm value in volts * * @throws IllegalArgumentException Invalid channel number passed */ public double getADAlarm (int channel, int alarmType, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); // extract alarm value and convert to voltage long temp_long = ( long ) (state [ALARM_OFFSET + channel * 2 + alarmType] & 0x00FF) << 8; return interpretVoltage(temp_long, getADRange(channel, state)); } /** * Extracts the alarm enable value of the specified channel from * the provided state buffer. The state buffer is retrieved from * the <CODE>readDevice()</CODE> method. * * @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 state current state of the state * returned from <CODE>readDevice()</CODE> * * @return <CODE>true</CODE> if specified alarm is enabled * * @throws IllegalArgumentException Invalid channel number passed */ public boolean getADAlarmEnable (int channel, int alarmType, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); return (Bit.arrayReadBit(2 + alarmType, channel * 2 + 1, state) == 1); } /** * Checks the alarm event value of the specified channel from the provided * state buffer. The state buffer is retrieved from the * <CODE>readDevice()</CODE> method. * * @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 state current state of the state * returned from <CODE>readDevice()</CODE> * * @return <CODE>true</CODE> if specified alarm occurred * * @throws IllegalArgumentException Invalid channel number passed */ public boolean hasADAlarmed (int channel, int alarmType, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); return (Bit.arrayReadBit(4 + alarmType, channel * 2 + 1, state) == 1); } /** * Extracts the conversion resolution of the specified channel from the * provided state buffer expressed in volts. The state is retrieved from the * <CODE>readDevice()</CODE> method. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param state current state of the state * returned from <CODE>readDevice()</CODE> * * @return resolution of channel in volts * * @throws IllegalArgumentException Invalid channel number passed */ public double getADResolution (int channel, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); int res = state [channel * 2] & 0x0F; // return resolution, if 0 then 16 bits if (res == 0) res = 16; return getADRange(channel, state) / ( double ) (1 << res); } /** * Extracts the input voltage range of the specified channel from * the provided state buffer. The state buffer is retrieved from * the <CODE>readDevice()</CODE> method. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param state current state of the state * returned from <CODE>readDevice()</CODE> * * @return A/D input voltage range * * @throws IllegalArgumentException Invalid channel number passed */ public double getADRange (int channel, byte[] state) { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); return (Bit.arrayReadBit(0, channel * 2 + 1, state) == 1) ? 5.12 : 2.56; } /** * Detects if the output is enabled for the specified channel from * the provided register buffer. The register buffer is retrieved * from the <CODE>readDevice()</CODE> method. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param state current state of the device * returned from <CODE>readDevice()</CODE> * * @return <CODE>true</CODE> if output is enabled on specified channel * * @throws IllegalArgumentException Invalid channel number passed */ public boolean isOutputEnabled (int channel, byte[] state) throws IllegalArgumentException { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); return (Bit.arrayReadBit(7, channel * 2, state) == 1); } /** * Detects if the output is enabled for the specified channel from * the provided register buffer. The register buffer is retrieved * from the <CODE>readDevice()</CODE> method. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @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 * * @throws IllegalArgumentException Invalid channel number passed */ public boolean getOutputState (int channel, byte[] state) throws IllegalArgumentException { // check for valid channel value if ((channel < 0) || (channel > 3)) throw new IllegalArgumentException("Invalid channel number"); return (Bit.arrayReadBit(6, channel * 2, state) == 1); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -