byte BlinkPlug::state () { byte saved = leds; ledOff(1+2); byte result = !digiRead() | (!digiRead2() << 1); ledOn(saved); return result; }
uint8_t SHT21::writeByte(uint8_t value) const { mode(OUTPUT); for (uint8_t i = 0x80; i != 0; i >>= 1) { digiWrite(value & i); clock(1); clock(0); } release(); clock(1); uint8_t error = digiRead(); clock(0); return error; }
uint8_t SHT21::readByte(uint8_t ack) const { uint8_t value = 0; for (uint8_t i = 0x80; i != 0; i >>= 1) { clock(1); if (digiRead()) value |= i; clock(0); } mode(OUTPUT); digiWrite(!ack); clock(1); clock(0); release(); return value; }
uint16_t Port::shiftRead(uint8_t bitOrder, uint8_t count) const { uint16_t value = 0, mask = bit(LSBFIRST ? 0 : count - 1); for (uint8_t i = 0; i < count; ++i) { digiWrite2(1); delayMicroseconds(5); if (digiRead()) value |= mask; if (bitOrder == LSBFIRST) mask <<= 1; else mask >>= 1; digiWrite2(0); delayMicroseconds(5); } return value; }
uint8_t SHT21::measure(uint8_t type, void (*delayFun)()) { start(); writeByte(type == TEMP? MEASURE_TEMP : MEASURE_HUMI); for (uint8_t i = 0; i < 250; ++i) { if (!digiRead()) { meas[type] = readByte(1) << 8; meas[type] |= readByte(1); uint8_t flipped = 0; for (uint8_t j = 0x80; j != 0; j >>= 1) { flipped >>= 1; } if (readByte(0) != flipped) break; return 0; } if (delayFun) delayFun(); else delay(1); }