float readTemperature(bit mode) { byte tempH, tempL; float returnData; initDS18B20(); writeOneByte( 0xCC ); // 跳过读序号列号的操作 writeOneByte( 0x44 ); // 启动温度转换 if( mode ) { delayMS(800); //12位分辨率,需要750ms测温 } initDS18B20(); writeOneByte( 0xCC ); //跳过读序号列号的操作 writeOneByte( 0xBE ); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 tempL = readOneByte(); //低位 tempH = readOneByte(); //高位 if( tempH & 0x80 ) returnData=(~( tempH * 256 + tempL) + 1) * (-0.0625); //零度以下, 温度转换,把高位低位做相应的运算转换为实际的温度 else returnData=( tempH * 256 + tempL ) * 0.0625; //零度以上 return( returnData ); }
uint8_t TwoWire::requestFrom(uint8_t address, int num_bytes) { if (num_bytes > WIRE_BUFSIZ) num_bytes = WIRE_BUFSIZ; rx_buf_idx = 0; rx_buf_len = 0; while (rx_buf_len < num_bytes) { if(!readOneByte(address, rx_buf + rx_buf_len)) rx_buf_len++; else break; } return rx_buf_len; }
uint8 TwoWire::requestFromOld(uint8 address, int num_bytes) { if (num_bytes > WIRE_BUFSIZ) num_bytes = WIRE_BUFSIZ; rx_buf_idx = 0; rx_buf_len = 0; while (rx_buf_len < num_bytes) { if(!readOneByte(address, rx_buf + rx_buf_len)) rx_buf_len++; else { Serial1.print("requestFrom failed at byte "); Serial1.print(rx_buf_len,10); Serial1.println(); break; } } return rx_buf_len; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CompressedFilter::ReadFromFile(): // Reads the compressed filter from the specified open file. // errorT CompressedFilter::ReadFromFile (FILE * fp) { ASSERT (fp != NULL); if (CompressedData) { delete[] CompressedData; } CFilterSize = readFourBytes (fp); CFilterCount = readFourBytes (fp); CompressedLength = readFourBytes (fp); CompressedData = NULL; if(CompressedLength > 0) { CompressedData = new byte [CompressedLength]; byte * pb = CompressedData; for (uint i=0; i < CompressedLength; i++, pb++) { *pb = readOneByte(fp); } } return OK; }
CryptoKey* V8ScriptValueDeserializerForModules::readCryptoKey() { // Read params. uint8_t rawKeyType; if (!readOneByte(&rawKeyType)) return nullptr; WebCryptoKeyAlgorithm algorithm; WebCryptoKeyType keyType = WebCryptoKeyTypeSecret; switch (rawKeyType) { case AesKeyTag: { uint32_t rawId; WebCryptoAlgorithmId id; uint32_t lengthBytes; if (!readUint32(&rawId) || !algorithmIdFromWireFormat(rawId, &id) || !readUint32(&lengthBytes) || lengthBytes > std::numeric_limits<unsigned short>::max() / 8u) return nullptr; algorithm = WebCryptoKeyAlgorithm::createAes(id, lengthBytes * 8); keyType = WebCryptoKeyTypeSecret; break; } case HmacKeyTag: { uint32_t lengthBytes; uint32_t rawHash; WebCryptoAlgorithmId hash; if (!readUint32(&lengthBytes) || lengthBytes > std::numeric_limits<unsigned>::max() / 8 || !readUint32(&rawHash) || !algorithmIdFromWireFormat(rawHash, &hash)) return nullptr; algorithm = WebCryptoKeyAlgorithm::createHmac(hash, lengthBytes * 8); keyType = WebCryptoKeyTypeSecret; break; } case RsaHashedKeyTag: { uint32_t rawId; WebCryptoAlgorithmId id; uint32_t rawKeyType; uint32_t modulusLengthBits; uint32_t publicExponentSize; const void* publicExponentBytes; uint32_t rawHash; WebCryptoAlgorithmId hash; if (!readUint32(&rawId) || !algorithmIdFromWireFormat(rawId, &id) || !readUint32(&rawKeyType) || !asymmetricKeyTypeFromWireFormat(rawKeyType, &keyType) || !readUint32(&modulusLengthBits) || !readUint32(&publicExponentSize) || !readRawBytes(publicExponentSize, &publicExponentBytes) || !readUint32(&rawHash) || !algorithmIdFromWireFormat(rawHash, &hash)) return nullptr; algorithm = WebCryptoKeyAlgorithm::createRsaHashed( id, modulusLengthBits, reinterpret_cast<const unsigned char*>(publicExponentBytes), publicExponentSize, hash); break; } case EcKeyTag: { uint32_t rawId; WebCryptoAlgorithmId id; uint32_t rawKeyType; uint32_t rawNamedCurve; WebCryptoNamedCurve namedCurve; if (!readUint32(&rawId) || !algorithmIdFromWireFormat(rawId, &id) || !readUint32(&rawKeyType) || !asymmetricKeyTypeFromWireFormat(rawKeyType, &keyType) || !readUint32(&rawNamedCurve) || !namedCurveFromWireFormat(rawNamedCurve, &namedCurve)) return nullptr; algorithm = WebCryptoKeyAlgorithm::createEc(id, namedCurve); break; } case NoParamsKeyTag: { uint32_t rawId; WebCryptoAlgorithmId id; if (!readUint32(&rawId) || !algorithmIdFromWireFormat(rawId, &id)) return nullptr; algorithm = WebCryptoKeyAlgorithm::createWithoutParams(id); break; } } if (algorithm.isNull()) return nullptr; // Read key usages. uint32_t rawUsages; WebCryptoKeyUsageMask usages; bool extractable; if (!readUint32(&rawUsages) || !keyUsagesFromWireFormat(rawUsages, &usages, &extractable)) return nullptr; // Read key data. uint32_t keyDataLength; const void* keyData; if (!readUint32(&keyDataLength) || !readRawBytes(keyDataLength, &keyData)) return nullptr; WebCryptoKey key = WebCryptoKey::createNull(); if (!Platform::current()->crypto()->deserializeKeyForClone( algorithm, keyType, extractable, usages, reinterpret_cast<const unsigned char*>(keyData), keyDataLength, key)) return nullptr; return CryptoKey::create(key); }
errorT TreeCache::ReadFile (const char * fname) { // Only read the file if the cache is empty: if (NumInUse > 0) { return OK; } #ifdef WINCE /*FILE * */Tcl_Channel fp; fileNameT fullname; strCopy (fullname, fname); strAppend (fullname, TREEFILE_SUFFIX); //fp = fopen (fullname, "rb"); fp = mySilent_Tcl_OpenFileChannel(NULL, fullname, "r", 0666); if (fp == NULL) { return ERROR_FileOpen; } my_Tcl_SetChannelOption(NULL, fp, "-encoding", "binary"); my_Tcl_SetChannelOption(NULL, fp, "-translation", "binary"); uint magic = readFourBytes (fp); if (magic != TREEFILE_MAGIC) { //fclose (fp); my_Tcl_Close(NULL, fp); #else FILE * fp; fileNameT fullname; strCopy (fullname, fname); strAppend (fullname, TREEFILE_SUFFIX); fp = fopen (fullname, "rb"); if (fp == NULL) { return ERROR_FileOpen; } uint magic = readFourBytes (fp); if (magic != TREEFILE_MAGIC) { fclose (fp); #endif return ERROR_Corrupt; } readTwoBytes (fp); // Scid Version; unused uint cacheSize = readFourBytes (fp); SetCacheSize (cacheSize); NumInUse = readFourBytes (fp); LowestTotal = readFourBytes (fp); LowestTotalIndex = readFourBytes(fp); for (uint count=0; count < NumInUse; count++) { cachedTreeT * ctree = &(Cache[count]); ctree->toMove = readOneByte (fp); for (squareT sq=0; sq < 64; sq++) { ctree->board[sq] = readOneByte (fp); } // Read the moves: ctree->tree.moveCount = readFourBytes (fp); ctree->tree.totalCount = readFourBytes (fp); uint numMoves = ctree->tree.moveCount; for (uint i=0; i < numMoves; i++) { // Read this move node: treeNodeT * tnode = &(ctree->tree.node[i]); readSimpleMove (fp, &(tnode->sm)); readString (fp, tnode->san, 8); for (uint res = 0; res < 4; res++) { tnode->freq[res] = readFourBytes (fp); } tnode->total = readFourBytes (fp); tnode->score = readFourBytes (fp); tnode->ecoCode = readTwoBytes (fp); tnode->eloCount = readFourBytes (fp); tnode->eloSum = readFourBytes (fp); tnode->perfCount = readFourBytes (fp); tnode->perfSum = readFourBytes (fp); tnode->yearCount = readFourBytes (fp); tnode->yearSum = readFourBytes (fp); } // Read the compressed filter: ctree->cfilter = new CompressedFilter; ctree->cfilter->ReadFromFile (fp); } #ifdef WINCE my_Tcl_Close(NULL, fp); #else fclose (fp); #endif return OK; }