📄 onewirecontainer26.java
字号:
throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] buffer = new byte [11]; byte[] result = new byte [8]; int crc8; // this device uses a crc 8 /* check validity of parameter */ if ((page < 0) || (page > 7)) throw new IllegalArgumentException("OneWireContainer26-Page " + page + " is an invalid page."); /* perform the read/verification */ if (doSpeedEnable) doSpeed(); if (adapter.select(address)) { /* recall memory to the scratchpad */ buffer [0] = RECALL_MEMORY_COMMAND; buffer [1] = ( byte ) page; adapter.dataBlock(buffer, 0, 2); /* perform the read scratchpad */ adapter.reset(); adapter.select(address); buffer [0] = READ_SCRATCHPAD_COMMAND; buffer [1] = ( byte ) page; for (int i = 2; i < 11; i++) buffer [i] = ( byte ) 0x0ff; adapter.dataBlock(buffer, 0, 11); /* do the crc check */ crc8 = CRC8.compute(buffer, 2, 9); if (crc8 != 0x0) throw new OneWireIOException( "OneWireContainer26-Bad CRC during read." + crc8); // copy the data into the result System.arraycopy(buffer, 2, result, 0, 8); } else throw new OneWireException("OneWireContainer26-device not found."); return result; } /** * Writes a page of memory to this device. Pages 3-6 are always * available for user storage and page 7 is available if the CA bit is set * to 0 (false) with <CODE>setFlag()</CODE>. * * @param page the page number * @param source data to be written to page * @param offset offset with page to begin writting * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public void writePage (int page, byte[] source, int offset) throws OneWireIOException, OneWireException { byte[] buffer = new byte [10]; /* check parameter validity */ if ((page < 0) || (page > 7)) throw new IllegalArgumentException("OneWireContainer26-Page " + page + " is an invalid page."); if (source.length < 8) throw new IllegalArgumentException( "OneWireContainer26-Invalid data page passed to writePage."); if (doSpeedEnable) doSpeed(); if (adapter.select(address)) { // write the page to the scratchpad first buffer [0] = WRITE_SCRATCHPAD_COMMAND; buffer [1] = ( byte ) page; System.arraycopy(source, offset, buffer, 2, 8); adapter.dataBlock(buffer, 0, 10); // now copy that part of the scratchpad to memory adapter.reset(); adapter.select(address); buffer [0] = COPY_SCRATCHPAD_COMMAND; buffer [1] = ( byte ) page; adapter.dataBlock(buffer, 0, 2); } else throw new OneWireException("OneWireContainer26-Device not found."); } /** * Checks the specified flag in the status/configuration register * and returns its status as a boolean. * * @param flagToGet flag bitmask. * Acceptable parameters: IAD_FLAG, CA_FLAG, EE_FLAG, AD_FLAG, TB_FLAG, * NVB_FLAG, ADB_FLAG * (may be ORed with | to check the status of more than one). * * @return state of flag * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public boolean getFlag (byte flagToGet) throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] data = readPage(0); if ((data [0] & flagToGet) != 0) return true; return false; } /** * Set one of the flags in the STATUS/CONFIGURATION register. * * @param bitmask of the flag to set * Acceptable parameters: IAD_FLAG, CA_FLAG, EE_FLAG, AD_FLAG, TB_FLAG, * NVB_FLAG, ADB_FLAG. * * @param flagValue value to set flag to * * @throws OneWireIOException Error writting data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public void setFlag (byte flagToSet, boolean flagValue) throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] data = readPage(0); if (flagValue) data [0] = ( byte ) (data [0] | flagToSet); else data [0] = ( byte ) (data [0] & ~(flagToSet)); writePage(0, data, 0); } /** * Get the instantaneous current. The IAD flag must be true!! * Remember to set the Sense resistor value using * <CODE>setSenseResitor(double)</CODE>. * * * @param state current state of device * @return current value in Amperes */ public double getCurrent (byte[] state) { short rawCurrent = ( short ) ((state [6] << 8) | (state [5] & 0x0ff)); return rawCurrent / (4096.0 * Rsens); } /** * Calculate the remaining capacity in mAH as outlined in the data sheet. * * @return battery capacity remaining in mAH * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public double getRemainingCapacity () throws OneWireIOException, OneWireException, IllegalArgumentException { int ica = getICA(); return (1000 * ica / (2048 * Rsens)); } /** * Determines if the battery is charging and returns a boolean. * * * @param state current state of device * * @return true if battery is changing, false if battery is idle or discharging * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public boolean isCharging (byte[] state) throws OneWireIOException, OneWireException, IllegalArgumentException { // positive current (if the thing is hooked up right) is charging if (getCurrent(state) > 0) return true; return false; } /** * Calibrate the current ADC. Although the part is shipped calibrated, * calibrations should be done whenever possible for best results. * NOTE: You MUST force zero current through Rsens (the sensor resistor) * while calibrating. * * @throws OneWireIOException Error calibrating * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public void calibrateCurrentADC () throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] data; byte currentLSB, currentMSB; // grab the current IAD settings so that we dont change anything boolean IADvalue = getFlag(IAD_FLAG); // the IAD bit must be set to "0" to write to the Offset Register setFlag(IAD_FLAG, false); // write all zeroes to the offset register data = readPage(1); data [5] = data [6] = 0; writePage(1, data, 0); // enable current measurements once again setFlag(IAD_FLAG, true); // read the Current Register value data = readPage(0); currentLSB = data [5]; currentMSB = data [6]; // disable current measurements so that we can write to the offset reg setFlag(IAD_FLAG, false); // change the sign of the current register value and store it as the offset data = readPage(1); data [5] = ( byte ) (~(currentLSB) + 1); data [6] = ( byte ) (~(currentMSB)); writePage(1, data, 0); // eset the IAD settings back to normal setFlag(IAD_FLAG, IADvalue); } /** * Set the minimum current measurement magnitude for which the ICA/CCA/DCA * are incremented. This is important for applications where the current * may get very small for long periods of time. Small currents can be * inaccurate by a high percentage, which leads to very inaccurate * accumulations. * * @param threshold minimum number of bits a current measurement must have to be accumulated, * Only 0,2,4 and 8 are valid parameters * * @throws OneWireIOException Error setting the threshold * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public void setThreshold (byte thresholdValue) throws OneWireIOException, OneWireException { byte thresholdReg; byte[] data; switch (thresholdValue) { case 0 : thresholdReg = 0; break; case 2 : thresholdReg = 64; break; case 4 : thresholdReg = ( byte ) 128; break; case 8 : thresholdReg = ( byte ) 192; break; default : throw new IllegalArgumentException( "OneWireContainer26-Threshold value must be 0,2,4, or 8."); } // first save their original IAD settings so we dont change anything boolean IADvalue = getFlag(IAD_FLAG); // current measurements must be off to write to the threshold register setFlag(IAD_FLAG, false); // write the threshold register data = readPage(0); data [7] = thresholdReg; writePage(0, data, 0); // set the IAD back to the way the user had it setFlag(IAD_FLAG, IADvalue); } /** * Retrieves the current ICA value in mVHr. * * @return value in the ICA register * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public int getICA () throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] data = readPage(1); return ( int ) (data [4] & 0x000000ff); } /** * Retrieves the current CCA value in mVHr. This value is accumulated over * the lifetime of the part (until it is set to 0 or the CA flag is set * to false) and includes only charging current (positive). * * @return CCA value * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public int getCCA () throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] data = readPage(7); return ((data [5] << 8) & 0x0000ff00) | (data [4] & 0x000000ff); } /** * Retrieves the value of the DCA in mVHr. This value is accumulated over * the lifetime of the part (until explicitly set to 0 or if the CA flag * is set to false) and includes only discharging current (negative). * * @return DCA value * * @throws OneWireIOException Error reading data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public int getDCA () throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] data = readPage(7); return ((data [7] << 8) & 0x0000ff00) | (data [6] & 0x000000ff); } /** * Set the value of the ICA. * * @param icaValue new ICA value * * @throws OneWireIOException Error writing data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public void setICA (int icaValue) throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] data = readPage(1); data [4] = ( byte ) (icaValue & 0x000000ff); writePage(1, data, 0); } /** * Set the value of the CCA. * * @param ccaValue new CCA value * * @throws OneWireIOException Error writing data * @throws OneWireException Could not find part * @throws IllegalArgumentException Bad parameters passed */ public void setCCA (int ccaValue) throws OneWireIOException, OneWireException, IllegalArgumentException { byte[] data = readPage(7); data [4] = ( byte ) (ccaValue & 0x00ff); data [5] = ( byte ) ((ccaValue & 0xff00) >>> 8);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -