static int dht11_decode(struct dht11 *dht11, int offset, int timeres) { int i, t, timing[DHT11_BITS_PER_READ], threshold; unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum; threshold = DHT11_DATA_BIT_HIGH / timeres; if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold) pr_err("dht11: WARNING: decoding ambiguous\n"); /* scale down with timeres and check validity */ for (i = 0; i < DHT11_BITS_PER_READ; ++i) { t = dht11->edges[offset + 2 * i + 2].ts - dht11->edges[offset + 2 * i + 1].ts; if (!dht11->edges[offset + 2 * i + 1].value) return -EIO; /* lost synchronisation */ timing[i] = t / timeres; } hum_int = dht11_decode_byte(timing, threshold); hum_dec = dht11_decode_byte(&timing[8], threshold); temp_int = dht11_decode_byte(&timing[16], threshold); temp_dec = dht11_decode_byte(&timing[24], threshold); checksum = dht11_decode_byte(&timing[32], threshold); if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) return -EIO; dht11->timestamp = ktime_get_real_ns(); if (hum_int < 20) { /* DHT22 */ dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) * ((temp_int & 0x80) ? -100 : 100); dht11->humidity = ((hum_int << 8) + hum_dec) * 100; } else if (temp_dec == 0 && hum_dec == 0) { /* DHT11 */ dht11->temperature = temp_int * 1000; dht11->humidity = hum_int * 1000; } else { dev_err(dht11->dev, "Don't know how to decode data: %d %d %d %d\n", hum_int, hum_dec, temp_int, temp_dec); return -EIO; } return 0; }
static int dht11_decode(struct dht11 *dht11, int offset) { int i, t; char bits[DHT11_BITS_PER_READ]; unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum; for (i = 0; i < DHT11_BITS_PER_READ; ++i) { t = dht11->edges[offset + 2 * i + 2].ts - dht11->edges[offset + 2 * i + 1].ts; if (!dht11->edges[offset + 2 * i + 1].value) return -EIO; /* lost synchronisation */ bits[i] = t > DHT11_THRESHOLD; } hum_int = dht11_decode_byte(bits); hum_dec = dht11_decode_byte(&bits[8]); temp_int = dht11_decode_byte(&bits[16]); temp_dec = dht11_decode_byte(&bits[24]); checksum = dht11_decode_byte(&bits[32]); if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) return -EIO; dht11->timestamp = ktime_get_boot_ns(); if (hum_int < 20) { /* DHT22 */ dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) * ((temp_int & 0x80) ? -100 : 100); dht11->humidity = ((hum_int << 8) + hum_dec) * 100; } else if (temp_dec == 0 && hum_dec == 0) { /* DHT11 */ dht11->temperature = temp_int * 1000; dht11->humidity = hum_int * 1000; } else { dev_err(dht11->dev, "Don't know how to decode data: %d %d %d %d\n", hum_int, hum_dec, temp_int, temp_dec); return -EIO; } return 0; }