int dht11_read(int gpio, int* temp, int* rh) { unsigned int cs, expected_cs; int max_cycles = 100000, thresh; /* Get DHT11's attention. */ set_gpio(gpio, 0); os_delay_us(20 * 1000); /* Switch GPIO pin to input and let it stabilize. */ read_gpio_pin(gpio); os_delay_us(1); /* Starting with high, wait for first 80us of L. */ await_change(gpio, &max_cycles); thresh = await_change(gpio, &max_cycles); /* First 80 us low. */ /* Then 80us of H. */ thresh += await_change(gpio, &max_cycles); /* Then 80 us high. */ if (max_cycles <= 0) return 0; /* We use this to calibrate our delay: take average of the two * and further divide by half to get number of cycles that represent 40us. */ thresh /= 4; /* Now read the data. */ cs = (*rh = dht11_read_bits(gpio, 8, thresh, &max_cycles)); cs += dht11_read_bits(gpio, 8, thresh, &max_cycles); /* Always 0. */ cs += (*temp = dht11_read_bits(gpio, 8, thresh, &max_cycles)); cs += dht11_read_bits(gpio, 8, thresh, &max_cycles); /* Always 0. */ expected_cs = dht11_read_bits(gpio, 8, thresh, &max_cycles); /* printf("%d %d %d==%d %d %d\r\n", *temp, *rh, cs, expected_cs, thresh, * max_cycles); */ return (max_cycles > 0 && cs == expected_cs); }
ICACHE_FLASH_ATTR static v7_val_t GPIO_in(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { v7_val_t pinv = v7_array_get(v7, args, 0); int pin; if (!v7_is_double(pinv)) { printf("non-numeric pin\n"); return v7_create_undefined(); } pin = v7_to_double(pinv); return v7_create_boolean(read_gpio_pin(pin)); }
int oem_gpio_set_irq_type(int gpio, unsigned int type) { if(type == IRQ_TYPE_EDGE_BOTH){ if(read_gpio_pin(gpio)){ type = IRQ_TYPE_EDGE_FALLING; }else{ type = IRQ_TYPE_EDGE_RISING; } } if(type == IRQ_TYPE_LEVEL_MASK){ if(read_gpio_pin(gpio)){ type = IRQ_TYPE_LEVEL_LOW; }else{ type = IRQ_TYPE_LEVEL_HIGH; } } switch(type){ case IRQ_TYPE_EDGE_RISING: __gpio_as_irq_rise_edge(gpio); break; case IRQ_TYPE_EDGE_FALLING: __gpio_as_irq_fall_edge(gpio); break; case IRQ_TYPE_LEVEL_HIGH: __gpio_as_irq_high_level(gpio); break; case IRQ_TYPE_LEVEL_LOW: __gpio_as_irq_low_level(gpio); break; default: return -EINVAL; } return 0; }