📄 onewirecontainer30.java
字号:
* @throws OneWireIOException Error reading data * @throws OneWireException Could not find part */ public void setRemainingCapacity (double remainingCapacity) throws OneWireIOException, OneWireException { int data; // if the internal resistor is used, it can be stored as is (in mAH) if (internalResistor) data = ( int ) (remainingCapacity * 4); else data = ( int ) (remainingCapacity * Rsens / .00626); // break into bytes and store writeByte(16, ( byte ) (data >> 8)); writeByte(17, ( byte ) (data & 0xff)); } /** * Calculates the remaining capacity in mAHours from the current * Accumulator. Accurate to +/- .25 mAH. * * @param state device state * @return mAHours of battery capacity remaining * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part */ public double getRemainingCapacity (byte[] state) throws OneWireIOException, OneWireException { double result = 0; // grab the data int data = ((state [16] & 0xff) << 8) | (state [17] & 0xff); // if the internal resistor is being used the part calculates it for us if (internalResistor) result = data / 4.0; // this equation can be found on the data sheet else result = data * .00626 / Rsens; return result; } /** * Sets the state for the Programmable Input/Output pin. In order to * operate as a switch, PIO must be tied to a pull-up resistor to VDD. * * @param on state of the PIO pin to set * * @throws OneWireIOException Error writting data * @throws OneWireException Could not find part */ //must have a pull-up resistor from PIO to VDD 4.7KOhm public void setLatchState (boolean on) throws OneWireIOException, OneWireException { //since bit 0 is read-only and bits 2-7 are don't cares, //we don't need to read location 8 first, we can just write writeByte(8, ( byte ) (on ? 0x40 : 0x00)); } /** * Returns the latch state of the Programmable Input/Ouput * pin on the DS2760. * * @return state of the Programmable Input/Ouput pin * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part */ public boolean getLatchState () throws OneWireIOException, OneWireException { return ((readByte(8) & 0x40) == 0x40); } /** * Clears the overvoltage, undervoltage, charge overcurrent, * and discharge overcurrent flags. Each time a violation * occurs, these flags stay set until reset. This method * resets all 4 flags. * * @throws OneWireIOException Error writting data * @throws OneWireException Could not find part */ public void clearConditions () throws OneWireIOException, OneWireException { byte protect_reg = readByte(0); writeByte(0, ( byte ) (protect_reg & 0x0f)); } ///////////////////////////////////////////////////////////////////// // // BEGIN CONTAINER INTERFACE METHODS // //////////////////////////////////////////////////////////////////// //-------- //-------- A/D Feature methods //-------- /** * Queries to get the number of channels supported by this A/D device. * Channel specific methods will use a channel number specified * by an integer from <CODE>[0 to (getNumberChannels() - 1)]</CODE>. * * @return number of channels */ public int getNumberADChannels () { return 2; } /** * Queries to see if this A/D measuring device has high/low * alarms. * * @return <CODE>true</CODE> if has high/low trips */ public boolean hasADAlarms () { return false; } /** * Queries to get an array of available ranges for the specified * A/D channel. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * * @return available ranges */ public double[] getADRanges (int channel) { double[] result = new double [1]; result [0] = 5.0; return result; } /** * Queries to get an array of available resolutions based * on the specified range on the specified A/D channel. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param range range of channel specified in channel parameter * * @return available resolutions */ public double[] getADResolutions (int channel, double range) { double[] result = new double [1]; result [0] = getADResolution(channel, null); return result; } /** * Queries to see if this A/D device supports doing multiple voltage * conversions at the same time. * * @return <CODE>true</CODE> if can do multi-channel voltage reads */ public boolean canADMultiChannelRead () { return false; } //-------- //-------- A/D IO Methods //-------- /** * Performs voltage conversion on the 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 Error writting data * @throws OneWireException Could not find device */ public void doADConvert (int channel, byte[] state) throws OneWireIOException, OneWireException { //this actually should be an airball as well... //BECAUSE... //the voltage is constantly being read when the part is //in active mode, so we can just read it out from the state. //the part only leaves active mode if the battery runs out //(i.e. voltage goes below threshold) in which case we should //return the lower bound anyway... } /** * 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 than 1 channel * is specified. * * @param doConvert channels to perform conversion on * @param state current state of the * device returned from <CODE>readDevice()</CODE> * * @throws OneWireIOException Error writting data * @throws OneWireException Device does not support multi-channel reading */ public void doADConvert (boolean[] doConvert, byte[] state) throws OneWireIOException, OneWireException { throw new OneWireException( "This device does not support multi-channel reading"); } /** * 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 than 1 channel. * * @param state current state of the * device returned from <CODE>readDevice()</CODE> * * @return voltage values for all channels * * @throws OneWireIOException Error writting data * @throws OneWireException Device does not support multi-channel reading */ public double[] getADVoltage (byte[] state) throws OneWireIOException, OneWireException { throw new OneWireException( "This device does not support multi-channel reading"); } /** * Reads a channel 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 than 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 the * device returned from <CODE>readDevice()</CODE> * * @return voltage value for the specified * channel * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find device */ public double getADVoltage (int channel, byte[] state) throws OneWireIOException, OneWireException { if(channel<0 || channel>1) throw new OneWireException("Invalid channel"); int data; double result = 0.0; // the measurement is put in two bytes, (MSB) and (LSB). data = (state [12 + channel*2] << 8) | (state [13 + channel*2] & 0x00ff); if(channel==0) { // the voltage measurement channel // Once the two bytes are ORed, a right shift of 5 must occur // (signed shift) data = data >> 5; } else { // the current sensing channel // Once the two bytes are ORed, a right shift of 3 must occur // (signed shift) data = data >> 3; } // that raw measurement is in 'resolution' units -> convert to volts result = data * getADResolution(channel, state); return result; } //-------- //-------- 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 the * device returned from <CODE>readDevice()</CODE> * * @return alarm_value in volts * * @throws OneWireException Device does not support A/D alarms */ public double getADAlarm (int channel, int alarmType, byte[] state) throws OneWireException { throw new OneWireException("This device does not have AD alarms"); } /** * 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 device * returned from <CODE>readDevice()</CODE> * * @return <CODE>true</CODE> if specified alarm is enabled * * @throws OneWireException Device does not support A/D alarms */ public boolean getADAlarmEnable (int channel, int alarmType, byte[] state) throws OneWireException { throw new OneWireException("This device does not have AD alarms"); } /** * Checks the A/D 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 device * returned from <CODE>readDevice()</CODE> * * @return <CODE>true</CODE> if specified alarm occurred * * @throws OneWireException Device does not support A/D alarms */ public boolean hasADAlarmed (int channel, int alarmType, byte[] state) throws OneWireException { throw new OneWireException("This device does not have AD alarms"); } /** * Extracts the A/D 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 device * returned from <CODE>readDevice()</CODE> * * @return A/D resolution of channel in volts */ public double getADResolution (int channel, byte[] state) { if(channel==0) { return 0.00488; //its always the same! }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -