aboutsummaryrefslogtreecommitdiffstats
path: root/python/examples/Adafruit_Python_DHT/source
diff options
context:
space:
mode:
authorgeremy@pubnub.com2014-12-28 17:06:19 -0800
committergeremy@pubnub.com2014-12-28 17:06:19 -0800
commitb89bc8cb03f0534805c2112d5fa4253f82ab616f (patch)
tree4559202652cbd265fe8a84e4933e36d127b2b465 /python/examples/Adafruit_Python_DHT/source
parentc2a3ee12ce2c42b05e1ee31d737d9a8703f4a580 (diff)
downloadpubnub-python-b89bc8cb03f0534805c2112d5fa4253f82ab616f.tar.bz2
adding open/close files for door, new humid/temp sensor
Diffstat (limited to 'python/examples/Adafruit_Python_DHT/source')
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_dht_read.c154
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_dht_read.h33
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_mmio.c73
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_mmio.h101
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_dht_read.c158
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_dht_read.h32
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_mmio.c55
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_mmio.h61
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Test/test_dht_read.c34
-rw-r--r--python/examples/Adafruit_Python_DHT/source/Test/test_dht_read.h26
-rw-r--r--python/examples/Adafruit_Python_DHT/source/_Beaglebone_Black_Driver.c49
-rw-r--r--python/examples/Adafruit_Python_DHT/source/_Raspberry_Pi_Driver.c49
-rw-r--r--python/examples/Adafruit_Python_DHT/source/_Test_Driver.c49
-rw-r--r--python/examples/Adafruit_Python_DHT/source/common_dht_read.c66
-rw-r--r--python/examples/Adafruit_Python_DHT/source/common_dht_read.h51
15 files changed, 991 insertions, 0 deletions
diff --git a/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_dht_read.c b/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_dht_read.c
new file mode 100644
index 0000000..0fa2761
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_dht_read.c
@@ -0,0 +1,154 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <stdlib.h>
+#include <string.h>
+
+#include "bbb_dht_read.h"
+#include "bbb_mmio.h"
+
+// This is the only processor specific magic value, the maximum amount of time to
+// spin in a loop before bailing out and considering the read a timeout. This should
+// be a high value, but if you're running on a much faster platform than a Raspberry
+// Pi or Beaglebone Black then it might need to be increased.
+#define DHT_MAXCOUNT 32000
+
+// Number of bit pulses to expect from the DHT. Note that this is 41 because
+// the first pulse is a constant 50 microsecond pulse, with 40 pulses to represent
+// the data afterwards.
+#define DHT_PULSES 41
+
+int bbb_dht_read(int type, int gpio_base, int gpio_number, float* humidity, float* temperature) {
+ // Validate humidity and temperature arguments and set them to zero.
+ if (humidity == NULL || temperature == NULL) {
+ return DHT_ERROR_ARGUMENT;
+ }
+ *temperature = 0.0f;
+ *humidity = 0.0f;
+
+ // Store the count that each DHT bit pulse is low and high.
+ // Make sure array is initialized to start at zero.
+ int pulseCounts[DHT_PULSES*2] = {0};
+
+ // Get GPIO pin and set it as an output.
+ gpio_t pin;
+ if (bbb_mmio_get_gpio(gpio_base, gpio_number, &pin) < 0) {
+ return DHT_ERROR_GPIO;
+ }
+ bbb_mmio_set_output(pin);
+
+ // Bump up process priority and change scheduler to try to try to make process more 'real time'.
+ set_max_priority();
+
+ // Set pin high for ~500 milliseconds.
+ bbb_mmio_set_high(pin);
+ sleep_milliseconds(500);
+
+ // The next calls are timing critical and care should be taken
+ // to ensure no unnecssary work is done below.
+
+ // Set pin low for ~20 milliseconds.
+ bbb_mmio_set_low(pin);
+ busy_wait_milliseconds(20);
+
+ // Set pin as input.
+ bbb_mmio_set_input(pin);
+
+ // Wait for DHT to pull pin low.
+ uint32_t count = 0;
+ while (bbb_mmio_input(pin)) {
+ if (++count >= DHT_MAXCOUNT) {
+ // Timeout waiting for response.
+ set_default_priority();
+ return DHT_ERROR_TIMEOUT;
+ }
+ }
+
+ // Record pulse widths for the expected result bits.
+ for (int i=0; i < DHT_PULSES*2; i+=2) {
+ // Count how long pin is low and store in pulseCounts[i]
+ while (!bbb_mmio_input(pin)) {
+ if (++pulseCounts[i] >= DHT_MAXCOUNT) {
+ // Timeout waiting for response.
+ set_default_priority();
+ return DHT_ERROR_TIMEOUT;
+ }
+ }
+ // Count how long pin is high and store in pulseCounts[i+1]
+ while (bbb_mmio_input(pin)) {
+ if (++pulseCounts[i+1] >= DHT_MAXCOUNT) {
+ // Timeout waiting for response.
+ set_default_priority();
+ return DHT_ERROR_TIMEOUT;
+ }
+ }
+ }
+
+ // Done with timing critical code, now interpret the results.
+
+ // Drop back to normal priority.
+ set_default_priority();
+
+ // Compute the average low pulse width to use as a 50 microsecond reference threshold.
+ // Ignore the first two readings because they are a constant 80 microsecond pulse.
+ uint32_t threshold = 0;
+ for (int i=2; i < DHT_PULSES*2; i+=2) {
+ threshold += pulseCounts[i];
+ }
+ threshold /= DHT_PULSES-1;
+
+ // Interpret each high pulse as a 0 or 1 by comparing it to the 50us reference.
+ // If the count is less than 50us it must be a ~28us 0 pulse, and if it's higher
+ // then it must be a ~70us 1 pulse.
+ uint8_t data[5] = {0};
+ for (int i=3; i < DHT_PULSES*2; i+=2) {
+ int index = (i-3)/16;
+ data[index] <<= 1;
+ if (pulseCounts[i] >= threshold) {
+ // One bit for long pulse.
+ data[index] |= 1;
+ }
+ // Else zero bit for short pulse.
+ }
+
+ // Useful debug info:
+ //printf("Data: 0x%x 0x%x 0x%x 0x%x 0x%x\n", data[0], data[1], data[2], data[3], data[4]);
+
+ // Verify checksum of received data.
+ if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
+ if (type == DHT11) {
+ // Get humidity and temp for DHT11 sensor.
+ *humidity = (float)data[0];
+ *temperature = (float)data[2];
+ }
+ else if (type == DHT22) {
+ // Calculate humidity and temp for DHT22 sensor.
+ *humidity = (data[0] * 256 + data[1]) / 10.0f;
+ *temperature = ((data[2] & 0x7F) * 256 + data[3]) / 10.0f;
+ if (data[2] & 0x80) {
+ *temperature *= -1.0f;
+ }
+ }
+ return DHT_SUCCESS;
+ }
+ else {
+ return DHT_ERROR_CHECKSUM;
+ }
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_dht_read.h b/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_dht_read.h
new file mode 100644
index 0000000..949ad2c
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_dht_read.h
@@ -0,0 +1,33 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#ifndef BBB_DHT_READ_H
+#define BBB_DHT_READ_H
+
+#include "../common_dht_read.h"
+
+// Read DHT sensor connected to GPIO bin GPIO<base>_<number>, for example P8_11 is GPIO1_13 with
+// base = 1 and number = 13. Humidity and temperature will be returned in the provided parameters.
+// If a successfull reading could be made a value of 0 (DHT_SUCCESS) will be returned. If there
+// was an error reading the sensor a negative value will be returned. Some errors can be ignored
+// and retried, specifically DHT_ERROR_TIMEOUT or DHT_ERROR_CHECKSUM.
+int bbb_dht_read(int type, int gpio_base, int gpio_number, float* humidity, float* temperature);
+
+#endif
diff --git a/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_mmio.c b/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_mmio.c
new file mode 100644
index 0000000..fda1beb
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_mmio.c
@@ -0,0 +1,73 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "bbb_mmio.h"
+
+#define GPIO_LENGTH 4096
+#define GPIO0_ADDR 0x44E07000
+#define GPIO1_ADDR 0x4804C000
+#define GPIO2_ADDR 0x481AC000
+#define GPIO3_ADDR 0x481AF000
+
+// Store mapping of GPIO base number to GPIO address.
+static uint32_t gpio_addresses[4] = { GPIO0_ADDR, GPIO1_ADDR, GPIO2_ADDR, GPIO3_ADDR };
+
+// Cache memory-mapped GPIO addresses.
+static volatile uint32_t* gpio_base[4] = { NULL };
+
+int bbb_mmio_get_gpio(int base, int number, gpio_t* gpio) {
+ // Validate input parameters.
+ if (gpio == NULL) {
+ return MMIO_ERROR_ARGUMENT;
+ }
+ if (base < 0 || base > 3) {
+ return MMIO_ERROR_ARGUMENT;
+ }
+ if (number < 0 || number > 31) {
+ return MMIO_ERROR_ARGUMENT;
+ }
+ // Map GPIO memory if its hasn't been mapped already.
+ if (gpio_base[base] == NULL) {
+ int fd = open("/dev/mem", O_RDWR | O_SYNC);
+ if (fd == -1) {
+ // Error opening /dev/mem. Probably not running as root.
+ return MMIO_ERROR_DEVMEM;
+ }
+ // Map GPIO memory to location in process space.
+ gpio_base[base] = (uint32_t*)mmap(NULL, GPIO_LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, gpio_addresses[base]);
+ if (gpio_base[base] == MAP_FAILED) {
+ // Don't save the result if the memory mapping failed.
+ gpio_base[base] = NULL;
+ return MMIO_ERROR_MMAP;
+ }
+ }
+ // Initialize and set GPIO fields.
+ memset(gpio, 0, sizeof(gpio));
+ gpio->base = gpio_base[base];
+ gpio->number = number;
+ return MMIO_SUCCESS;
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_mmio.h b/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_mmio.h
new file mode 100644
index 0000000..b6d5faa
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Beaglebone_Black/bbb_mmio.h
@@ -0,0 +1,101 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+// Simple fast memory-mapped GPIO library for the Beaglebone Black.
+// Allows reading and writing GPIO at very high speeds, up to ~2.6mhz!
+
+/*
+ // Example usage:
+
+ #include <stdio.h>
+ #include "bbb_mmio.h"
+
+ int main(int argc, char* argv[]) {
+ // Get GPIO pin.
+ // See the giant table of of pins in the system reference manual for details
+ // on the base and number for a given GPIO:
+ // https://github.com/CircuitCo/BeagleBone-Black/blob/master/BBB_SRM.pdf?raw=true
+ // Section 7 Connectors, table 12 shows P8_11 maps to GPIO1_13, so 1 is the
+ // gpio base and 13 is the gpio number.
+ gpio_t p8_11;
+ if (bbb_mmio_get_gpio(1, 13, &p8_11) < 0) {
+ printf("Couldn't get requested GPIO pin!\n");
+ return 1;
+ }
+ // Set pin as output.
+ bbb_mmio_set_output(p8_11);
+ // Toggle the pin high and low as fast as possible.
+ // This generates a signal at about 2.6mhz in my tests.
+ // Each pulse high/low is only about 200 nanoseconds long!
+ while (1) {
+ bbb_mmio_set_high(p8_11);
+ bbb_mmio_set_low(p8_11);
+ }
+ return 0;
+ }
+
+*/
+
+#ifndef BBB_MMIO_H
+#define BBB_MMIO_H
+
+#include <stdint.h>
+
+#define MMIO_SUCCESS 0
+#define MMIO_ERROR_ARGUMENT -1
+#define MMIO_ERROR_DEVMEM -2
+#define MMIO_ERROR_MMAP -3
+
+#define MMIO_OE_ADDR 0x134
+#define MMIO_GPIO_DATAOUT 0x13C
+#define MMIO_GPIO_DATAIN 0x138
+#define MMIO_GPIO_CLEARDATAOUT 0x190
+#define MMIO_GPIO_SETDATAOUT 0x194
+
+// Define struct to represent a GPIO pin based on its base memory address and number.
+typedef struct {
+ volatile uint32_t* base;
+ int number;
+} gpio_t;
+
+int bbb_mmio_get_gpio(int base, int number, gpio_t* gpio);
+
+static inline void bbb_mmio_set_output(gpio_t gpio) {
+ gpio.base[MMIO_OE_ADDR/4] &= (0xFFFFFFFF ^ (1 << gpio.number));
+}
+
+static inline void bbb_mmio_set_input(gpio_t gpio) {
+ gpio.base[MMIO_OE_ADDR/4] |= (1 << gpio.number);
+}
+
+static inline void bbb_mmio_set_high(gpio_t gpio) {
+ gpio.base[MMIO_GPIO_SETDATAOUT/4] = 1 << gpio.number;
+}
+
+static inline void bbb_mmio_set_low(gpio_t gpio) {
+ gpio.base[MMIO_GPIO_CLEARDATAOUT/4] = 1 << gpio.number;
+}
+
+static inline uint32_t bbb_mmio_input(gpio_t gpio) {
+ return gpio.base[MMIO_GPIO_DATAIN/4] & (1 << gpio.number);
+}
+
+#endif
diff --git a/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_dht_read.c b/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_dht_read.c
new file mode 100644
index 0000000..0789e60
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_dht_read.c
@@ -0,0 +1,158 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <stdbool.h>
+#include <stdlib.h>
+
+#include "pi_dht_read.h"
+#include "pi_mmio.h"
+
+// This is the only processor specific magic value, the maximum amount of time to
+// spin in a loop before bailing out and considering the read a timeout. This should
+// be a high value, but if you're running on a much faster platform than a Raspberry
+// Pi or Beaglebone Black then it might need to be increased.
+#define DHT_MAXCOUNT 32000
+
+// Number of bit pulses to expect from the DHT. Note that this is 41 because
+// the first pulse is a constant 50 microsecond pulse, with 40 pulses to represent
+// the data afterwards.
+#define DHT_PULSES 41
+
+int pi_dht_read(int type, int pin, float* humidity, float* temperature) {
+ // Validate humidity and temperature arguments and set them to zero.
+ if (humidity == NULL || temperature == NULL) {
+ return DHT_ERROR_ARGUMENT;
+ }
+ *temperature = 0.0f;
+ *humidity = 0.0f;
+
+ // Initialize GPIO library.
+ if (pi_mmio_init() < 0) {
+ return DHT_ERROR_GPIO;
+ }
+
+ // Store the count that each DHT bit pulse is low and high.
+ // Make sure array is initialized to start at zero.
+ int pulseCounts[DHT_PULSES*2] = {0};
+
+ // Set pin to output.
+ pi_mmio_set_output(pin);
+
+ // Bump up process priority and change scheduler to try to try to make process more 'real time'.
+ set_max_priority();
+
+ // Set pin high for ~500 milliseconds.
+ pi_mmio_set_high(pin);
+ sleep_milliseconds(500);
+
+ // The next calls are timing critical and care should be taken
+ // to ensure no unnecssary work is done below.
+
+ // Set pin low for ~20 milliseconds.
+ pi_mmio_set_low(pin);
+ busy_wait_milliseconds(20);
+
+ // Set pin at input.
+ pi_mmio_set_input(pin);
+ // Need a very short delay before reading pins or else value is sometimes still low.
+ for (volatile int i = 0; i < 50; ++i) {
+ }
+
+ // Wait for DHT to pull pin low.
+ uint32_t count = 0;
+ while (pi_mmio_input(pin)) {
+ if (++count >= DHT_MAXCOUNT) {
+ // Timeout waiting for response.
+ set_default_priority();
+ return DHT_ERROR_TIMEOUT;
+ }
+ }
+
+ // Record pulse widths for the expected result bits.
+ for (int i=0; i < DHT_PULSES*2; i+=2) {
+ // Count how long pin is low and store in pulseCounts[i]
+ while (!pi_mmio_input(pin)) {
+ if (++pulseCounts[i] >= DHT_MAXCOUNT) {
+ // Timeout waiting for response.
+ set_default_priority();
+ return DHT_ERROR_TIMEOUT;
+ }
+ }
+ // Count how long pin is high and store in pulseCounts[i+1]
+ while (pi_mmio_input(pin)) {
+ if (++pulseCounts[i+1] >= DHT_MAXCOUNT) {
+ // Timeout waiting for response.
+ set_default_priority();
+ return DHT_ERROR_TIMEOUT;
+ }
+ }
+ }
+
+ // Done with timing critical code, now interpret the results.
+
+ // Drop back to normal priority.
+ set_default_priority();
+
+ // Compute the average low pulse width to use as a 50 microsecond reference threshold.
+ // Ignore the first two readings because they are a constant 80 microsecond pulse.
+ uint32_t threshold = 0;
+ for (int i=2; i < DHT_PULSES*2; i+=2) {
+ threshold += pulseCounts[i];
+ }
+ threshold /= DHT_PULSES-1;
+
+ // Interpret each high pulse as a 0 or 1 by comparing it to the 50us reference.
+ // If the count is less than 50us it must be a ~28us 0 pulse, and if it's higher
+ // then it must be a ~70us 1 pulse.
+ uint8_t data[5] = {0};
+ for (int i=3; i < DHT_PULSES*2; i+=2) {
+ int index = (i-3)/16;
+ data[index] <<= 1;
+ if (pulseCounts[i] >= threshold) {
+ // One bit for long pulse.
+ data[index] |= 1;
+ }
+ // Else zero bit for short pulse.
+ }
+
+ // Useful debug info:
+ //printf("Data: 0x%x 0x%x 0x%x 0x%x 0x%x\n", data[0], data[1], data[2], data[3], data[4]);
+
+ // Verify checksum of received data.
+ if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
+ if (type == DHT11) {
+ // Get humidity and temp for DHT11 sensor.
+ *humidity = (float)data[0];
+ *temperature = (float)data[2];
+ }
+ else if (type == DHT22) {
+ // Calculate humidity and temp for DHT22 sensor.
+ *humidity = (data[0] * 256 + data[1]) / 10.0f;
+ *temperature = ((data[2] & 0x7F) * 256 + data[3]) / 10.0f;
+ if (data[2] & 0x80) {
+ *temperature *= -1.0f;
+ }
+ }
+ return DHT_SUCCESS;
+ }
+ else {
+ return DHT_ERROR_CHECKSUM;
+ }
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_dht_read.h b/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_dht_read.h
new file mode 100644
index 0000000..9132e96
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_dht_read.h
@@ -0,0 +1,32 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#ifndef PI_DHT_READ_H
+#define PI_DHT_READ_H
+
+#include "../common_dht_read.h"
+
+// Read DHT sensor connected to GPIO pin (using BCM numbering). Humidity and temperature will be
+// returned in the provided parameters. If a successfull reading could be made a value of 0
+// (DHT_SUCCESS) will be returned. If there was an error reading the sensor a negative value will
+// be returned. Some errors can be ignored and retried, specifically DHT_ERROR_TIMEOUT or DHT_ERROR_CHECKSUM.
+int pi_dht_read(int sensor, int pin, float* humidity, float* temperature);
+
+#endif
diff --git a/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_mmio.c b/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_mmio.c
new file mode 100644
index 0000000..6755230
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_mmio.c
@@ -0,0 +1,55 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+// Based on code from Gert van Loo & Dom: http://elinux.org/RPi_Low-level_peripherals#GPIO_Code_examples
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "pi_mmio.h"
+
+#define BASE 0x20000000
+#define GPIO_BASE (BASE + 0x200000)
+#define GPIO_LENGTH 4096
+
+volatile uint32_t* pi_mmio_gpio = NULL;
+
+int pi_mmio_init(void) {
+ if (pi_mmio_gpio == NULL) {
+ int fd = open("/dev/mem", O_RDWR | O_SYNC);
+ if (fd == -1) {
+ // Error opening /dev/mem. Probably not running as root.
+ return MMIO_ERROR_DEVMEM;
+ }
+ // Map GPIO memory to location in process space.
+ pi_mmio_gpio = (uint32_t*)mmap(NULL, GPIO_LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE);
+ close(fd);
+ if (pi_mmio_gpio == MAP_FAILED) {
+ // Don't save the result if the memory mapping failed.
+ pi_mmio_gpio = NULL;
+ return MMIO_ERROR_MMAP;
+ }
+ }
+ return MMIO_SUCCESS;
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_mmio.h b/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_mmio.h
new file mode 100644
index 0000000..d47a9c3
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Raspberry_Pi/pi_mmio.h
@@ -0,0 +1,61 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+// Based on code from Gert van Loo & Dom: http://elinux.org/RPi_Low-level_peripherals#GPIO_Code_examples
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+// Simple fast memory-mapped GPIO library for the Raspberry Pi.
+#ifndef PI_MMIO_H
+#define PI_MMIO_H
+
+#include <stdint.h>
+
+#define MMIO_SUCCESS 0
+#define MMIO_ERROR_DEVMEM -1
+#define MMIO_ERROR_MMAP -2
+
+volatile uint32_t* pi_mmio_gpio;
+
+int pi_mmio_init(void);
+
+static inline void pi_mmio_set_input(const int gpio_number) {
+ // Set GPIO register to 000 for specified GPIO number.
+ *(pi_mmio_gpio+((gpio_number)/10)) &= ~(7<<(((gpio_number)%10)*3));
+}
+
+static inline void pi_mmio_set_output(const int gpio_number) {
+ // First set to 000 using input function.
+ pi_mmio_set_input(gpio_number);
+ // Next set bit 0 to 1 to set output.
+ *(pi_mmio_gpio+((gpio_number)/10)) |= (1<<(((gpio_number)%10)*3));
+}
+
+static inline void pi_mmio_set_high(const int gpio_number) {
+ *(pi_mmio_gpio+7) = 1 << gpio_number;
+}
+
+static inline void pi_mmio_set_low(const int gpio_number) {
+ *(pi_mmio_gpio+10) = 1 << gpio_number;
+}
+
+static inline uint32_t pi_mmio_input(const int gpio_number) {
+ return *(pi_mmio_gpio+13) & (1 << gpio_number);
+}
+
+#endif
diff --git a/python/examples/Adafruit_Python_DHT/source/Test/test_dht_read.c b/python/examples/Adafruit_Python_DHT/source/Test/test_dht_read.c
new file mode 100644
index 0000000..be06744
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Test/test_dht_read.c
@@ -0,0 +1,34 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <stdlib.h>
+
+#include "test_dht_read.h"
+
+int test_dht_read(int type, int pin, float* humidity, float* temperature) {
+ // Validate humidity and temperature arguments and set them to zero.
+ if (humidity == NULL || temperature == NULL) {
+ return -1;
+ }
+ *temperature = 42.0f;
+ *humidity = 50.0f;
+
+ return 0;
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/Test/test_dht_read.h b/python/examples/Adafruit_Python_DHT/source/Test/test_dht_read.h
new file mode 100644
index 0000000..b4d2601
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/Test/test_dht_read.h
@@ -0,0 +1,26 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#ifndef TEST_DHT_READ_H
+#define TEST_DHT_READ_H
+
+int test_dht_read(int sensor, int pin, float* humidity, float* temperature);
+
+#endif
diff --git a/python/examples/Adafruit_Python_DHT/source/_Beaglebone_Black_Driver.c b/python/examples/Adafruit_Python_DHT/source/_Beaglebone_Black_Driver.c
new file mode 100644
index 0000000..329aead
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/_Beaglebone_Black_Driver.c
@@ -0,0 +1,49 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <Python.h>
+
+#include "Beaglebone_Black/bbb_dht_read.h"
+
+// Wrap calling dht_read function and expose it as a DHT.read Python module & function.
+static PyObject* Beaglebone_Black_Driver_read(PyObject *self, PyObject *args)
+{
+ // Parse sensor and pin integer arguments.
+ int sensor, base, number;
+ if (!PyArg_ParseTuple(args, "iii", &sensor, &base, &number)) {
+ return NULL;
+ }
+ // Call dht_read and return result code, humidity, and temperature.
+ float humidity = 0, temperature = 0;
+ int result = bbb_dht_read(sensor, base, number, &humidity, &temperature);
+ return Py_BuildValue("iff", result, humidity, temperature);
+}
+
+// Boilerplate python module method list and initialization functions below.
+
+static PyMethodDef module_methods[] = {
+ {"read", Beaglebone_Black_Driver_read, METH_VARARGS, "Read DHT sensor value on a Beaglebone Black."},
+ {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC initBeaglebone_Black_Driver(void)
+{
+ Py_InitModule("Beaglebone_Black_Driver", module_methods);
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/_Raspberry_Pi_Driver.c b/python/examples/Adafruit_Python_DHT/source/_Raspberry_Pi_Driver.c
new file mode 100644
index 0000000..a867597
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/_Raspberry_Pi_Driver.c
@@ -0,0 +1,49 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <Python.h>
+
+#include "Raspberry_Pi/pi_dht_read.h"
+
+// Wrap calling dht_read function and expose it as a DHT.read Python module & function.
+static PyObject* Raspberry_Pi_Driver_read(PyObject *self, PyObject *args)
+{
+ // Parse sensor and pin integer arguments.
+ int sensor, pin;
+ if (!PyArg_ParseTuple(args, "ii", &sensor, &pin)) {
+ return NULL;
+ }
+ // Call dht_read and return result code, humidity, and temperature.
+ float humidity = 0, temperature = 0;
+ int result = pi_dht_read(sensor, pin, &humidity, &temperature);
+ return Py_BuildValue("iff", result, humidity, temperature);
+}
+
+// Boilerplate python module method list and initialization functions below.
+
+static PyMethodDef module_methods[] = {
+ {"read", Raspberry_Pi_Driver_read, METH_VARARGS, "Read DHT sensor value on a Raspberry Pi."},
+ {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC initRaspberry_Pi_Driver(void)
+{
+ Py_InitModule("Raspberry_Pi_Driver", module_methods);
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/_Test_Driver.c b/python/examples/Adafruit_Python_DHT/source/_Test_Driver.c
new file mode 100644
index 0000000..36257e5
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/_Test_Driver.c
@@ -0,0 +1,49 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <Python.h>
+
+#include "Test/test_dht_read.h"
+
+// Wrap calling dht_read function and expose it as a DHT.read Python module & function.
+static PyObject* Test_Driver_read(PyObject *self, PyObject *args)
+{
+ // Parse sensor and pin integer arguments.
+ int sensor, pin;
+ if (!PyArg_ParseTuple(args, "ii", &sensor, &pin)) {
+ return NULL;
+ }
+ // Call dht_read and return result code, humidity, and temperature.
+ float humidity = 0, temperature = 0;
+ int result = test_dht_read(sensor, pin, &humidity, &temperature);
+ return Py_BuildValue("iff", result, humidity, temperature);
+}
+
+// Boilerplate python module method list and initialization functions below.
+
+static PyMethodDef module_methods[] = {
+ {"read", Test_Driver_read, METH_VARARGS, "Mock DHT read function."},
+ {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC initTest_Driver(void)
+{
+ Py_InitModule("Test_Driver", module_methods);
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/common_dht_read.c b/python/examples/Adafruit_Python_DHT/source/common_dht_read.c
new file mode 100644
index 0000000..3bbb663
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/common_dht_read.c
@@ -0,0 +1,66 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#include <errno.h>
+#include <sched.h>
+#include <string.h>
+#include <sys/time.h>
+#include <time.h>
+
+#include "common_dht_read.h"
+
+void busy_wait_milliseconds(uint32_t millis) {
+ // Set delay time period.
+ struct timeval deltatime;
+ deltatime.tv_sec = millis / 1000;
+ deltatime.tv_usec = (millis % 1000) * 1000;
+ struct timeval walltime;
+ // Get current time and add delay to find end time.
+ gettimeofday(&walltime, NULL);
+ struct timeval endtime;
+ timeradd(&walltime, &deltatime, &endtime);
+ // Tight loop to waste time (and CPU) until enough time as elapsed.
+ while (timercmp(&walltime, &endtime, <)) {
+ gettimeofday(&walltime, NULL);
+ }
+}
+
+void sleep_milliseconds(uint32_t millis) {
+ struct timespec sleep;
+ sleep.tv_sec = millis / 1000;
+ sleep.tv_nsec = (millis % 1000) * 1000000L;
+ while (clock_nanosleep(CLOCK_MONOTONIC, 0, &sleep, &sleep) && errno == EINTR);
+}
+
+void set_max_priority(void) {
+ struct sched_param sched;
+ memset(&sched, 0, sizeof(sched));
+ // Use FIFO scheduler with highest priority for the lowest chance of the kernel context switching.
+ sched.sched_priority = sched_get_priority_max(SCHED_FIFO);
+ sched_setscheduler(0, SCHED_FIFO, &sched);
+}
+
+void set_default_priority(void) {
+ struct sched_param sched;
+ memset(&sched, 0, sizeof(sched));
+ // Go back to default scheduler with default 0 priority.
+ sched.sched_priority = 0;
+ sched_setscheduler(0, SCHED_OTHER, &sched);
+}
diff --git a/python/examples/Adafruit_Python_DHT/source/common_dht_read.h b/python/examples/Adafruit_Python_DHT/source/common_dht_read.h
new file mode 100644
index 0000000..bb56c67
--- /dev/null
+++ b/python/examples/Adafruit_Python_DHT/source/common_dht_read.h
@@ -0,0 +1,51 @@
+// Copyright (c) 2014 Adafruit Industries
+// Author: Tony DiCola
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#ifndef COMMON_DHT_READ_H
+#define COMMON_DHT_READ_H
+
+#include <stdint.h>
+
+// Define errors and return values.
+#define DHT_ERROR_TIMEOUT -1
+#define DHT_ERROR_CHECKSUM -2
+#define DHT_ERROR_ARGUMENT -3
+#define DHT_ERROR_GPIO -4
+#define DHT_SUCCESS 0
+
+// Define sensor types.
+#define DHT11 11
+#define DHT22 22
+#define AM2302 22
+
+// Busy wait delay for most accurate timing, but high CPU usage.
+// Only use this for short periods of time (a few hundred milliseconds at most)!
+void busy_wait_milliseconds(uint32_t millis);
+
+// General delay that sleeps so CPU usage is low, but accuracy is potentially bad.
+void sleep_milliseconds(uint32_t millis);
+
+// Increase scheduling priority and algorithm to try to get 'real time' results.
+void set_max_priority(void);
+
+// Drop scheduling priority back to normal/default.
+void set_default_priority(void);
+
+#endif